-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(npm-publish): Use libnpm/publish instead of subprocess execution
- Loading branch information
Showing
6 changed files
with
127 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,53 @@ | ||
"use strict"; | ||
|
||
const fs = require("fs-extra"); | ||
const log = require("libnpm/log"); | ||
const path = require("path"); | ||
|
||
const ChildProcessUtilities = require("@lerna/child-process"); | ||
const getExecOpts = require("@lerna/get-npm-exec-opts"); | ||
const log = require("libnpm/log"); | ||
const publish = require("libnpm/publish"); | ||
const figgyPudding = require("figgy-pudding"); | ||
const runLifecycle = require("@lerna/run-lifecycle"); | ||
|
||
module.exports = npmPublish; | ||
|
||
function npmPublish(pkg, tag, { npmClient, registry }) { | ||
const PublishConfig = figgyPudding( | ||
{ | ||
"dry-run": { default: false }, | ||
dryRun: "dry-run", | ||
tag: { default: "latest" }, | ||
}, | ||
{ | ||
other(key) { | ||
// allow any other keys _except_ circular objects | ||
return key !== "log" && key !== "logstream"; | ||
}, | ||
} | ||
); | ||
|
||
function npmPublish(pkg, tag, _opts) { | ||
log.verbose("publish", pkg.name); | ||
|
||
const distTag = tag && tag.trim(); | ||
const opts = getExecOpts(pkg, registry); | ||
const args = ["publish", "--ignore-scripts"]; | ||
const deets = { projectScope: pkg.name }; | ||
|
||
if (distTag) { | ||
args.push("--tag", distTag); | ||
if (tag) { | ||
deets.tag = tag; | ||
} | ||
|
||
if (npmClient === "yarn") { | ||
// skip prompt for new version, use existing instead | ||
// https://yarnpkg.com/en/docs/cli/publish#toc-yarn-publish-new-version | ||
args.push("--new-version", pkg.version, "--non-interactive", "--no-git-tag-version"); | ||
// yarn also needs to be told to stop creating git tags: https://git.io/fAr1P | ||
const opts = PublishConfig(_opts, deets); | ||
const tarFilePath = path.join(pkg.location, pkg.tarball.filename); | ||
|
||
let chain = Promise.resolve(); | ||
|
||
if (!opts.dryRun) { | ||
chain = chain.then(() => fs.readFile(tarFilePath)); | ||
chain = chain.then(tarData => publish(pkg.toJSON(), tarData, opts.toJSON())); | ||
} | ||
|
||
// always add tarball file, created by npmPack() | ||
args.push(pkg.tarball.filename); | ||
chain = chain.then(() => runLifecycle(pkg, "publish", opts)); | ||
chain = chain.then(() => runLifecycle(pkg, "postpublish", opts)); | ||
|
||
// don't leave the generated tarball hanging around after success | ||
chain = chain.then(() => fs.remove(tarFilePath)); | ||
|
||
log.silly("exec", npmClient, args); | ||
return ChildProcessUtilities.exec(npmClient, args, opts).then(() => | ||
// don't leave the generated tarball hanging around after success | ||
fs.remove(path.join(pkg.location, pkg.tarball.filename)).then(() => pkg) | ||
); | ||
// pipelined Package instance | ||
return chain.then(() => pkg); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters