Skip to content

Commit 9c24a25

Browse files
committed
feat(utils): Add "vendored" npm-conf
This contains updates to the defaults and types from npm 5.8.0
1 parent 98c8be6 commit 9c24a25

17 files changed

+800
-18
lines changed

commands/bootstrap/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
const dedent = require("dedent");
44
const getPort = require("get-port");
5-
const npmConf = require("npm-conf");
65
const path = require("path");
76
const pFinally = require("p-finally");
87
const pMap = require("p-map");
@@ -11,6 +10,7 @@ const pWaterfall = require("p-waterfall");
1110

1211
const Command = require("@lerna/command");
1312
const rimrafDir = require("@lerna/rimraf-dir");
13+
const npmConf = require("@lerna/npm-conf");
1414
const npmInstall = require("@lerna/npm-install");
1515
const runLifecycle = require("@lerna/run-lifecycle");
1616
const batchPackages = require("@lerna/batch-packages");

commands/bootstrap/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"@lerna/batch-packages": "file:../../utils/batch-packages",
3939
"@lerna/command": "file:../../core/command",
4040
"@lerna/filter-options": "file:../../core/filter-options",
41+
"@lerna/npm-conf": "file:../../utils/npm-conf",
4142
"@lerna/npm-install": "file:../../utils/npm-install",
4243
"@lerna/rimraf-dir": "file:../../utils/rimraf-dir",
4344
"@lerna/run-lifecycle": "file:../../utils/run-lifecycle",
@@ -49,7 +50,6 @@
4950
"get-port": "^3.2.0",
5051
"load-json-file": "^4.0.0",
5152
"multimatch": "^2.1.0",
52-
"npm-conf": "^1.1.3",
5353
"npmlog": "^4.1.2",
5454
"p-finally": "^1.0.0",
5555
"p-map": "^1.2.0",

commands/create/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ const camelCase = require("camelcase");
88
const dedent = require("dedent");
99
const initPackageJson = require("pify")(require("init-package-json"));
1010
const npa = require("npm-package-arg");
11-
const npmConf = require("npm-conf");
1211
const slash = require("slash");
1312

1413
const Command = require("@lerna/command");
1514
const ChildProcessUtilities = require("@lerna/child-process");
15+
const npmConf = require("@lerna/npm-conf");
1616
const ValidationError = require("@lerna/validation-error");
1717
const builtinNpmrc = require("./lib/builtin-npmrc");
1818
const catFile = require("./lib/cat-file");

commands/create/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@
3434
"dependencies": {
3535
"@lerna/child-process": "file:../../core/child-process",
3636
"@lerna/command": "file:../../core/command",
37+
"@lerna/npm-conf": "file:../../utils/npm-conf",
3738
"@lerna/validation-error": "file:../../core/validation-error",
3839
"camelcase": "^4.1.0",
3940
"dedent": "^0.7.0",
4041
"fs-extra": "^5.0.0",
4142
"globby": "^8.0.1",
4243
"init-package-json": "^1.10.3",
43-
"npm-conf": "^1.1.3",
4444
"npm-package-arg": "^6.0.0",
4545
"pify": "^3.0.0",
4646
"semver": "^5.5.0",

commands/publish/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const os = require("os");
44
const chalk = require("chalk");
55
const dedent = require("dedent");
66
const minimatch = require("minimatch");
7-
const npmConf = require("npm-conf");
87
const path = require("path");
98
const pFinally = require("p-finally");
109
const pMap = require("p-map");
@@ -20,6 +19,7 @@ const GitUtilities = require("@lerna/git-utils");
2019
const PromptUtilities = require("@lerna/prompt");
2120
const output = require("@lerna/output");
2221
const collectUpdates = require("@lerna/collect-updates");
22+
const npmConf = require("@lerna/npm-conf");
2323
const npmDistTag = require("@lerna/npm-dist-tag");
2424
const npmPublish = require("@lerna/npm-publish");
2525
const runLifecycle = require("@lerna/run-lifecycle");

commands/publish/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"@lerna/command": "file:../../core/command",
3737
"@lerna/conventional-commits": "file:../../core/conventional-commits",
3838
"@lerna/git-utils": "file:../../core/git-utils",
39+
"@lerna/npm-conf": "file:../../utils/npm-conf",
3940
"@lerna/npm-dist-tag": "file:../../utils/npm-dist-tag",
4041
"@lerna/npm-publish": "file:../../utils/npm-publish",
4142
"@lerna/output": "file:../../utils/output",
@@ -46,7 +47,6 @@
4647
"chalk": "^2.3.1",
4748
"dedent": "^0.7.0",
4849
"minimatch": "^3.0.4",
49-
"npm-conf": "^1.1.3",
5050
"p-finally": "^1.0.0",
5151
"p-map": "^1.2.0",
5252
"p-reduce": "^1.0.0",

package-lock.json

Lines changed: 10 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

utils/npm-conf/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# `@lerna/npm-conf`
2+
3+
> Vendored npm-conf with updates
4+
5+
## Usage
6+
7+
See [npm-conf](https://github.com/kevva/npm-conf#readme)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"use strict";
2+
3+
const npmConf = require("..");
4+
5+
describe("@lerna/npm-conf", () => {
6+
it("exports default factory", () => {
7+
expect(npmConf).toBeDefined();
8+
expect(typeof npmConf).toBe("function");
9+
});
10+
11+
it("exports named defaults", () => {
12+
const { defaults } = npmConf;
13+
expect(defaults).toBeDefined();
14+
expect(typeof defaults).toBe("object");
15+
});
16+
17+
it("exports named Conf", () => {
18+
const { Conf } = npmConf;
19+
expect(Conf).toBeDefined();
20+
expect(typeof Conf).toBe("function");
21+
});
22+
23+
it("exports named toNerfDart", () => {
24+
const { toNerfDart } = npmConf;
25+
expect(toNerfDart).toBeDefined();
26+
expect(typeof toNerfDart).toBe("function");
27+
expect(toNerfDart("https://npm.example.com")).toBe("//npm.example.com/");
28+
expect(toNerfDart("https://npm.example.com/some-api/npm-virtual/")).toBe(
29+
"//npm.example.com/some-api/npm-virtual/"
30+
);
31+
});
32+
});

utils/npm-conf/lib/conf.js

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
"use strict";
2+
3+
const fs = require("fs");
4+
const path = require("path");
5+
const { ConfigChain } = require("config-chain");
6+
const findPrefix = require("./find-prefix");
7+
const parseField = require("./parse-field");
8+
9+
class Conf extends ConfigChain {
10+
// https://github.com/npm/npm/blob/latest/lib/config/core.js#L208-L222
11+
constructor(base) {
12+
super(base);
13+
this.root = base;
14+
}
15+
16+
// https://github.com/npm/npm/blob/latest/lib/config/core.js#L332-L342
17+
add(data, marker) {
18+
try {
19+
for (const x of Object.keys(data)) {
20+
// eslint-disable-next-line no-param-reassign
21+
data[x] = parseField(data[x], x);
22+
}
23+
} catch (err) {
24+
throw err;
25+
}
26+
27+
return super.add(data, marker);
28+
}
29+
30+
// https://github.com/npm/npm/blob/latest/lib/config/core.js#L312-L325
31+
addFile(file, name = file) {
32+
const marker = { __source__: name };
33+
34+
this.sources[name] = { path: file, type: "ini" };
35+
this.push(marker);
36+
this._await();
37+
38+
try {
39+
const contents = fs.readFileSync(file, "utf8");
40+
this.addString(contents, file, "ini", marker);
41+
} catch (err) {
42+
this.add({}, marker);
43+
}
44+
45+
return this;
46+
}
47+
48+
// https://github.com/npm/npm/blob/latest/lib/config/core.js#L344-L360
49+
addEnv(env = process.env) {
50+
const conf = {};
51+
52+
Object.keys(env)
53+
.filter(x => /^npm_config_/i.test(x))
54+
.forEach(x => {
55+
if (!env[x]) {
56+
return;
57+
}
58+
59+
// leave first char untouched, even if it is a '_'
60+
// convert all other '_' to '-'
61+
const p = x
62+
.toLowerCase()
63+
.replace(/^npm_config_/, "")
64+
.replace(/(?!^)_/g, "-");
65+
66+
conf[p] = env[x];
67+
});
68+
69+
return super.addEnv("", conf, "env");
70+
}
71+
72+
// https://github.com/npm/npm/blob/latest/lib/config/load-prefix.js
73+
loadPrefix() {
74+
const cli = this.list[0];
75+
76+
Object.defineProperty(this, "prefix", {
77+
enumerable: true,
78+
set: prefix => {
79+
const g = this.get("global");
80+
this[g ? "globalPrefix" : "localPrefix"] = prefix;
81+
},
82+
get: () => {
83+
const g = this.get("global");
84+
return g ? this.globalPrefix : this.localPrefix;
85+
},
86+
});
87+
88+
Object.defineProperty(this, "globalPrefix", {
89+
enumerable: true,
90+
set: prefix => {
91+
this.set("prefix", prefix);
92+
},
93+
get: () => path.resolve(this.get("prefix")),
94+
});
95+
96+
let p;
97+
98+
Object.defineProperty(this, "localPrefix", {
99+
enumerable: true,
100+
set: prefix => {
101+
p = prefix;
102+
},
103+
get: () => p,
104+
});
105+
106+
if (Object.prototype.hasOwnProperty.call(cli, "prefix")) {
107+
p = path.resolve(cli.prefix);
108+
} else {
109+
try {
110+
p = findPrefix(process.cwd());
111+
} catch (err) {
112+
throw err;
113+
}
114+
}
115+
116+
return p;
117+
}
118+
119+
// https://github.com/npm/npm/blob/latest/lib/config/load-cafile.js
120+
loadCAFile(file) {
121+
if (!file) {
122+
return;
123+
}
124+
125+
try {
126+
const contents = fs.readFileSync(file, "utf8");
127+
const delim = "-----END CERTIFICATE-----";
128+
const output = contents
129+
.split(delim)
130+
.filter(x => Boolean(x.trim()))
131+
.map(x => x.trimLeft() + delim);
132+
133+
this.set("ca", output);
134+
} catch (err) {
135+
if (err.code === "ENOENT") {
136+
return;
137+
}
138+
139+
throw err;
140+
}
141+
}
142+
143+
// https://github.com/npm/npm/blob/latest/lib/config/set-user.js
144+
loadUser() {
145+
const defConf = this.root;
146+
147+
if (this.get("global")) {
148+
return;
149+
}
150+
151+
if (process.env.SUDO_UID) {
152+
defConf.user = Number(process.env.SUDO_UID);
153+
return;
154+
}
155+
156+
const prefix = path.resolve(this.get("prefix"));
157+
158+
try {
159+
const stats = fs.statSync(prefix);
160+
defConf.user = stats.uid;
161+
} catch (err) {
162+
if (err.code === "ENOENT") {
163+
return;
164+
}
165+
166+
throw err;
167+
}
168+
}
169+
}
170+
171+
module.exports = Conf;

0 commit comments

Comments
 (0)