-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathcli.ts
More file actions
272 lines (233 loc) · 7.39 KB
/
cli.ts
File metadata and controls
272 lines (233 loc) · 7.39 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
#!/usr/bin/env node
import yargs from 'yargs';
import fs from 'fs';
import temp from 'temp';
import open from 'open';
import chalk from 'chalk';
import { groupBy, isString } from 'lodash';
import { explore } from './api';
import { ExploreOptions, ReplaceMap, ExploreResult } from './index';
import { AppError, getErrorMessage, ErrorContext } from './app-error';
/** Parsed CLI arguments */
interface Arguments {
_: string[];
json?: string;
tsv?: string;
html?: string;
onlyMapped?: boolean;
excludeSourceMap?: boolean;
noRoot?: boolean;
replace?: string[];
with?: string[];
coverage?: string;
}
function parseArguments(): Arguments {
const argv = yargs
.strict()
.scriptName('source-map-explorer')
.usage('Analyze and debug space usage through source maps.')
.usage('Usage:')
.usage(
'$0 script.js [script.js.map] [--json [result.json] | --html [result.html] | --tsv [result.csv]] [-m | --only-mapped] [--exclude-source-map] [--replace=BEFORE_1 BEFORE_2 --with=AFTER_1 AFTER_2] [--no-root] [--version] [--help | -h]'
)
.example('$0 script.js script.js.map', 'Explore bundle')
.example('$0 script.js', 'Explore bundle with inline source map')
.example('$0 dist/js/*.*', 'Explore all bundles inside dist/js folder')
.example('$0 script.js --tsv', 'Explore and output result as TSV to stdout')
.example('$0 script.js --json result.json', 'Explore and save result as JSON to the file')
.demandCommand(1, 'At least one js file must be specified')
.options({
json: {
type: 'string',
description:
'If filename specified save output as JSON to specified file otherwise output to stdout.',
conflicts: ['tsv', 'html'],
},
tsv: {
type: 'string',
description:
'If filename specified save output as TSV to specified file otherwise output to stdout.',
conflicts: ['json', 'html'],
},
html: {
type: 'string',
description:
'If filename specified save output as HTML to specified file otherwise output to stdout rather than opening a browser.',
conflicts: ['json', 'tsv'],
},
'only-mapped': {
alias: 'm',
type: 'boolean',
description:
'Exclude "unmapped" bytes from the output. This will result in total counts less than the file size',
},
'exclude-source-map': {
type: 'boolean',
description: 'Exclude source map comment size from output',
},
'no-root': {
type: 'boolean',
description:
'To simplify the visualization, source-map-explorer will remove any prefix shared by all sources. If you wish to disable this behavior, set --no-root.',
},
replace: {
type: 'string',
array: true,
description:
'Apply a simple find/replace on source file names. This can be used to fix some oddities with paths that appear in the source map generation process. Accepts regular expressions.',
implies: 'with',
},
with: {
type: 'string',
array: true,
description: 'See --replace.',
implies: 'replace',
},
coverage: {
type: 'string',
normalize: true,
description:
'If the path to a valid a chrome code coverage JSON export is supplied, the tree map will be colorized according to which percentage of the modules code was executed',
},
})
.group(['json', 'tsv', 'html'], 'Output:')
.group(['replace', 'with'], 'Replace:')
.help('h')
.alias('h', 'help')
.showHelpOnFail(false, 'Specify --help for available options')
.wrap(null) // Do not limit line length
.parserConfiguration({
'boolean-negation': false, // Allow --no-root
})
.check(argv => {
if (argv.replace && argv.with && argv.replace.length !== argv.with.length) {
throw new Error('--replace flags must be paired with --with flags');
}
return true;
})
.parse();
// Trim extra quotes
const quoteRegex = /'/g;
argv._ = argv._.map(path => path.replace(quoteRegex, ''));
return argv;
}
export function logError(message: string | ErrorContext, error?: Error): void {
if (!isString(message)) {
message = getErrorMessage(message);
}
if (error) {
console.error(chalk.red(message), error);
} else {
console.error(chalk.red(message));
}
}
export function logWarn(message: string): void {
console.warn(chalk.yellow(message));
}
export function logInfo(message: string): void {
console.log(chalk.green(message));
}
/**
* Create options object for `explore` method
*/
function getExploreOptions(argv: Arguments): ExploreOptions {
const {
json,
tsv,
html,
replace: replaceItems,
with: withItems,
onlyMapped,
excludeSourceMap: excludeSourceMapComment,
noRoot,
coverage,
} = argv;
let replaceMap: ReplaceMap | undefined;
if (replaceItems && withItems) {
replaceMap = replaceItems.reduce<ReplaceMap>((result, item, index) => {
result[item] = withItems[index];
return result;
}, {});
}
return {
output: {
// By default CLI needs result in HTML in order to create a temporary file
format: isString(json) ? 'json' : isString(tsv) ? 'tsv' : 'html',
filename: json || tsv || html,
},
replaceMap,
onlyMapped,
excludeSourceMapComment,
noRoot,
coverage,
};
}
/**
* Write HTML content to a temporary file and open the file in a browser
*/
export async function writeHtmlToTempFile(html?: string): Promise<void> {
if (!html) {
return;
}
try {
const tempFile = temp.path({ prefix: 'sme-result-', suffix: '.html' });
fs.writeFileSync(tempFile, html);
const childProcess = await open(tempFile);
if (childProcess.stderr) {
// Catch error output from child process
childProcess.stderr.once('data', (error: Buffer) => {
logError({ code: 'CannotOpenTempFile', tempFile, error });
});
}
} catch (error) {
throw new AppError({ code: 'CannotCreateTempFile' }, error);
}
}
function outputErrors({ errors }: ExploreResult): void {
if (errors.length === 0) {
return;
}
// Group errors by bundle name
const groupedErrors = groupBy(errors, 'bundleName');
Object.entries(groupedErrors).forEach(([bundleName, errors]) => {
console.group(bundleName);
const hasManyErrors = errors.length > 1;
errors.forEach((error, index) => {
const message = `${hasManyErrors ? `${index + 1}. ` : ''}${error.message}`;
if (error.isWarning) {
logWarn(message);
} else {
logError(message);
}
});
console.groupEnd();
});
}
if (require.main === module) {
const argv = parseArguments();
const isOutputFormatSpecified = [argv.json, argv.tsv, argv.html].some(isString);
const options = getExploreOptions(argv);
explore(argv._, options)
.then(result => {
if (isOutputFormatSpecified && options.output) {
const filename = options.output.filename;
if (filename) {
logInfo(`Output saved to ${filename}`);
outputErrors(result);
} else {
console.log(result.output);
}
} else {
writeHtmlToTempFile(result.output).then(() => {
outputErrors(result);
});
}
})
.catch(error => {
if (error.errors) {
outputErrors(error);
} else {
logError('Failed to explore', error);
}
});
}