Skip to content

Commit

Permalink
- Added option to output duration and time
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennenoel committed Apr 13, 2024
1 parent 763de08 commit db3eaa8
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
31 changes: 29 additions & 2 deletions packages/cli/src/managers/shell.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import {exec, spawn} from "child_process";
import {ConsoleManager} from "./console.manager";
import {injectable} from "tsyringe";
import {PathManager} from "./path.manager";
import {DateUtil} from "@pristine-ts/common"

@injectable()
export class ShellManager {
constructor(private readonly consoleManager: ConsoleManager, private readonly pathManager: PathManager) {
constructor(private readonly consoleManager: ConsoleManager,
private readonly pathManager: PathManager,
private readonly dateUtil: DateUtil) {
}

execute(command: string, options?: {
Expand All @@ -14,6 +17,8 @@ export class ShellManager {
maxBuffer?: number,
outputStdout?: boolean,
outputStderr?: boolean,
outputDuration?: boolean,
outputTimeBeforeExecutingCommand?: boolean,
}): Promise<string> {
return new Promise<string>((resolve, reject) => {
const env = process.env;
Expand All @@ -24,13 +29,21 @@ export class ShellManager {

const outputStdout = options?.outputStdout ?? true;
const outputStderr = options?.outputStderr ?? true;
const outputDuration = options?.outputDuration ?? true;
const outputTimeBeforeExecutingCommand = options?.outputTimeBeforeExecutingCommand ?? true;

if(directory) {
const absoluteDirectory = this.pathManager.getPathRelativeToCurrentExecutionDirectory(directory);
finalCommand = "cd " + absoluteDirectory + " && " + command;
}

outputStdout && this.consoleManager.writeLine(finalCommand);
const start = new Date();

if(outputTimeBeforeExecutingCommand) {
this.consoleManager.writeLine(start.toISOString());
}

this.consoleManager.writeLine(finalCommand);

if(streamStdout) {
const child = spawn(finalCommand, [], { shell: true, env });
Expand All @@ -49,6 +62,13 @@ export class ShellManager {
return reject(code);
}

// Output the duration in human readable format
if(outputDuration) {
const end = new Date();
const duration = end.getTime() - start.getTime();
this.consoleManager.writeLine(`Executed in: ${this.dateUtil.formatDuration(duration)}`);
}

return resolve(code + "");
});
}
Expand All @@ -64,6 +84,13 @@ export class ShellManager {
return resolve(stderr);
}

// Output the duration in human readable format
if(outputDuration) {
const end = new Date();
const duration = end.getTime() - start.getTime();
this.consoleManager.writeLine(`Executed in: ${this.dateUtil.formatDuration(duration)}`);
}

outputStdout && this.consoleManager.writeLine(stdout);
return resolve(stdout);
})
Expand Down
24 changes: 24 additions & 0 deletions packages/common/src/utils/date.util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {injectable} from "tsyringe"
@injectable()
export class DateUtil {
formatDuration(milliseconds: number) {
if(milliseconds === 0) {
return "0ms";
}

const seconds = Math.floor(milliseconds / 1000);
const minutes = Math.floor(seconds / 60);
const hours = Math.floor(minutes / 60);
const days = Math.floor(hours / 24);
const years = Math.floor(days / 365);

const parts = [];
if (years > 0) parts.push(`${years} year${years > 1 ? 's' : ''}`);
if (days > 0) parts.push(`${days} day${days > 1 ? 's' : ''}`);
if (hours > 0) parts.push(`${hours} hour${hours > 1 ? 's' : ''}`);
if (minutes > 0) parts.push(`${minutes} minute${minutes > 1 ? 's' : ''}`);
parts.push(`${seconds % 60} second${seconds % 60 > 1 ? 's' : ''}`);

return parts.join(", ").replace(/, (.*)$/, " and $1");
}
}
1 change: 1 addition & 0 deletions packages/common/src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./date.util";
export * from "./metadata.util";
export * from "./request.util";

0 comments on commit db3eaa8

Please sign in to comment.