| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 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)); |