Skip to content

Commit

Permalink
Merge pull request #203 from chouchouji/feat-local-npmrc
Browse files Browse the repository at this point in the history
feat: support switching registry locally
  • Loading branch information
iosh authored Nov 30, 2024
2 parents d269d13 + 9f04e63 commit 6b268a5
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/slow-crabs-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'nrm': minor
---

Added set registry locally. Thanks @chouchouji
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
33 changes: 33 additions & 0 deletions tests/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const chalk = require('chalk');
const { onHome, onTest } = require('.././actions.js');
const { spawn } = require('node:child_process');
const stripAnsi = require('strip-ansi');
const { readFile, writeFile } = require('.././helpers');
const { NPMRC, REGISTRIES } = require('.././constants');
const isWin = process.platform === 'win32';

jest.setTimeout(20000);
Expand Down Expand Up @@ -58,6 +60,37 @@ it('nrm use <registry>', async () => {
.end();
});

it('nrm use <registry> local', async () => {
await coffee
.spawn('nrm', ['use', 'cnpm', 'local'], { shell: isWin })
.expect('stdout', /The registry has been changed to 'cnpm'/g)
.expect('code', 0)
.end();

const npmrc = await readFile(NPMRC, { encoding: 'utf-8' });

expect(npmrc.registry).toBe(REGISTRIES.cnpm.registry);

await coffee.spawn('nrm', ['current'], { shell: isWin }).expect('stdout', /cnpm/g).expect('code', 0).end();
});

it('nrm use <registry> local with user config', async () => {
await writeFile(NPMRC, { abc: '123' });

await coffee
.spawn('nrm', ['use', 'cnpm', 'local'], { shell: isWin })
.expect('stdout', /The registry has been changed to 'cnpm'/g)
.expect('code', 0)
.end();

const npmrc = await readFile(NPMRC, { encoding: 'utf-8' });

expect(npmrc.registry).toBe(REGISTRIES.cnpm.registry);
expect(npmrc.abc).toBe('123');

await coffee.spawn('nrm', ['current'], { shell: isWin }).expect('stdout', /cnpm/g).expect('code', 0).end();
});

it('nrm use without argument', async () => {
const { stdout } = spawn('nrm', ['use'], { shell: isWin });

Expand Down

0 comments on commit 6b268a5

Please sign in to comment.