Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add new Task type for scripts with args so that we can pass them to ni #235

Merged
merged 1 commit into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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