|
| 1 | +#!/usr/bin/env node |
| 2 | +'use strict' |
| 3 | + |
| 4 | +const pm = require('which-pm-runs') |
| 5 | +const findRoot = require('find-root') |
| 6 | +const detectLibc = require('detect-libc') |
| 7 | +const origin = require('remote-origin-url') |
| 8 | +const ghurl = require('github-url-from-git') |
| 9 | +const napi = require('napi-build-utils') |
| 10 | +const isForkPr = require('is-fork-pr') |
| 11 | +const escapeStringRe = require('escape-string-regexp') |
| 12 | +const os = require('os') |
| 13 | +const fs = require('fs') |
| 14 | +const path = require('path') |
| 15 | +const hasOwnProperty = Object.prototype.hasOwnProperty |
| 16 | + |
| 17 | +const data = { |
| 18 | + pm: optional(pm), |
| 19 | + npm: { |
| 20 | + env: mask( |
| 21 | + filter(process.env, (k) => /^npm_/i.test(k) && !/^npm_config__/i.test(k)), |
| 22 | + { |
| 23 | + npm_config_email: '***', |
| 24 | + 'npm_config_init.author.name': '***', |
| 25 | + 'npm_config_init.author.url': '***' |
| 26 | + } |
| 27 | + ) |
| 28 | + }, |
| 29 | + pkg: packageInfo(), |
| 30 | + git: gitInfo(), |
| 31 | + ci: ciInfo(), |
| 32 | + webpack: typeof __webpack_require__ === 'function', // eslint-disable-line |
| 33 | + electron: !!(process.versions.electron || process.env.ELECTRON_RUN_AS_NODE), |
| 34 | + alpine: process.platform === 'linux' && fs.existsSync('/etc/alpine-release'), |
| 35 | + proxy: { |
| 36 | + http: process.env.http_proxy || process.env.HTTP_PROXY || null, |
| 37 | + https: process.env.https_proxy || process.env.HTTPS_PROXY || null |
| 38 | + }, |
| 39 | + process: { |
| 40 | + ...filter(process, function (k, v) { |
| 41 | + return k !== 'moduleLoadList' && ( |
| 42 | + typeof v === 'string' || typeof v === 'number' || Array.isArray(v) |
| 43 | + ) |
| 44 | + }), |
| 45 | + cwd: process.cwd(), |
| 46 | + config: process.config, |
| 47 | + versions: process.versions, |
| 48 | + uid: optional(() => process.getuid()), |
| 49 | + gid: optional(() => process.getgid()), |
| 50 | + egid: optional(() => process.getegid()), |
| 51 | + euid: optional(() => process.geteuid()), |
| 52 | + groups: optional(() => process.getgroups()) |
| 53 | + }, |
| 54 | + streams: { |
| 55 | + stdin: { isTTY: process.stdin.isTTY }, |
| 56 | + stdout: { isTTY: process.stdout.isTTY }, |
| 57 | + stderr: { isTTY: process.stderr.isTTY } |
| 58 | + }, |
| 59 | + os: { |
| 60 | + type: optional(() => os.type()), |
| 61 | + release: optional(() => os.release()), |
| 62 | + version: optional(() => os.version()), |
| 63 | + hostname: optional(() => os.hostname()), |
| 64 | + tmpdir: optional(() => os.tmpdir()), |
| 65 | + homedir: optional(() => os.homedir()), |
| 66 | + userInfo: optional(() => os.userInfo()) |
| 67 | + }, |
| 68 | + libc: { |
| 69 | + family: detectLibc.family || null, |
| 70 | + version: detectLibc.version || null |
| 71 | + }, |
| 72 | + napi: napi.getNapiVersion() || null, |
| 73 | + path: { |
| 74 | + sep: path.sep, |
| 75 | + delimiter: path.delimiter |
| 76 | + }, |
| 77 | + env: { |
| 78 | + ...filter(process.env, (k) => !/^(npm_|ConEmu|java_|vbox_)/i.test(k)), |
| 79 | + path: undefined, |
| 80 | + Path: undefined, |
| 81 | + PATH: uniq( |
| 82 | + (process.env.path || process.env.PATH || process.env.Path) |
| 83 | + .split(path.delimiter) |
| 84 | + .filter(Boolean) |
| 85 | + ) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +console.log(JSON.stringify(data, replacer(), 2)) |
| 90 | + |
| 91 | +function filter (input, include) { |
| 92 | + const output = {} |
| 93 | + |
| 94 | + for (const k in input) { |
| 95 | + if (!hasOwnProperty.call(input, k)) continue |
| 96 | + if (!include(k, input[k])) continue |
| 97 | + |
| 98 | + output[k] = input[k] |
| 99 | + } |
| 100 | + |
| 101 | + return output |
| 102 | +} |
| 103 | + |
| 104 | +function mask (input, masks) { |
| 105 | + if (process.env.CI) return input |
| 106 | + |
| 107 | + for (const k in masks) { |
| 108 | + if (!hasOwnProperty.call(masks, k)) continue |
| 109 | + if (!hasOwnProperty.call(input, k)) continue |
| 110 | + if (!input[k]) continue |
| 111 | + |
| 112 | + input[k] = masks[k] |
| 113 | + } |
| 114 | + |
| 115 | + return input |
| 116 | +} |
| 117 | + |
| 118 | +function optional (fn) { |
| 119 | + try { |
| 120 | + return fn() |
| 121 | + } catch { |
| 122 | + return null |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +function uniq (arr) { |
| 127 | + return Array.from(new Set(arr)) |
| 128 | +} |
| 129 | + |
| 130 | +function replacer () { |
| 131 | + const username = os.userInfo().username |
| 132 | + const hostname = os.hostname() |
| 133 | + const usernameRe = new RegExp(escapeStringRe(username), 'gi') |
| 134 | + const hostnameRe = new RegExp(escapeStringRe(hostname), 'gi') |
| 135 | + |
| 136 | + return function (key, value) { |
| 137 | + if (typeof value === 'string') { |
| 138 | + if (/token|password|secret/i.test(key)) return '***' |
| 139 | + if (process.env.CI) return value |
| 140 | + |
| 141 | + return value |
| 142 | + .replace(usernameRe, 'USERNAME') |
| 143 | + .replace(hostnameRe, 'HOSTNAME') |
| 144 | + } else { |
| 145 | + return value |
| 146 | + } |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +function packageInfo () { |
| 151 | + const result = { root: null, git: null, path: null, pkg: null } |
| 152 | + |
| 153 | + try { |
| 154 | + const root = findRoot() |
| 155 | + const gitdir = path.join(root, '.git') |
| 156 | + |
| 157 | + result.root = root |
| 158 | + result.git = fs.existsSync(gitdir) ? gitdir : null |
| 159 | + result.path = path.join(root, 'package.json') |
| 160 | + result.pkg = JSON.parse(fs.readFileSync(result.path, 'utf8')) |
| 161 | + } catch {} |
| 162 | + |
| 163 | + return result |
| 164 | +} |
| 165 | + |
| 166 | +function gitInfo (cwd) { |
| 167 | + const result = {} |
| 168 | + |
| 169 | + // Don't pass cwd for now (jonschlinkert/parse-git-config#13) |
| 170 | + result.origin = optional(() => origin.sync(/* cwd */)) |
| 171 | + |
| 172 | + if (result.origin) { |
| 173 | + result.github_url = optional(() => ghurl(origin)) |
| 174 | + } |
| 175 | + |
| 176 | + return result |
| 177 | +} |
| 178 | + |
| 179 | +function ciInfo () { |
| 180 | + if (!process.env.CI) { |
| 181 | + return null |
| 182 | + } |
| 183 | + |
| 184 | + return { |
| 185 | + name: optional(() => isForkPr.getCiName()), |
| 186 | + is_fork_pr: optional(() => isForkPr.isForkPr()) |
| 187 | + } |
| 188 | +} |
0 commit comments