UNPKG

bia

Version:

a tool for download git repository

146 lines (141 loc) 6.33 kB
/* * @Author: hzxulin@yeah.net * @Date: 2018-12-04 16:03:16 * @Last Modified by: hzxulin@yeah.net * @Last Modified time: 2018-12-12 15:16:36 */ const path = require('path') const utils = require('./utils') const question = require('./question') const NPM_CONFIG = require('../../config/npm.json') module.exports = (cmd, opts) => { switch (cmd) { case 'list': { if (opts.config) { question.chooseConfigOperate().then(_res => { switch (_res) { case 'add': { // 添加配置信息 question.getUpdateConfig().then(res => { let npmConfList = [res].concat(NPM_CONFIG.list) let newNpmConf = Object.assign({}, NPM_CONFIG, { list: npmConfList, }) return utils.updateConfig(newNpmConf) }).then(() => { console.log('== 配置文件添加成功 ==') }).catch(err => { console.log(err) }) break } case 'update': { // 更新配置信息 let nameIndex = 0 utils.getConfigList().then(res => { return question.getConfigNameIndex(res) }).then(res => { nameIndex = res return question.getUpdateConfig(NPM_CONFIG.list[nameIndex]) }).then(res => { let npmConfList = NPM_CONFIG.list npmConfList.splice(nameIndex, 1, res) let newNpmConf = Object.assign({}, NPM_CONFIG, { list: npmConfList, }) return utils.updateConfig(newNpmConf) }).then(() => { console.log('== 配置文件更新成功 ==') }).catch(err => { console.log(err) }) break } case 'delete': { // 删除配置信息 utils.getConfigList().then(res => { return question.getConfigNameIndex(res) }).then(res => { let npmConfList = NPM_CONFIG.list npmConfList.splice(res, 1) let newNpmConf = Object.assign({}, NPM_CONFIG, { list: npmConfList, }) return utils.updateConfig(newNpmConf) }).then(() => { console.log('== 配置文件更新成功 ==') }).catch(err => { console.log(err) }) break } case 'import': { // 导入配置 let dist = path.resolve(__dirname, '../../config/npm.json') question.chooseImportFile().then(res => { utils.moveFileToDist(res, dist).then(res => { console.log(`== 已成功导入 ${res} 文件 ==`) }).catch(err => { console.log(err) }) }) break } case 'export': { // 导出配置 let ori = path.resolve(__dirname, '../../config/npm.json') question.chooseOutputFile().then(res => { utils.moveFileToDist(ori, res).then(res => { console.log(`== 已成功导出 ${res} 文件 ==`) }).catch(err => { console.log(err) }) }) break } default: { break } } }) } else { // 展示当前的npm源列表 let allRegistry = NPM_CONFIG.list utils.getCurrentRegistry().then(_cur => { let _newList = allRegistry.map(_v => { return Object.assign({ current: (_v.registry == _cur ? '*' : ''), }, _v) }) utils.showNpmList(_newList) }).catch(_err => { console.log(_err) }) } break } case 'default': default: { // 切换npm源 let nameIndex = 0 let allRegistry = NPM_CONFIG.list utils.getCurrentRegistry().then(_cur => { let _newList = allRegistry.map((_v, _i) => { return { name: `${_v.registry == _cur ? '[*]' : ''} ${_v.name} ${_v.registry}`, value: _i, } }) return question.getConfigNameIndex(_newList) }).then(res => { nameIndex = res return utils.useNpmConfig(allRegistry[nameIndex].registry) }).then((res) => { console.log(`== npm源切换成功,最新的源为 ${res} ==`) }).catch(err => { console.log(err) }) break } } }