-
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(utils): Add "vendored" npm-conf
This contains updates to the defaults and types from npm 5.8.0
- Loading branch information
Showing
17 changed files
with
800 additions
and
18 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
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
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
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) |
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 |
---|---|---|
@@ -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/" | ||
); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -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; |
Oops, something went wrong.