forked from reg-viz/reg-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
171 lines (155 loc) · 6.11 KB
/
cli.js
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
169
170
171
#!/usr/bin/env node
/* @flow */
import { Spinner } from 'cli-spinner';
import meow from 'meow';
import path from 'path';
import compare from './';
import log from './log';
import fs from 'fs';
// import notifier from './notifier';
import { BALLOT_X, CHECK_MARK, GREEK_CROSS, MINUS } from './icon';
import createReport from './report';
const spinner = new Spinner();
spinner.setSpinnerString(18);
const cli = meow(
`
Usage
$ reg-cli /path/to/actual-dir /path/to/expected-dir /path/to/diff-dir
Options
-U, --update Update expected images.(Copy \`actual images\` to \`expected images\`).
-J, --json Specified json report path. If omitted ./reg.json.
-I, --ignoreChange If true, error will not be thrown when image change detected.
-E, --extendedErrors If true, also added/deleted images will throw an error. If omitted false.
-R, --report Output html report to specified directory.
--junit Output junit report to specified file.
-P, --urlPrefix Add prefix to all image src.
-M, --matchingThreshold Matching threshold, ranges from 0 to 1. Smaller values make the comparison more sensitive. 0 by default.
-T, --thresholdRate Rate threshold for detecting change. When the difference ratio of the image is larger than the set rate detects the change.
-S, --thresholdPixel Pixel threshold for detecting change. When the difference pixel of the image is larger than the set pixel detects the change. This value takes precedence over \`thresholdRate\`.
-C, --concurrency How many processes launches in parallel. If omitted 4.
-A, --enableAntialias. Enable antialias. If omitted false.
-X, --additionalDetection. Enable additional difference detection(highly experimental). Select "none" or "client" (default: "none").
-F, --from Generate report from json. Please specify json file. If set, only report will be output without comparing images.
-D, --customDiffMessage Pass custom massage that will logged to the terminal when there is a diff.
Examples
$ reg-cli /path/to/actual-dir /path/to/expected-dir /path/to/diff-dir -U -D ./reg.json
`,
{
alias: {
U: 'update',
J: 'json',
I: 'ignoreChange',
E: 'extendedErrors',
R: 'report',
P: 'urlPrefix',
M: 'matchingThreshold',
T: 'thresholdRate',
S: 'thresholdPixel',
C: 'concurrency',
A: 'enableAntialias',
X: 'additionalDetection',
F: 'from',
D: 'customDiffMessage'
},
},
);
if (!cli.flags.from) {
if (!process.argv[2] || !process.argv[3] || !process.argv[4]) {
log.fail('please specify actual, expected and diff images directory.');
log.fail('e.g.: $ reg-cli /path/to/actual-dir /path/to/expected-dir /path/to/diff-dir');
process.exit(1);
}
}
const json = cli.flags.json ? cli.flags.json.toString() : './reg.json'; // default output path
const urlPrefix = typeof cli.flags.urlPrefix === 'string' ? cli.flags.urlPrefix : './';
const report = typeof cli.flags.report === 'string' ? cli.flags.report : !!cli.flags.report ? './report.html' : '';
const junitReport = typeof cli.flags.junit === 'string' ? cli.flags.junit : !!cli.flags.junit ? './junit.xml' : '';
const actualDir = process.argv[2];
const expectedDir = process.argv[3];
const diffDir = process.argv[4];
const update = !!cli.flags.update;
const extendedErrors = !!cli.flags.extendedErrors;
const ignoreChange = !!cli.flags.ignoreChange;
const enableClientAdditionalDetection = cli.flags.additionalDetection === 'client';
const from = String(cli.flags.from || '');
const customDiffMessage = String(cli.flags.customDiffMessage || `\nInspect your code changes, re-run with \`-U\` to update them. `);
// If from option specified, generate report from json and exit.
if (from) {
let json: string = '';
try {
json = fs.readFileSync(from, { encoding: 'utf8' });
} catch (e) {
log.fail('Failed to read specify json.');
log.fail(e);
process.exit(1);
}
try {
const params = JSON.parse(json);
createReport({
...params,
json: json || './reg.json',
report: report || './report.html',
junitReport: junitReport || '',
extendedErrors,
urlPrefix: urlPrefix || '',
enableClientAdditionalDetection,
fromJSON: true,
});
process.exit(0);
} catch (e) {
log.fail('Failed to parse json. Please specify valid json.');
log.fail(e);
process.exit(1);
}
}
const observer = compare({
actualDir,
expectedDir,
diffDir,
update,
report,
junitReport,
extendedErrors,
json,
urlPrefix,
matchingThreshold: Number(cli.flags.matchingThreshold || 0),
thresholdRate: Number(cli.flags.thresholdRate),
thresholdPixel: Number(cli.flags.thresholdPixel),
concurrency: Number(cli.flags.concurrency || 4),
enableAntialias: !!cli.flags.enableAntialias,
enableClientAdditionalDetection,
});
observer.once('start', () => spinner.start());
observer.on('compare', params => {
spinner.stop(true);
const file = path.join(`${actualDir}`, `${params.path}`);
switch (params.type) {
case 'delete':
return log.warn(`${MINUS} delete ${file}`);
case 'new':
return log.info(`${GREEK_CROSS} append ${file}`);
case 'pass':
return log.success(`${CHECK_MARK} pass ${file}`);
case 'fail':
return log.fail(`${BALLOT_X} change ${file}`);
}
spinner.start();
});
observer.once('update', () => log.success(`✨ your expected images are updated ✨`));
observer.once('complete', ({ failedItems, deletedItems, newItems, passedItems }) => {
spinner.stop(true);
log.info('\n');
if (failedItems.length) log.fail(`${BALLOT_X} ${failedItems.length} file(s) changed.`);
if (deletedItems.length) log.warn(`${MINUS} ${deletedItems.length} file(s) deleted.`);
if (newItems.length) log.info(`${GREEK_CROSS} ${newItems.length} file(s) appended.`);
if (passedItems.length) log.success(`${CHECK_MARK} ${passedItems.length} file(s) passed.`);
if (!update && (failedItems.length > 0 || (extendedErrors && (newItems.length > 0 || deletedItems.length > 0)))) {
log.fail(customDiffMessage);
if (!ignoreChange) process.exit(1);
}
return process.exit(0);
});
observer.once('error', error => {
log.fail(error);
process.exit(1);
});