Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error handling if files in package is not an array #8370

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions __tests__/commands/pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,10 @@ test.concurrent('pack should match dotfiles with globs', (): Promise<void> => {
expect(files.sort()).toEqual(expected.sort());
});
});

test.concurrent('pack should error if files is not an array', (): Promise<void> => {
return runPack([], {}, 'files-not-array', (config, reporter, stdout): void => {
expect(stdout).not.toMatch(/Wrote tarball to/);
expect(stdout).toMatch(/"files" property in package\.json must be an Array/);
});
});
2 changes: 2 additions & 0 deletions __tests__/fixtures/pack/files-not-array/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* @flow */
console.log('hello world');
7 changes: 7 additions & 0 deletions __tests__/fixtures/pack/files-not-array/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "files-not-array",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"files": {}
}
4 changes: 4 additions & 0 deletions src/cli/commands/pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ export async function packTarball(

// `files` field
if (onlyFiles) {
if (!(onlyFiles instanceof Array)) {
throw new MessageError(config.reporter.lang('cacheFolderMissingPack'));
}

let lines = [
'*', // ignore all files except those that are explicitly included with a negation filter
];
Expand Down
1 change: 1 addition & 0 deletions src/reporters/lang/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ const messages = {
'The package $0 requires a flat dependency graph. Add `"flat": true` to your package.json and try again.',
noName: `Package doesn't have a name.`,
noVersion: `Package doesn't have a version.`,
cacheFolderMissingPack: `"files" property in package.json must be an Array`,
answerRequired: 'An answer is required.',
missingWhyDependency: 'Missing package name, folder or path to file to identify why a package has been installed',
bugReport: 'If you think this is a bug, please open a bug report with the information provided in $0.',
Expand Down