Skip to content

Commit

Permalink
add changeset and test
Browse files Browse the repository at this point in the history
  • Loading branch information
iosh committed Dec 14, 2024
1 parent df2c456 commit 19eff3c
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/grumpy-bags-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"nrm": patch
---

Added delete multiple registry. Thanks @chouchouji
21 changes: 18 additions & 3 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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'
);
}
78 changes: 75 additions & 3 deletions tests/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -281,3 +288,68 @@ it('nrm home <registry> [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 <space> to select, <a>
to toggle all, <i> to invert selection, and <enter> 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 <space> to select, <a>
to toggle all, <i> to invert selection, and <enter> to proceed)
${radioOff} test
${pointer}${radioOff} test1
${radioOff} test2"
`);
});
});

0 comments on commit 19eff3c

Please sign in to comment.