/
main.ts
executable file
·168 lines (149 loc) · 4.88 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { error, getInput, setFailed, setOutput } from '@actions/core';
import { context } from '@actions/github';
import { readFile } from 'jsonfile';
import pkgUp from 'pkg-up';
import { v4 as uuid } from 'uuid';
import path from 'path';
import getEnv from '../bin/lib/getEnv';
import { createLogger } from '../bin/lib/log';
import parseArgs from '../bin/lib/parseArgs';
import { runAll } from '../bin/main';
const maybe = (a: string, b: any = undefined) => {
if (!a) {
return b;
}
try {
return JSON.parse(a);
} catch (e) {
return a;
}
};
const getCommit = (event: typeof context) => {
switch (event.eventName) {
case 'pull_request':
case 'pull_request_review':
case 'pull_request_target': {
return {
owner: event.payload.repository.owner.login,
repo: event.payload.repository.name,
branch: event.payload.pull_request.head.ref,
ref: event.ref || event.payload.pull_request.head.ref,
sha: event.payload.pull_request.head.sha,
};
}
case 'push': {
return {
owner: event.payload.repository.owner.login,
repo: event.payload.repository.name,
branch: event.payload.ref.replace('refs/heads/', ''),
ref: event.payload.ref,
sha: event.payload.after,
};
}
default: {
setFailed(`${event.eventName} event is not supported in this action`);
return null;
}
}
};
interface Output {
url: string;
buildUrl: string;
storybookUrl: string;
code: number;
}
async function runChromatic(options): Promise<Output> {
const sessionId = uuid();
const env = getEnv();
const log = createLogger(sessionId, env);
const packagePath = await pkgUp(); // the user's own package.json
const packageJson = await readFile(packagePath);
const ctx = {
...parseArgs([]),
packagePath,
packageJson,
env,
log,
sessionId,
flags: options,
} as any;
await runAll(ctx);
return {
url: ctx.build?.webUrl,
code: ctx.exitCode,
buildUrl: ctx.build?.webUrl,
storybookUrl: ctx.build?.cachedUrl,
};
}
async function run() {
const commit = getCommit(context);
if (!commit) {
return;
}
const { branch, sha } = commit;
try {
// Remember to keep this list in sync with ../action.yml
const projectToken = getInput('projectToken') || getInput('appCode'); // backwards compatibility
const workingDir = getInput('workingDir');
const buildScriptName = getInput('buildScriptName');
const scriptName = getInput('scriptName');
const exec = getInput('exec');
const skip = getInput('skip');
const doNotStart = getInput('doNotStart');
const storybookPort = getInput('storybookPort');
const storybookUrl = getInput('storybookUrl');
const storybookBuildDir = getInput('storybookBuildDir');
const storybookHttps = getInput('storybookHttps');
const storybookCert = getInput('storybookCert');
const storybookKey = getInput('storybookKey');
const storybookCa = getInput('storybookCa');
const preserveMissing = getInput('preserveMissing');
const autoAcceptChanges = getInput('autoAcceptChanges');
const allowConsoleErrors = getInput('allowConsoleErrors');
const exitZeroOnChanges = getInput('exitZeroOnChanges');
const exitOnceUploaded = getInput('exitOnceUploaded');
const ignoreLastBuildOnBranch = getInput('ignoreLastBuildOnBranch');
process.env.CHROMATIC_SHA = sha;
process.env.CHROMATIC_BRANCH = branch;
process.chdir(path.join(process.cwd(), workingDir || ''));
const output = await runChromatic({
projectToken,
workingDir: maybe(workingDir),
buildScriptName: maybe(buildScriptName),
scriptName: maybe(scriptName),
exec: maybe(exec),
skip: maybe(skip),
doNotStart: maybe(doNotStart),
storybookPort: maybe(storybookPort),
storybookUrl: maybe(storybookUrl),
storybookBuildDir: maybe(storybookBuildDir),
storybookHttps: maybe(storybookHttps),
storybookCert: maybe(storybookCert),
storybookKey: maybe(storybookKey),
storybookCa: maybe(storybookCa),
fromCI: true,
interactive: false,
preserveMissing: maybe(preserveMissing),
autoAcceptChanges: maybe(autoAcceptChanges),
exitZeroOnChanges: maybe(exitZeroOnChanges, true),
exitOnceUploaded: maybe(exitOnceUploaded, false),
allowConsoleErrors: maybe(allowConsoleErrors, false),
ignoreLastBuildOnBranch: maybe(ignoreLastBuildOnBranch),
});
setOutput('url', output.url);
setOutput('buildUrl', output.buildUrl);
setOutput('storybookUrl', output.storybookUrl);
setOutput('code', output.code.toString());
if (output.code !== 0) {
setFailed('non-zero exit code');
}
process.exit(output.code);
} catch (e) {
if (e.message) error(e.message);
if (e.stack) error(e.stack);
if (e.description) error(e.description);
setFailed(e.message);
process.exit(1);
}
}
run();