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

Check existence and validity of package.json #334

Merged
merged 8 commits into from May 21, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions bin/main.js
Expand Up @@ -15,7 +15,9 @@ import { rewriteErrorMessage } from './lib/utils';
import getTasks from './tasks';
import fatalError from './ui/messages/errors/fatalError';
import fetchError from './ui/messages/errors/fetchError';
import invalidPackageJson from './ui/messages/errors/invalidPackageJson';
import missingStories from './ui/messages/errors/missingStories';
import noPackageJson from './ui/messages/errors/noPackageJson';
import runtimeError from './ui/messages/errors/runtimeError';
import taskError from './ui/messages/errors/taskError';
import intro from './ui/messages/info/intro';
Expand All @@ -24,8 +26,18 @@ export async function main(argv) {
const sessionId = uuid();
const env = getEnv();
const log = createLogger(sessionId, env);

const packagePath = await pkgUp(); // the user's own package.json
if (!packagePath) {
log.error(noPackageJson());
process.exit(253);
ghengeveld marked this conversation as resolved.
Show resolved Hide resolved
}

const packageJson = await readFile(packagePath);
if (typeof packageJson !== 'object' || typeof packageJson.scripts !== 'object') {
log.error(invalidPackageJson(packagePath));
process.exit(252);
}

// Warning: chromaui/action directly invokes runAll, so if new properties or arguments are added
// here, they must also be added to the GitHub Action.
Expand Down
11 changes: 11 additions & 0 deletions bin/ui/messages/errors/invalidPackageJson.js
@@ -0,0 +1,11 @@
import chalk from 'chalk';
import dedent from 'ts-dedent';

import { error } from '../../components/icons';

export default (packagePath) =>
dedent(chalk`
${error} {bold Invalid package.json}
Found invalid package.json at {bold ${packagePath}}
Make sure this is a valid Node.js package file, is readable, and contains a {bold "scripts"} block.
`);
7 changes: 7 additions & 0 deletions bin/ui/messages/errors/invalidPackageJson.stories.js
@@ -0,0 +1,7 @@
import invalidPackageJson from './invalidPackageJson';

export default {
title: 'CLI/Messages/Errors',
};

export const InvalidPackageJson = () => invalidPackageJson('/path/to/package.json');
12 changes: 12 additions & 0 deletions bin/ui/messages/errors/noPackageJson.js
@@ -0,0 +1,12 @@
import chalk from 'chalk';
import dedent from 'ts-dedent';

import { error } from '../../components/icons';

export default () =>
dedent(chalk`
${error} {bold No package.json found}
Chromatic only works from inside a JavaScript project.
Expecting a package.json somewhere up the directory tree.
ghengeveld marked this conversation as resolved.
Show resolved Hide resolved
Are you sure you're running from your project directory?
`);
7 changes: 7 additions & 0 deletions bin/ui/messages/errors/noPackageJson.stories.js
@@ -0,0 +1,7 @@
import noPackageJson from './noPackageJson';

export default {
title: 'CLI/Messages/Errors',
};

export const NoPackageJson = () => noPackageJson();