Skip to content

Commit

Permalink
fix: add new Task type for scripts with args so that we can pass them…
Browse files Browse the repository at this point in the history
… to ni (#235)

fix: add new Task type for scripts with args so that we can pass them to ni for quoting
  • Loading branch information
dominikg authored Jul 31, 2023
1 parent 8b8d1ec commit 2ba8f9b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
7 changes: 5 additions & 2 deletions tests/nx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ export async function test(options: RunOptions) {
...options,
repo: 'nrwl/nx',
branch: 'master',
build: 'build-project vite --skip-nx-cache',
test: ['test vite --skip-nx-cache', 'e2e e2e-vite --skip-nx-cache'],
build: { script: 'build-project', args: ['vite', '--skip-nx-cache'] },
test: [
{ script: 'test', args: ['vite', '--skip-nx-cache'] },
{ script: 'e2e', args: ['e2e-vite', '--skip-nx-cache'] },
],
})
}
2 changes: 1 addition & 1 deletion tests/vitest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ export async function test(options: RunOptions) {
...options,
repo: 'vitest-dev/vitest',
build: 'build',
test: 'test:run --allowOnly',
test: { script: 'test:run', args: ['--allowOnly'] },
})
}
2 changes: 1 addition & 1 deletion types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface RunOptions {
beforeTest?: Task | Task[]
}

type Task = string | (() => Promise<any>)
type Task = string | { script: string; args?: string[] } | (() => Promise<any>)

export interface CommandOptions {
suites?: string[]
Expand Down
15 changes: 13 additions & 2 deletions utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,26 @@ function toCommand(
if (task == null || task === '') {
continue
} else if (typeof task === 'string') {
const scriptOrBin = task.trim().split(/\s+/)[0]
if (scripts?.[scriptOrBin] != null) {
if (scripts[task] != null) {
const runTaskWithAgent = getCommand(agent, 'run', [task])
await $`${runTaskWithAgent}`
} else {
await $`${task}`
}
} else if (typeof task === 'function') {
await task()
} else if (task?.script) {
if (scripts[task.script] != null) {
const runTaskWithAgent = getCommand(agent, 'run', [
task.script,
...(task.args ?? []),
])
await $`${runTaskWithAgent}`
} else {
throw new Error(
`invalid task, script "${task.script}" does not exist in package.json`,
)
}
} else {
throw new Error(
`invalid task, expected string or function but got ${typeof task}: ${task}`,
Expand Down

0 comments on commit 2ba8f9b

Please sign in to comment.