diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index fe364f32b68716059c8db13d82b4a165afbb1ef8..6f0d299ebe8e5a9323ab308315fa9b6a8b819230 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -14,6 +14,7 @@ For a detailed view of what's changed, refer to the {url-repo}/commits[commit hi * rename `on_failure` key on run config to `failure`; keep `on_failure` key as alias (#38) * map the `env.PWD` context variable to the resolved value of the `dir` key on the run entry (#31) +* cache the result of previous where calls on Windows to avoid redundant lookups (#40) === Fixed diff --git a/packages/collector-extension/lib/index.js b/packages/collector-extension/lib/index.js index 0140f5f0f75b1c83230f2c9acac13f15f04b8c73..4905b9005d74f9e32f627763249f1731c00e5efa 100644 --- a/packages/collector-extension/lib/index.js +++ b/packages/collector-extension/lib/index.js @@ -29,6 +29,7 @@ module.exports.register = function ({ config: { createWorktrees = 'auto', keepWo const cacheDir = ospath.join(getBaseCacheDir(playbook), 'collector') await fsp.mkdir(cacheDir, { recursive: true }) const gitCache = {} + const runCache = { where: {} } const managedWorktrees = new Map() for (const componentVersionBucket of contentAggregate.slice()) { const { origins = [] } = componentVersionBucket @@ -126,7 +127,7 @@ module.exports.register = function ({ config: { createWorktrees = 'auto', keepWo for (const clean of cleans) await fsp.rm(clean.dir, { recursive: true, force: true }) for (const { dir: cwd, command, local, shell, env, failure } of runs) { try { - await runCommand(command, { cwd, local, shell: !!shell, env, quiet }) + await runCommand(command, { cwd, local, shell: !!shell, env, quiet, cache: runCache }) } catch (err) { const loc = worktree || url const qualifier = worktree ? ' ' : remote && worktree === false ? ` ` : '' diff --git a/packages/collector-extension/lib/util/run-command.js b/packages/collector-extension/lib/util/run-command.js index ef08927cc872a59958646e94bc734fd7a8906c24..6cee10b290fea9905e29aeb25edcbda9ba838f22 100644 --- a/packages/collector-extension/lib/util/run-command.js +++ b/packages/collector-extension/lib/util/run-command.js @@ -16,7 +16,7 @@ async function runCommand (cmd = '', opts = {}) { let cmdv = parseCommand(String(cmd), { preserveQuotes: shell }) if (!cmdv.length) throw new TypeError('Command not specified') let cmd0 = cmdv[0] - const { quiet, local, ...spawnOpts } = opts + const { quiet, local, cache: { where = {} } = {}, ...spawnOpts } = opts if (IS_WIN) { if (~cmd0.indexOf('/')) cmdv[0] = cmd0 = cmd0.replace(/[/]/g, ospath.sep) if (shell) { @@ -27,7 +27,7 @@ async function runCommand (cmd = '', opts = {}) { if (bare && local) cmdv[0] = '.' + ospath.sep + cmd0 } else { if (bare && !local) { - cmdv[0] = await new Promise((resolve) => { + cmdv[0] = where[cmd0] ??= await new Promise((resolve) => { execFile('where', [cmd0 + '.???'], { cwd: opts.cwd, windowsHide: true }, (_, results) => { if (!(results = results.trimEnd())) return resolve(cmd0) const exts = results.split('\r\n', 2).reduce((accum, it) => { diff --git a/packages/collector-extension/test/collector-extension-test.js b/packages/collector-extension/test/collector-extension-test.js index 66b6238a17909b046945c6a684b45979848f90b7..125286e3698472829e1da4ceb91bab5464567e1a 100644 --- a/packages/collector-extension/test/collector-extension-test.js +++ b/packages/collector-extension/test/collector-extension-test.js @@ -862,7 +862,7 @@ describe('collector extension', () => { it('should run batch script on PATH in Windows', async () => { const collectorConfig = { - run: { command: 'npm run gen-antora-yml' }, + run: [{ command: 'npm run' }, { command: 'npm run gen-antora-yml' }], scan: { dir: 'target/docs' }, } await runScenario({