-
-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·322 lines (279 loc) · 9.4 KB
/
cli.js
File metadata and controls
executable file
·322 lines (279 loc) · 9.4 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#!/usr/bin/env node
const prettyCLI = require('@tryghost/pretty-cli');
const ui = require('@tryghost/pretty-cli').ui;
const _ = require('lodash');
const {default: chalk} = require('chalk');
const gscan = require('../lib');
const ghostVersions = require('../lib/utils').versions;
/**
* @typedef {object} CliArgv
* @property {string} themePath
* @property {boolean} [zip]
* @property {boolean} [v1]
* @property {boolean} [v2]
* @property {boolean} [v3]
* @property {boolean} [v4]
* @property {boolean} [v5]
* @property {boolean} [v6]
* @property {boolean} [canary]
* @property {boolean} [fatal]
* @property {boolean} [verbose]
* @property {string[]} [labs]
*/
/**
* @typedef {object} CheckOptions
* @property {'cli'} format
* @property {string} [checkVersion]
* @property {boolean} [verbose]
* @property {boolean} [onlyFatalErrors]
* @property {Record<string, true>} [labs]
*/
/**
* @typedef {object} ResultFailure
* @property {string} ref
* @property {string} [message]
*/
/**
* @typedef {object} FormattedResult
* @property {'error' | 'warning' | 'recommendation' | 'feature'} level
* @property {string} rule
* @property {string} details
* @property {ResultFailure[]} [failures]
*/
/**
* @typedef {object} FormattedTheme
* @property {string} checkedVersion
* @property {{error: FormattedResult[], warning: FormattedResult[], recommendation: FormattedResult[]}} results
*/
const levels = {
error: chalk.red,
warning: chalk.yellow,
recommendation: chalk.yellow,
feature: chalk.green
};
/**
* @param {CliArgv} argv
* @returns {CheckOptions}
*/
function resolveOptions(argv) {
/** @type {CheckOptions} */
const options = {format: 'cli'};
if (argv.canary) {
options.checkVersion = ghostVersions.canary;
} else {
const versionKey = Object.keys(ghostVersions).find(k => k.startsWith('v') && argv[k]);
options.checkVersion = versionKey || ghostVersions.default;
}
options.verbose = argv.verbose;
options.onlyFatalErrors = argv.fatal;
if (argv.labs) {
options.labs = {};
argv.labs.forEach((flag) => {
options.labs[flag] = true;
});
}
return options;
}
/**
* @param {CliArgv} argv
* @param {CheckOptions} options
* @returns {Promise<void>}
*/
async function runCheck(argv, options) {
if (options.onlyFatalErrors) {
ui.log(chalk.bold('\nChecking theme compatibility (fatal issues only)...'));
} else {
ui.log(chalk.bold('\nChecking theme compatibility...'));
}
try {
const theme = argv.zip
? await gscan.checkZip(argv.themePath, options)
: await gscan.check(argv.themePath, options);
outputResults(theme, options);
} catch (err) {
if (argv.zip) {
ui.log(err);
} else {
ui.log(err.message);
if (err.code === 'ENOTDIR') {
ui.log('Did you mean to add the -z flag to read a zip file?');
}
}
}
}
/**
* @param {FormattedResult} result
* @param {CheckOptions} options
*/
function outputResult(result, options) {
ui.log(levels[result.level](`- ${_.capitalize(result.level)}:`), result.rule);
if (options.verbose) {
ui.log(`${chalk.bold('\nDetails:')} ${result.details}`);
}
if (result.failures && result.failures.length) {
if (options.verbose) {
ui.log(''); // extra line-break
ui.log(`${chalk.bold('Affected Files:')}`);
result.failures.forEach((failure) => {
let message = failure.ref;
if (failure.message) {
message += ` - ${failure.message}`;
}
ui.log(message);
});
} else {
ui.log(`${chalk.bold('Affected Files:')} ${_.uniq(_.map(result.failures, 'ref')).join(', ')}`);
}
}
ui.log(''); // extra line-break
}
/**
* @param {string} word
* @param {number} count
* @returns {string}
*/
function formatCount(word, count) {
return `${count} ${count === 1 ? word : `${word}s`}`;
}
/**
* @param {FormattedTheme} theme
* @param {CheckOptions} options
* @returns {string}
*/
function getSummary(theme, options) {
let summaryText = '';
const errorCount = theme.results.error.length;
const warnCount = theme.results.warning.length;
const checkSymbol = '\u2713';
if (errorCount === 0 && warnCount === 0) {
if (options.onlyFatalErrors) {
summaryText = `${chalk.green(checkSymbol)} Your theme has no fatal compatibility issues with Ghost ${theme.checkedVersion}`;
} else {
summaryText = `${chalk.green(checkSymbol)} Your theme is compatible with Ghost ${theme.checkedVersion}`;
}
} else {
summaryText = `Your theme has`;
if (!_.isEmpty(theme.results.error)) {
summaryText += chalk.red.bold(` ${formatCount('error', theme.results.error.length)}`);
}
if (!_.isEmpty(theme.results.error) && !_.isEmpty(theme.results.warning)) {
summaryText += ' and';
}
if (!_.isEmpty(theme.results.warning)) {
summaryText += chalk.yellow.bold(` ${formatCount('warning', theme.results.warning.length)}`);
}
summaryText += '!';
// NOTE: had to subtract the number of 'invisible' formating symbols
// needs update if formatting above changes
const hiddenSymbols = 38;
summaryText += '\n' + _.repeat('-', (summaryText.length - hiddenSymbols));
}
return summaryText;
}
/**
* @param {*} theme - Raw theme object; mutated into FormattedTheme by gscan.format()
* @param {CheckOptions} options
*/
function outputResults(theme, options) {
try {
/** @type {FormattedTheme} */
theme = gscan.format(theme, options);
} catch (err) {
ui.log.error('Error formating result, some results may be missing.');
ui.log.error(err);
}
const errorCount = theme.results.error.length;
ui.log('\n' + getSummary(theme, options));
if (!_.isEmpty(theme.results.error)) {
ui.log(chalk.red.bold('\nErrors'));
ui.log(chalk.red.bold('------'));
ui.log(chalk.red('Important to fix, functionality may be degraded.\n'));
_.each(theme.results.error, rule => outputResult(rule, options));
}
if (!_.isEmpty(theme.results.warning)) {
ui.log(chalk.yellow.bold('\nWarnings'));
ui.log(chalk.yellow.bold('--------'));
_.each(theme.results.warning, rule => outputResult(rule, options));
}
if (!_.isEmpty(theme.results.recommendation)) {
ui.log(chalk.yellow.bold('\nRecommendations'));
ui.log(chalk.yellow.bold('---------------'));
_.each(theme.results.recommendation, rule => outputResult(rule, options));
}
ui.log(`\nGet more help at ${chalk.cyan.underline('https://docs.ghost.org/themes/')}`);
ui.log(`You can also check theme compatibility at ${chalk.cyan.underline('https://gscan.ghost.org/')}`);
// The CLI feature is mainly used to run gscan programatically in tests within themes.
// Exiting with error code for warnings, causes the test to fail, even tho a theme
// upload via Ghost Admin would be possible without showing errors/warning.
// See also here: https://github.com/TryGhost/Blog/pull/41#issuecomment-484525754
// TODO: make failing for warnings configurable by e. g. passing an option, so we can
// disable it for the usage with tests
if (errorCount > 0) {
process.exit(1);
} else {
process.exit(0);
}
}
function main() {
prettyCLI
.configure({
name: 'gscan'
})
.groupOrder([
'Sources:',
'Utilities:',
'Commands:',
'Arguments:',
'Required Options:',
'Options:',
'Global Options:'
])
.positional('<themePath>', {
paramsDesc: 'Theme folder or .zip file path',
mustExist: true
})
.boolean('-z, --zip', {
desc: 'Theme path points to a zip file'
})
.boolean('-1, --v1', {
desc: 'Check theme for Ghost 1.0 compatibility'
})
.boolean('-2, --v2', {
desc: 'Check theme for Ghost 2.0 compatibility'
})
.boolean('-3, --v3', {
desc: 'Check theme for Ghost 3.0 compatibility'
})
.boolean('-4, --v4', {
desc: 'Check theme for Ghost 4.0 compatibility'
})
.boolean('-5, --v5', {
desc: 'Check theme for Ghost 5.0 compatibility'
})
.boolean('-6, --v6', {
desc: 'Check theme for Ghost 6.0 compatibility'
})
.boolean('-c, --canary', {
desc: 'Check theme for Ghost 6.0 compatibility (alias for --v6)'
})
.boolean('-f, --fatal', {
desc: 'Only show fatal errors that prevent upgrading Ghost'
})
.boolean('--verbose', {
desc: 'Output check details'
})
.array('--labs', {
desc: 'a list of labs flags'
})
.parseAndExit()
.then((argv) => {
const options = resolveOptions(argv);
runCheck(argv, options);
});
}
module.exports = {formatCount, getSummary, outputResult, outputResults, resolveOptions, runCheck};
if (require.main === module) {
// Remove all Node warnings only when run as a CLI, not when imported as a module
process.removeAllListeners('warning');
main();
}