Skip to content

Commit

Permalink
feat: update
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Aug 26, 2024
1 parent f1a8ae9 commit 571d519
Show file tree
Hide file tree
Showing 33 changed files with 119 additions and 99 deletions.
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export * from 'package-manager-detector/constants'
export * from 'package-manager-detector/commands'

export * from './config'
export * from './detect'
export * from './parse'
Expand Down
31 changes: 24 additions & 7 deletions src/parse.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Agent, Command, ResolvedCommand } from 'package-manager-detector'
import { resolveCommand } from 'package-manager-detector'
import { exclude } from './utils'
import type { Runner } from './runner'
import { COMMANDS, constructCommand } from '.'

export class UnsupportedCommand extends Error {
constructor({ agent, command }: { agent: Agent, command: Command }) {
Expand All @@ -14,11 +14,12 @@ export function getCommand(
command: Command,
args: string[] = [],
): ResolvedCommand {
const result = resolveCommand(agent, command, args)
if (!COMMANDS[agent])
throw new Error(`Unsupported agent "${agent}"`)
if (!COMMANDS[agent][command])
throw new UnsupportedCommand({ agent, command })

if (!result)
throw new Error(`Unsupported agent or command "${agent} ${command}"`)
return result
return constructCommand(COMMANDS[agent][command], args)!
}

export const parseNi = <Runner>((agent, args, ctx) => {
Expand Down Expand Up @@ -47,12 +48,20 @@ export const parseNr = <Runner>((agent, args) => {
if (args.length === 0)
args.push('start')

let hasIfPresent = false
if (args.includes('--if-present')) {
args = exclude(args, '--if-present')
args[0] = `--if-present ${args[0]}`
hasIfPresent = true
}

return getCommand(agent, 'run', args)
const cmd = getCommand(agent, 'run', args)
if (!cmd)
return cmd

if (hasIfPresent)
cmd.args.splice(1, 0, '--if-present')

return cmd
})

export const parseNu = <Runner>((agent, args) => {
Expand All @@ -75,3 +84,11 @@ export const parseNlx = <Runner>((agent, args) => {
export const parseNa = <Runner>((agent, args) => {
return getCommand(agent, 'agent', args)
})

export function serializeCommand(command?: ResolvedCommand) {
if (!command)
return undefined
if (command.args.length === 0)
return command.command
return `${command.command} ${command.args.map(i => i.includes(' ') ? `"${i}"` : i).join(' ')}`
}
6 changes: 3 additions & 3 deletions test/na/bun.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { expect, it } from 'vitest'
import { parseNa } from '../../src/commands'
import { parseNa, serializeCommand } from '../../src/commands'

const agent = 'bun'
function _(arg: string, expected: string) {
return () => {
return async () => {
expect(
parseNa(agent, arg.split(' ').filter(Boolean)),
serializeCommand(await parseNa(agent, arg.split(' ').filter(Boolean))),
).toBe(
expected,
)
Expand Down
6 changes: 3 additions & 3 deletions test/na/npm.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { expect, it } from 'vitest'
import { parseNa } from '../../src/commands'
import { parseNa, serializeCommand } from '../../src/commands'

const agent = 'npm'
function _(arg: string, expected: string) {
return () => {
return async () => {
expect(
parseNa(agent, arg.split(' ').filter(Boolean)),
serializeCommand(await parseNa(agent, arg.split(' ').filter(Boolean))),
).toBe(
expected,
)
Expand Down
6 changes: 3 additions & 3 deletions test/na/pnpm.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { expect, it } from 'vitest'
import { parseNa } from '../../src/commands'
import { parseNa, serializeCommand } from '../../src/commands'

const agent = 'pnpm'
function _(arg: string, expected: string) {
return () => {
return async () => {
expect(
parseNa(agent, arg.split(' ').filter(Boolean)),
serializeCommand(await parseNa(agent, arg.split(' ').filter(Boolean))),
).toBe(
expected,
)
Expand Down
6 changes: 3 additions & 3 deletions test/na/yarn.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { expect, it } from 'vitest'
import { parseNa } from '../../src/commands'
import { parseNa, serializeCommand } from '../../src/commands'

const agent = 'yarn'
function _(arg: string, expected: string) {
return () => {
return async () => {
expect(
parseNa(agent, arg.split(' ').filter(Boolean)),
serializeCommand(await parseNa(agent, arg.split(' ').filter(Boolean))),
).toBe(
expected,
)
Expand Down
6 changes: 3 additions & 3 deletions test/na/yarn@berry.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { expect, it } from 'vitest'
import { parseNa } from '../../src/commands'
import { parseNa, serializeCommand } from '../../src/commands'

const agent = 'yarn@berry'
function _(arg: string, expected: string) {
return () => {
return async () => {
expect(
parseNa(agent, arg.split(' ').filter(Boolean)),
serializeCommand(await parseNa(agent, arg.split(' ').filter(Boolean))),
).toBe(
expected,
)
Expand Down
6 changes: 3 additions & 3 deletions test/ni/bun.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { expect, it } from 'vitest'
import { parseNi } from '../../src/commands'
import { parseNi, serializeCommand } from '../../src/commands'

const agent = 'bun'
function _(arg: string, expected: string) {
return () => {
return async () => {
expect(
parseNi(agent, arg.split(' ').filter(Boolean)),
serializeCommand(await parseNi(agent, arg.split(' ').filter(Boolean))),
).toBe(
expected,
)
Expand Down
6 changes: 3 additions & 3 deletions test/ni/npm.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { expect, it } from 'vitest'
import { parseNi } from '../../src/commands'
import { parseNi, serializeCommand } from '../../src/commands'

const agent = 'npm'
function _(arg: string, expected: string) {
return () => {
return async () => {
expect(
parseNi(agent, arg.split(' ').filter(Boolean)),
serializeCommand(await parseNi(agent, arg.split(' ').filter(Boolean))),
).toBe(
expected,
)
Expand Down
6 changes: 3 additions & 3 deletions test/ni/pnpm.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { expect, it } from 'vitest'
import { parseNi } from '../../src/commands'
import { parseNi, serializeCommand } from '../../src/commands'

const agent = 'pnpm'
function _(arg: string, expected: string) {
return () => {
return async () => {
expect(
parseNi(agent, arg.split(' ').filter(Boolean)),
serializeCommand(await parseNi(agent, arg.split(' ').filter(Boolean))),
).toBe(
expected,
)
Expand Down
6 changes: 3 additions & 3 deletions test/ni/yarn.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { expect, it } from 'vitest'
import { parseNi } from '../../src/commands'
import { parseNi, serializeCommand } from '../../src/commands'

const agent = 'yarn'
function _(arg: string, expected: string) {
return () => {
return async () => {
expect(
parseNi(agent, arg.split(' ').filter(Boolean)),
serializeCommand(await parseNi(agent, arg.split(' ').filter(Boolean))),
).toBe(
expected,
)
Expand Down
6 changes: 3 additions & 3 deletions test/ni/yarn@berry.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { expect, it } from 'vitest'
import { parseNi } from '../../src/commands'
import { parseNi, serializeCommand } from '../../src/commands'

const agent = 'yarn@berry'
function _(arg: string, expected: string) {
return () => {
return async () => {
expect(
parseNi(agent, arg.split(' ').filter(Boolean)),
serializeCommand(await parseNi(agent, arg.split(' ').filter(Boolean))),
).toBe(
expected,
)
Expand Down
6 changes: 3 additions & 3 deletions test/nlx/bun.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { expect, it } from 'vitest'
import { parseNlx } from '../../src/commands'
import { parseNlx, serializeCommand } from '../../src/commands'

const agent = 'bun'
function _(arg: string, expected: string) {
return () => {
return async () => {
expect(
parseNlx(agent, arg.split(' ').filter(Boolean)),
serializeCommand(await parseNlx(agent, arg.split(' ').filter(Boolean))),
).toBe(
expected,
)
Expand Down
6 changes: 3 additions & 3 deletions test/nlx/npm.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { expect, it } from 'vitest'
import { parseNlx } from '../../src/commands'
import { parseNlx, serializeCommand } from '../../src/commands'

const agent = 'npm'
function _(arg: string, expected: string) {
return () => {
return async () => {
expect(
parseNlx(agent, arg.split(' ').filter(Boolean)),
serializeCommand(await parseNlx(agent, arg.split(' ').filter(Boolean))),
).toBe(
expected,
)
Expand Down
6 changes: 3 additions & 3 deletions test/nlx/pnpm.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { expect, it } from 'vitest'
import { parseNlx } from '../../src/commands'
import { parseNlx, serializeCommand } from '../../src/commands'

const agent = 'pnpm'
function _(arg: string, expected: string) {
return () => {
return async () => {
expect(
parseNlx(agent, arg.split(' ').filter(Boolean)),
serializeCommand(await parseNlx(agent, arg.split(' ').filter(Boolean))),
).toBe(
expected,
)
Expand Down
6 changes: 3 additions & 3 deletions test/nlx/yarn.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { expect, it } from 'vitest'
import { parseNlx } from '../../src/commands'
import { parseNlx, serializeCommand } from '../../src/commands'

const agent = 'yarn'
function _(arg: string, expected: string) {
return () => {
return async () => {
expect(
parseNlx(agent, arg.split(' ').filter(Boolean)),
serializeCommand(await parseNlx(agent, arg.split(' ').filter(Boolean))),
).toBe(
expected,
)
Expand Down
6 changes: 3 additions & 3 deletions test/nlx/yarn@berry.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { expect, it } from 'vitest'
import { parseNlx } from '../../src/commands'
import { parseNlx, serializeCommand } from '../../src/commands'

const agent = 'yarn@berry'
function _(arg: string, expected: string) {
return () => {
return async () => {
expect(
parseNlx(agent, arg.split(' ').filter(Boolean)),
serializeCommand(await parseNlx(agent, arg.split(' ').filter(Boolean))),
).toBe(
expected,
)
Expand Down
6 changes: 3 additions & 3 deletions test/nr/bun.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { expect, it } from 'vitest'
import { parseNr } from '../../src/commands'
import { parseNr, serializeCommand } from '../../src/commands'

const agent = 'bun'
function _(arg: string, expected: string) {
return () => {
return async () => {
expect(
parseNr(agent, arg.split(' ').filter(Boolean)),
serializeCommand(await parseNr(agent, arg.split(' ').filter(Boolean))),
).toBe(
expected,
)
Expand Down
6 changes: 3 additions & 3 deletions test/nr/npm.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { expect, it } from 'vitest'
import { parseNr } from '../../src/commands'
import { parseNr, serializeCommand } from '../../src/commands'

const agent = 'npm'
function _(arg: string, expected: string) {
return () => {
return async () => {
expect(
parseNr(agent, arg.split(' ').filter(Boolean)),
serializeCommand(await parseNr(agent, arg.split(' ').filter(Boolean))),
).toBe(
expected,
)
Expand Down
6 changes: 3 additions & 3 deletions test/nr/pnpm.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { expect, it } from 'vitest'
import { parseNr } from '../../src/commands'
import { parseNr, serializeCommand } from '../../src/commands'

const agent = 'pnpm'
function _(arg: string, expected: string) {
return () => {
return async () => {
expect(
parseNr(agent, arg.split(' ').filter(Boolean)),
serializeCommand(await parseNr(agent, arg.split(' ').filter(Boolean))),
).toBe(
expected,
)
Expand Down
6 changes: 3 additions & 3 deletions test/nr/yarn.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { expect, it } from 'vitest'
import { parseNr } from '../../src/commands'
import { parseNr, serializeCommand } from '../../src/commands'

const agent = 'yarn'
function _(arg: string, expected: string) {
return () => {
return async () => {
expect(
parseNr(agent, arg.split(' ').filter(Boolean)),
serializeCommand(await parseNr(agent, arg.split(' ').filter(Boolean))),
).toBe(
expected,
)
Expand Down
6 changes: 3 additions & 3 deletions test/nr/yarn@berry.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { expect, it } from 'vitest'
import { parseNr } from '../../src/commands'
import { parseNr, serializeCommand } from '../../src/commands'

const agent = 'yarn@berry'
function _(arg: string, expected: string) {
return () => {
return async () => {
expect(
parseNr(agent, arg.split(' ').filter(Boolean)),
serializeCommand(await parseNr(agent, arg.split(/\s/g).filter(Boolean))),
).toBe(
expected,
)
Expand Down
6 changes: 3 additions & 3 deletions test/nu/bun.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { expect, it } from 'vitest'
import { parseNu } from '../../src/commands'
import { parseNu, serializeCommand } from '../../src/commands'

const agent = 'bun'
function _(arg: string, expected: string | null) {
return () => {
return async () => {
expect(
parseNu(agent, arg.split(' ').filter(Boolean)),
serializeCommand(await parseNu(agent, arg.split(' ').filter(Boolean))),
).toBe(
expected,
)
Expand Down
6 changes: 3 additions & 3 deletions test/nu/npm.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { expect, it } from 'vitest'
import { parseNu } from '../../src/commands'
import { parseNu, serializeCommand } from '../../src/commands'

const agent = 'npm'
function _(arg: string, expected: string) {
return () => {
return async () => {
expect(
parseNu(agent, arg.split(' ').filter(Boolean)),
serializeCommand(await parseNu(agent, arg.split(' ').filter(Boolean))),
).toBe(
expected,
)
Expand Down
Loading

0 comments on commit 571d519

Please sign in to comment.