Skip to content

Commit dba6545

Browse files
committed
Initial
0 parents  commit dba6545

File tree

7 files changed

+299
-0
lines changed

7 files changed

+299
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
package-lock.json
3+
tmp/

.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

.travis.yml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
language: node_js
2+
3+
os:
4+
- linux
5+
- osx
6+
- windows
7+
8+
node_js:
9+
- 10
10+
- 12
11+
- 14
12+
13+
jobs:
14+
include:
15+
- name: npm 7
16+
os: linux
17+
node_js: 14
18+
before_install: npm i npm@7 -g
19+
20+
notifications:
21+
email: false

LICENSE

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
The MIT License (MIT)
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
SOFTWARE.

README.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# npm-script-context
2+
3+
**Print environment, runtime, platform & more in npm scripts.**
4+
5+
[![npm status](http://img.shields.io/npm/v/npm-script-context.svg)](https://www.npmjs.org/package/npm-script-context)
6+
[![node](https://img.shields.io/node/v/npm-script-context.svg)](https://www.npmjs.org/package/npm-script-context)
7+
[![Travis](https://img.shields.io/travis/com/prebuild/npm-script-context.svg)](https://travis-ci.com/prebuild/npm-script-context)
8+
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
9+
10+
## Usage
11+
12+
```
13+
npm install npm-script-context
14+
npm-script-context
15+
```
16+
17+
Installing will also print info. For an example of the output, see [our CI](https://travis-ci.com/prebuild/npm-script-context). Note that the output depends on how and where it's run, for example whether it's installed as an npm dependency, git dependency or standalone.
18+
19+
## License
20+
21+
[MIT](LICENSE)

cli.js

+188
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
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+
}

package.json

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"name": "npm-script-context",
3+
"version": "0.0.0",
4+
"description": "Print environment, runtime, platform & more in npm scripts",
5+
"license": "MIT",
6+
"files": [
7+
"cli.js"
8+
],
9+
"bin": "./cli.js",
10+
"scripts": {
11+
"test": "standard && depcheck",
12+
"preinstall": "node cli.js",
13+
"install": "node cli.js",
14+
"postinstall": "node cli.js"
15+
},
16+
"dependencies": {
17+
"detect-libc": "^1.0.3",
18+
"escape-string-regexp": "^4.0.0",
19+
"find-root": "^1.1.0",
20+
"github-url-from-git": "^1.5.0",
21+
"is-fork-pr": "^2.5.0",
22+
"napi-build-utils": "^1.0.2",
23+
"remote-origin-url": "^2.0.0",
24+
"which-pm-runs": "^1.0.0"
25+
},
26+
"devDependencies": {
27+
"depcheck": "^1.2.0",
28+
"standard": "^14.3.4"
29+
},
30+
"repository": {
31+
"type": "git",
32+
"url": "https://github.com/prebuild/npm-script-context.git"
33+
},
34+
"homepage": "https://github.com/prebuild/npm-script-context",
35+
"keywords": [
36+
"prebuild",
37+
"build",
38+
"npm",
39+
"script",
40+
"install",
41+
"environment"
42+
],
43+
"engines": {
44+
"node": ">=10"
45+
}
46+
}

0 commit comments

Comments
 (0)