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