Skip to content

Commit 426d3f6

Browse files
matz3RandomByte
authored andcommitted
[FEATURE] Enable creating CPU profiles via UI5_CLI_PROFILE=true
1 parent 0b0210a commit 426d3f6

File tree

4 files changed

+67
-6
lines changed

4 files changed

+67
-6
lines changed

bin/ui5.cjs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ const ui5 = {
9090
},
9191

9292
async invokeCLI(pkg) {
93+
if (process.env.UI5_CLI_PROFILE) {
94+
const profile = await import("../lib/utils/profile.js");
95+
await profile.start();
96+
}
9397
const {default: cli} = await import("../lib/cli/cli.js");
9498
await cli(pkg);
9599
},
@@ -101,7 +105,8 @@ const ui5 = {
101105
} else {
102106
const localInstallationInvoked = await ui5.invokeLocalInstallation(pkg);
103107
if (!localInstallationInvoked) {
104-
await ui5.invokeCLI(pkg);
108+
const exitCode = await ui5.invokeCLI(pkg);
109+
process.exit(exitCode);
105110
}
106111
}
107112
}

lib/cli/base.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,5 @@ export default function(cli) {
135135
process.stderr.write(chalk.dim(`See 'ui5 --help'`));
136136
process.stderr.write("\n");
137137
}
138-
process.exit(1);
139138
});
140139
}
141-

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+
await profile.stop();
83+
}
84+
}
85+
86+
return exitCode;
7487
};

lib/utils/profile.js

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

0 commit comments

Comments
 (0)