Skip to content

Commit

Permalink
feat(utils): Add "vendored" npm-conf
Browse files Browse the repository at this point in the history
This contains updates to the defaults and types from npm 5.8.0
  • Loading branch information
evocateur committed Mar 20, 2018
1 parent 98c8be6 commit 9c24a25
Show file tree
Hide file tree
Showing 17 changed files with 800 additions and 18 deletions.
2 changes: 1 addition & 1 deletion commands/bootstrap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const dedent = require("dedent");
const getPort = require("get-port");
const npmConf = require("npm-conf");
const path = require("path");
const pFinally = require("p-finally");
const pMap = require("p-map");
Expand All @@ -11,6 +10,7 @@ const pWaterfall = require("p-waterfall");

const Command = require("@lerna/command");
const rimrafDir = require("@lerna/rimraf-dir");
const npmConf = require("@lerna/npm-conf");
const npmInstall = require("@lerna/npm-install");
const runLifecycle = require("@lerna/run-lifecycle");
const batchPackages = require("@lerna/batch-packages");
Expand Down
2 changes: 1 addition & 1 deletion commands/bootstrap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@lerna/batch-packages": "file:../../utils/batch-packages",
"@lerna/command": "file:../../core/command",
"@lerna/filter-options": "file:../../core/filter-options",
"@lerna/npm-conf": "file:../../utils/npm-conf",
"@lerna/npm-install": "file:../../utils/npm-install",
"@lerna/rimraf-dir": "file:../../utils/rimraf-dir",
"@lerna/run-lifecycle": "file:../../utils/run-lifecycle",
Expand All @@ -49,7 +50,6 @@
"get-port": "^3.2.0",
"load-json-file": "^4.0.0",
"multimatch": "^2.1.0",
"npm-conf": "^1.1.3",
"npmlog": "^4.1.2",
"p-finally": "^1.0.0",
"p-map": "^1.2.0",
Expand Down
2 changes: 1 addition & 1 deletion commands/create/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ const camelCase = require("camelcase");
const dedent = require("dedent");
const initPackageJson = require("pify")(require("init-package-json"));
const npa = require("npm-package-arg");
const npmConf = require("npm-conf");
const slash = require("slash");

const Command = require("@lerna/command");
const ChildProcessUtilities = require("@lerna/child-process");
const npmConf = require("@lerna/npm-conf");
const ValidationError = require("@lerna/validation-error");
const builtinNpmrc = require("./lib/builtin-npmrc");
const catFile = require("./lib/cat-file");
Expand Down
2 changes: 1 addition & 1 deletion commands/create/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@
"dependencies": {
"@lerna/child-process": "file:../../core/child-process",
"@lerna/command": "file:../../core/command",
"@lerna/npm-conf": "file:../../utils/npm-conf",
"@lerna/validation-error": "file:../../core/validation-error",
"camelcase": "^4.1.0",
"dedent": "^0.7.0",
"fs-extra": "^5.0.0",
"globby": "^8.0.1",
"init-package-json": "^1.10.3",
"npm-conf": "^1.1.3",
"npm-package-arg": "^6.0.0",
"pify": "^3.0.0",
"semver": "^5.5.0",
Expand Down
2 changes: 1 addition & 1 deletion commands/publish/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const os = require("os");
const chalk = require("chalk");
const dedent = require("dedent");
const minimatch = require("minimatch");
const npmConf = require("npm-conf");
const path = require("path");
const pFinally = require("p-finally");
const pMap = require("p-map");
Expand All @@ -20,6 +19,7 @@ const GitUtilities = require("@lerna/git-utils");
const PromptUtilities = require("@lerna/prompt");
const output = require("@lerna/output");
const collectUpdates = require("@lerna/collect-updates");
const npmConf = require("@lerna/npm-conf");
const npmDistTag = require("@lerna/npm-dist-tag");
const npmPublish = require("@lerna/npm-publish");
const runLifecycle = require("@lerna/run-lifecycle");
Expand Down
2 changes: 1 addition & 1 deletion commands/publish/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@lerna/command": "file:../../core/command",
"@lerna/conventional-commits": "file:../../core/conventional-commits",
"@lerna/git-utils": "file:../../core/git-utils",
"@lerna/npm-conf": "file:../../utils/npm-conf",
"@lerna/npm-dist-tag": "file:../../utils/npm-dist-tag",
"@lerna/npm-publish": "file:../../utils/npm-publish",
"@lerna/output": "file:../../utils/output",
Expand All @@ -46,7 +47,6 @@
"chalk": "^2.3.1",
"dedent": "^0.7.0",
"minimatch": "^3.0.4",
"npm-conf": "^1.1.3",
"p-finally": "^1.0.0",
"p-map": "^1.2.0",
"p-reduce": "^1.0.0",
Expand Down
22 changes: 10 additions & 12 deletions package-lock.json

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

7 changes: 7 additions & 0 deletions utils/npm-conf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# `@lerna/npm-conf`

> Vendored npm-conf with updates
## Usage

See [npm-conf](https://github.com/kevva/npm-conf#readme)
32 changes: 32 additions & 0 deletions utils/npm-conf/__tests__/npm-conf.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"use strict";

const npmConf = require("..");

describe("@lerna/npm-conf", () => {
it("exports default factory", () => {
expect(npmConf).toBeDefined();
expect(typeof npmConf).toBe("function");
});

it("exports named defaults", () => {
const { defaults } = npmConf;
expect(defaults).toBeDefined();
expect(typeof defaults).toBe("object");
});

it("exports named Conf", () => {
const { Conf } = npmConf;
expect(Conf).toBeDefined();
expect(typeof Conf).toBe("function");
});

it("exports named toNerfDart", () => {
const { toNerfDart } = npmConf;
expect(toNerfDart).toBeDefined();
expect(typeof toNerfDart).toBe("function");
expect(toNerfDart("https://npm.example.com")).toBe("//npm.example.com/");
expect(toNerfDart("https://npm.example.com/some-api/npm-virtual/")).toBe(
"//npm.example.com/some-api/npm-virtual/"
);
});
});
171 changes: 171 additions & 0 deletions utils/npm-conf/lib/conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
"use strict";

const fs = require("fs");
const path = require("path");
const { ConfigChain } = require("config-chain");
const findPrefix = require("./find-prefix");
const parseField = require("./parse-field");

class Conf extends ConfigChain {
// https://github.com/npm/npm/blob/latest/lib/config/core.js#L208-L222
constructor(base) {
super(base);
this.root = base;
}

// https://github.com/npm/npm/blob/latest/lib/config/core.js#L332-L342
add(data, marker) {
try {
for (const x of Object.keys(data)) {
// eslint-disable-next-line no-param-reassign
data[x] = parseField(data[x], x);
}
} catch (err) {
throw err;
}

return super.add(data, marker);
}

// https://github.com/npm/npm/blob/latest/lib/config/core.js#L312-L325
addFile(file, name = file) {
const marker = { __source__: name };

this.sources[name] = { path: file, type: "ini" };
this.push(marker);
this._await();

try {
const contents = fs.readFileSync(file, "utf8");
this.addString(contents, file, "ini", marker);
} catch (err) {
this.add({}, marker);
}

return this;
}

// https://github.com/npm/npm/blob/latest/lib/config/core.js#L344-L360
addEnv(env = process.env) {
const conf = {};

Object.keys(env)
.filter(x => /^npm_config_/i.test(x))
.forEach(x => {
if (!env[x]) {
return;
}

// leave first char untouched, even if it is a '_'
// convert all other '_' to '-'
const p = x
.toLowerCase()
.replace(/^npm_config_/, "")
.replace(/(?!^)_/g, "-");

conf[p] = env[x];
});

return super.addEnv("", conf, "env");
}

// https://github.com/npm/npm/blob/latest/lib/config/load-prefix.js
loadPrefix() {
const cli = this.list[0];

Object.defineProperty(this, "prefix", {
enumerable: true,
set: prefix => {
const g = this.get("global");
this[g ? "globalPrefix" : "localPrefix"] = prefix;
},
get: () => {
const g = this.get("global");
return g ? this.globalPrefix : this.localPrefix;
},
});

Object.defineProperty(this, "globalPrefix", {
enumerable: true,
set: prefix => {
this.set("prefix", prefix);
},
get: () => path.resolve(this.get("prefix")),
});

let p;

Object.defineProperty(this, "localPrefix", {
enumerable: true,
set: prefix => {
p = prefix;
},
get: () => p,
});

if (Object.prototype.hasOwnProperty.call(cli, "prefix")) {
p = path.resolve(cli.prefix);
} else {
try {
p = findPrefix(process.cwd());
} catch (err) {
throw err;
}
}

return p;
}

// https://github.com/npm/npm/blob/latest/lib/config/load-cafile.js
loadCAFile(file) {
if (!file) {
return;
}

try {
const contents = fs.readFileSync(file, "utf8");
const delim = "-----END CERTIFICATE-----";
const output = contents
.split(delim)
.filter(x => Boolean(x.trim()))
.map(x => x.trimLeft() + delim);

this.set("ca", output);
} catch (err) {
if (err.code === "ENOENT") {
return;
}

throw err;
}
}

// https://github.com/npm/npm/blob/latest/lib/config/set-user.js
loadUser() {
const defConf = this.root;

if (this.get("global")) {
return;
}

if (process.env.SUDO_UID) {
defConf.user = Number(process.env.SUDO_UID);
return;
}

const prefix = path.resolve(this.get("prefix"));

try {
const stats = fs.statSync(prefix);
defConf.user = stats.uid;
} catch (err) {
if (err.code === "ENOENT") {
return;
}

throw err;
}
}
}

module.exports = Conf;
Loading

0 comments on commit 9c24a25

Please sign in to comment.