Skip to content

Commit

Permalink
fix: npm registry and proxy can be set when initialized.
Browse files Browse the repository at this point in the history
  • Loading branch information
cpselvis committed Sep 10, 2017
1 parent 2982f3d commit a9a8119
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 16 deletions.
2 changes: 1 addition & 1 deletion lib/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Feflow {
this.pluginDir = pathFn.join(base, 'node_modules') + sep;

this.config = utils.parseYaml(rcPath); // Read feflow local config.

this.log = logger({
debug: Boolean(args.debug)
});
Expand Down
37 changes: 30 additions & 7 deletions lib/core/initClient.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
'use strict';

const fs = require('hexo-fs');
const inquirer = require('inquirer');
const Promise = require('bluebird');
const utils = require('../utils');

/**
* Init feflow client, including ~/.feflow, ~/.feflow/package.json, ~/.feflow/.feflowrc.yml
Expand All @@ -10,11 +12,12 @@ class Client {

constructor(ctx) {
this.ctx = ctx;
this.log = ctx.log;
}

initHome() {
const ctx = this.ctx;
const baseDir = ctx.baseDir;
const { baseDir, log } = ctx;

return new Promise(function(resolve) {
if (fs.existsSync(baseDir) && fs.statSync(baseDir).isFile()) {
Expand All @@ -24,13 +27,15 @@ class Client {
if (!fs.existsSync(baseDir)) {
fs.mkdirsSync(baseDir);
}

log.debug('.feflow 目录已经创建');
resolve(ctx);
});
}

initPkg() {
const ctx = this.ctx;
const pkgPath = ctx.pkgPath;
const { pkgPath, log } = ctx;

return new Promise(function(resolve) {
if (!fs.existsSync(pkgPath)) {
Expand All @@ -40,19 +45,37 @@ class Client {
"private": true
}, null, 4));
}

log.debug('.feflow/package.json 文件已经创建');
resolve(ctx);
});
}

initLocalRc() {
const ctx = this.ctx;
const rcPath = ctx.rcPath;
const { rcPath, config, log } = ctx;

return new Promise(function(resolve) {
if (!fs.existsSync(rcPath)) {
console.log('rc file not exists');
if (!fs.existsSync(rcPath) || !config || !config.registry) {
inquirer.prompt([{
type: 'input',
name: 'registry',
message: '请输入npm的registry:',
default: 'http://registry.npmjs.org/'
}, {
type: 'input',
name: 'proxy',
message: '请输入npm的proxy(默认为空):'
}]).then((answer) => {
utils.safeDump(answer, rcPath);
log.debug('.feflow/.feflowrc.yml 配置文件已经创建');

resolve(ctx);
});
} else {
log.debug('.feflow/.feflowrc.yml 配置文件已经创建');
resolve(ctx);
}
resolve(ctx);
});
}
}
Expand All @@ -61,7 +84,7 @@ class Client {
module.exports = function(ctx) {
const client = new Client(ctx);

return Promise.each([
return Promise.all([
client.initHome(),
client.initPkg(),
client.initLocalRc()
Expand Down
26 changes: 19 additions & 7 deletions lib/internal/install/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,29 @@ class Plugin {
* return code 0 means success, 1 means failure
*/
execNpmCommand(cmd, modules, where) {
const { registry, proxy } = this.ctx.config;

return new Promise((resolve, reject) => {
const args = [cmd].concat(modules).concat('--color=always');
const tnpm = spawn('tnpm', args, {cwd: where});
let args = [cmd].concat(modules).concat('--color=always');
if (registry) {
args = args.concat(`--registry=${registry}`);
}
if (proxy) {
args = args.concat(`--proxy=${proxy}`);
}
console.log(args)
const npm = spawn('npm', args, {cwd: where});

let output = '';
tnpm.stdout.on('data', (data) => {
npm.stdout.on('data', (data) => {
output += data;
}).pipe(process.stdout);

tnpm.stderr.on('data', (data) => {
npm.stderr.on('data', (data) => {
output += data;
}).pipe(process.stderr);

tnpm.on('close', (code) => {
npm.on('close', (code) => {
if (!code) {
resolve({cod: 0, data: output});
} else {
Expand Down Expand Up @@ -122,7 +131,7 @@ class Plugin {
const diffPlugins = [];

plugins.map(function (name, index) {
const localVersion = localVersions[index];
const localVersion = localVersions[index] || null;
const ltsVersion = ltsVersions[index];
if (localVersion !== ltsVersion) {
diffPlugins.push(name);
Expand Down Expand Up @@ -173,10 +182,13 @@ class Plugin {
* @returns {Array|*}
*/
getLts(plugins) {
const config = this.ctx.config;
const registry = config && config.registry;

return plugins.map(function (name) {
return new Promise(function (resolve, reject) {
const options = {
url: `http://r.tnpm.oa.com/${name}/latest`,
url: `${registry}${name}/latest`,
method: 'GET'
};

Expand Down
3 changes: 2 additions & 1 deletion lib/utils/yaml.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ function parseYaml(path) {
if (fs.existsSync(path)) {
try {
config = yaml.safeLoad(fs.readFileSync(path));

} catch (e) {
console.log(e);
}
Expand All @@ -36,4 +37,4 @@ function safeDump(obj, path) {
}

exports.parseYaml = parseYaml;
exports.safeDump = safeDump;
exports.safeDump = safeDump;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"cross-spawn": "^5.1.0",
"easy-table": "^1.1.0",
"figlet": "^1.2.0",
"generator-webapp": "^3.0.1",
"hexo-fs": "^0.2.1",
"inquirer": "^3.0.6",
"lodash": "^4.17.4",
Expand Down

0 comments on commit a9a8119

Please sign in to comment.