Skip to content

Commit

Permalink
feat: support switching registry locally
Browse files Browse the repository at this point in the history
  • Loading branch information
chouchouji committed Nov 26, 2024
1 parent fe0293c commit 5e6bdca
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Usage: nrm [options] [command]
current Show current registry name
-u --show-url Show the registry URL instead of the name
use [registry] Change registry to registry
-l --local Switch local registry
add <registry> <url> [home] Add one custom registry
login <registry> [value] Set authorize information for a registry with a base64 encoded string or username and password
-a --always-auth Set is always auth
Expand Down
17 changes: 14 additions & 3 deletions actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const open = require('open');
const chalk = require('chalk');
const select = require('@inquirer/select').default;
const { fetch } = require('undici');
const path = require('path');
const {
exit,
readFile,
Expand All @@ -15,6 +16,7 @@ const {
isRegistryNotFound,
isInternalRegistry,
} = require('./helpers');
const process = require('./process');

const { NRMRC, NPMRC, AUTH, EMAIL, ALWAYS_AUTH, REPOSITORY, REGISTRY, HOME } = require('./constants');

Expand Down Expand Up @@ -54,7 +56,7 @@ async function onCurrent({ showUrl }) {
printMessages([`You are using ${chalk.green(showUrl ? registry[REGISTRY] : name)} registry.`]);
}

async function onUse(name) {
async function onUse(name, { local }) {
const registries = await getRegistries();

// if name is undefined, select the registry alias from list
Expand All @@ -70,8 +72,17 @@ async function onUse(name) {
}

const registry = registries[name];
const npmrc = await readFile(NPMRC);
await writeFile(NPMRC, Object.assign(npmrc, registry));

if (local) {
// switch local registry
const localNPMRCPath = path.resolve(process.cwd(), '.npmrc');
const npmrc = await readFile(localNPMRCPath);
await writeFile(localNPMRCPath, Object.assign(npmrc, registry));
} else {
// switch global registry
const npmrc = await readFile(NPMRC);
await writeFile(NPMRC, Object.assign(npmrc, registry));
}

printSuccess(`The registry has been changed to '${name}'.`);
}
Expand Down
6 changes: 5 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ program
.description('Show current registry name or URL')
.action(actions.onCurrent);

program.command('use [name]').description('Change current registry').action(actions.onUse);
program
.command('use [name]')
.description('Change current registry')
.option('-l, --local', 'Switch local registry')
.action(actions.onUse);

program.command('add <name> <url> [home]').description('Add custom registry').action(actions.onAdd);

Expand Down

0 comments on commit 5e6bdca

Please sign in to comment.