Comparing version 1.3.1 to 1.4.0
182
cli.js
@@ -16,2 +16,4 @@ #!/usr/bin/env node | ||
const CGRRC = path.join(HOME, '.cgrrc'); | ||
const CGRCF = path.join(HOME, '.cgrcf'); | ||
const npmRe = { | ||
@@ -25,2 +27,7 @@ get: 'npm config get registry', | ||
}; | ||
const pnpmRe = { | ||
get: 'pnpm config get registry', | ||
set: 'pnpm config set registry' | ||
}; | ||
const typeArrTip = ['N', 'Y', 'P']; | ||
@@ -60,2 +67,12 @@ program.version(PKG.version); | ||
program | ||
.command('on [type]') | ||
.description('Enable pnpm or other type') | ||
.action(onEnable); | ||
program | ||
.command('off [type]') | ||
.description('Disable pnpm or other type') | ||
.action(onDisable); | ||
program | ||
.command('help') | ||
@@ -77,14 +94,17 @@ .description('Print this help') | ||
getCurrentRegistry(function (curArr) { | ||
var info = [''], | ||
const info = [''], | ||
allRegistries = getAllRegistry(); | ||
Object.keys(allRegistries).forEach(function (key) { | ||
var item = allRegistries[key], | ||
const item = allRegistries[key], | ||
registry = String(item.registry), | ||
prefixIndex = curArr.indexOf(registry), | ||
curRegistryArr = curArr.filter(function(key) { | ||
return key === registry; | ||
}), | ||
prefix = | ||
prefixIndex === -1 | ||
? ' ' | ||
: `${curArr.length === 1 ? '*' : prefixIndex === 0 ? 'N' : 'Y'} `; | ||
info.push(prefix + key + line(key, 8) + registry); | ||
: `${curRegistryArr.length > 1 ? '*' : typeArrTip[prefixIndex]} `; | ||
info.push(`${prefix}${key}${line(key, 8)}${registry}`); | ||
}); | ||
@@ -99,16 +119,15 @@ | ||
getCurrentRegistry(function (curArr) { | ||
var info = [''], | ||
const info = [''], | ||
allRegistries = getAllRegistry(); | ||
Object.keys(allRegistries).forEach(function (key) { | ||
var item = allRegistries[key], | ||
const item = allRegistries[key], | ||
registry = String(item.registry), | ||
prefixIndex = curArr.indexOf(registry); | ||
prefixIndex = curArr.indexOf(registry), | ||
curRegistryArr = curArr.filter(function(key) { | ||
return key === registry; | ||
}); | ||
if (prefixIndex !== -1) { | ||
info.push( | ||
`${curArr.length === 1 ? '*' : prefixIndex === 0 ? 'N' : 'Y'} ${key}${line( | ||
key, | ||
8 | ||
)}${registry}` | ||
); | ||
const prefix = `${curRegistryArr.length > 1 ? '*' : typeArrTip[prefixIndex]} `; | ||
info.push(`${prefix}${key}${line(key, 8)}${registry}`); | ||
} | ||
@@ -123,5 +142,6 @@ }); | ||
function onUse(name, type) { | ||
var allRegistries = getAllRegistry(); | ||
const allRegistries = getAllRegistry(); | ||
const pnpmEnable = checkPnpm(); | ||
if (allRegistries.hasOwnProperty(name)) { | ||
var registry = allRegistries[name], | ||
const registry = allRegistries[name], | ||
info = [''], | ||
@@ -133,10 +153,21 @@ registrySet = 'registry has been set to:'; | ||
if (errN && errY) return exit([stderrN, stderrY]); | ||
info.push(errN ? stderrN : ` npm ${registrySet} ${registry.registry}`); | ||
info.push(errY ? stderrY : ` yarn ${registrySet} ${registry.registry}`); | ||
info.push(''); | ||
printMsg(info); | ||
if (pnpmEnable) { | ||
exec(`${pnpmRe.set} ${registry.registry}`, function (errP, stdoutP, stderrP) { | ||
info.push(errN ? stderrN : ` npm ${registrySet} ${registry.registry}`); | ||
info.push(errY ? stderrY : ` yarn ${registrySet} ${registry.registry}`); | ||
info.push(errP ? stderrP : ` pnpm ${registrySet} ${registry.registry}`); | ||
info.push(''); | ||
printMsg(info); | ||
}); | ||
} else { | ||
info.push(errN ? stderrN : ` npm ${registrySet} ${registry.registry}`); | ||
info.push(errY ? stderrY : ` yarn ${registrySet} ${registry.registry}`); | ||
info.push(''); | ||
printMsg(info); | ||
} | ||
}); | ||
}); | ||
} else { | ||
var smType = type.toLowerCase(); | ||
const smType = type.toLowerCase(); | ||
if (smType === 'npm' || smType === 'n') { | ||
@@ -156,5 +187,19 @@ exec(`${npmRe.set} ${registry.registry}`, function (err, stdout, stderr) { | ||
}); | ||
} else if (smType === 'pnpm' || smType === 'p') { | ||
if (pnpmEnable) { | ||
exec(`${pnpmRe.set} ${registry.registry}`, function (err, stdout, stderr) { | ||
if (err) return exit([stderr]); | ||
info.push(` pnpm ${registrySet} ${registry.registry}`); | ||
info.push(''); | ||
printMsg(info); | ||
}); | ||
} else { | ||
info.push(' cgr pnpm disabled, enable pnpm:'); | ||
info.push(' cgr on pnpm'); | ||
info.push(''); | ||
printMsg(info); | ||
} | ||
} else { | ||
info.push(' cgr use <registry> [type]'); | ||
info.push(' type must be oneOf yarn | y | npm | n'); | ||
info.push(' type must be oneOf yarn | y | npm | n | pnpm | p'); | ||
info.push(''); | ||
@@ -170,3 +215,3 @@ printMsg(info); | ||
function onDel(name) { | ||
var customRegistries = getCustomRegistry(); | ||
const customRegistries = getCustomRegistry(); | ||
if (!customRegistries.hasOwnProperty(name)) return; | ||
@@ -186,5 +231,5 @@ getCurrentRegistry(function (curArr) { | ||
function onAdd(name, url, home) { | ||
var customRegistries = getCustomRegistry(); | ||
const customRegistries = getCustomRegistry(); | ||
if (customRegistries.hasOwnProperty(name)) return; | ||
var config = (customRegistries[name] = {}); | ||
const config = (customRegistries[name] = {}); | ||
if (url[url.length - 1] !== '/') url += '/'; // ensure url end with / | ||
@@ -217,3 +262,3 @@ config.registry = url; | ||
function (name, cbk) { | ||
var registry = toTest[name], | ||
const registry = toTest[name], | ||
start = +new Date(); | ||
@@ -231,12 +276,15 @@ request(registry.registry + 'pedding', function (error) { | ||
getCurrentRegistry(function (curArr) { | ||
var msg = ['']; | ||
const msg = ['']; | ||
results.forEach(function (result) { | ||
var registry = String(result.registry), | ||
const registry = String(result.registry), | ||
prefixIndex = curArr.indexOf(registry), | ||
curRegistryArr = curArr.filter(function(key) { | ||
return key === registry; | ||
}), | ||
prefix = | ||
prefixIndex === -1 | ||
? ' ' | ||
: `${curArr.length === 1 ? '*' : prefixIndex === 0 ? 'N' : 'Y'} `, | ||
suffix = result.error ? 'Fetch Error' : result.time + 'ms'; | ||
msg.push(prefix + result.name + line(result.name, 8) + suffix); | ||
: `${curRegistryArr.length > 1 ? '*' : typeArrTip[prefixIndex]} `, | ||
suffix = result.error ? 'Fetch Error' : `${result.time}ms`; | ||
msg.push(`${prefix}${result.name}${line(result.name, 8)}${suffix}`); | ||
}); | ||
@@ -250,2 +298,21 @@ msg.push(''); | ||
function onEnable(type) { | ||
const config = getConfig(); | ||
if (config[type]) return; | ||
setConfig(Object.assign(config, { [type]: true }), function (err) { | ||
if (err) return exit([err]); | ||
printMsg(['', ' cgr enable ' + type + ' success', '']); | ||
}); | ||
} | ||
function onDisable(type) { | ||
const config = getConfig(); | ||
if (!config[type]) return; | ||
delete config[type]; | ||
setConfig(config, function (err) { | ||
if (err) return exit([err]); | ||
printMsg(['', ' cgr disable ' + type + ' success', '']); | ||
}); | ||
}; | ||
/*//////////////// helper methods /////////////////*/ | ||
@@ -260,15 +327,18 @@ | ||
if (errN && errY) return exit([stderrN, stderrY]); | ||
if (errN) console.log(stderrN); | ||
if (errY) console.log(stderrY); | ||
var npmRegistry = stdoutN.trim(), | ||
yarnRegistry = stdoutY.trim(); | ||
if (npmRegistry[npmRegistry.length - 1] !== '/') { | ||
npmRegistry = npmRegistry + '/'; | ||
} | ||
if (yarnRegistry[yarnRegistry.length - 1] !== '/') { | ||
yarnRegistry = yarnRegistry + '/'; | ||
} | ||
if (npmRegistry === yarnRegistry) { | ||
cbk([npmRegistry]); | ||
if (checkPnpm()) { | ||
exec(pnpmRe.get, function (errP, stdoutP, stderrP) { | ||
if (errN) console.log(stderrN); | ||
if (errY) console.log(stderrY); | ||
if (errP) console.log(stderrP); | ||
const npmRegistry = getRegistry(stdoutN.trim()), | ||
yarnRegistry = getRegistry(stdoutY.trim()), | ||
pnpmRegistry = getRegistry(stdoutP.trim()); | ||
cbk([npmRegistry, yarnRegistry, pnpmRegistry]); | ||
}); | ||
} else { | ||
if (errN) console.log(stderrN); | ||
if (errY) console.log(stderrY); | ||
const npmRegistry = getRegistry(stdoutN.trim()), | ||
yarnRegistry = getRegistry(stdoutY.trim()); | ||
cbk([npmRegistry, yarnRegistry]); | ||
@@ -280,2 +350,9 @@ } | ||
function getRegistry(registry) { | ||
if (registry[registry.length - 1] !== '/') { | ||
registry = registry + '/'; | ||
} | ||
return registry; | ||
} | ||
function getCustomRegistry() { | ||
@@ -289,2 +366,19 @@ return fs.existsSync(CGRRC) ? ini.parse(fs.readFileSync(CGRRC, 'utf-8')) : {}; | ||
function getConfig() { | ||
return fs.existsSync(CGRCF) ? JSON.parse(fs.readFileSync(CGRCF, 'utf-8')) : {}; | ||
} | ||
function setConfig(config, cbk) { | ||
fs.writeFile(CGRCF, JSON.stringify(config), 'utf-8', cbk); | ||
} | ||
function checkType(type) { | ||
const config = getConfig(); | ||
return config[type]; | ||
} | ||
function checkPnpm() { | ||
return checkType('pnpm'); | ||
}; | ||
function getAllRegistry() { | ||
@@ -309,4 +403,4 @@ return extend({}, registries, getCustomRegistry()); | ||
function line(str, len) { | ||
var line = new Array(Math.max(1, len - str.length)).join('-'); | ||
return ' ' + line + ' '; | ||
const line = new Array(Math.max(1, len - str.length)).join('-'); | ||
return ` ${line} `; | ||
} |
{ | ||
"name": "cgr", | ||
"version": "1.3.1", | ||
"version": "1.4.0", | ||
"description": "yarn & npm registry manager can help you easy and fast switch between different registries, now include: cnpm, taobao, nj(nodejitsu), rednpm, edunpm", | ||
@@ -5,0 +5,0 @@ "bin": { |
# cgr -- change registry | yarn & npm registry manager | ||
[![npm](https://img.shields.io/npm/v/cgr.svg)](https://www.npmjs.com/package/cgr) | ||
[![NPM downloads](http://img.shields.io/npm/dm/cgr.svg?style=flat)](https://npmjs.org/package/cgr) | ||
[![dependency Status](https://snyk.io/test/npm/cgr/badge.svg?style=flat-square)](https://snyk.io/test/npm/cgr) | ||
@@ -27,3 +29,3 @@ `cgr` can help you easy and fast switch between different npm or yarn registries, | ||
``` | ||
$ cgr ls //N for npm, Y for yarn | ||
$ cgr ls //N for npm, Y for yarn, P for pnpm | ||
@@ -59,2 +61,23 @@ N npm ---- https://registry.npmjs.org/ | ||
``` | ||
$ cgr on pnpm //enable pnpm switch registry | ||
cgr enable pnpm success | ||
``` | ||
``` | ||
$ cgr off pnpm //disable pnpm switch registry | ||
cgr disable pnpm success | ||
``` | ||
``` | ||
$ cgr use cnpm p //pnpm switch registry to cnpm | ||
pnpm registry has been set to: http://r.cnpmjs.org/ | ||
``` | ||
## Usage | ||
@@ -72,2 +95,4 @@ | ||
test [registry] Show the response time for one or all registries | ||
on [type] Enable pnpm or other type | ||
off [type] Disable pnpm or other type | ||
help Print this help | ||
@@ -74,0 +99,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16324
363
117