Skip to content

Commit

Permalink
chore: update
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jul 13, 2024
1 parent 63e77e5 commit 8bcfe60
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 66 deletions.
34 changes: 9 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ npm i -g @antfu/ni

<a href='https://docs.npmjs.com/cli/v6/commands/npm'>npm</a> · <a href='https://yarnpkg.com'>yarn</a> · <a href='https://pnpm.io/'>pnpm</a> · <a href='https://bun.sh/'>bun</a>


<br>


### `ni` - install

```bash
Expand Down Expand Up @@ -55,20 +53,6 @@ ni --frozen
# bun install --no-save
```

```bash
ni -i

# interactively select the dependency to install
# fetches all similar packages by name
```

```bash
ni -

# uninstall the last
# installed package
```

```bash
ni -g eslint

Expand All @@ -80,6 +64,13 @@ ni -g eslint
# this uses default agent, regardless your current working directory
```

```bash
ni -i

# interactively select the dependency to install
# search for packages by name
```

<br>

### `nr` - run
Expand Down Expand Up @@ -158,14 +149,14 @@ nun webpack
```bash
nun

# interactively select
# interactively select
# the dependency to remove
```

```bash
nun -m

# interactive select,
# interactive select,
# but with multiple dependencies
```

Expand All @@ -178,13 +169,6 @@ nun -g silent
# bun remove -g silent
```

```bash
nun -

# uninstall the last
# installed package
```

<br>

### `nci` - clean install
Expand Down
33 changes: 20 additions & 13 deletions src/commands/ni.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import process from 'node:process'
import type { Choice } from '@posva/prompts'
import prompts from '@posva/prompts'
import { Fzf } from 'fzf'
import terminalLink from 'terminal-link'
import c from 'kleur'
import { parseNi } from '../parse'
import { runCli } from '../runner'
import { exclude, invariant } from '../utils'
import { exclude } from '../utils'
import { fetchNpmPackages } from '../fetch'

runCli(async (agent, args, ctx) => {
Expand All @@ -20,17 +21,24 @@ runCli(async (agent, args, ctx) => {
const { pattern } = await prompts({
type: 'text',
name: 'pattern',
message: 'package name',
message: 'search for package',
})

fetchPattern = pattern
}

invariant(fetchPattern)
if (!fetchPattern) {
process.exitCode = 1
return
}

const packages = await fetchNpmPackages(fetchPattern)

invariant(packages.length, 'No results found')
if (!packages.length) {
console.error('No results found')
process.exitCode = 1
return
}

const fzf = new Fzf(packages, {
selector: (item: Choice) => item.title,
Expand All @@ -42,15 +50,18 @@ runCli(async (agent, args, ctx) => {
name: 'dependency',
choices: packages,
instructions: false,
message: 'choose package',
limit: 25,
message: 'choose a package to install',
limit: 15,
async suggest(input: string, choices: Choice[]) {
const results = fzf.find(input)
return results.map(r => choices.find((c: any) => c.value === r.item.value))
},
})

invariant(dependency)
if (!dependency) {
process.exitCode = 1
return
}

args = exclude(args, '-d', '-p', '-i')

Expand All @@ -60,14 +71,10 @@ runCli(async (agent, args, ctx) => {
*/
const canInstallPeers = ['npm', 'pnpm'].includes(agent)

const { npm, repository } = dependency.links

const pkgLink = terminalLink(dependency.name, repository ?? npm)

const { mode } = await prompts({
type: 'select',
name: 'mode',
message: `install ${pkgLink} as`,
message: `install ${c.yellow(dependency.name)} as`,
choices: [
{
title: 'prod',
Expand Down
2 changes: 1 addition & 1 deletion src/commands/nr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ runCli(async (agent, args, ctx) => {
return
args.push(fn)
}
catch (e) {
catch {
process.exit(1)
}
}
Expand Down
14 changes: 10 additions & 4 deletions src/commands/nun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Fzf } from 'fzf'
import { parseNun } from '../parse'
import { runCli } from '../runner'
import { getPackageJSON } from '../fs'
import { exclude, invariant } from '../utils'
import { exclude } from '../utils'

runCli(async (agent, args, ctx) => {
const isInteractive = !args.length && !ctx?.programmatic
Expand All @@ -17,7 +17,10 @@ runCli(async (agent, args, ctx) => {

const raw = Object.entries(allDependencies) as [string, string][]

invariant(raw.length, 'No dependencies found')
if (!raw.length) {
console.error('No dependencies found')
return
}

const fzf = new Fzf(raw, {
selector: ([dep, version]) => `${dep} ${version}`,
Expand Down Expand Up @@ -52,15 +55,18 @@ runCli(async (agent, args, ctx) => {
},
})

invariant(depsToRemove.length)
if (!depsToRemove) {
process.exitCode = 1
return
}

const isSingleDependency = typeof depsToRemove === 'string'

if (isSingleDependency)
args.push(depsToRemove)
else args.push(...depsToRemove)
}
catch (e) {
catch {
process.exit(1)
}
}
Expand Down
13 changes: 8 additions & 5 deletions src/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import process from 'node:process'
import type { Choice } from '@posva/prompts'
import terminalLink from 'terminal-link'
import { limitText } from './utils'
import c from 'kleur'
import { formatPackageWithUrl } from './utils'

export interface NpmPackage {
name: string
Expand Down Expand Up @@ -31,12 +31,15 @@ export async function fetchNpmPackages(pattern: string): Promise<Choice[]> {
.then(res => res.json()) as NpmRegistryResponse

return result.objects.map(({ package: pkg }) => ({
title: terminalLink(pkg.name, pkg.links.repository ?? pkg.links.npm),
title: formatPackageWithUrl(
`${pkg.name.padEnd(30, ' ')} ${c.blue(`v${pkg.version}`)}`,
pkg.links.repository ?? pkg.links.npm,
terminalColumns,
),
value: pkg,
description: limitText(pkg.version, terminalColumns - 20),
}))
}
catch (e) {
catch {
console.error('Error when fetching npm registry')
process.exit(1)
}
Expand Down
31 changes: 15 additions & 16 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { Buffer } from 'node:buffer'
import process from 'node:process'
import which from 'which'
import c from 'kleur'
import terminalLink from 'terminal-link'

export const CLI_TEMP_DIR = join(os.tmpdir(), 'antfu-ni')

Expand Down Expand Up @@ -93,24 +94,22 @@ export async function writeFileSafe(
return false
}

export function invariant(condition: unknown, errorMsg?: string) {
if (condition)
return

if (errorMsg) {
console.error(errorMsg)
process.exit(1)
}

/**
* if message is not provided
* just silently exit
*/
process.exit(0)
}

export function limitText(text: string, maxWidth: number) {
if (text.length <= maxWidth)
return text
return `${text.slice(0, maxWidth)}${c.dim('…')}`
}

export function formatPackageWithUrl(pkg: string, url?: string, limits = 80) {
return url
? terminalLink(
pkg,
url,
{
fallback: (_, url) => (pkg.length + url.length > limits)
? pkg
: pkg + c.dim(` - ${url}`),
},
)
: pkg
}
4 changes: 2 additions & 2 deletions taze.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import { defineConfig } from 'taze'

export default defineConfig({
ignorePaths: [
'test/fixtures'
]
'test/fixtures',
],
})

0 comments on commit 8bcfe60

Please sign in to comment.