1 | import { readFile } from 'jsonfile';
|
2 | import Listr from 'listr';
|
3 | import pkgUp from 'pkg-up';
|
4 | import { v4 as uuid } from 'uuid';
|
5 |
|
6 | import GraphQLClient from './io/GraphQLClient';
|
7 | import checkForUpdates from './lib/checkForUpdates';
|
8 | import checkPackageJson from './lib/checkPackageJson';
|
9 | import getEnv from './lib/getEnv';
|
10 | import getOptions from './lib/getOptions';
|
11 | import { createLogger } from './lib/log';
|
12 | import NonTTYRenderer from './lib/NonTTYRenderer';
|
13 | import parseArgs from './lib/parseArgs';
|
14 | import { rewriteErrorMessage } from './lib/utils';
|
15 | import getTasks from './tasks';
|
16 | import fatalError from './ui/messages/errors/fatalError';
|
17 | import fetchError from './ui/messages/errors/fetchError';
|
18 | import missingStories from './ui/messages/errors/missingStories';
|
19 | import runtimeError from './ui/messages/errors/runtimeError';
|
20 | import taskError from './ui/messages/errors/taskError';
|
21 | import intro from './ui/messages/info/intro';
|
22 |
|
23 | export async function main(argv) {
|
24 | const sessionId = uuid();
|
25 | const env = getEnv();
|
26 | const log = createLogger(sessionId, env);
|
27 | const packagePath = await pkgUp();
|
28 | const packageJson = await readFile(packagePath);
|
29 |
|
30 |
|
31 |
|
32 | const ctx = { env, log, sessionId, packageJson, packagePath, ...parseArgs(argv) };
|
33 | await runAll(ctx);
|
34 |
|
35 | log.info('');
|
36 | process.exit(ctx.exitCode);
|
37 | }
|
38 |
|
39 | export async function runAll(ctx) {
|
40 |
|
41 | await Promise.all([runBuild(ctx), checkForUpdates(ctx)]);
|
42 |
|
43 | if (!ctx.exitCode || ctx.exitCode === 1) {
|
44 | await checkPackageJson(ctx);
|
45 | }
|
46 | }
|
47 |
|
48 | export async function runBuild(ctx) {
|
49 | ctx.log.info('');
|
50 | ctx.log.info(intro(ctx));
|
51 |
|
52 | try {
|
53 | ctx.options = await getOptions(ctx);
|
54 | } catch (e) {
|
55 | ctx.log.info('');
|
56 | ctx.log.error(e.message);
|
57 | ctx.exitCode = 254;
|
58 | return;
|
59 | }
|
60 |
|
61 | try {
|
62 | ctx.client = new GraphQLClient({
|
63 | uri: `${ctx.env.CHROMATIC_INDEX_URL}/graphql`,
|
64 | headers: {
|
65 | 'x-chromatic-session-id': ctx.sessionId,
|
66 | 'x-chromatic-cli-version': ctx.pkg.version,
|
67 | },
|
68 | retries: 3,
|
69 | log: ctx.log,
|
70 | });
|
71 |
|
72 | try {
|
73 | ctx.log.info('');
|
74 | if (ctx.options.interactive) ctx.log.queue();
|
75 | const options = ctx.options.interactive ? {} : { renderer: NonTTYRenderer, log: ctx.log };
|
76 | await new Listr(getTasks(ctx.options), options).run(ctx);
|
77 | } catch (err) {
|
78 | if (err.code === 'ECONNREFUSED' || err.name === 'StatusCodeError') {
|
79 | ctx.log.info('');
|
80 | ctx.log.error(fetchError(ctx, err));
|
81 | return;
|
82 | }
|
83 | if (err.message.startsWith('Cannot run a build with no stories')) {
|
84 | throw rewriteErrorMessage(err, missingStories(ctx));
|
85 | }
|
86 | throw rewriteErrorMessage(err, taskError(ctx, err));
|
87 | } finally {
|
88 |
|
89 | const { runtimeErrors, runtimeWarnings } = ctx;
|
90 | if ((runtimeErrors && runtimeErrors.length) || (runtimeWarnings && runtimeWarnings.length)) {
|
91 | ctx.log.info('');
|
92 | ctx.log.error(runtimeError(ctx));
|
93 | }
|
94 |
|
95 | ctx.log.flush();
|
96 | if (ctx.stopApp) ctx.stopApp();
|
97 | if (ctx.closeTunnel) ctx.closeTunnel();
|
98 | }
|
99 | } catch (error) {
|
100 | const errors = [].concat(error);
|
101 |
|
102 | if (errors.length && !ctx.userError) {
|
103 | ctx.log.info('');
|
104 | ctx.log.error(fatalError(ctx, errors));
|
105 | }
|
106 |
|
107 |
|
108 | if (!ctx.exitCode) ctx.exitCode = 255;
|
109 | }
|
110 | }
|