diff --git a/.changeset/grumpy-bags-travel.md b/.changeset/grumpy-bags-travel.md new file mode 100644 index 0000000..76ded81 --- /dev/null +++ b/.changeset/grumpy-bags-travel.md @@ -0,0 +1,5 @@ +--- +"nrm": patch +--- + +Added delete multiple registry. Thanks @chouchouji diff --git a/src/helpers.ts b/src/helpers.ts index 7b20b1e..badea92 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -53,9 +53,7 @@ export function printError(error: string) { } export function printMessages(messages: string[]) { - for (const message of messages) { - console.log(message); - } + console.log(messages.join('\n')); } export function geneDashLine(message: string, length: number) { @@ -105,3 +103,20 @@ export function exit(error?: string) { error && printError(error); process.exit(1); } + +export function isUnicodeSupported() { + if (process.platform !== 'win32') { + return process.env['TERM'] !== 'linux'; + } + + return ( + Boolean(process.env.WT_SESSION) || + Boolean(process.env.TERMINUS_SUBLIME) || + process.env.ConEmuTask === '{cmd::Cmder}' || + process.env.TERM_PROGRAM === 'Terminus-Sublime' || + process.env.TERM_PROGRAM === 'vscode' || + process.env.TERM === 'xterm-256color' || + process.env.TERM === 'alacritty' || + process.env.TERMINAL_EMULATOR === 'JetBrains-JediTerm' + ); +} diff --git a/tests/cli.test.ts b/tests/cli.test.ts index 8554359..876f14e 100644 --- a/tests/cli.test.ts +++ b/tests/cli.test.ts @@ -14,12 +14,19 @@ import { vi, } from 'vitest'; +import { unlink } from 'node:fs/promises'; import { onHome, onTest } from '../src/actions'; -import { NPMRC, REGISTRIES } from '../src/constants'; -import { readFile, writeFile } from '../src/helpers'; +import { NPMRC, NRMRC, REGISTRIES } from '../src/constants'; +import { isUnicodeSupported, readFile, writeFile } from '../src/helpers'; const isWin = process.platform === 'win32'; +const shouldUseMain = isUnicodeSupported(); + +const pointer = shouldUseMain ? '❯' : '>'; +const radioOff = shouldUseMain ? '◯' : '( )'; +const radioOn = shouldUseMain ? '◉' : '(*)'; + vi.setConfig({ testTimeout: 20000, }); @@ -138,7 +145,7 @@ it('nrm use without argument', async () => { expect( message, ).toBe(`? Please select the registry you want to use (Use arrow keys) -${isWin ? '>' : '❯'} npm +${pointer} npm yarn tencent cnpm @@ -281,3 +288,68 @@ it('nrm home [browser]', async () => { await onHome('cnpm'); expect(open).toHaveBeenCalled(); }); + +describe('nrm delete without argument (use keyword to select delete)', () => { + const registries = [ + { name: 'test', url: 'http://localhost:3000' }, + { name: 'test1', url: 'http://localhost:3001' }, + { name: 'test2', url: 'http://localhost:3002' }, + ]; + beforeEach(async () => { + for (const registry of registries) { + await coffee + .spawn('nrm', ['add', `${registry.name}`, `${registry.url}`], { + shell: isWin, + }) + .expect('stdout', /success/g) + .expect('code', 0) + .end(); + } + }); + + afterEach(async () => { + await unlink(NRMRC); + }); + + it('nrm delete', async () => { + const { stdout } = spawn('nrm', ['del'], { shell: isWin }); + + const message = await new Promise((resolve) => { + stdout.on('data', (data) => { + resolve(stripAnsi(data.toString()).trim()); + }); + }); + + expect(message).toMatchInlineSnapshot(` + "? Please select the registries you want to delete (Press to select, + to toggle all, to invert selection, and to proceed) + ${pointer}${radioOff} test + ${radioOff} test1 + ${radioOff} test2" + `); + }); + + it('nrm delete (with keyword input)', async () => { + const { stdout, stdin } = spawn('nrm', ['del'], { shell: isWin }); + stdin.write('\u001b[B'); + + const message = await new Promise((resolve) => { + const m: string[] = []; + stdout.on('data', (data) => { + m.push(stripAnsi(data.toString()).trim()); + // get the last output + if (m.length === 2) { + resolve(m[m.length - 1]); + } + }); + }); + + expect(message).toMatchInlineSnapshot(` + "? Please select the registries you want to delete (Press to select, + to toggle all, to invert selection, and to proceed) + ${radioOff} test + ${pointer}${radioOff} test1 + ${radioOff} test2" + `); + }); +});