From aaafab83d0d0ef6f1ff213b19eb466d3e2c4765f Mon Sep 17 00:00:00 2001 From: isaacs Date: Mon, 15 Mar 2021 17:01:10 -0700 Subject: Remove old config definitions and flattening logic PR-URL: https://github.com/npm/cli/pull/2878 Credit: @isaacs Close: #2878 Reviewed-by: @isaacs --- lib/utils/config.js | 394 ---------------------------------------------- lib/utils/flat-options.js | 254 ------------------------------ 2 files changed, 648 deletions(-) delete mode 100644 lib/utils/config.js delete mode 100644 lib/utils/flat-options.js (limited to 'lib/utils') diff --git a/lib/utils/config.js b/lib/utils/config.js deleted file mode 100644 index 3ca976613..000000000 --- a/lib/utils/config.js +++ /dev/null @@ -1,394 +0,0 @@ -// defaults, types, and shorthands - -const { - typeDefs: { - semver: { type: semver }, - Umask: { type: Umask }, - url: { type: url }, - path: { type: path }, - }, -} = require('@npmcli/config') - -const { version: npmVersion } = require('../../package.json') - -const ciDetect = require('@npmcli/ci-detect') -const ciName = ciDetect() - -const isWindows = require('./is-windows.js') - -const editor = process.env.EDITOR || - process.env.VISUAL || - (isWindows ? 'notepad.exe' : 'vi') - -const shell = isWindows ? process.env.ComSpec || 'cmd' - : process.env.SHELL || 'sh' - -const { tmpdir, networkInterfaces } = require('os') -const getLocalAddresses = () => { - try { - return Object.values(networkInterfaces()).map( - int => int.map(({ address }) => address) - ).reduce((set, addrs) => set.concat(addrs), [undefined]) - } catch (e) { - return [undefined] - } -} - -const unicode = /UTF-?8$/i.test( - process.env.LC_ALL || process.env.LC_CTYPE || process.env.LANG -) - -// use LOCALAPPDATA on Windows, if set -// https://github.com/npm/cli/pull/899 -const cacheRoot = (isWindows && process.env.LOCALAPPDATA) || '~' -const cacheExtra = isWindows ? 'npm-cache' : '.npm' -const cache = `${cacheRoot}/${cacheExtra}` - -const defaults = { - access: null, - all: false, - 'allow-same-version': false, - also: null, - 'always-auth': false, - audit: true, - 'audit-level': null, - _auth: null, - 'auth-type': 'legacy', - before: null, - 'bin-links': true, - browser: null, - ca: null, - cache, - 'cache-lock-retries': 10, - 'cache-lock-stale': 60000, - 'cache-lock-wait': 10000, - 'cache-max': Infinity, - 'cache-min': 10, - cafile: null, - call: '', - cert: null, - 'ci-name': ciName || null, - cidr: null, - color: process.env.NO_COLOR == null, - 'commit-hooks': true, - depth: null, - description: true, - dev: false, - diff: [], - 'diff-unified': null, - 'diff-ignore-all-space': false, - 'diff-name-only': false, - 'diff-no-prefix': false, - 'diff-src-prefix': '', - 'diff-dst-prefix': '', - 'diff-text': false, - 'dry-run': false, - editor, - 'engine-strict': false, - 'fetch-retries': 2, - 'fetch-retry-factor': 10, - 'fetch-retry-maxtimeout': 60000, - 'fetch-retry-mintimeout': 10000, - 'fetch-timeout': 5 * 60 * 1000, - force: false, - 'foreground-script': false, - 'format-package-lock': true, - fund: true, - git: 'git', - 'git-tag-version': true, - global: false, - 'global-style': false, - // `globalconfig` has its default defined outside of this module - heading: 'npm', - 'https-proxy': null, - 'if-present': false, - 'ignore-prepublish': false, - 'ignore-scripts': false, - include: [], - 'include-staged': false, - 'init-author-email': '', - 'init-author-name': '', - 'init-author-url': '', - 'init-license': 'ISC', - 'init-module': '~/.npm-init.js', - 'init-version': '1.0.0', - 'init.author.email': '', - 'init.author.name': '', - 'init.author.url': '', - 'init.license': 'ISC', - 'init.module': '~/.npm-init.js', - 'init.version': '1.0.0', - json: false, - key: null, - 'legacy-bundling': false, - 'legacy-peer-deps': false, - link: false, - 'local-address': undefined, - loglevel: 'notice', - 'logs-max': 10, - long: false, - maxsockets: 50, - message: '%s', - 'node-options': null, - 'node-version': process.version, - noproxy: null, - 'npm-version': npmVersion, - offline: false, - omit: [], - only: null, - optional: true, - otp: null, - package: [], - 'package-lock': true, - 'package-lock-only': false, - parseable: false, - 'prefer-offline': false, - 'prefer-online': false, - // `prefix` has its default defined outside of this module - preid: '', - production: process.env.NODE_ENV === 'production', - progress: !ciName, - proxy: null, - 'read-only': false, - 'rebuild-bundle': true, - registry: 'https://registry.npmjs.org/', - rollback: true, - save: true, - 'save-bundle': false, - 'save-dev': false, - 'save-exact': false, - 'save-optional': false, - 'save-prefix': '^', - 'save-prod': false, - scope: '', - 'script-shell': null, - 'scripts-prepend-node-path': 'warn-only', - searchexclude: null, - searchlimit: 20, - searchopts: '', - searchstaleness: 15 * 60, - shell, - shrinkwrap: true, - 'sign-git-commit': false, - 'sign-git-tag': false, - 'sso-poll-frequency': 500, - 'sso-type': 'oauth', - 'strict-peer-deps': false, - 'strict-ssl': true, - tag: 'latest', - 'tag-version-prefix': 'v', - timing: false, - tmp: tmpdir(), - umask: 0, - unicode, - 'update-notifier': true, - usage: false, - 'user-agent': 'npm/{npm-version} ' + - 'node/{node-version} ' + - '{platform} ' + - '{arch} ' + - '{ci}', - userconfig: '~/.npmrc', - version: false, - versions: false, - viewer: isWindows ? 'browser' : 'man', -} - -const types = { - access: [null, 'restricted', 'public'], - all: Boolean, - 'allow-same-version': Boolean, - also: [null, 'dev', 'development'], - _auth: [null, String], - 'always-auth': Boolean, - audit: Boolean, - 'audit-level': ['low', 'moderate', 'high', 'critical', 'none', null], - 'auth-type': ['legacy', 'sso', 'saml', 'oauth'], - before: [null, Date], - 'bin-links': Boolean, - browser: [null, Boolean, String], - ca: [null, String, Array], - cache: path, - 'cache-lock-retries': Number, - 'cache-lock-stale': Number, - 'cache-lock-wait': Number, - 'cache-max': Number, - 'cache-min': Number, - cafile: path, - call: String, - cert: [null, String], - 'ci-name': [null, String], - cidr: [null, String, Array], - color: ['always', Boolean], - 'commit-hooks': Boolean, - depth: [null, Number], - description: Boolean, - dev: Boolean, - diff: [String, Array], - 'diff-unified': [null, Number], - 'diff-ignore-all-space': Boolean, - 'diff-name-only': Boolean, - 'diff-no-prefix': Boolean, - 'diff-src-prefix': String, - 'diff-dst-prefix': String, - 'diff-text': Boolean, - 'dry-run': Boolean, - editor: String, - 'engine-strict': Boolean, - 'fetch-retries': Number, - 'fetch-retry-factor': Number, - 'fetch-retry-maxtimeout': Number, - 'fetch-retry-mintimeout': Number, - 'fetch-timeout': Number, - force: Boolean, - 'foreground-script': Boolean, - 'format-package-lock': Boolean, - fund: Boolean, - git: String, - 'git-tag-version': Boolean, - global: Boolean, - 'global-style': Boolean, - globalconfig: path, - heading: String, - 'https-proxy': [null, url], - 'if-present': Boolean, - 'ignore-prepublish': Boolean, - 'ignore-scripts': Boolean, - include: [Array, 'prod', 'dev', 'optional', 'peer'], - 'include-staged': Boolean, - 'init-author-email': String, - 'init-author-name': String, - 'init-author-url': ['', url], - 'init-license': String, - 'init-module': path, - 'init-version': semver, - 'init.author.email': String, - 'init.author.name': String, - 'init.author.url': ['', url], - 'init.license': String, - 'init.module': path, - 'init.version': semver, - json: Boolean, - key: [null, String], - 'legacy-bundling': Boolean, - 'legacy-peer-deps': Boolean, - link: Boolean, - 'local-address': getLocalAddresses(), - loglevel: [ - 'silent', - 'error', - 'warn', - 'notice', - 'http', - 'timing', - 'info', - 'verbose', - 'silly', - ], - 'logs-max': Number, - long: Boolean, - maxsockets: Number, - message: String, - 'node-options': [null, String], - 'node-version': [null, semver], - noproxy: [null, String, Array], - 'npm-version': semver, - offline: Boolean, - omit: [Array, 'dev', 'optional', 'peer'], - only: [null, 'dev', 'development', 'prod', 'production'], - optional: Boolean, - otp: [null, String], - package: [String, Array], - 'package-lock': Boolean, - 'package-lock-only': Boolean, - parseable: Boolean, - 'prefer-offline': Boolean, - 'prefer-online': Boolean, - prefix: path, - preid: String, - production: Boolean, - progress: Boolean, - proxy: [null, false, url], // allow proxy to be disabled explicitly - 'read-only': Boolean, - 'rebuild-bundle': Boolean, - registry: [null, url], - rollback: Boolean, - save: Boolean, - 'save-bundle': Boolean, - 'save-dev': Boolean, - 'save-exact': Boolean, - 'save-optional': Boolean, - 'save-prefix': String, - 'save-prod': Boolean, - scope: String, - 'script-shell': [null, String], - 'scripts-prepend-node-path': [Boolean, 'auto', 'warn-only'], - searchexclude: [null, String], - searchlimit: Number, - searchopts: String, - searchstaleness: Number, - shell: String, - shrinkwrap: Boolean, - 'sign-git-commit': Boolean, - 'sign-git-tag': Boolean, - 'sso-poll-frequency': Number, - 'sso-type': [null, 'oauth', 'saml'], - 'strict-peer-deps': Boolean, - 'strict-ssl': Boolean, - tag: String, - 'tag-version-prefix': String, - timing: Boolean, - tmp: path, - umask: Umask, - unicode: Boolean, - 'update-notifier': Boolean, - usage: Boolean, - 'user-agent': String, - userconfig: path, - version: Boolean, - versions: Boolean, - viewer: String, -} - -const shorthands = { - '?': ['--usage'], - a: ['--all'], - B: ['--save-bundle'], - C: ['--prefix'], - c: ['--call'], - D: ['--save-dev'], - d: ['--loglevel', 'info'], - dd: ['--loglevel', 'verbose'], - ddd: ['--loglevel', 'silly'], - desc: ['--description'], - E: ['--save-exact'], - 'enjoy-by': ['--before'], - f: ['--force'], - g: ['--global'], - H: ['--usage'], - h: ['--usage'], - help: ['--usage'], - l: ['--long'], - local: ['--no-global'], - m: ['--message'], - n: ['--no-yes'], - 'no-desc': ['--no-description'], - 'no-reg': ['--no-registry'], - noreg: ['--no-registry'], - O: ['--save-optional'], - P: ['--save-prod'], - p: ['--parseable'], - porcelain: ['--parseable'], - q: ['--loglevel', 'warn'], - quiet: ['--loglevel', 'warn'], - readonly: ['--read-only'], - reg: ['--registry'], - S: ['--save'], - s: ['--loglevel', 'silent'], - silent: ['--loglevel', 'silent'], - v: ['--version'], - verbose: ['--loglevel', 'verbose'], - y: ['--yes'], -} - -module.exports = { defaults, types, shorthands } diff --git a/lib/utils/flat-options.js b/lib/utils/flat-options.js deleted file mode 100644 index c082e4137..000000000 --- a/lib/utils/flat-options.js +++ /dev/null @@ -1,254 +0,0 @@ -// return a flattened config object with canonical names suitable for -// passing to dependencies like arborist, pacote, npm-registry-fetch, etc. - -const log = require('npmlog') -const crypto = require('crypto') -const querystring = require('querystring') -const npmSession = crypto.randomBytes(8).toString('hex') -log.verbose('npm-session', npmSession) -const { join } = require('path') - -const buildOmitList = obj => { - const include = obj.include || [] - const omit = new Set((obj.omit || []) - .filter(type => !include.includes(type))) - const only = obj.only - - if (/^prod(uction)?$/.test(only) || obj.production) - omit.add('dev') - - if (/dev/.test(obj.also)) - omit.delete('dev') - - if (obj.dev) - omit.delete('dev') - - if (obj.optional === false) - omit.add('optional') - - obj.omit = [...omit] - - // it would perhaps make more sense to put this in @npmcli/config, but - // since we can set dev to be omitted in multiple various legacy ways, - // it's better to set it here once it's all resolved. - if (obj.omit.includes('dev')) - process.env.NODE_ENV = 'production' - - return [...omit] -} - -// turn an object with npm-config style keys into an options object -// with camelCase values. This doesn't account for the stuff that is -// not pulled from the config keys, that's all handled only for the -// main function which acts on the npm object itself. Used by the -// flatOptions generator, and by the publishConfig handling logic. -const flatten = obj => ({ - includeStaged: obj['include-staged'], - preferDedupe: obj['prefer-dedupe'], - ignoreScripts: obj['ignore-scripts'], - nodeVersion: obj['node-version'], - cache: join(obj.cache, '_cacache'), - global: obj.global, - - registry: obj.registry, - scope: obj.scope, - access: obj.access, - alwaysAuth: obj['always-auth'], - audit: obj.audit, - auditLevel: obj['audit-level'], - _auth: obj._auth, - authType: obj['auth-type'], - ssoType: obj['sso-type'], - ssoPollFrequency: obj['sso-poll-frequency'], - before: obj.before, - browser: obj.browser, - ca: obj.ca, - cafile: obj.cafile, - cert: obj.cert, - key: obj.key, - - // token creation options - cidr: obj.cidr, - readOnly: obj['read-only'], - - // npm version options - preid: obj.preid, - tagVersionPrefix: obj['tag-version-prefix'], - allowSameVersion: obj['allow-same-version'], - - // npm version git options - message: obj.message, - commitHooks: obj['commit-hooks'], - gitTagVersion: obj['git-tag-version'], - signGitCommit: obj['sign-git-commit'], - signGitTag: obj['sign-git-tag'], - - // only used for npm ls in v7, not update - depth: obj.depth, - all: obj.all, - - // Output configs - unicode: obj.unicode, - json: obj.json, - long: obj.long, - parseable: obj.parseable, - - // options for npm search - search: { - description: obj.description, - exclude: obj.searchexclude, - limit: obj.searchlimit || 20, - opts: querystring.parse(obj.searchopts), - staleness: obj.searchstaleness, - }, - - diff: obj.diff, - diffUnified: obj['diff-unified'], - diffIgnoreAllSpace: obj['diff-ignore-all-space'], - diffNameOnly: obj['diff-name-only'], - diffNoPrefix: obj['diff-no-prefix'], - diffSrcPrefix: obj['diff-src-prefix'], - diffDstPrefix: obj['diff-dst-prefix'], - diffText: obj['diff-text'], - - dryRun: obj['dry-run'], - engineStrict: obj['engine-strict'], - - retry: { - retries: obj['fetch-retries'], - factor: obj['fetch-retry-factor'], - maxTimeout: obj['fetch-retry-maxtimeout'], - minTimeout: obj['fetch-retry-mintimeout'], - }, - - timeout: obj['fetch-timeout'], - - force: obj.force, - - formatPackageLock: obj['format-package-lock'], - fund: obj.fund, - - // binary locators - git: obj.git, - viewer: obj.viewer, - editor: obj.editor, - - // configs that affect how we build trees - binLinks: obj['bin-links'], - rebuildBundle: obj['rebuild-bundle'], - // --no-shrinkwrap is the same as --no-package-lock - packageLock: !(obj['package-lock'] === false || - obj.shrinkwrap === false), - packageLockOnly: obj['package-lock-only'], - globalStyle: obj['global-style'], - legacyBundling: obj['legacy-bundling'], - foregroundScripts: !!obj['foreground-scripts'], - scriptShell: obj['script-shell'] || undefined, - shell: obj.shell, - omit: buildOmitList(obj), - legacyPeerDeps: obj['legacy-peer-deps'], - strictPeerDeps: obj['strict-peer-deps'], - - // npx stuff - call: obj.call, - package: obj.package, - - // used to build up the appropriate {add:{...}} options to Arborist.reify - save: obj.save, - saveBundle: obj['save-bundle'] && !obj['save-peer'], - saveType: obj['save-optional'] && obj['save-peer'] - ? 'peerOptional' - : obj['save-optional'] ? 'optional' - : obj['save-dev'] ? 'dev' - : obj['save-peer'] ? 'peer' - : obj['save-prod'] ? 'prod' - : null, - savePrefix: obj['save-exact'] ? '' - : obj['save-prefix'], - - // configs for npm-registry-fetch - otp: obj.otp, - offline: obj.offline, - preferOffline: getPreferOffline(obj), - preferOnline: getPreferOnline(obj), - strictSSL: obj['strict-ssl'], - defaultTag: obj.tag, - userAgent: obj['user-agent'], - - // yes, it's fine, just do it, jeez, stop asking - yes: obj.yes, - - ...getScopesAndAuths(obj), - - // npm fund exclusive option to select an item from a funding list - which: obj.which, - - // socks proxy can be configured in https-proxy or proxy field - // note that the various (HTTPS_|HTTP_|]PROXY environs will be - // respected if this is not set. - proxy: obj['https-proxy'] || obj.proxy, - noProxy: obj.noproxy, -}) - -const flatOptions = npm => npm.flatOptions || Object.freeze({ - // flatten the config object - ...flatten(npm.config.list[0]), - - // Note that many of these do not come from configs or cli flags - // per se, though they may be implied or defined by them. - log, - npmSession, - dmode: npm.modes.exec, - fmode: npm.modes.file, - umask: npm.modes.umask, - hashAlgorithm: 'sha1', // XXX should this be sha512? - color: !!npm.color, - projectScope: npm.projectScope, - npmVersion: npm.version, - - // npm.command is not set until AFTER flatOptions are defined - // so we need to make this a getter. - get npmCommand () { - return npm.command - }, - - tmp: npm.tmp, - prefix: npm.prefix, - globalPrefix: npm.globalPrefix, - localPrefix: npm.localPrefix, - npmBin: require.main && require.main.filename, - nodeBin: process.env.NODE || process.execPath, - get tag () { - return npm.config.get('tag') - }, -}) - -const getPreferOnline = obj => { - const po = obj['prefer-online'] - if (po !== undefined) - return po - - return obj['cache-max'] <= 0 -} - -const getPreferOffline = obj => { - const po = obj['prefer-offline'] - if (po !== undefined) - return po - - return obj['cache-min'] >= 9999 -} - -// pull out all the @scope: and //host:key config fields -// these are used by npm-registry-fetch for authing against registries -const getScopesAndAuths = obj => { - const scopesAndAuths = {} - // pull out all the @scope:... configs into a flat object. - for (const key in obj) { - if (/@.*:registry$/i.test(key) || /^\/\//.test(key)) - scopesAndAuths[key] = obj[key] - } - return scopesAndAuths -} - -module.exports = Object.assign(flatOptions, { flatten }) -- cgit v1.2.3