Skip to content

Commit

Permalink
feat: support npm registry parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Nov 1, 2019
1 parent d6c3582 commit d9adfcf
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
35 changes: 35 additions & 0 deletions packages/midway-init/lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const { Input, Select, Form } = require('enquirer');
const chalk = require('chalk');
const { getParser } = require('./parser');
const { EventEmitter } = require('events');
const fs = require('fs');
const os = require('os');

async function sleep(timeout) {
return new Promise(resolve => {
Expand Down Expand Up @@ -55,6 +57,10 @@ class MidwayInitCommand extends EventEmitter {
this.targetPath = argv.dir;
}

if (argv.registry) {
this.registryUrl = this.getRegistryByType(argv.registry);
}

if (argv.type) {
// support --type argument
this.templateName = argv.type;
Expand Down Expand Up @@ -101,6 +107,7 @@ class MidwayInitCommand extends EventEmitter {
npmClient: this.npmClient,
npmPackage: packageName || this.templateList[this.templateName].package,
targetPath: this.targetPath,
registryUrl: this.registryUrl,
});
await this.execBoilerplate(generator);
}
Expand Down Expand Up @@ -175,6 +182,34 @@ class MidwayInitCommand extends EventEmitter {
// .log(`Successfully generated boilerplate for template: "${this.options.template}"`);
console.log();
}

/**
* get registryUrl by short name
* @param {String} key - short name, support `china / npm / npmrc`, default to read from .npmrc
* @return {String} registryUrl
*/
getRegistryByType(key) {
switch (key) {
case 'china':
return 'https://registry.npm.taobao.org';
case 'npm':
return 'https://registry.npmjs.org';
default: {
if (/^https?:/.test(key)) {
return key.replace(/\/$/, '');
} else {
// support .npmrc
const home = os.homedir();
let url = process.env.npm_registry || process.env.npm_config_registry || 'https://registry.npmjs.org';
if (fs.existsSync(path.join(home, '.cnpmrc')) || fs.existsSync(path.join(home, '.tnpmrc'))) {
url = 'https://registry.npm.taobao.org';
}
url = url.replace(/\/$/, '');
return url;
}
}
}
}
}

module.exports = MidwayInitCommand;
4 changes: 0 additions & 4 deletions packages/midway-init/lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ const getParserOptions = () => {
description: 'npm registry, support china/npm/custom, default to auto detect',
alias: 'r',
},
silent: {
type: 'boolean',
description: 'don\'t ask, just use default value',
},
};
};

Expand Down

0 comments on commit d9adfcf

Please sign in to comment.