Refactor: continues to rewrite to TS

This commit is contained in:
SukkaW
2023-11-15 16:26:46 +08:00
parent ec338a659f
commit 99589cf2fc
32 changed files with 258 additions and 471 deletions

33
Build/lib/trace-runner.ts Normal file
View 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 };