Skip to content

Commit 224250c

Browse files
authored
chore(dev-feature): add support to profile the CLI. (angular#3961)
This requires that one "npm install v8-profiler" where the CLI lives (not going to add it as a dev dependency), and run with something like "NG_CLI_PROFILING=serve ng serve". This will create a "serve.cpuprofile" that can be opened in Chrome.
1 parent 7bd165b commit 224250c

File tree

1 file changed

+23
-0
lines changed
  • packages/angular-cli/bin

1 file changed

+23
-0
lines changed

packages/angular-cli/bin/ng

+23
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,29 @@ function _fromPackageJson(cwd) {
3636
}
3737

3838

39+
// Check if we need to profile this CLI run.
40+
let profiler = null;
41+
if (process.env['NG_CLI_PROFILING']) {
42+
profiler = require('v8-profiler');
43+
profiler.startProfiling();
44+
function exitHandler(options, err) {
45+
if (options.cleanup) {
46+
const cpuProfile = profiler.stopProfiling();
47+
fs.writeFileSync(path.resolve(process.cwd(), process.env.NG_CLI_PROFILING) + '.cpuprofile',
48+
JSON.stringify(cpuProfile));
49+
}
50+
51+
if (options.exit) {
52+
process.exit();
53+
}
54+
}
55+
56+
process.on('exit', exitHandler.bind(null, { cleanup: true }));
57+
process.on('SIGINT', exitHandler.bind(null, { exit: true }));
58+
process.on('uncaughtException', exitHandler.bind(null, { exit: true }));
59+
}
60+
61+
3962
resolve('angular-cli', { basedir: process.cwd() },
4063
function (error, projectLocalCli) {
4164
var cli;

0 commit comments

Comments
 (0)