Skip to content

Commit

Permalink
feat: uninstall command nrm (#17)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <[email protected]>
  • Loading branch information
vltansky and antfu authored May 3, 2021
1 parent 460aac2 commit 0b6ac9d
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 3 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,36 @@ if the corresponding node manager is not present, this command will install it g

<br>

### `nrm` - uninstall

```bash
nrm

# npm uninstall axios
# yarn remove axios
# pnpm remove axios
```

```bash
nrm @types/node -D

# npm uninstall @types/node -D
# yarn remove @types/node -D
# pnpm remove @types/node -D
```

```bash
nrm -g iroiro

# npm uninstall -g iroiro
# yarn global remove iroiro
# pnpm remove -g iroiro

# this uses default agent, regardless your current working directory
```

<br>

### Config

```ini
Expand Down
3 changes: 3 additions & 0 deletions bin/nrm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env node
'use strict'
require('../dist/nrm')
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"nci": "bin/nci.js",
"nr": "bin/nr.js",
"nu": "bin/nu.js",
"nx": "bin/nx.js"
"nx": "bin/nx.js",
"nrm": "bin/nrm.js"
},
"bugs": {
"url": "https://github.com/antfu/ni/issues"
Expand All @@ -34,8 +35,9 @@
"nr": "esno src/nr.ts",
"nu": "esno src/nu.ts",
"nx": "esno src/nx.ts",
"nrm": "esno src/nrm.ts",
"dev": "esno src/ni.ts",
"build": "rimraf dist && tsup src/ni.ts src/nci.ts src/nr.ts src/nu.ts src/nx.ts src/index.ts --format cjs,esm --dts",
"build": "rimraf dist && tsup src/ni.ts src/nci.ts src/nr.ts src/nu.ts src/nx.ts src/nrm.ts src/index.ts --format cjs,esm --dts",
"release": "npx git-ensure && npx bumpp --commit --push --tag",
"lint": "eslint \"**/*.ts\"",
"lint:fix": "npm run lint -- --fix",
Expand Down
6 changes: 6 additions & 0 deletions src/agents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export const AGENTS = {
'upgrade': 'npm update {0}',
'upgrade-interactive': null,
'execute': 'npx {0}',
'uninstall': 'npm uninstall {0}',
'global_uninstall': 'npm uninstall -g {0}',
},
yarn: {
'run': 'yarn run {0}',
Expand All @@ -24,6 +26,8 @@ export const AGENTS = {
'upgrade': 'yarn upgrade {0}',
'upgrade-interactive': 'yarn upgrade-interactive',
'execute': 'yarn dls {0}',
'uninstall': 'yarn remove {0}',
'global_uninstall': 'yarn global remove {0}',
},
pnpm: {
'run': npmRun('pnpm'),
Expand All @@ -34,6 +38,8 @@ export const AGENTS = {
'upgrade': 'pnpm update {0}',
'upgrade-interactive': 'pnpm update -i',
'execute': 'pnpx {0}',
'uninstall': 'pnpm remove {0}',
'global_uninstall': 'pnpm remove -g {0}',
},
}

Expand Down
8 changes: 7 additions & 1 deletion src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export function getCommand(

if (!c)
throw new Error(`Command "${command}" is not support by agent "${agent}"`)

return c.replace('{0}', args.join(' ')).trim()
}

Expand Down Expand Up @@ -61,6 +60,13 @@ export function parseNu(agent: Agent, args: string[]): string {
return getCommand(agent, 'upgrade', args)
}


export function parseNrm(agent: Agent, args: string[]): string {
if (args.includes('-g'))
return getCommand(agent, 'global_uninstall', exclude(args, '-g'))
return getCommand(agent, 'uninstall', args)
}

export function parseNx(agent: Agent, args: string[]): string {
return getCommand(agent, 'execute', args)
}
4 changes: 4 additions & 0 deletions src/nrm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { parseNrm } from './commands'
import { runCli } from './runner'

runCli(parseNrm)
18 changes: 18 additions & 0 deletions test/nrm/npm.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import test, { ExecutionContext } from 'ava'
import { parseNrm } from '../../src/commands'

const agent = 'npm'
const _ = (arg: string, expected: string) => (t: ExecutionContext) => {
t.is(
parseNrm(agent, arg.split(' ').filter(Boolean)),
expected,
)
}

test('single remove', _('axios', 'npm uninstall axios'))

test('multiple', _('eslint @types/node', 'npm uninstall eslint @types/node'))

test('-D', _('eslint @types/node -D', 'npm uninstall eslint @types/node -D'))

test('global', _('eslint -g', 'npm uninstall -g eslint'))
18 changes: 18 additions & 0 deletions test/nrm/pnpm.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import test, { ExecutionContext } from 'ava'
import { parseNrm } from '../../src/commands'

const agent = 'pnpm'
const _ = (arg: string, expected: string) => (t: ExecutionContext) => {
t.is(
parseNrm(agent, arg.split(' ').filter(Boolean)),
expected,
)
}

test('single remove', _('axios', 'pnpm remove axios'))

test('multiple', _('eslint @types/node', 'pnpm remove eslint @types/node'))

test('-D', _('eslint @types/node -D', 'pnpm remove eslint @types/node -D'))

test('global', _('eslint -g', 'pnpm remove -g eslint'))
18 changes: 18 additions & 0 deletions test/nrm/yarn.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import test, { ExecutionContext } from 'ava'
import { parseNrm } from '../../src/commands'

const agent = 'yarn'
const _ = (arg: string, expected: string) => (t: ExecutionContext) => {
t.is(
parseNrm(agent, arg.split(' ').filter(Boolean)),
expected,
)
}

test('single remove', _('axios', 'yarn remove axios'))

test('multiple', _('eslint @types/node', 'yarn remove eslint @types/node'))

test('-D', _('eslint @types/node -D', 'yarn remove eslint @types/node -D'))

test('global', _('eslint ni -g', 'yarn global remove eslint ni'))

0 comments on commit 0b6ac9d

Please sign in to comment.