-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #755 from chromaui/tom/ap-3253-update-cli-to-be-ca…
…llable-from-node-and-return-buildid Add a node entry point
- Loading branch information
Showing
285 changed files
with
751 additions
and
544 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
action/main.js | ||
dist | ||
node_modules | ||
subdir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
name: Smoke test via node api | ||
on: push | ||
|
||
jobs: | ||
self-test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
with: | ||
fetch-depth: 0 | ||
- uses: actions/setup-node@v3 | ||
with: | ||
node-version: 16 | ||
- run: yarn | ||
- name: run chromatic via node | ||
run: yarn ts-node ./scripts/run-via-node.ts | ||
env: | ||
LOG_LEVEL: debug | ||
DEBUG: chromatic-cli | ||
CHROMATIC_PROJECT_TOKEN: 47c20821d2c2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1 @@ | ||
const Observable = require('zen-observable'); | ||
|
||
global.Observable = Observable; | ||
require('any-observable/register')('global.Observable'); | ||
|
||
require('./main'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,144 +1,5 @@ | ||
import Listr from 'listr'; | ||
import { v4 as uuid } from 'uuid'; | ||
import readPkgUp from 'read-pkg-up'; | ||
|
||
import GraphQLClient from './io/GraphQLClient'; | ||
import HTTPClient from './io/HTTPClient'; | ||
import checkForUpdates from './lib/checkForUpdates'; | ||
import checkPackageJson from './lib/checkPackageJson'; | ||
import getEnv from './lib/getEnv'; | ||
import getOptions from './lib/getOptions'; | ||
import { createLogger } from './lib/log'; | ||
import NonTTYRenderer from './lib/NonTTYRenderer'; | ||
import parseArgs from './lib/parseArgs'; | ||
import { exitCodes, setExitCode } from './lib/setExitCode'; | ||
import { rewriteErrorMessage } from './lib/utils'; | ||
import { writeChromaticDiagnostics } from './lib/writeChromaticDiagnostics'; | ||
import getTasks from './tasks'; | ||
import { Context } from './types'; | ||
import fatalError from './ui/messages/errors/fatalError'; | ||
import fetchError from './ui/messages/errors/fetchError'; | ||
import graphqlError from './ui/messages/errors/graphqlError'; | ||
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'; | ||
import { endActivity } from './ui/components/activity'; | ||
import { run } from '../node-src'; | ||
|
||
export async function main(argv: string[]) { | ||
const sessionId: string = uuid(); | ||
const env = getEnv(); | ||
const log = createLogger(sessionId, env); | ||
|
||
const pkgInfo = await readPkgUp({ cwd: process.cwd() }); | ||
if (!pkgInfo) { | ||
log.error(noPackageJson()); | ||
process.exit(253); | ||
} | ||
|
||
const { path: packagePath, packageJson } = pkgInfo; | ||
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. | ||
const ctx: Partial<Context> = { | ||
env, | ||
log, | ||
sessionId, | ||
packageJson, | ||
packagePath, | ||
...parseArgs(argv), | ||
}; | ||
await runAll(ctx); | ||
|
||
log.info(''); | ||
process.exit(ctx.exitCode); | ||
} | ||
|
||
export async function runAll(ctx) { | ||
setExitCode(ctx, exitCodes.OK); | ||
|
||
ctx.http = (ctx.http as HTTPClient) || new HTTPClient(ctx); | ||
|
||
// Run these in parallel; neither should ever reject | ||
await Promise.all([runBuild(ctx), checkForUpdates(ctx)]); | ||
|
||
if (ctx.exitCode === 0 || ctx.exitCode === 1) { | ||
await checkPackageJson(ctx); | ||
} | ||
|
||
if (ctx.flags.diagnostics) { | ||
await writeChromaticDiagnostics(ctx); | ||
} | ||
} | ||
|
||
export async function runBuild(ctx: Context) { | ||
ctx.log.info(''); | ||
ctx.log.info(intro(ctx)); | ||
|
||
try { | ||
ctx.options = await getOptions(ctx); | ||
} catch (e) { | ||
ctx.log.info(''); | ||
ctx.log.error(fatalError(ctx, [e])); | ||
setExitCode(ctx, exitCodes.INVALID_OPTIONS, true); | ||
return; | ||
} | ||
|
||
try { | ||
ctx.client = new GraphQLClient(ctx, `${ctx.env.CHROMATIC_INDEX_URL}/graphql`, { | ||
headers: { | ||
'x-chromatic-session-id': ctx.sessionId, | ||
'x-chromatic-cli-version': ctx.pkg.version, | ||
}, | ||
retries: 3, | ||
}); | ||
|
||
try { | ||
ctx.log.info(''); | ||
if (ctx.options.interactive) ctx.log.queue(); // queue up any log messages while Listr is running | ||
const options = ctx.options.interactive ? {} : { renderer: NonTTYRenderer, log: ctx.log }; | ||
await new Listr(getTasks(ctx.options), options).run(ctx); | ||
} catch (err) { | ||
endActivity(ctx); | ||
if (err.code === 'ECONNREFUSED' || err.name === 'StatusCodeError') { | ||
setExitCode(ctx, exitCodes.FETCH_ERROR); | ||
throw rewriteErrorMessage(err, fetchError(ctx, err)); | ||
} | ||
if (err.name === 'GraphQLError') { | ||
setExitCode(ctx, exitCodes.GRAPHQL_ERROR); | ||
throw rewriteErrorMessage(err, graphqlError(ctx, err)); | ||
} | ||
if (err.message.startsWith('Cannot run a build with no stories')) { | ||
setExitCode(ctx, exitCodes.BUILD_NO_STORIES); | ||
throw rewriteErrorMessage(err, missingStories(ctx)); | ||
} | ||
throw rewriteErrorMessage(err, taskError(ctx, err)); | ||
} finally { | ||
// Handle potential runtime errors from JSDOM | ||
const { runtimeErrors, runtimeWarnings } = ctx; | ||
if ((runtimeErrors && runtimeErrors.length) || (runtimeWarnings && runtimeWarnings.length)) { | ||
ctx.log.info(''); | ||
ctx.log.error(runtimeError(ctx)); | ||
} | ||
|
||
ctx.log.flush(); | ||
} | ||
} catch (error) { | ||
const errors = [].concat(error); // GraphQLClient might throw an array of errors | ||
|
||
if (errors.length && !ctx.userError) { | ||
ctx.log.info(''); | ||
ctx.log.error(fatalError(ctx, errors)); | ||
} | ||
|
||
if (!ctx.exitCode) { | ||
setExitCode(ctx, exitCodes.UNKNOWN_ERROR); | ||
} | ||
} | ||
return run({ argv }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.