/
npm-publish.js
90 lines (74 loc) · 2.5 KB
/
npm-publish.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"use strict";
const fs = require("fs-extra");
const path = require("path");
const log = require("npmlog");
const { publish } = require("libnpmpublish");
const pify = require("pify");
const readJSON = require("read-package-json");
const figgyPudding = require("figgy-pudding");
const runLifecycle = require("@lerna/run-lifecycle");
const npa = require("npm-package-arg");
const otplease = require("@lerna/otplease");
module.exports = npmPublish;
const readJSONAsync = pify(readJSON);
const PublishConfig = figgyPudding(
{
"dry-run": { default: false },
dryRun: "dry-run",
log: { default: log },
"project-scope": {},
projectScope: "project-scope",
tag: { default: "latest" },
},
{
other() {
// open it up for the sake of tests
return true;
},
}
);
function npmPublish(pkg, tarFilePath, _opts, otpCache) {
const { scope } = npa(pkg.name);
// pass only the package scope to libnpmpublish
const opts = PublishConfig(_opts, {
projectScope: scope,
});
opts.log.verbose("publish", pkg.name);
let chain = Promise.resolve();
if (!opts.dryRun) {
chain = chain.then(() => {
let { manifestLocation } = pkg;
if (pkg.contents !== pkg.location) {
// "rebase" manifest used to generated directory
manifestLocation = path.join(pkg.contents, "package.json");
}
return Promise.all([fs.readFile(tarFilePath), readJSONAsync(manifestLocation)]);
});
chain = chain.then(([tarData, manifest]) => {
// non-default tag needs to override publishConfig.tag,
// which is merged over opts.tag in libnpmpublish
if (
opts.tag !== "latest" &&
manifest.publishConfig &&
manifest.publishConfig.tag &&
manifest.publishConfig.tag !== opts.tag
) {
// eslint-disable-next-line no-param-reassign
manifest.publishConfig.tag = opts.tag;
}
return otplease(innerOpts => publish(manifest, tarData, innerOpts), opts, otpCache).catch(err => {
opts.log.silly("", err);
opts.log.error(err.code, (err.body && err.body.error) || err.message);
// avoid dumping logs, this isn't a lerna problem
err.name = "ValidationError";
// ensure process exits non-zero
process.exitCode = "errno" in err ? err.errno : 1;
// re-throw to break chain upstream
throw err;
});
});
}
chain = chain.then(() => runLifecycle(pkg, "publish", opts));
chain = chain.then(() => runLifecycle(pkg, "postpublish", opts));
return chain;
}