Skip to content

Commit 70fdd6b

Browse files
committed
[FEATURE] Enable creating CPU profiles via UI5_CLI_PROFILE=true
1 parent dcf99cc commit 70fdd6b

File tree

4 files changed

+58
-6
lines changed

4 files changed

+58
-6
lines changed

bin/ui5.cjs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ const ui5 = {
7777
},
7878

7979
async invokeCLI(pkg) {
80+
if (process.env.UI5_CLI_PROFILE) {
81+
const profile = await import("../lib/utils/profile.js");
82+
await profile.start();
83+
}
8084
const {default: cli} = await import("../lib/cli/cli.js");
8185
await cli(pkg);
8286
},
@@ -88,7 +92,8 @@ const ui5 = {
8892
}
8993
const localInstallationInvoked = await ui5.invokeLocalInstallation(pkg);
9094
if (!localInstallationInvoked) {
91-
await ui5.invokeCLI(pkg);
95+
const exitCode = await ui5.invokeCLI(pkg);
96+
process.exit(exitCode);
9297
}
9398
}
9499
};

lib/cli/base.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,5 @@ export default function(cli) {
7272
console.log("");
7373
console.log(chalk.dim(`See 'ui5 --help' or 'ui5 build --help' for help`));
7474
}
75-
process.exit(1);
7675
});
7776
}
78-

lib/cli/cli.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,20 @@ export default async (pkg) => {
6868
// Format terminal output to full available width
6969
cli.wrap(cli.terminalWidth());
7070

71-
// yargs registers a get method on the argv property.
72-
// The property needs to be accessed to initialize everything.
73-
cli.argv;
71+
let exitCode = 0;
72+
try {
73+
// Call parse to run yargs
74+
await cli.parse();
75+
} catch (err) {
76+
// Error is already handled via .fail callback in ./base.js
77+
exitCode = 1;
78+
} finally {
79+
// Stop profiling after CLI finished execution
80+
if (process.env.UI5_CLI_PROFILE) {
81+
const profile = await import("../utils/profile.js");
82+
profile.stop();
83+
}
84+
}
85+
86+
return exitCode;
7487
};

lib/utils/profile.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import fs from "node:fs";
2+
import {Session} from "node:inspector";
3+
4+
let session;
5+
6+
export function start() {
7+
return new Promise((resolve) => {
8+
if (session) {
9+
resolve();
10+
return;
11+
}
12+
session = new Session();
13+
session.connect();
14+
session.post("Profiler.enable", () => {
15+
session.post("Profiler.start", () => {
16+
resolve();
17+
});
18+
});
19+
});
20+
}
21+
22+
export function stop() {
23+
if (!session) {
24+
return;
25+
}
26+
session.post("Profiler.stop", (err, {profile}) => {
27+
if (err) {
28+
return;
29+
}
30+
const d = new Date();
31+
const timestamp =
32+
`${d.getFullYear()}-${d.getMonth()+1}-${d.getDate()}_${d.getHours()}-${d.getMinutes()}-${d.getSeconds()}`;
33+
34+
fs.writeFileSync(`./ui5_${timestamp}.cpuprofile`, JSON.stringify(profile));
35+
});
36+
}

0 commit comments

Comments
 (0)