Skip to content

Commit

Permalink
fix: publish in workspace (#4627)
Browse files Browse the repository at this point in the history
close #4348
  • Loading branch information
zkochan committed Apr 27, 2022
1 parent 9d2264b commit 104663a
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/chatty-rivers-roll.md
@@ -0,0 +1,5 @@
---
"@pnpm/run-npm": patch
---

If running npm errors, throw an error.
6 changes: 6 additions & 0 deletions .changeset/nasty-baboons-return.md
@@ -0,0 +1,6 @@
---
"@pnpm/plugin-commands-publishing": patch
"pnpm": patch
---

`pnpm publish` should work correctly in a workspace, when the latest npm CLI is installed [#4348](https://github.com/pnpm/pnpm/issues/4348).
2 changes: 1 addition & 1 deletion packages/plugin-commands-publishing/package.json
Expand Up @@ -55,7 +55,6 @@
"load-json-file": "^6.2.0",
"path-exists": "^4.0.0",
"tar": "^6.1.4",
"tempy": "^1.0.0",
"write-yaml-file": "^4.2.0"
},
"dependencies": {
Expand All @@ -81,6 +80,7 @@
"realpath-missing": "^1.1.0",
"render-help": "^1.0.1",
"tar-stream": "^2.2.0",
"tempy": "^1.0.0",
"write-json-file": "^4.3.0"
},
"peerDependencies": {
Expand Down
3 changes: 3 additions & 0 deletions packages/plugin-commands-publishing/src/pack.ts
Expand Up @@ -107,6 +107,9 @@ export async function handler (
if (!opts.ignoreScripts) {
await _runScriptsIfPresent(['postpack'], entryManifest)
}
if (opts.dir !== dir) {
return path.join(dir, tarballName)
}
return path.relative(opts.dir, path.join(dir, tarballName))
}

Expand Down
40 changes: 29 additions & 11 deletions packages/plugin-commands-publishing/src/publish.ts
Expand Up @@ -11,6 +11,7 @@ import rimraf from '@zkochan/rimraf'
import pick from 'ramda/src/pick'
import realpathMissing from 'realpath-missing'
import renderHelp from 'render-help'
import tempy from 'tempy'
import * as pack from './pack'
import recursivePublish, { PublishRecursiveOpts } from './recursivePublish'
import { getCurrentBranch, isGitRepo, isRemoteHistoryClean, isWorkingTreeClean } from './gitChecks'
Expand Down Expand Up @@ -186,23 +187,21 @@ Do you want to continue?`,
}
}

// We have to publish the tarball from another location.
// Otherwise, npm would publish the package with the package.json file
// from the current working directory, ignoring the package.json file
// that was generated and packed to the tarball.
const packDestination = tempy.directory()
const tarballName = await pack.handler({
...opts,
dir,
packDestination,
})
const tarballDir = path.dirname(path.join(dir, tarballName))
const localNpmrc = path.join(tarballDir, '.npmrc')
const copyNpmrc = !existsSync(localNpmrc) && opts.workspaceDir && existsSync(path.join(opts.workspaceDir, '.npmrc'))
if (copyNpmrc && opts.workspaceDir) {
await fs.copyFile(path.join(opts.workspaceDir, '.npmrc'), localNpmrc)
}
await copyNpmrc({ dir, workspaceDir: opts.workspaceDir, packDestination })
const { status } = runNpm(opts.npmPath, ['publish', '--ignore-scripts', path.basename(tarballName), ...args], {
cwd: tarballDir,
cwd: packDestination,
})
await rimraf(path.join(dir, tarballName))
if (copyNpmrc) {
await rimraf(localNpmrc)
}
await rimraf(packDestination)

if (status != null && status !== 0) {
return { exitCode: status }
Expand All @@ -216,6 +215,25 @@ Do you want to continue?`,
return { manifest }
}

async function copyNpmrc (
{ dir, workspaceDir, packDestination }: {
dir: string
workspaceDir?: string
packDestination: string
}
) {
const localNpmrc = path.join(dir, '.npmrc')
if (existsSync(localNpmrc)) {
await fs.copyFile(localNpmrc, path.join(packDestination, '.npmrc'))
return
}
if (!workspaceDir) return
const workspaceNpmrc = path.join(workspaceDir, '.npmrc')
if (existsSync(workspaceNpmrc)) {
await fs.copyFile(workspaceNpmrc, path.join(packDestination, '.npmrc'))
}
}

export async function runScriptsIfPresent (
opts: RunLifecycleHookOptions,
scriptNames: string[],
Expand Down
4 changes: 3 additions & 1 deletion packages/run-npm/src/index.ts
Expand Up @@ -26,9 +26,11 @@ export function runScriptSync (
}
) {
opts = Object.assign({}, opts)
return spawn.sync(command, args, Object.assign({}, opts, {
const result = spawn.sync(command, args, Object.assign({}, opts, {
env: createEnv(opts),
}))
if (result.error) throw result.error
return result
}

function createEnv (
Expand Down
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 104663a

Please sign in to comment.