mirror of
https://github.com/SukkaW/Surge.git
synced 2026-02-02 20:11:54 +08:00
Refactor: continues to rewrite to TS
This commit is contained in:
33
Build/lib/trace-runner.ts
Normal file
33
Build/lib/trace-runner.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import path from 'path';
|
||||
|
||||
const traceSync = <T>(prefix: string, fn: () => T): T => {
|
||||
const start = performance.now();
|
||||
const result = fn();
|
||||
const end = performance.now();
|
||||
console.log(`${prefix}: ${(end - start).toFixed(3)}ms`);
|
||||
return result;
|
||||
};
|
||||
export { traceSync };
|
||||
|
||||
const traceAsync = async <T>(prefix: string, fn: () => Promise<T>): Promise<T> => {
|
||||
const start = performance.now();
|
||||
const result = await fn();
|
||||
const end = performance.now();
|
||||
console.log(`${prefix}: ${(end - start).toFixed(3)}ms`);
|
||||
return result;
|
||||
};
|
||||
export { traceAsync };
|
||||
|
||||
const task = <T>(__filename: string, fn: () => Promise<T>, customname: string | null = null) => {
|
||||
const taskName = customname ?? path.basename(__filename, path.extname(__filename));
|
||||
return async () => {
|
||||
console.log(`🏃 [${taskName}] Start executing`);
|
||||
const start = performance.now();
|
||||
await fn();
|
||||
const end = performance.now();
|
||||
console.log(`✅ [${taskName}] Executed successfully: ${(end - start).toFixed(3)}ms`);
|
||||
|
||||
return { start, end, taskName } as const;
|
||||
};
|
||||
};
|
||||
export { task };
|
||||
Reference in New Issue
Block a user