diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 41417cb54d5c907240da0568a06c1271ae2473c5..fc19c954c0dda2742fd5dd80a38c0dc1f696185d 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -6,6 +6,10 @@ For a detailed view of what's changed, refer to the {url-repo}/commits[commit hi == Unreleased +=== Added + +* log command with context at info level before running it unless quiet is set (#43) + === Changed * only report the base call instead of the whole command when the command cannot be found diff --git a/docs/modules/ROOT/pages/configure-run.adoc b/docs/modules/ROOT/pages/configure-run.adoc index ac61750d1d3e9bfad0641c18a238269847ee5092..8a4a1816c93d523b5b833caab7bbf0e0013b2455 100644 --- a/docs/modules/ROOT/pages/configure-run.adoc +++ b/docs/modules/ROOT/pages/configure-run.adoc @@ -145,7 +145,8 @@ This alternation is not often needed if the command has arguments. === Output By default, both the stdout (output stream) and stderr (error stream) of the command are directed to the current terminal. -If you want to suppress the stdout, you can set the `runtime.quiet` key in the playbook to false. +The extension also logs the command it's about to run at the info level. +If you want to suppress the stdout and log info message, you can set the `runtime.quiet` key in the playbook to false. It's not currently possible to suppress the stderr. === Use the current Node.js diff --git a/packages/collector-extension/lib/index.js b/packages/collector-extension/lib/index.js index 2f55ea661bee504cf6ec1b40c37c8689488da353..d97de16f194b54913935ba15b6dd8a555b5acea1 100644 --- a/packages/collector-extension/lib/index.js +++ b/packages/collector-extension/lib/index.js @@ -134,10 +134,15 @@ module.exports.register = function ({ config: { createWorktrees = 'auto', keepWo for (const { clean: cleans, run: runs, scan: scans } of collectors) { 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) { + let commandContext try { + if (!quiet && (logger ??= this.getLogger(PACKAGE_NAME)).isLevelEnabled('info')) { + commandContext = createCommandContext(url, remote, startPath, worktree, reftype, refname) + logger.info(`run.command: ${command}${commandContext}`) + } await runCommand(command, { cwd, local, shell: !!shell, env, quiet, cache: runCache }) } catch (err) { - const commandContext = createCommandContext(url, remote, startPath, worktree, reftype, refname) + commandContext ??= createCommandContext(url, remote, startPath, worktree, reftype, refname) err.message = err.message.replace(/$/m, commandContext) const failureAction = typeof failure === 'string' ? failure : 'throw' if (failureAction === 'throw') throw Object.assign(err, { message: `(${PACKAGE_NAME}): ${err.message}` }) diff --git a/packages/collector-extension/test/collector-extension-test.js b/packages/collector-extension/test/collector-extension-test.js index 14b4dd02d3c82b71e598514f917925a38f5acf2e..ea67adbda0e587ce56adabd54dc49d62158d9415 100644 --- a/packages/collector-extension/test/collector-extension-test.js +++ b/packages/collector-extension/test/collector-extension-test.js @@ -152,7 +152,8 @@ describe('collector extension', () => { return repo } - const runScenario = async ({ repoName, branches, tags, startPath, local, collectorConfig, before, after }) => { + const runScenario = async (opts) => { + const { repoName, branches, tags, startPath, local, collectorConfig, quiet, before, after } = opts const repo = await createRepository({ repoName, branches, tags, startPath, collectorConfig }) const playbook = { content: { @@ -166,7 +167,7 @@ describe('collector extension', () => { ], }, env: process.env, - runtime: { cacheDir: CACHE_DIR, quiet: true }, + runtime: { cacheDir: CACHE_DIR, quiet: quiet == null ? true : quiet }, } const contentAggregate = await aggregateContent(playbook) if (before) isAsync(before) ? await before(contentAggregate, playbook) : before(contentAggregate, playbook) @@ -250,6 +251,20 @@ describe('collector extension', () => { }) }) + it('should log command at info level if log level enabled', async () => { + const collectorConfig = { run: { command: '$NODE .gen-start-page.js' } } + const expectedMessage = `run.command: "${process.execPath}" .gen-start-page.js (url: http://localhost:${gitServerPort}/at-root/.git | branch: main)` + await runScenario({ repoName: 'at-root', collectorConfig, quiet: false }) + assert.equal(messages.length, 1) + const message = messages[0] + const expectedMessageObj = { + name: '@antora/collector-extension', + level: 'info', + msg: expectedMessage, + } + assertx.deepEqualSlice(message, expectedMessageObj) + }) + it('should keep temporary worktree if specified', async () => { const collectorConfig = { globalConfig: { keepWorktrees: true },