Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- Added option to output duration and time #691

Merged
merged 5 commits into from
Apr 16, 2024
Merged
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
34 changes: 31 additions & 3 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,22 @@ 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() + ": " + finalCommand);
} else {
outputStdout && this.consoleManager.writeLine(finalCommand);
}


if(streamStdout) {
const child = spawn(finalCommand, [], { shell: true, env });
Expand All @@ -49,6 +63,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,9 +85,16 @@ 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);
})
})
}
}
}
54 changes: 54 additions & 0 deletions packages/common/src/utils/date.util.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import {DateUtil} from "./date.util";

describe("DateUtil", () => {
let dateUtil: DateUtil;

beforeEach(() => {
dateUtil = new DateUtil();
});

it('should format duration correctly for milliseconds', () => {
const result = dateUtil.formatDuration(500);
expect(result).toBe("500 ms");
});

it('should format duration correctly for seconds', () => {
const result = dateUtil.formatDuration(1000);
expect(result).toBe("1 second and 0 ms");
});

it('should format duration correctly for minutes', () => {
const result = dateUtil.formatDuration(60000);
expect(result).toBe("1 minute, 0 second and 0 ms");
});

it('should format duration correctly for hours', () => {
const result = dateUtil.formatDuration(3600000);
expect(result).toBe("1 hour, 0 minute, 0 second and 0 ms");
});

it('should format duration correctly for days', () => {
const result = dateUtil.formatDuration(86400000);
expect(result).toBe("1 day, 0 hour, 0 minute, 0 second and 0 ms");
});

it('should format duration correctly for years', () => {
const result = dateUtil.formatDuration(31536000000);
expect(result).toBe("1 year, 0 day, 0 hour, 0 minute, 0 second and 0 ms");
});

it('should format duration correctly for multiple time units', () => {
const result = dateUtil.formatDuration(90061000);
expect(result).toBe("1 day, 1 hour, 1 minute, 1 second and 0 ms");
});

it('should handle zero duration', () => {
const result = dateUtil.formatDuration(0);
expect(result).toBe("0 ms");
});

it('should handle negative duration', () => {
const result = dateUtil.formatDuration(-1000);
expect(result).toBe("0 ms");
});
});
28 changes: 28 additions & 0 deletions packages/common/src/utils/date.util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import "reflect-metadata"
import {injectable} from "tsyringe"

@injectable()
export class DateUtil {
formatDuration(milliseconds: number) {
const parts = [];
const units = [
{ name: "year", duration: 31536000000 }, // 1000ms*60s*60m*24h*365d
{ name: "day", duration: 86400000 }, // 1000*60*60*24
{ name: "hour", duration: 3600000 }, // 1000*60*60
{ name: "minute", duration: 60000 }, // 1000*60
{ name: "second", duration: 1000 }, // 1000
{ name: "ms", duration: 1 }, // 1
]

for(let i = 0; i < units.length; i++) {
const unit = units[i];
const value = Math.floor(milliseconds / unit.duration);
if(value > 0 || (parts.length > 0 && value === 0)) {
parts.push(`${value} ${unit.name}${value > 1 && unit.name !== "ms" ? 's' : ''}`);
milliseconds -= value * unit.duration;
}
}

return parts.join(", ").replace(/, ([a-zA-Z0-9 ]*)$/, " and $1") || '0 ms';
}
}
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";
Loading