diff --git a/package.json b/package.json index de976c979593..c6321b4085ae 100644 --- a/package.json +++ b/package.json @@ -138,7 +138,6 @@ "@types/debug": "^4.1.12", "@types/escape-html": "^1.0.4", "@types/fs-extra": "^11.0.4", - "@types/humanize-duration": "^3.27.3", "@types/jsdom": "^21.1.6", "@types/lodash.template": "^4.5.3", "@types/mark.js": "^8.11.12", @@ -164,7 +163,6 @@ "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 3477519c9e09..5d10e226f904 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -114,9 +114,6 @@ importers: '@types/fs-extra': specifier: ^11.0.4 version: 11.0.4 - '@types/humanize-duration': - specifier: ^3.27.3 - version: 3.27.3 '@types/jsdom': specifier: ^21.1.6 version: 21.1.6 @@ -192,9 +189,6 @@ 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) @@ -1160,10 +1154,6 @@ 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: @@ -2803,10 +2793,6 @@ packages: engines: {node: '>=16.17.0'} dev: true - /humanize-duration@3.31.0: - resolution: {integrity: sha512-fRrehgBG26NNZysRlTq1S+HPtDpp3u+Jzdc/d5A4cEzOD86YLAkDaJyJg8krSdCi7CJ+s7ht3fwRj8Dl+Btd0w==} - dev: true - /iconv-lite@0.6.3: resolution: {integrity: sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==} engines: {node: '>=0.10.0'} diff --git a/src/node/build/build.ts b/src/node/build/build.ts index f6467fb4c4f5..5a5ba4a5b3eb 100644 --- a/src/node/build/build.ts +++ b/src/node/build/build.ts @@ -74,16 +74,16 @@ export async function build( } try { - const { clientResult, serverResult, pageToHashMap } = await task( - 'building client + server bundles', - () => bundle(siteConfig, buildOptions) + const { clientResult, serverResult, pageToHashMap } = await bundle( + siteConfig, + buildOptions ) if (process.env.BUNDLE_ONLY) { return } - await task('rendering pages', async (updateProgress) => { + await task('rendering pages', async () => { const renderEntry = pathToFileURL(path.join(siteConfig.tempDir, 'app.js')).toString() + '?t=' + @@ -165,14 +165,9 @@ export async function build( } const pages = ['404.md', ...siteConfig.pages] - let count_done = 0 - await pMap( - pages, - (page) => task(page).then(updateProgress(++count_done, pages.length)), - { - concurrency: siteConfig.concurrency - } - ) + await pMap(pages, task, { + concurrency: siteConfig.concurrency + }) }) // emit page hash map for the case where a user session is open diff --git a/src/node/plugins/localSearchPlugin.ts b/src/node/plugins/localSearchPlugin.ts index 673e5eeb6039..5a62fb495ed2 100644 --- a/src/node/plugins/localSearchPlugin.ts +++ b/src/node/plugins/localSearchPlugin.ts @@ -17,7 +17,6 @@ import { type Awaitable } from '../shared' import { processIncludes } from '../utils/processIncludes' -import { updateCurrentTask, clearLine } from '../utils/task' import type { PageSplitSection } from '../../../types/local-search' const debug = _debug('vitepress:local-search') @@ -176,12 +175,10 @@ export async function localSearchPlugin( } async function indexLocale(locale: string) { - let numIndexed = 0 - const update = () => - updateCurrentTask(++numIndexed, files.length, `🔍️ indexing ${locale}`) const files = [...filesByLocale.get(locale)!] - const task = (f: string) => indexFile(f, parallel).then(update) - await pMap(files, task, { concurrency: siteConfig.concurrency }) + await pMap(files, (f) => indexFile(f, parallel), { + concurrency: siteConfig.concurrency + }) } const parallel = shouldUseParallel(siteConfig, 'local-search') @@ -264,7 +261,7 @@ async function* splitPageIntoSections( // Skip duplicate id, content will be treated as normal text if (existingIdSet.has(id)) { console.error( - `${clearLine}⚠️ Duplicate heading id "${id}" in ${pageName}` + `\x1b[2K\r ⚠️ Duplicate heading id "${id}" in ${pageName}` ) continue } diff --git a/src/node/utils/task.ts b/src/node/utils/task.ts index 91779af44296..1d8a3322cfed 100644 --- a/src/node/utils/task.ts +++ b/src/node/utils/task.ts @@ -1,80 +1,18 @@ import ora from 'ora' -import humanizeDuration from 'humanize-duration' -import c from 'picocolors' -import { workerMeta } from '../worker' -export const okMark = c.green('✓') -export const failMark = c.red('✖') -export const clearLine = '\x1b[2K\r' - -export type UpdateHandle = ( - done?: number, - total?: number, - subtask?: string -) => any - -let updateHandle: UpdateHandle | null = null - -export const updateCurrentTask: UpdateHandle = (...args) => { - if (workerMeta) workerMeta.updateCurrentTask(...args) - else if (updateHandle) updateHandle(...args) - else if (!process.stderr.isTTY) { - return - } else if (args.length === 0) { - process.stderr.write(clearLine) - } else { - const name = args[2] || 'unknown task' - process.stderr.write( - `${clearLine}${name} [${args.slice(0, 2).join(' / ')}]` - ) - } -} - -export async function task( - taskName: string, - task: (update: UpdateHandle) => Promise -): Promise { - if (workerMeta) { - let retVal: T - await workerMeta.task(taskName, async (handle: UpdateHandle) => { - retVal = await task(handle) - }) - return retVal! - } +export const okMark = '\x1b[32m✓\x1b[0m' +export const failMark = '\x1b[31m✖\x1b[0m' +export async function task(taskName: string, task: () => Promise) { const spinner = ora({ discardStdin: false }) spinner.start(taskName + '...') - updateHandle = (done, total, subtask) => { - const taskFullName = subtask ? `${taskName} - ${subtask}` : taskName - if (done === undefined) { - spinner.text = taskFullName + '...' - } else if (total === undefined) { - spinner.text = `${taskFullName} [ ${done} ]` - } else { - // match length to display them in same width - const _total = `${total}` - const _done = `${done}`.padStart(_total.length, ' ') - spinner.text = `${taskFullName} [ ${_done} / ${_total} ]` - } - } - - const timeStart = performance.now() - let success = true - try { - return await task(updateHandle) + await task() } catch (e) { - success = false + spinner.stopAndPersist({ symbol: failMark }) throw e - } finally { - updateHandle = null - const timeEnd = performance.now() - const duration = humanizeDuration(timeEnd - timeStart, { - maxDecimalPoints: 2 - }) - const text = `${taskName} - ${duration}` - const symbol = success ? okMark : failMark - spinner.stopAndPersist({ symbol, text }) } + + spinner.stopAndPersist({ symbol: okMark }) } diff --git a/src/node/worker.ts b/src/node/worker.ts index 6e716f079776..4628d0170a7a 100644 --- a/src/node/worker.ts +++ b/src/node/worker.ts @@ -3,7 +3,6 @@ import RPCContext, { deferPromise, type RPCContextOptions } from 'rpc-magic-proxy' -import { task, updateCurrentTask } from './utils/task' import c from 'picocolors' import Queue from './utils/queue' import _debug from 'debug' @@ -83,9 +82,7 @@ export async function launchWorkers(numWorkers: number, context: Object) { workerId, dispatchWork, // Save some RPC overhead when debugger is not active - debug: debug.enabled ? debug : null, - task, - updateCurrentTask + debug: debug.enabled ? debug : null }, initWorkerHooks, getNextTask, @@ -137,8 +134,6 @@ export let workerMeta: { workerId: string dispatchWork: typeof dispatchWork debug: typeof debug - task: typeof task - updateCurrentTask: typeof updateCurrentTask } | null = null const registry: Map = new Map()