Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion bin/ui5.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ const ui5 = {
},

async invokeCLI(pkg) {
if (process.env.UI5_CLI_PROFILE) {
const profile = await import("../lib/utils/profile.js");
await profile.start();
}
const {default: cli} = await import("../lib/cli/cli.js");
await cli(pkg);
},
Expand All @@ -88,7 +92,8 @@ const ui5 = {
} else {
const localInstallationInvoked = await ui5.invokeLocalInstallation(pkg);
if (!localInstallationInvoked) {
await ui5.invokeCLI(pkg);
const exitCode = await ui5.invokeCLI(pkg);
process.exit(exitCode);
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions lib/cli/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,5 @@ export default function(cli) {
console.log("");
console.log(chalk.dim(`See 'ui5 --help'`));
}
process.exit(1);
});
}

19 changes: 16 additions & 3 deletions lib/cli/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,20 @@ export default async (pkg) => {
// Format terminal output to full available width
cli.wrap(cli.terminalWidth());

// yargs registers a get method on the argv property.
// The property needs to be accessed to initialize everything.
cli.argv;
let exitCode = 0;
try {
// Call parse to run yargs
await cli.parse();
} catch (err) {
// Error is already handled via .fail callback in ./base.js
exitCode = 1;
} finally {
// Stop profiling after CLI finished execution
if (process.env.UI5_CLI_PROFILE) {
const profile = await import("../utils/profile.js");
await profile.stop();
}
}

return exitCode;
};
45 changes: 45 additions & 0 deletions lib/utils/profile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {writeFile} from "node:fs/promises";
import {Session} from "node:inspector";

let session;

export async function start() {
if (session) {
return;
}
session = new Session();
session.connect();
await new Promise((resolve) => {
session.post("Profiler.enable", () => {
session.post("Profiler.start", () => {
resolve();
});
});
});
}

async function writeProfile(profile) {
const d = new Date();
const timestamp =
`${d.getFullYear()}-${d.getMonth()+1}-${d.getDate()}_${d.getHours()}-${d.getMinutes()}-${d.getSeconds()}`;

await writeFile(`./ui5_${timestamp}.cpuprofile`, JSON.stringify(profile));
}

export async function stop() {
if (!session) {
return;
}
const profile = await new Promise((resolve) => {
session.post("Profiler.stop", (err, {profile}) => {
if (err) {
resolve(null);
} else {
resolve(profile);
}
});
});
if (profile) {
await writeProfile(profile);
}
}