-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcli.ts
More file actions
104 lines (89 loc) · 3.19 KB
/
cli.ts
File metadata and controls
104 lines (89 loc) · 3.19 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
#!/usr/bin/env node
/* eslint-disable no-console, header/header */
/*
* Copyright (c) Trainline Limited, 2020. All rights reserved.
* See LICENSE.md in the project root for license information.
*/
import 'core-js';
import { Command } from 'commander';
import { readFileSync } from 'fs';
import path from 'path';
import { DataSourceBranchType } from './dataSources/BaseDataSource';
import * as cliDataSources from './dataSources/cliIndex';
import { ExecOptions } from './CliProgram';
import config from './config';
import plugins from './plugins';
import normalizeStats from './helpers/normalizeStats';
const packageJson = JSON.parse(
readFileSync(path.normalize(path.join(__dirname, '../package.json')), 'utf-8')
);
const program = new Command('webpack-bundle-delta');
program.version(packageJson.version);
async function exec({ dataSource, baseSha, headSha }: ExecOptions) {
let exitCode = 0;
const userConfig = await config();
const baseCompilationStats = await dataSource.getCompilationStats(
DataSourceBranchType.base,
baseSha
);
const headCompilationStats = await dataSource.getCompilationStats(
DataSourceBranchType.head,
headSha
);
const normalizedBaseStats = normalizeStats(baseCompilationStats);
const normalizedHeadStats = normalizeStats(headCompilationStats);
const pluginInstances = await plugins(userConfig, normalizedBaseStats, normalizedHeadStats);
const errors = (await Promise.all(pluginInstances.map((plugin) => plugin.errorMessages())))
.flat()
.filter((line) => !!line && line.length);
const warnings = (await Promise.all(pluginInstances.map((plugin) => plugin.warningMessages())))
.flat()
.filter((line) => !!line && line.length);
const summaryLines = (
await Promise.all(pluginInstances.map((plugin) => plugin.summaryOutput()))
).filter((line) => !!line);
const detailsLines = (
await Promise.all(pluginInstances.map((plugin) => plugin.cliOutput()))
).filter((line) => !!line);
if (!summaryLines.length && !detailsLines.length) {
throw new Error('no data to display, has it been configured correctly?');
}
let formattedSummary = '';
if (summaryLines.length === 1) {
// eslint-disable-next-line prefer-destructuring
formattedSummary = summaryLines[0];
} else if (summaryLines.length) {
formattedSummary = summaryLines.map((line) => `- ${line}`).join('\n');
}
console.log(
`
# Webpack Bundle Delta
${formattedSummary}
${detailsLines.join('\n\n\n')}`.trim()
);
if (errors.length) {
console.error(
`\n\nThe follow error${errors.length > 1 ? 's' : ''} occurred: ${
errors.length === 1 ? errors[0] : `\n${errors.map((line) => `- ${line}`).join('\n')}`
}`
);
exitCode = errors.length;
}
if (warnings.length) {
console.warn(
`\n\nThe follow warning${warnings.length > 1 ? 's' : ''} occurred: ${
warnings.length === 1 ? warnings[0] : `\n${warnings.map((line) => `- ${line}`).join('\n')}`
}`
);
}
process.exit(exitCode);
}
(async () => {
try {
Object.values(cliDataSources).forEach((cliDataSource) => cliDataSource(program, exec));
await program.parseAsync(process.argv);
} catch (error) {
console.error(error);
process.exit(1);
}
})();