Skip to content

Commit

Permalink
fix: ensure command output ends with newline (#3565)
Browse files Browse the repository at this point in the history
Co-authored-by: Zoltan Kochan <z@kochan.io>
  • Loading branch information
lukashass and zkochan committed Jul 10, 2021
1 parent 1e570b6 commit a1b85a7
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 53 deletions.
5 changes: 5 additions & 0 deletions .changeset/shy-days-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"pnpm": patch
---

A trailing newline should always be added to the output.
98 changes: 47 additions & 51 deletions packages/pnpm/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,61 +200,57 @@ export default async function run (inputArgv: string[]) {
config.workspaceDir = wsDir
}

// NOTE: we defer the next stage, otherwise reporter might not catch all the logs
const [output, exitCode] = await new Promise((resolve, reject) => {
setTimeout(async () => {
if (!isCI && !selfUpdate && !config.offline && !config.preferOffline && !config.fallbackCommandUsed) {
checkForUpdates(config).catch(() => { /* Ignore */ })
}

if (config.force === true && !config.fallbackCommandUsed) {
logger.warn({
message: 'using --force I sure hope you know what you are doing',
prefix: config.dir,
})
}
let { output, exitCode }: { output: string | null, exitCode: number } = await (async () => {
// NOTE: we defer the next stage, otherwise reporter might not catch all the logs
await new Promise<void>((resolve) => setTimeout(() => resolve(), 0))
if (!isCI && !selfUpdate && !config.offline && !config.preferOffline && !config.fallbackCommandUsed) {
checkForUpdates(config).catch(() => { /* Ignore */ })
}

scopeLogger.debug({
...(
!cliOptions['recursive']
? { selected: 1 }
: {
selected: Object.keys(config.selectedProjectsGraph!).length,
total: config.allProjects!.length,
}
),
...(workspaceDir ? { workspacePrefix: workspaceDir } : {}),
if (config.force === true && !config.fallbackCommandUsed) {
logger.warn({
message: 'using --force I sure hope you know what you are doing',
prefix: config.dir,
})
}

try {
if (config.useNodeVersion != null) {
const nodePath = await node.getNodeDir(config)
config.extraBinPaths.push(nodePath)
}
let result = pnpmCmds[cmd ?? 'help'](
// TypeScript doesn't currently infer that the type of config
// is `Omit<typeof config, 'reporter'>` after the `delete config.reporter` statement
config as Omit<typeof config, 'reporter'>,
cliParams
)
if (result instanceof Promise) {
result = await result
}
if (!result) {
resolve([null, 0])
return
}
if (typeof result === 'string') {
resolve([result, 0])
return
}
resolve([result['output'], result['exitCode']])
} catch (err) {
reject(err)
}
}, 0)
})
scopeLogger.debug({
...(
!cliOptions['recursive']
? { selected: 1 }
: {
selected: Object.keys(config.selectedProjectsGraph!).length,
total: config.allProjects!.length,
}
),
...(workspaceDir ? { workspacePrefix: workspaceDir } : {}),
})

if (config.useNodeVersion != null) {
const nodePath = await node.getNodeDir(config)
config.extraBinPaths.push(nodePath)
}
let result = pnpmCmds[cmd ?? 'help'](
// TypeScript doesn't currently infer that the type of config
// is `Omit<typeof config, 'reporter'>` after the `delete config.reporter` statement
config as Omit<typeof config, 'reporter'>,
cliParams
)
if (result instanceof Promise) {
result = await result
}
if (!result) {
return { output: null, exitCode: 0 }
}
if (typeof result === 'string') {
return { output: result, exitCode: 0 }
}
return result
})()
if (output) {
if (!output.endsWith('\n')) {
output = `${output}\n`
}
write(output)
}
if (!cmd) {
Expand Down
4 changes: 2 additions & 2 deletions packages/pnpm/test/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test('pnpm bin', async () => {
const result = execPnpmSync(['bin'])

expect(result.status).toStrictEqual(0)
expect(result.stdout.toString()).toBe(path.resolve('node_modules/.bin'))
expect(result.stdout.toString().trim()).toBe(path.resolve('node_modules/.bin'))
})

test('pnpm bin -g', async () => {
Expand All @@ -20,5 +20,5 @@ test('pnpm bin -g', async () => {
const result = execPnpmSync(['bin', '-g'])

expect(result.status).toStrictEqual(0)
expect(process.env[PATH]!).toContain(result.stdout.toString())
expect(process.env[PATH]!).toContain(result.stdout.toString().trim())
})

0 comments on commit a1b85a7

Please sign in to comment.