trace-runner.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import path from 'path';
  2. const traceSync = <T>(prefix: string, fn: () => T): T => {
  3. const start = performance.now();
  4. const result = fn();
  5. const end = performance.now();
  6. console.log(`${prefix}: ${(end - start).toFixed(3)}ms`);
  7. return result;
  8. };
  9. export { traceSync };
  10. const traceAsync = async <T>(prefix: string, fn: () => Promise<T>): Promise<T> => {
  11. const start = performance.now();
  12. const result = await fn();
  13. const end = performance.now();
  14. console.log(`${prefix}: ${(end - start).toFixed(3)}ms`);
  15. return result;
  16. };
  17. export { traceAsync };
  18. export interface TaskResult {
  19. readonly start: number;
  20. readonly end: number;
  21. readonly taskName: string;
  22. }
  23. const task = <T>(importMetaPath: string, fn: () => Promise<T>, customname: string | null = null) => {
  24. const taskName = customname ?? path.basename(importMetaPath, path.extname(importMetaPath));
  25. return async () => {
  26. console.log(`🏃 [${taskName}] Start executing`);
  27. const start = performance.now();
  28. await fn();
  29. const end = performance.now();
  30. console.log(`✅ [${taskName}] Executed successfully: ${(end - start).toFixed(3)}ms`);
  31. return { start, end, taskName } as TaskResult;
  32. };
  33. };
  34. export { task };