Skip to content

Commit

Permalink
fix: better error message for bad package.json (#1547)
Browse files Browse the repository at this point in the history
* fix: better error message for bad package.json

* Update src/renderer/file-manager.ts

Co-authored-by: David Sanders <[email protected]>

* Update src/renderer/remote-loader.ts

Co-authored-by: David Sanders <[email protected]>

* fix: initialize const variable

---------

Co-authored-by: David Sanders <[email protected]>
  • Loading branch information
codebytere and dsanders11 authored Feb 20, 2024
1 parent bfb5642 commit 1fa0b62
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 10 deletions.
15 changes: 10 additions & 5 deletions src/renderer/file-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,16 @@ export class FileManager {
for (const [name, value] of Object.entries(files)) {
if (name === PACKAGE_NAME) {
const { remoteLoader } = window.app;
const { dependencies, devDependencies } = JSON.parse(value);
const deps: Record<string, string> = {
...dependencies,
...devDependencies,
};

const deps: Record<string, string> = {};
try {
const { dependencies, devDependencies } = JSON.parse(value);
Object.assign(deps, dependencies, devDependencies);
} catch {
await this.appState.showErrorDialog(
'Could not open Fiddle - invalid JSON found in package.json',
);
}

// If the project specifies an Electron version, we want to tell Fiddle to run
// it with that version by default.
Expand Down
12 changes: 7 additions & 5 deletions src/renderer/remote-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,13 @@ export class RemoteLoader {
: data.content;

if (id === PACKAGE_NAME) {
const { dependencies, devDependencies } = JSON.parse(content);
const deps: Record<string, string> = {
...dependencies,
...devDependencies,
};
const deps: Record<string, string> = {};
try {
const { dependencies, devDependencies } = JSON.parse(content);
Object.assign(deps, dependencies, devDependencies);
} catch (e) {
throw new Error('Invalid JSON found in package.json');
}

// If the gist specifies an Electron version, we want to tell Fiddle to run
// it with that version by default.
Expand Down
13 changes: 13 additions & 0 deletions tests/renderer/file-manager-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ describe('FileManager', () => {
});
});

it('handles bad JSON in package.json', async () => {
const badPj =
'{"main":"main.js","devDependencies":{"electron":"17.0.0",}}';
const values = { ...editorValues, [PACKAGE_NAME]: badPj };

await fm.openFiddle(filePath, values);
expect(app.state.showErrorDialog).toHaveBeenCalledWith(
expect.stringMatching(
/Could not open Fiddle - invalid JSON found in package.json/i,
),
);
});

it('respects the Electron version specified in package.json', async () => {
const pj = {
main: MAIN_JS,
Expand Down
23 changes: 23 additions & 0 deletions tests/renderer/remote-loader-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,29 @@ describe('RemoteLoader', () => {
expect(app.replaceFiddle).toBeCalledWith(editorValues, { gistId });
});

it('handles bad JSON in package.json', async () => {
const gistId = 'badjsontestid';
const badPj =
'{"main":"main.js","devDependencies":{"electron":"17.0.0",}}';

store.gistId = gistId;
mockGistFiles[PACKAGE_NAME] = { content: badPj };
mockRepos.push({
name: PACKAGE_NAME,
download_url: `https://${PACKAGE_NAME}`,
});

mocked(getOctokit).mockResolvedValue({
gists: mockGetGists,
} as unknown as Octokit);

const result = await instance.fetchGistAndLoad(gistId);
expect(result).toBe(false);
expect(store.showErrorDialog).toHaveBeenCalledWith(
expect.stringMatching(/Invalid JSON found in package.json/i),
);
});

it('handles gist fiddle devDependencies', async () => {
const gistId = 'pjsontestid';
const pj = {
Expand Down

0 comments on commit 1fa0b62

Please sign in to comment.