Skip to content

Environment variables #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
# Created by https://www.gitignore.io/api/node

### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

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

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

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

# Dependency directories
node_modules
jspm_packages
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Proj
dist
# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env


# End of https://www.gitignore.io/api/node

.npmrc
dist
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
language: node_js
dist: trusty
node_js:
- "stable"
- "4"
19 changes: 9 additions & 10 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
# Test against this version of Node.js
version: "{build}"
skip_branch_with_pr: true
skip_tags: true
build: off

environment:
matrix:
# node.js
- nodejs_version: "stable"
- nodejs_version: "4"
- nodejs_version: stable
- nodejs_version: 4

# Install scripts. (runs after repo cloning)
install:
# Get the latest stable version of Node.js or io.js
# install Node.js
- ps: Install-Product node $env:nodejs_version
# install modules
- npm install

# Post-install test scripts.
# to run your custom scripts instead of automatic tests
test_script:
# run tests
- npm test

# Don't actually build.
build: off
2 changes: 2 additions & 0 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

'use strict';
const npmrc = require('./npmrc');
const env = require('./env');
const path = require('path');

npmrc(process.argv, path.join(process.cwd(), '.npmrc')).catch(console.error);
env(process.argv).catch(console.error);
100 changes: 100 additions & 0 deletions lib/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
'use strict';
const path = require('path');
const os = require('os');
const exec = require('mz/child_process').execFile;
const fs = require('fs-extra');
const config = require('./config');


function envForWin(argv) {
const MIRRORS = config(argv);

// 将环境变量转化为数组
let reg = Object.keys(MIRRORS).sort().map(key => {
if (MIRRORS[key]) {
return `"${ key.toUpperCase() }"="${ MIRRORS[key] }"`;
}
}).filter(Boolean);

// 环境变量与预期完全一致时,什么都不做
if (!reg.length) {
return Promise.resolve();
}

// 将数组拼接为windows的reg文件格式
reg.unshift(
'Windows Registry Editor Version 5.00',
'',
'[HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment]'
);

reg = reg.join('\r\n');
// reg文件保存路径
const regFilePath = path.join(__dirname, '../dist/env.reg');

// 写入reg文件
return fs.outputFile(regFilePath, reg).then(() => {
var regedit = path.join(process.env.windir, 'regedit.exe');
var args = ['//S', regFilePath];
// 尝试直接使用windows的注册表编辑器导入reg文件
return exec(regedit, args).catch(function() {
// 权限不足时,尝试在命令行中调用注册表编辑器
return exec(regedit, args, {
shell: true
});
}).catch(function(err) {
// 权限不足时抛出异常
err.code = 'EACCES';
throw err;
});
});
}

function envInProfile(argv) {
const MIRRORS = config(argv);

// 环境变量信息转换为shell脚本
let sh = Object.keys(MIRRORS).sort().map(key => {
if (MIRRORS[key]) {
return `export ${ key.toUpperCase() }=${ MIRRORS[key] }`;
}
}).filter(Boolean);

// 环境变量与预期完全一致时,什么都不做
if (!sh.length) {
return Promise.resolve();
}

sh.unshift('\n\n# Created by mirror-config-china');
sh.push('# End of mirror-config-china\n');

const home = os.homedir();
let shFilePath;
[
'.bashrc',
'.bash_profile',
'.zshrc'
].some(file => {
file = path.join(home, file);
if (fs.existsSync(file)) {
shFilePath = file;
return shFilePath;
}
});

return fs.readFile(shFilePath, 'utf8').then(content => {
const newContent = content
.replace(/\n+# Created by mirror-config-china\n(export\s+.*?\n)*# End of mirror-config-china\n+/, '\n')
.trimRight() + sh.join('\n');

if (newContent !== content) {
fs.writeFile(shFilePath, newContent);
}
});
}

if (process.platform === 'win32') {
module.exports = envForWin;
} else {
module.exports = envInProfile;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@
"pretest": "eslint lib/**/*.js *.js",
"test": "mocha"
},
"version": "2.0.1"
"version": "2.1.0"
}
10 changes: 9 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@ var describe = require('mocha').describe;
var it = require('mocha').it;
var assert = require('assert');

describe('env', function() {
describe.skip('environment variables', function() {
it('electron', function() {
assert.ok(process.env.ELECTRON_MIRROR);
});
it('node', function() {
assert.ok(process.env.NODEJS_ORG_MIRROR);
});
});

describe('npm config', function() {
it('electron', function() {
assert.ok(process.env.npm_config_electron_mirror);
});
Expand Down