Skip to content

Commit

Permalink
feat: allow overriding which command to run in the workspace via a ta…
Browse files Browse the repository at this point in the history
…sk option
  • Loading branch information
apaleslimghost committed Apr 24, 2024
1 parent ae093bd commit a617d77
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 4 deletions.
4 changes: 3 additions & 1 deletion lib/schemas/src/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { SmokeTestSchema } from './tasks/n-test'
import { CypressSchema } from './tasks/cypress'
import { HerokuProductionSchema } from './tasks/heroku-production'
import { ServerlessRunSchema } from './tasks/serverless-run'
import { WorkspaceCommandSchema } from './tasks/workspace-command'

export const TaskSchemas = {
Babel: BabelSchema,
Expand All @@ -30,7 +31,8 @@ export const TaskSchemas = {
ServerlessRun: ServerlessRunSchema,
TypeScript: TypeScriptSchema,
UploadAssetsToS3: UploadAssetsToS3Schema,
Webpack: WebpackSchema
Webpack: WebpackSchema,
WorkspaceCommand: WorkspaceCommandSchema
}

export type TaskOptions = InferSchemaOptions<typeof TaskSchemas>
9 changes: 9 additions & 0 deletions lib/schemas/src/tasks/workspace-command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { z } from 'zod'

export const WorkspaceCommandSchema = z.object({
command: z.string().optional()
})

export type WorkspaceCommandOptions = z.infer<typeof WorkspaceCommandSchema>

export const Schema = WorkspaceCommandSchema
4 changes: 4 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions plugins/monorepo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"dotcom-tool-kit": "3.x"
},
"dependencies": {
"@dotcom-tool-kit/schemas": "^1.0.0",
"@npmcli/map-workspaces": "^3.0.6"
},
"devDependencies": {
Expand Down
9 changes: 6 additions & 3 deletions plugins/monorepo/src/tasks/workspace-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import path from 'path'
import { loadConfig } from 'dotcom-tool-kit/lib/config'
import { runTasksFromConfig } from 'dotcom-tool-kit/lib/tasks'
import { ToolKitError } from '@dotcom-tool-kit/error'
import { WorkspaceCommandSchema } from '@dotcom-tool-kit/schemas/lib/tasks/workspace-command'

export default class WorkspaceCommand extends Task {
export default class WorkspaceCommand extends Task<{ task: typeof WorkspaceCommandSchema }> {
async runPackageCommand(packageId: string, packagePath: string, command: string, files?: string[]) {
const config = await loadConfig(this.logger, { root: packagePath })

Expand All @@ -20,7 +21,9 @@ export default class WorkspaceCommand extends Task {
const workspaces = await mapWorkspaces({ cwd, pkg })

const results = await Promise.allSettled(
Array.from(workspaces, ([id, packagePath]) => this.runPackageCommand(id, packagePath, command, files))
Array.from(workspaces, ([id, packagePath]) =>
this.runPackageCommand(id, packagePath, this.options.command ?? command, files)
)
)

const erroredCommands = results.filter(
Expand All @@ -29,7 +32,7 @@ export default class WorkspaceCommand extends Task {

if (erroredCommands.length) {
// TODO improve error messages
const error = new ToolKitError(`error running workspace command ${command}`)
const error = new ToolKitError(`error running workspace command ${this.options.command ?? command}`)
error.details = erroredCommands.map((result) => result.reason.toString()).join('\n\n')
throw error
}
Expand Down
3 changes: 3 additions & 0 deletions plugins/monorepo/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"references": [
{
"path": "../../lib/base"
},
{
"path": "../../lib/schemas"
}
],
"include": ["src/**/*"]
Expand Down

0 comments on commit a617d77

Please sign in to comment.