diff --git a/package.json b/package.json index bfc8ec25..22a3b6e2 100644 --- a/package.json +++ b/package.json @@ -137,6 +137,7 @@ "@types/debug": "^4.1.12", "@types/escape-html": "^1.0.4", "@types/fs-extra": "^11.0.4", + "@types/humanize-duration": "^3.27.3", "@types/lodash.template": "^4.5.3", "@types/mark.js": "^8.11.12", "@types/markdown-it-attrs": "^4.1.3", @@ -160,6 +161,7 @@ "fs-extra": "^11.2.0", "get-port": "^7.0.0", "gray-matter": "^4.0.3", + "humanize-duration": "^3.31.0", "lint-staged": "^15.2.0", "lodash.template": "^4.5.0", "lru-cache": "^10.1.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ba4c0aaf..24948b52 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -111,6 +111,9 @@ importers: '@types/fs-extra': specifier: ^11.0.4 version: 11.0.4 + '@types/humanize-duration': + specifier: ^3.27.3 + version: 3.27.3 '@types/lodash.template': specifier: ^4.5.3 version: 4.5.3 @@ -180,6 +183,9 @@ importers: gray-matter: specifier: ^4.0.3 version: 4.0.3 + humanize-duration: + specifier: ^3.31.0 + version: 3.31.0 lint-staged: specifier: ^15.2.0 version: 15.2.0(supports-color@9.4.0) @@ -1146,6 +1152,10 @@ packages: resolution: {integrity: sha512-D0CFMMtydbJAegzOyHjtiKPLlvnm3iTZyZRSZoLq2mRhDdmLfIWOCYPfQJ4cu2erKghU++QvjcUjp/5h7hESpA==} dev: true + /@types/humanize-duration@3.27.3: + resolution: {integrity: sha512-wiiiFYjnrYDJE/ujU7wS/NShqp12IKrejozjDtcejP0zYi+cjyjVcfZHwcFUDKVJ7tHGsmgeW2ED92ABIIjfpg==} + dev: true + /@types/jquery@3.5.29: resolution: {integrity: sha512-oXQQC9X9MOPRrMhPHHOsXqeQDnWeCDT3PelUIg/Oy8FAbzSZtFHRjc7IpbfFVmpLtJ+UOoywpRsuO5Jxjybyeg==} dependencies: @@ -2742,6 +2752,10 @@ packages: engines: {node: '>=16.17.0'} dev: true + /humanize-duration@3.31.0: + resolution: {integrity: sha512-fRrehgBG26NNZysRlTq1S+HPtDpp3u+Jzdc/d5A4cEzOD86YLAkDaJyJg8krSdCi7CJ+s7ht3fwRj8Dl+Btd0w==} + dev: true + /inflight@1.0.6: resolution: {integrity: sha512-k92I/b08q4wvFscXCLvqfsHCrjrF7yiXsQuIVvVE7N82W3+aqpzuUdBbfhWcy/FZR3/4IgflMgKLOsvPDrGCJA==} dependencies: diff --git a/src/node/build/build.ts b/src/node/build/build.ts index 7dca478a..46e4d5c0 100644 --- a/src/node/build/build.ts +++ b/src/node/build/build.ts @@ -15,12 +15,13 @@ import { task } from '../utils/task' import { bundle } from './bundle' import { generateSitemap } from './generateSitemap' import { renderPage } from './render' +import humanizeDuration from 'humanize-duration' export async function build( root?: string, buildOptions: BuildOptions & { base?: string; mpa?: string } = {} ) { - const start = Date.now() + const timeStart = performance.now() process.env.NODE_ENV = 'production' const siteConfig = await resolveConfig(root, 'build', 'production') @@ -143,9 +144,11 @@ export async function build( await siteConfig.buildEnd?.(siteConfig) clearCache() - siteConfig.logger.info( - `build complete in ${((Date.now() - start) / 1000).toFixed(2)}s.` - ) + const timeEnd = performance.now() + const duration = humanizeDuration(timeEnd - timeStart, { + maxDecimalPoints: 2 + }) + siteConfig.logger.info(`build complete in ${duration}.`) } function linkVue() { diff --git a/src/node/utils/task.ts b/src/node/utils/task.ts index 1d8a3322..667caf67 100644 --- a/src/node/utils/task.ts +++ b/src/node/utils/task.ts @@ -1,4 +1,5 @@ import ora from 'ora' +import humanizeDuration from 'humanize-duration' export const okMark = '\x1b[32m✓\x1b[0m' export const failMark = '\x1b[31m✖\x1b[0m' @@ -7,12 +8,20 @@ export async function task(taskName: string, task: () => Promise) { const spinner = ora({ discardStdin: false }) spinner.start(taskName + '...') + let symbol = okMark + const timeStart = performance.now() + try { await task() } catch (e) { - spinner.stopAndPersist({ symbol: failMark }) + symbol = failMark throw e + } finally { + const timeEnd = performance.now() + const duration = humanizeDuration(timeEnd - timeStart, { + maxDecimalPoints: 2 + }) + const text = `${taskName} (${duration})` + spinner.stopAndPersist({ symbol, text }) } - - spinner.stopAndPersist({ symbol: okMark }) }