Skip to content
This repository was archived by the owner on Jun 17, 2021. It is now read-only.

Commit 710426b

Browse files
committed
feat(api): add read() function
1 parent c9e0eea commit 710426b

File tree

4 files changed

+124
-5
lines changed

4 files changed

+124
-5
lines changed

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# libnpmconfig [![npm version](https://img.shields.io/npm/v/libnpmconfig.svg)](https://npm.im/libnpmconfig) [![license](https://img.shields.io/npm/l/libnpmconfig.svg)](https://npm.im/libnpmconfig) [![Travis](https://img.shields.io/travis/npm/libnpmconfig.svg)](https://travis-ci.org/npm/libnpmconfig) [![AppVeyor](https://ci.appveyor.com/api/projects/status/github/zkat/libnpmconfig?svg=true)](https://ci.appveyor.com/project/zkat/libnpmconfig) [![Coverage Status](https://coveralls.io/repos/github/npm/libnpmconfig/badge.svg?branch=latest)](https://coveralls.io/github/npm/libnpmconfig?branch=latest)
2+
3+
[`libnpmconfig`](https://github.com/npm/libnpmconfig) is a Node.js library for
4+
programmatically managing npm's configuration files and data.
5+
6+
## Example
7+
8+
```js
9+
const config = require('libnpmconfig')
10+
11+
console.log('configured registry:', config.read({
12+
registry: 'https://default.registry/'
13+
}))
14+
// => configured registry: https://registry.npmjs.org
15+
```
16+
17+
## Install
18+
19+
`$ npm install libnpmconfig`
20+
21+
## Table of Contents
22+
23+
* [Example](#example)
24+
* [Install](#install)
25+
* [API](#api)
26+
27+
### API
28+
29+
##### <a name="read"></a> `> read(moreOpts, readOpts)`
30+
31+
Reads configurations from the filesystem and the env and returns a
32+
[`figgy-pudding`](https://npm.im/figgy-pudding) object with the configuration
33+
values.
34+
35+
If `moreOpts` is provided, it will be merged with the returned config pudding,
36+
shadowing any read values.
37+
38+
If `readOpts.cwd` is provided, it will be used instead of `process.cwd()` as the
39+
starting point for config searching.
40+
41+
If `readOpts.userconfig` is provided, it will be used as the user configuration
42+
file, shadowing `moreOpts.userconfig`, which is also acceptable.

index.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
'use strict'
2+
3+
const fs = require('fs')
4+
const figgyPudding = require('figgy-pudding')
5+
const ini = require('ini')
6+
const os = require('os')
7+
const path = require('path')
8+
9+
const NpmConfig = figgyPudding({}, {
10+
// Open up the pudding object.
11+
other () { return true }
12+
})
13+
14+
const ConfigOpts = figgyPudding({
15+
cache: { default: path.join(os.homedir(), '.npm') },
16+
cwd: { default: () => process.cwd() },
17+
userconfig: { default: path.join(os.homedir(), '.npmrc') }
18+
})
19+
20+
module.exports.read = getNpmConfig
21+
function getNpmConfig (_opts, configOpts) {
22+
const opts = NpmConfig(_opts)
23+
const copts = ConfigOpts(configOpts)
24+
const configs = copts.cwd.split(path.sep).reduce((acc, next) => {
25+
acc.path = path.join(acc.path, next)
26+
acc.configs.push(maybeReadIni(path.join(acc.path, '.npmrc')))
27+
acc.configs.push(maybeReadIni(path.join(acc.path, 'npmrc')))
28+
return acc
29+
}, {
30+
path: '',
31+
configs: []
32+
}).configs.concat(
33+
maybeReadIni(copts.userconfig || opts.userconfig)
34+
).filter(x => x)
35+
const env = Object.keys(process.env).reduce((acc, key) => {
36+
if (key.match(/^npm_config_/i)) {
37+
const newKey = key.toLowerCase()
38+
.replace(/^npm_config_/i, '')
39+
.replace(/(?!^)_/g, '-')
40+
acc[newKey] = process.env[key]
41+
}
42+
return acc
43+
}, {})
44+
const newOpts = NpmConfig(...configs, env, _opts)
45+
if (newOpts.cache) {
46+
return newOpts.concat({
47+
cache: path.join(newOpts.cache, '_cacache')
48+
})
49+
} else {
50+
return newOpts
51+
}
52+
}
53+
54+
function maybeReadIni (f) {
55+
let txt
56+
try {
57+
txt = fs.readFileSync(f, 'utf8')
58+
} catch (err) {
59+
if (err.code === 'ENOENT') {
60+
return ''
61+
} else {
62+
throw err
63+
}
64+
}
65+
return ini.parse(txt)
66+
}

package-lock.json

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

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{
22
"name": "libnpmconfig",
3+
"version": "1.0.0",
4+
"description": "Standalone library for reading/writing/managing npm configurations",
35
"author": {
46
"name": "Kat Marchán",
57
"email": "kzm@zkat.tech",
@@ -11,7 +13,7 @@
1113
"release": "standard-version -s",
1214
"postrelease": "npm publish && git push --follow-tags",
1315
"pretest": "standard",
14-
"test": "tap -J -100 test/*.js",
16+
"test": "tap -J --100 test/*.js",
1517
"update-coc": "weallbehave -o . && git add CODE_OF_CONDUCT.md && git commit -m 'docs(coc): updated CODE_OF_CONDUCT.md'",
1618
"update-contrib": "weallcontribute -o . && git add CONTRIBUTING.md && git commit -m 'docs(contributing): updated CONTRIBUTING.md'"
1719
},
@@ -27,5 +29,9 @@
2729
"url": "https://github.com/npm/libnpmconfig.git"
2830
},
2931
"bugs": "https://github.com/npm/libnpmconfig/issues",
30-
"homepage": "https://npmjs.com/package/libnpmconfig"
32+
"homepage": "https://npmjs.com/package/libnpmconfig",
33+
"dependencies": {
34+
"figgy-pudding": "^3.5.1",
35+
"ini": "^1.3.5"
36+
}
3137
}

0 commit comments

Comments
 (0)