Skip to content

Commit d6aefb2

Browse files
authored
Environment variables (#13)
* Support environment variables * testcase * debug ci config * debug ci config * debug * skip testcase of environment * update appveyor config
1 parent 5609034 commit d6aefb2

File tree

7 files changed

+151
-16
lines changed

7 files changed

+151
-16
lines changed

.gitignore

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1+
# Created by https://www.gitignore.io/api/node
2+
3+
### Node ###
14
# Logs
25
logs
36
*.log
47
npm-debug.log*
8+
yarn-debug.log*
9+
yarn-error.log*
510

611
# Runtime data
712
pids
813
*.pid
914
*.seed
15+
*.pid.lock
1016

1117
# Directory for instrumented libs generated by jscoverage/JSCover
1218
lib-cov
@@ -20,23 +26,42 @@ coverage
2026
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
2127
.grunt
2228

29+
# Bower dependency directory (https://bower.io/)
30+
bower_components
31+
2332
# node-waf configuration
2433
.lock-wscript
2534

2635
# Compiled binary addons (http://nodejs.org/api/addons.html)
2736
build/Release
2837

2938
# Dependency directories
30-
node_modules
31-
jspm_packages
39+
node_modules/
40+
jspm_packages/
41+
42+
# Typescript v1 declaration files
43+
typings/
3244

3345
# Optional npm cache directory
3446
.npm
3547

48+
# Optional eslint cache
49+
.eslintcache
50+
3651
# Optional REPL history
3752
.node_repl_history
3853

39-
# Proj
40-
dist
54+
# Output of 'npm pack'
55+
*.tgz
56+
57+
# Yarn Integrity file
58+
.yarn-integrity
59+
60+
# dotenv environment variables file
61+
.env
62+
63+
64+
# End of https://www.gitignore.io/api/node
4165

4266
.npmrc
67+
dist

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
language: node_js
2+
dist: trusty
23
node_js:
34
- "stable"
45
- "4"

appveyor.yml

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
# Test against this version of Node.js
1+
version: "{build}"
2+
skip_branch_with_pr: true
3+
skip_tags: true
4+
build: off
5+
26
environment:
37
matrix:
4-
# node.js
5-
- nodejs_version: "stable"
6-
- nodejs_version: "4"
8+
- nodejs_version: stable
9+
- nodejs_version: 4
710

811
# Install scripts. (runs after repo cloning)
912
install:
10-
# Get the latest stable version of Node.js or io.js
13+
# install Node.js
1114
- ps: Install-Product node $env:nodejs_version
1215
# install modules
1316
- npm install
1417

15-
# Post-install test scripts.
18+
# to run your custom scripts instead of automatic tests
1619
test_script:
17-
# run tests
1820
- npm test
19-
20-
# Don't actually build.
21-
build: off

lib/cli.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
'use strict';
44
const npmrc = require('./npmrc');
5+
const env = require('./env');
56
const path = require('path');
67

78
npmrc(process.argv, path.join(process.cwd(), '.npmrc')).catch(console.error);
9+
env(process.argv).catch(console.error);

lib/env.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
'use strict';
2+
const path = require('path');
3+
const os = require('os');
4+
const exec = require('mz/child_process').execFile;
5+
const fs = require('fs-extra');
6+
const config = require('./config');
7+
8+
9+
function envForWin(argv) {
10+
const MIRRORS = config(argv);
11+
12+
// 将环境变量转化为数组
13+
let reg = Object.keys(MIRRORS).sort().map(key => {
14+
if (MIRRORS[key]) {
15+
return `"${ key.toUpperCase() }"="${ MIRRORS[key] }"`;
16+
}
17+
}).filter(Boolean);
18+
19+
// 环境变量与预期完全一致时,什么都不做
20+
if (!reg.length) {
21+
return Promise.resolve();
22+
}
23+
24+
// 将数组拼接为windows的reg文件格式
25+
reg.unshift(
26+
'Windows Registry Editor Version 5.00',
27+
'',
28+
'[HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment]'
29+
);
30+
31+
reg = reg.join('\r\n');
32+
// reg文件保存路径
33+
const regFilePath = path.join(__dirname, '../dist/env.reg');
34+
35+
// 写入reg文件
36+
return fs.outputFile(regFilePath, reg).then(() => {
37+
var regedit = path.join(process.env.windir, 'regedit.exe');
38+
var args = ['//S', regFilePath];
39+
// 尝试直接使用windows的注册表编辑器导入reg文件
40+
return exec(regedit, args).catch(function() {
41+
// 权限不足时,尝试在命令行中调用注册表编辑器
42+
return exec(regedit, args, {
43+
shell: true
44+
});
45+
}).catch(function(err) {
46+
// 权限不足时抛出异常
47+
err.code = 'EACCES';
48+
throw err;
49+
});
50+
});
51+
}
52+
53+
function envInProfile(argv) {
54+
const MIRRORS = config(argv);
55+
56+
// 环境变量信息转换为shell脚本
57+
let sh = Object.keys(MIRRORS).sort().map(key => {
58+
if (MIRRORS[key]) {
59+
return `export ${ key.toUpperCase() }=${ MIRRORS[key] }`;
60+
}
61+
}).filter(Boolean);
62+
63+
// 环境变量与预期完全一致时,什么都不做
64+
if (!sh.length) {
65+
return Promise.resolve();
66+
}
67+
68+
sh.unshift('\n\n# Created by mirror-config-china');
69+
sh.push('# End of mirror-config-china\n');
70+
71+
const home = os.homedir();
72+
let shFilePath;
73+
[
74+
'.bashrc',
75+
'.bash_profile',
76+
'.zshrc'
77+
].some(file => {
78+
file = path.join(home, file);
79+
if (fs.existsSync(file)) {
80+
shFilePath = file;
81+
return shFilePath;
82+
}
83+
});
84+
85+
return fs.readFile(shFilePath, 'utf8').then(content => {
86+
const newContent = content
87+
.replace(/\n+# Created by mirror-config-china\n(export\s+.*?\n)*# End of mirror-config-china\n+/, '\n')
88+
.trimRight() + sh.join('\n');
89+
90+
if (newContent !== content) {
91+
fs.writeFile(shFilePath, newContent);
92+
}
93+
});
94+
}
95+
96+
if (process.platform === 'win32') {
97+
module.exports = envForWin;
98+
} else {
99+
module.exports = envInProfile;
100+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@
3434
"pretest": "eslint lib/**/*.js *.js",
3535
"test": "mocha"
3636
},
37-
"version": "2.0.1"
37+
"version": "2.1.0"
3838
}

test/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,16 @@ var describe = require('mocha').describe;
33
var it = require('mocha').it;
44
var assert = require('assert');
55

6-
describe('env', function() {
6+
describe.skip('environment variables', function() {
7+
it('electron', function() {
8+
assert.ok(process.env.ELECTRON_MIRROR);
9+
});
10+
it('node', function() {
11+
assert.ok(process.env.NODEJS_ORG_MIRROR);
12+
});
13+
});
714

15+
describe('npm config', function() {
816
it('electron', function() {
917
assert.ok(process.env.npm_config_electron_mirror);
1018
});

0 commit comments

Comments
 (0)