├── .editorconfig ├── .gitattributes ├── .gitignore ├── .npmrc ├── .travis.yml ├── index.js ├── lib ├── conf.js ├── defaults.js ├── make.js ├── types.js └── util.js ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | *.js text eol=lf 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '8' 4 | - '6' 5 | - '4' 6 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const path = require('path'); 3 | const Conf = require('./lib/conf'); 4 | const defaults = require('./lib/defaults'); 5 | 6 | // https://github.com/npm/npm/blob/latest/lib/config/core.js#L101-L200 7 | module.exports = opts => { 8 | const conf = new Conf(Object.assign({}, defaults.defaults)); 9 | 10 | conf.add(Object.assign({}, opts), 'cli'); 11 | conf.addEnv(); 12 | conf.loadPrefix(); 13 | 14 | const projectConf = path.resolve(conf.localPrefix, '.npmrc'); 15 | const userConf = conf.get('userconfig'); 16 | 17 | if (!conf.get('global') && projectConf !== userConf) { 18 | conf.addFile(projectConf, 'project'); 19 | } else { 20 | conf.add({}, 'project'); 21 | } 22 | 23 | conf.addFile(conf.get('userconfig'), 'user'); 24 | 25 | if (conf.get('prefix')) { 26 | const etc = path.resolve(conf.get('prefix'), 'etc'); 27 | conf.root.globalconfig = path.resolve(etc, 'npmrc'); 28 | conf.root.globalignorefile = path.resolve(etc, 'npmignore'); 29 | } 30 | 31 | conf.addFile(conf.get('globalconfig'), 'global'); 32 | conf.loadUser(); 33 | 34 | const caFile = conf.get('cafile'); 35 | 36 | if (caFile) { 37 | conf.loadCAFile(caFile); 38 | } 39 | 40 | return conf; 41 | }; 42 | 43 | module.exports.defaults = Object.assign({}, defaults.defaults); 44 | -------------------------------------------------------------------------------- /lib/conf.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const fs = require('fs'); 3 | const path = require('path'); 4 | const ConfigChain = require('config-chain').ConfigChain; 5 | const util = require('./util'); 6 | 7 | class Conf extends ConfigChain { 8 | // https://github.com/npm/npm/blob/latest/lib/config/core.js#L208-L222 9 | constructor(base) { 10 | super(base); 11 | this.root = base; 12 | } 13 | 14 | // https://github.com/npm/npm/blob/latest/lib/config/core.js#L332-L342 15 | add(data, marker) { 16 | try { 17 | for (const x of Object.keys(data)) { 18 | data[x] = util.parseField(data[x], x); 19 | } 20 | } catch (err) { 21 | throw err; 22 | } 23 | 24 | return super.add(data, marker); 25 | } 26 | 27 | // https://github.com/npm/npm/blob/latest/lib/config/core.js#L312-L325 28 | addFile(file, name) { 29 | name = name || file; 30 | 31 | const marker = {__source__: name}; 32 | 33 | this.sources[name] = {path: file, type: 'ini'}; 34 | this.push(marker); 35 | this._await(); 36 | 37 | try { 38 | const contents = fs.readFileSync(file, 'utf8'); 39 | this.addString(contents, file, 'ini', marker); 40 | } catch (err) { 41 | this.add({}, marker); 42 | } 43 | 44 | return this; 45 | } 46 | 47 | // https://github.com/npm/npm/blob/latest/lib/config/core.js#L344-L360 48 | addEnv(env) { 49 | env = env || process.env; 50 | 51 | const conf = {}; 52 | 53 | Object.keys(env) 54 | .filter(x => /^npm_config_/i.test(x)) 55 | .forEach(x => { 56 | if (!env[x]) { 57 | return; 58 | } 59 | 60 | const p = x.toLowerCase() 61 | .replace(/^npm_config_/, '') 62 | .replace(/(?!^)_/g, '-'); 63 | 64 | conf[p] = env[x]; 65 | }); 66 | 67 | return super.addEnv('', conf, 'env'); 68 | } 69 | 70 | // https://github.com/npm/npm/blob/latest/lib/config/load-prefix.js 71 | loadPrefix() { 72 | const cli = this.list[0]; 73 | 74 | Object.defineProperty(this, 'prefix', { 75 | enumerable: true, 76 | set: prefix => { 77 | const g = this.get('global'); 78 | this[g ? 'globalPrefix' : 'localPrefix'] = prefix; 79 | }, 80 | get: () => { 81 | const g = this.get('global'); 82 | return g ? this.globalPrefix : this.localPrefix; 83 | } 84 | }); 85 | 86 | Object.defineProperty(this, 'globalPrefix', { 87 | enumerable: true, 88 | set: prefix => { 89 | this.set('prefix', prefix); 90 | }, 91 | get: () => { 92 | return path.resolve(this.get('prefix')); 93 | } 94 | }); 95 | 96 | let p; 97 | 98 | Object.defineProperty(this, 'localPrefix', { 99 | enumerable: true, 100 | set: prefix => { 101 | p = prefix; 102 | }, 103 | get: () => { 104 | return p; 105 | } 106 | }); 107 | 108 | if (Object.prototype.hasOwnProperty.call(cli, 'prefix')) { 109 | p = path.resolve(cli.prefix); 110 | } else { 111 | try { 112 | const prefix = util.findPrefix(process.cwd()); 113 | p = prefix; 114 | } catch (err) { 115 | throw err; 116 | } 117 | } 118 | 119 | return p; 120 | } 121 | 122 | // https://github.com/npm/npm/blob/latest/lib/config/load-cafile.js 123 | loadCAFile(file) { 124 | if (!file) { 125 | return; 126 | } 127 | 128 | try { 129 | const contents = fs.readFileSync(file, 'utf8'); 130 | const delim = '-----END CERTIFICATE-----'; 131 | const output = contents 132 | .split(delim) 133 | .filter(x => Boolean(x.trim())) 134 | .map(x => x.trimLeft() + delim); 135 | 136 | this.set('ca', output); 137 | } catch (err) { 138 | if (err.code === 'ENOENT') { 139 | return; 140 | } 141 | 142 | throw err; 143 | } 144 | } 145 | 146 | // https://github.com/npm/npm/blob/latest/lib/config/set-user.js 147 | loadUser() { 148 | const defConf = this.root; 149 | 150 | if (this.get('global')) { 151 | return; 152 | } 153 | 154 | if (process.env.SUDO_UID) { 155 | defConf.user = Number(process.env.SUDO_UID); 156 | return; 157 | } 158 | 159 | const prefix = path.resolve(this.get('prefix')); 160 | 161 | try { 162 | const stats = fs.statSync(prefix); 163 | defConf.user = stats.uid; 164 | } catch (err) { 165 | if (err.code === 'ENOENT') { 166 | return; 167 | } 168 | 169 | throw err; 170 | } 171 | } 172 | } 173 | 174 | module.exports = Conf; 175 | -------------------------------------------------------------------------------- /lib/defaults.js: -------------------------------------------------------------------------------- 1 | 2 | // Generated with `lib/make.js` 3 | 'use strict'; 4 | const os = require('os'); 5 | const path = require('path'); 6 | 7 | const temp = os.tmpdir(); 8 | const uidOrPid = process.getuid ? process.getuid() : process.pid; 9 | const hasUnicode = () => true; 10 | const isWindows = process.platform === 'win32'; 11 | 12 | const osenv = { 13 | editor: () => process.env.EDITOR || process.env.VISUAL || (isWindows ? 'notepad.exe' : 'vi'), 14 | shell: () => isWindows ? (process.env.COMSPEC || 'cmd.exe') : (process.env.SHELL || '/bin/bash') 15 | }; 16 | 17 | const umask = { 18 | fromString: () => process.umask() 19 | }; 20 | 21 | let home = os.homedir(); 22 | 23 | if (home) { 24 | process.env.HOME = home; 25 | } else { 26 | home = path.resolve(temp, 'npm-' + uidOrPid); 27 | } 28 | 29 | const cacheExtra = process.platform === 'win32' ? 'npm-cache' : '.npm'; 30 | const cacheRoot = process.platform === 'win32' && process.env.APPDATA || home; 31 | const cache = path.resolve(cacheRoot, cacheExtra); 32 | 33 | let defaults; 34 | let globalPrefix; 35 | 36 | Object.defineProperty(exports, 'defaults', { 37 | get: function () { 38 | if (defaults) return defaults; 39 | 40 | if (process.env.PREFIX) { 41 | globalPrefix = process.env.PREFIX; 42 | } else if (process.platform === 'win32') { 43 | // c:\node\node.exe --> prefix=c:\node\ 44 | globalPrefix = path.dirname(process.execPath); 45 | } else { 46 | // /usr/local/bin/node --> prefix=/usr/local 47 | globalPrefix = path.dirname(path.dirname(process.execPath)); // destdir only is respected on Unix 48 | 49 | if (process.env.DESTDIR) { 50 | globalPrefix = path.join(process.env.DESTDIR, globalPrefix); 51 | } 52 | } 53 | 54 | defaults = { 55 | access: null, 56 | 'allow-same-version': false, 57 | 'always-auth': false, 58 | also: null, 59 | 'auth-type': 'legacy', 60 | 'bin-links': true, 61 | browser: null, 62 | ca: null, 63 | cafile: null, 64 | cache: cache, 65 | 'cache-lock-stale': 60000, 66 | 'cache-lock-retries': 10, 67 | 'cache-lock-wait': 10000, 68 | 'cache-max': Infinity, 69 | 'cache-min': 10, 70 | cert: null, 71 | color: true, 72 | depth: Infinity, 73 | description: true, 74 | dev: false, 75 | 'dry-run': false, 76 | editor: osenv.editor(), 77 | 'engine-strict': false, 78 | force: false, 79 | 'fetch-retries': 2, 80 | 'fetch-retry-factor': 10, 81 | 'fetch-retry-mintimeout': 10000, 82 | 'fetch-retry-maxtimeout': 60000, 83 | git: 'git', 84 | 'git-tag-version': true, 85 | global: false, 86 | globalconfig: path.resolve(globalPrefix, 'etc', 'npmrc'), 87 | 'global-style': false, 88 | group: process.platform === 'win32' ? 0 : process.env.SUDO_GID || process.getgid && process.getgid(), 89 | 'ham-it-up': false, 90 | heading: 'npm', 91 | 'if-present': false, 92 | 'ignore-prepublish': false, 93 | 'ignore-scripts': false, 94 | 'init-module': path.resolve(home, '.npm-init.js'), 95 | 'init-author-name': '', 96 | 'init-author-email': '', 97 | 'init-author-url': '', 98 | 'init-version': '1.0.0', 99 | 'init-license': 'ISC', 100 | json: false, 101 | key: null, 102 | 'legacy-bundling': false, 103 | link: false, 104 | 'local-address': undefined, 105 | loglevel: 'notice', 106 | logstream: process.stderr, 107 | 'logs-max': 10, 108 | long: false, 109 | maxsockets: 50, 110 | message: '%s', 111 | 'metrics-registry': null, 112 | 'node-version': process.version, 113 | 'offline': false, 114 | 'onload-script': false, 115 | only: null, 116 | optional: true, 117 | 'package-lock': true, 118 | parseable: false, 119 | 'prefer-offline': false, 120 | 'prefer-online': false, 121 | prefix: globalPrefix, 122 | production: process.env.NODE_ENV === 'production', 123 | 'progress': !process.env.TRAVIS && !process.env.CI, 124 | 'proprietary-attribs': true, 125 | proxy: null, 126 | 'https-proxy': null, 127 | 'user-agent': 'npm/{npm-version} ' + 'node/{node-version} ' + '{platform} ' + '{arch}', 128 | 'rebuild-bundle': true, 129 | registry: 'https://registry.npmjs.org/', 130 | rollback: true, 131 | save: true, 132 | 'save-bundle': false, 133 | 'save-dev': false, 134 | 'save-exact': false, 135 | 'save-optional': false, 136 | 'save-prefix': '^', 137 | 'save-prod': false, 138 | scope: '', 139 | 'script-shell': null, 140 | 'scripts-prepend-node-path': 'warn-only', 141 | searchopts: '', 142 | searchexclude: null, 143 | searchlimit: 20, 144 | searchstaleness: 15 * 60, 145 | 'send-metrics': false, 146 | shell: osenv.shell(), 147 | shrinkwrap: true, 148 | 'sign-git-tag': false, 149 | 'sso-poll-frequency': 500, 150 | 'sso-type': 'oauth', 151 | 'strict-ssl': true, 152 | tag: 'latest', 153 | 'tag-version-prefix': 'v', 154 | timing: false, 155 | tmp: temp, 156 | unicode: hasUnicode(), 157 | 'unsafe-perm': process.platform === 'win32' || process.platform === 'cygwin' || !(process.getuid && process.setuid && process.getgid && process.setgid) || process.getuid() !== 0, 158 | usage: false, 159 | user: process.platform === 'win32' ? 0 : 'nobody', 160 | userconfig: path.resolve(home, '.npmrc'), 161 | umask: process.umask ? process.umask() : umask.fromString('022'), 162 | version: false, 163 | versions: false, 164 | viewer: process.platform === 'win32' ? 'browser' : 'man', 165 | _exit: true 166 | }; 167 | return defaults; 168 | } 169 | }) 170 | -------------------------------------------------------------------------------- /lib/make.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const fs = require('fs'); 3 | const path = require('path'); 4 | const babylon = require('babylon'); 5 | const generate = require('babel-generator').default; 6 | const traverse = require('babel-traverse').default; 7 | 8 | const defaultsTemplate = body => ` 9 | // Generated with \`lib/make.js\` 10 | 'use strict'; 11 | const os = require('os'); 12 | const path = require('path'); 13 | 14 | const temp = os.tmpdir(); 15 | const uidOrPid = process.getuid ? process.getuid() : process.pid; 16 | const hasUnicode = () => true; 17 | const isWindows = process.platform === 'win32'; 18 | 19 | const osenv = { 20 | editor: () => process.env.EDITOR || process.env.VISUAL || (isWindows ? 'notepad.exe' : 'vi'), 21 | shell: () => isWindows ? (process.env.COMSPEC || 'cmd.exe') : (process.env.SHELL || '/bin/bash') 22 | }; 23 | 24 | const umask = { 25 | fromString: () => process.umask() 26 | }; 27 | 28 | let home = os.homedir(); 29 | 30 | if (home) { 31 | process.env.HOME = home; 32 | } else { 33 | home = path.resolve(temp, 'npm-' + uidOrPid); 34 | } 35 | 36 | const cacheExtra = process.platform === 'win32' ? 'npm-cache' : '.npm'; 37 | const cacheRoot = process.platform === 'win32' ? process.env.APPDATA : home; 38 | const cache = path.resolve(cacheRoot, cacheExtra); 39 | 40 | let defaults; 41 | let globalPrefix; 42 | 43 | ${body} 44 | `; 45 | 46 | const typesTemplate = body => ` 47 | // Generated with \`lib/make.js\` 48 | 'use strict'; 49 | const path = require('path'); 50 | const Stream = require('stream').Stream; 51 | const url = require('url'); 52 | 53 | const Umask = () => {}; 54 | const getLocalAddresses = () => []; 55 | const semver = () => {}; 56 | 57 | ${body} 58 | `; 59 | 60 | const defaults = require.resolve('npm/lib/config/defaults'); 61 | const ast = babylon.parse(fs.readFileSync(defaults, 'utf8')); 62 | 63 | const isDefaults = node => 64 | node.callee.type === 'MemberExpression' && 65 | node.callee.object.name === 'Object' && 66 | node.callee.property.name === 'defineProperty' && 67 | node.arguments.some(x => x.name === 'exports'); 68 | 69 | const isTypes = node => 70 | node.type === 'MemberExpression' && 71 | node.object.name === 'exports' && 72 | node.property.name === 'types'; 73 | 74 | let defs; 75 | let types; 76 | 77 | traverse(ast, { 78 | CallExpression(path) { 79 | if (isDefaults(path.node)) { 80 | defs = path.node; 81 | } 82 | }, 83 | AssignmentExpression(path) { 84 | if (path.node.left && isTypes(path.node.left)) { 85 | types = path.node; 86 | } 87 | } 88 | }); 89 | 90 | fs.writeFileSync(path.join(__dirname, 'defaults.js'), defaultsTemplate(generate(defs, {}, ast).code)); 91 | fs.writeFileSync(path.join(__dirname, 'types.js'), typesTemplate(generate(types, {}, ast).code)); 92 | -------------------------------------------------------------------------------- /lib/types.js: -------------------------------------------------------------------------------- 1 | 2 | // Generated with `lib/make.js` 3 | 'use strict'; 4 | const path = require('path'); 5 | const Stream = require('stream').Stream; 6 | const url = require('url'); 7 | 8 | const Umask = () => {}; 9 | const getLocalAddresses = () => []; 10 | const semver = () => {}; 11 | 12 | exports.types = { 13 | access: [null, 'restricted', 'public'], 14 | 'allow-same-version': Boolean, 15 | 'always-auth': Boolean, 16 | also: [null, 'dev', 'development'], 17 | 'auth-type': ['legacy', 'sso', 'saml', 'oauth'], 18 | 'bin-links': Boolean, 19 | browser: [null, String], 20 | ca: [null, String, Array], 21 | cafile: path, 22 | cache: path, 23 | 'cache-lock-stale': Number, 24 | 'cache-lock-retries': Number, 25 | 'cache-lock-wait': Number, 26 | 'cache-max': Number, 27 | 'cache-min': Number, 28 | cert: [null, String], 29 | color: ['always', Boolean], 30 | depth: Number, 31 | description: Boolean, 32 | dev: Boolean, 33 | 'dry-run': Boolean, 34 | editor: String, 35 | 'engine-strict': Boolean, 36 | force: Boolean, 37 | 'fetch-retries': Number, 38 | 'fetch-retry-factor': Number, 39 | 'fetch-retry-mintimeout': Number, 40 | 'fetch-retry-maxtimeout': Number, 41 | git: String, 42 | 'git-tag-version': Boolean, 43 | global: Boolean, 44 | globalconfig: path, 45 | 'global-style': Boolean, 46 | group: [Number, String], 47 | 'https-proxy': [null, url], 48 | 'user-agent': String, 49 | 'ham-it-up': Boolean, 50 | 'heading': String, 51 | 'if-present': Boolean, 52 | 'ignore-prepublish': Boolean, 53 | 'ignore-scripts': Boolean, 54 | 'init-module': path, 55 | 'init-author-name': String, 56 | 'init-author-email': String, 57 | 'init-author-url': ['', url], 58 | 'init-license': String, 59 | 'init-version': semver, 60 | json: Boolean, 61 | key: [null, String], 62 | 'legacy-bundling': Boolean, 63 | link: Boolean, 64 | // local-address must be listed as an IP for a local network interface 65 | // must be IPv4 due to node bug 66 | 'local-address': getLocalAddresses(), 67 | loglevel: ['silent', 'error', 'warn', 'notice', 'http', 'timing', 'info', 'verbose', 'silly'], 68 | logstream: Stream, 69 | 'logs-max': Number, 70 | long: Boolean, 71 | maxsockets: Number, 72 | message: String, 73 | 'metrics-registry': [null, String], 74 | 'node-version': [null, semver], 75 | offline: Boolean, 76 | 'onload-script': [null, String], 77 | only: [null, 'dev', 'development', 'prod', 'production'], 78 | optional: Boolean, 79 | 'package-lock': Boolean, 80 | parseable: Boolean, 81 | 'prefer-offline': Boolean, 82 | 'prefer-online': Boolean, 83 | prefix: path, 84 | production: Boolean, 85 | progress: Boolean, 86 | 'proprietary-attribs': Boolean, 87 | proxy: [null, false, url], 88 | // allow proxy to be disabled explicitly 89 | 'rebuild-bundle': Boolean, 90 | registry: [null, url], 91 | rollback: Boolean, 92 | save: Boolean, 93 | 'save-bundle': Boolean, 94 | 'save-dev': Boolean, 95 | 'save-exact': Boolean, 96 | 'save-optional': Boolean, 97 | 'save-prefix': String, 98 | 'save-prod': Boolean, 99 | scope: String, 100 | 'script-shell': [null, String], 101 | 'scripts-prepend-node-path': [false, true, 'auto', 'warn-only'], 102 | searchopts: String, 103 | searchexclude: [null, String], 104 | searchlimit: Number, 105 | searchstaleness: Number, 106 | 'send-metrics': Boolean, 107 | shell: String, 108 | shrinkwrap: Boolean, 109 | 'sign-git-tag': Boolean, 110 | 'sso-poll-frequency': Number, 111 | 'sso-type': [null, 'oauth', 'saml'], 112 | 'strict-ssl': Boolean, 113 | tag: String, 114 | timing: Boolean, 115 | tmp: path, 116 | unicode: Boolean, 117 | 'unsafe-perm': Boolean, 118 | usage: Boolean, 119 | user: [Number, String], 120 | userconfig: path, 121 | umask: Umask, 122 | version: Boolean, 123 | 'tag-version-prefix': String, 124 | versions: Boolean, 125 | viewer: String, 126 | _exit: Boolean 127 | } 128 | -------------------------------------------------------------------------------- /lib/util.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const fs = require('fs'); 3 | const path = require('path'); 4 | const types = require('./types'); 5 | 6 | // https://github.com/npm/npm/blob/latest/lib/config/core.js#L409-L423 7 | const envReplace = str => { 8 | if (typeof str !== 'string' || !str) { 9 | return str; 10 | } 11 | 12 | // Replace any ${ENV} values with the appropriate environment 13 | const regex = /(\\*)\$\{([^}]+)\}/g; 14 | 15 | return str.replace(regex, (orig, esc, name) => { 16 | esc = esc.length > 0 && esc.length % 2; 17 | 18 | if (esc) { 19 | return orig; 20 | } 21 | 22 | if (process.env[name] === undefined) { 23 | throw new Error(`Failed to replace env in config: ${orig}`); 24 | } 25 | 26 | return process.env[name]; 27 | }); 28 | }; 29 | 30 | // https://github.com/npm/npm/blob/latest/lib/config/core.js#L362-L407 31 | const parseField = (field, key) => { 32 | if (typeof field !== 'string') { 33 | return field; 34 | } 35 | 36 | const typeList = [].concat(types[key]); 37 | const isPath = typeList.indexOf(path) !== -1; 38 | const isBool = typeList.indexOf(Boolean) !== -1; 39 | const isString = typeList.indexOf(String) !== -1; 40 | const isNumber = typeList.indexOf(Number) !== -1; 41 | 42 | field = `${field}`.trim(); 43 | 44 | if (/^".*"$/.test(field)) { 45 | try { 46 | field = JSON.parse(field); 47 | } catch (err) { 48 | throw new Error(`Failed parsing JSON config key ${key}: ${field}`); 49 | } 50 | } 51 | 52 | if (isBool && !isString && field === '') { 53 | return true; 54 | } 55 | 56 | switch (field) { // eslint-disable-line default-case 57 | case 'true': { 58 | return true; 59 | } 60 | 61 | case 'false': { 62 | return false; 63 | } 64 | 65 | case 'null': { 66 | return null; 67 | } 68 | 69 | case 'undefined': { 70 | return undefined; 71 | } 72 | } 73 | 74 | field = envReplace(field); 75 | 76 | if (isPath) { 77 | const regex = process.platform === 'win32' ? /^~(\/|\\)/ : /^~\//; 78 | 79 | if (regex.test(field) && process.env.HOME) { 80 | field = path.resolve(process.env.HOME, field.substr(2)); 81 | } 82 | 83 | field = path.resolve(field); 84 | } 85 | 86 | if (isNumber && !field.isNan()) { 87 | field = Number(field); 88 | } 89 | 90 | return field; 91 | }; 92 | 93 | // https://github.com/npm/npm/blob/latest/lib/config/find-prefix.js 94 | const findPrefix = name => { 95 | name = path.resolve(name); 96 | 97 | let walkedUp = false; 98 | 99 | while (path.basename(name) === 'node_modules') { 100 | name = path.dirname(name); 101 | walkedUp = true; 102 | } 103 | 104 | if (walkedUp) { 105 | return name; 106 | } 107 | 108 | const find = (name, original) => { 109 | const regex = /^[a-zA-Z]:(\\|\/)?$/; 110 | 111 | if (name === '/' || (process.platform === 'win32' && regex.test(name))) { 112 | return original; 113 | } 114 | 115 | try { 116 | const files = fs.readdirSync(name); 117 | 118 | if (files.indexOf('node_modules') !== -1 || files.indexOf('package.json') !== -1) { 119 | return name; 120 | } 121 | 122 | const dirname = path.dirname(name); 123 | 124 | if (dirname === name) { 125 | return original; 126 | } 127 | 128 | return find(dirname, original); 129 | } catch (err) { 130 | if (name === original) { 131 | if (err.code === 'ENOENT') { 132 | return original; 133 | } 134 | 135 | throw err; 136 | } 137 | 138 | return original; 139 | } 140 | }; 141 | 142 | return find(name, name); 143 | }; 144 | 145 | exports.envReplace = envReplace; 146 | exports.findPrefix = findPrefix; 147 | exports.parseField = parseField; 148 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Kevin Mårtensson (github.com/kevva) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "npm-conf", 3 | "version": "1.1.3", 4 | "description": "Get the npm config", 5 | "license": "MIT", 6 | "repository": "kevva/npm-conf", 7 | "author": { 8 | "name": "Kevin Martensson", 9 | "email": "kevinmartensson@gmail.com", 10 | "url": "github.com/kevva" 11 | }, 12 | "engines": { 13 | "node": ">=4" 14 | }, 15 | "scripts": { 16 | "prepublish": "node lib/make.js", 17 | "test": "xo && ava" 18 | }, 19 | "files": [ 20 | "index.js", 21 | "lib" 22 | ], 23 | "keywords": [ 24 | "conf", 25 | "config", 26 | "global", 27 | "npm", 28 | "path", 29 | "prefix", 30 | "rc" 31 | ], 32 | "dependencies": { 33 | "config-chain": "^1.1.11", 34 | "pify": "^3.0.0" 35 | }, 36 | "devDependencies": { 37 | "ava": "*", 38 | "babel-generator": "^6.24.1", 39 | "babel-traverse": "^6.24.1", 40 | "babylon": "^6.17.1", 41 | "npm": "^5.0.4", 42 | "xo": "*" 43 | }, 44 | "xo": { 45 | "ignores": [ 46 | "lib/defaults.js", 47 | "lib/types.js" 48 | ] 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # npm-conf [![Build Status](https://travis-ci.org/kevva/npm-conf.svg?branch=master)](https://travis-ci.org/kevva/npm-conf) 2 | 3 | > Get the npm config 4 | 5 | 6 | ## Install 7 | 8 | ``` 9 | $ npm install npm-conf 10 | ``` 11 | 12 | 13 | ## Usage 14 | 15 | ```js 16 | const npmConf = require('npm-conf'); 17 | 18 | const conf = npmConf(); 19 | 20 | conf.get('prefix') 21 | //=> //=> /Users/unicorn/.npm-packages 22 | 23 | conf.get('registry') 24 | //=> https://registry.npmjs.org/ 25 | ``` 26 | 27 | To get a list of all available `npm` config options: 28 | 29 | ```bash 30 | $ npm config list --long 31 | ``` 32 | 33 | 34 | ## API 35 | 36 | ### npmConf() 37 | 38 | Returns the `npm` config. 39 | 40 | ### npmConf.defaults 41 | 42 | Returns the default `npm` config. 43 | 44 | 45 | ## License 46 | 47 | MIT © [Kevin Mårtensson](https://github.com/kevva) 48 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import npmCore from 'npm/lib/config/core'; 2 | import npmDefaults from 'npm/lib/config/defaults'; 3 | import pify from 'pify'; 4 | import test from 'ava'; 5 | import m from '.'; 6 | 7 | test('mirror npm config', async t => { 8 | const conf = m(); 9 | const npmconf = await pify(npmCore.load)(); 10 | 11 | t.is(conf.globalPrefix, npmconf.globalPrefix); 12 | t.is(conf.localPrefix, npmconf.localPrefix); 13 | t.is(conf.get('prefix'), npmconf.get('prefix')); 14 | t.is(conf.get('registry'), npmconf.get('registry')); 15 | t.is(conf.get('tmp'), npmconf.get('tmp')); 16 | }); 17 | 18 | test('mirror npm defaults', t => { 19 | t.deepEqual(m.defaults, npmDefaults.defaults); 20 | }); 21 | --------------------------------------------------------------------------------