diff options
author | Minteck <nekostarfan@gmail.com> | 2021-08-24 14:41:48 +0200 |
---|---|---|
committer | Minteck <nekostarfan@gmail.com> | 2021-08-24 14:41:48 +0200 |
commit | d25e11bee6ca5ca523884da132d18e1400e077b9 (patch) | |
tree | 8af39fde19f7ed640a60fb397c7edd647dff1c4c /node_modules/npm-conf/lib/make.js | |
download | kartik-iridium-d25e11bee6ca5ca523884da132d18e1400e077b9.tar.gz kartik-iridium-d25e11bee6ca5ca523884da132d18e1400e077b9.tar.bz2 kartik-iridium-d25e11bee6ca5ca523884da132d18e1400e077b9.zip |
Initial commit
Diffstat (limited to 'node_modules/npm-conf/lib/make.js')
-rw-r--r-- | node_modules/npm-conf/lib/make.js | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/node_modules/npm-conf/lib/make.js b/node_modules/npm-conf/lib/make.js new file mode 100644 index 0000000..fb79d6c --- /dev/null +++ b/node_modules/npm-conf/lib/make.js @@ -0,0 +1,91 @@ +'use strict'; +const fs = require('fs'); +const path = require('path'); +const babylon = require('babylon'); +const generate = require('babel-generator').default; +const traverse = require('babel-traverse').default; + +const defaultsTemplate = body => ` + // Generated with \`lib/make.js\` + 'use strict'; + const os = require('os'); + const path = require('path'); + + const temp = os.tmpdir(); + const uidOrPid = process.getuid ? process.getuid() : process.pid; + const hasUnicode = () => true; + const isWindows = process.platform === 'win32'; + + const osenv = { + editor: () => process.env.EDITOR || process.env.VISUAL || (isWindows ? 'notepad.exe' : 'vi'), + shell: () => isWindows ? (process.env.COMSPEC || 'cmd.exe') : (process.env.SHELL || '/bin/bash') + }; + + const umask = { + fromString: () => process.umask() + }; + + let home = os.homedir(); + + if (home) { + process.env.HOME = home; + } else { + home = path.resolve(temp, 'npm-' + uidOrPid); + } + + const cacheExtra = process.platform === 'win32' ? 'npm-cache' : '.npm'; + const cacheRoot = process.platform === 'win32' ? process.env.APPDATA : home; + const cache = path.resolve(cacheRoot, cacheExtra); + + let defaults; + let globalPrefix; + + ${body} +`; + +const typesTemplate = body => ` + // Generated with \`lib/make.js\` + 'use strict'; + const path = require('path'); + const Stream = require('stream').Stream; + const url = require('url'); + + const Umask = () => {}; + const getLocalAddresses = () => []; + const semver = () => {}; + + ${body} +`; + +const defaults = require.resolve('npm/lib/config/defaults'); +const ast = babylon.parse(fs.readFileSync(defaults, 'utf8')); + +const isDefaults = node => + node.callee.type === 'MemberExpression' && + node.callee.object.name === 'Object' && + node.callee.property.name === 'defineProperty' && + node.arguments.some(x => x.name === 'exports'); + +const isTypes = node => + node.type === 'MemberExpression' && + node.object.name === 'exports' && + node.property.name === 'types'; + +let defs; +let types; + +traverse(ast, { + CallExpression(path) { + if (isDefaults(path.node)) { + defs = path.node; + } + }, + AssignmentExpression(path) { + if (path.node.left && isTypes(path.node.left)) { + types = path.node; + } + } +}); + +fs.writeFileSync(path.join(__dirname, 'defaults.js'), defaultsTemplate(generate(defs, {}, ast).code)); +fs.writeFileSync(path.join(__dirname, 'types.js'), typesTemplate(generate(types, {}, ast).code)); |