mirror of
https://github.com/SukkaW/Surge.git
synced 2026-02-02 12:01:53 +08:00
Perf: improve reject build
This commit is contained in:
@@ -1,24 +1,24 @@
|
||||
import path from 'path';
|
||||
import picocolors from 'picocolors';
|
||||
|
||||
function traceSync<T>(prefix: string, fn: () => T): T {
|
||||
type Formatter = (result: string) => string;
|
||||
|
||||
export function traceSync<T>(prefix: string, fn: () => T, timeFormatter: Formatter = picocolors.blue): T {
|
||||
const start = Bun.nanoseconds();
|
||||
const result = fn();
|
||||
const end = Bun.nanoseconds();
|
||||
console.log(`${picocolors.gray(`[${((end - start) / 1e6).toFixed(3)}ms]`)} ${prefix}`);
|
||||
console.log(`${timeFormatter(`[${((end - start) / 1e6).toFixed(3)}ms]`)} ${prefix}`);
|
||||
return result;
|
||||
}
|
||||
traceSync.skip = <T>(prefix: string, fn: () => T): T => fn();
|
||||
export { traceSync };
|
||||
traceSync.skip = <T>(_prefix: string, fn: () => T): T => fn();
|
||||
|
||||
const traceAsync = async <T>(prefix: string, fn: () => Promise<T>): Promise<T> => {
|
||||
export const traceAsync = async <T>(prefix: string, fn: () => Promise<T>, timeFormatter: Formatter = picocolors.blue): Promise<T> => {
|
||||
const start = Bun.nanoseconds();
|
||||
const result = await fn();
|
||||
const end = Bun.nanoseconds();
|
||||
console.log(`${picocolors.gray(`[${((end - start) / 1e6).toFixed(3)}ms]`)} ${prefix}`);
|
||||
console.log(`${timeFormatter(`[${((end - start) / 1e6).toFixed(3)}ms]`)} ${prefix}`);
|
||||
return result;
|
||||
};
|
||||
export { traceAsync };
|
||||
|
||||
export interface TaskResult {
|
||||
readonly start: number,
|
||||
@@ -26,16 +26,15 @@ export interface TaskResult {
|
||||
readonly taskName: string
|
||||
}
|
||||
|
||||
const task = <T>(importMetaPath: string, fn: () => Promise<T>, customname: string | null = null) => {
|
||||
export const task = <T>(importMetaPath: string, fn: () => Promise<T>, customname: string | null = null) => {
|
||||
const taskName = customname ?? path.basename(importMetaPath, path.extname(importMetaPath));
|
||||
return async () => {
|
||||
console.log(`🏃 [${taskName}] Start executing`);
|
||||
const start = Bun.nanoseconds();
|
||||
await fn();
|
||||
const end = Bun.nanoseconds();
|
||||
console.log(`✅ [${taskName}] [${((end - start) / 1e6).toFixed(3)}ms] Executed successfully`);
|
||||
console.log(`✅ [${taskName}] ${picocolors.blue(`[${((end - start) / 1e6).toFixed(3)}ms]`)} Executed successfully`);
|
||||
|
||||
return { start, end, taskName } as TaskResult;
|
||||
};
|
||||
};
|
||||
export { task };
|
||||
|
||||
Reference in New Issue
Block a user