-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
329 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
import { MigrateCommand } from '@/commands/migrate/migrate.command' | ||
import { RunCommand } from '@/commands/run/run.command' | ||
import { RunManyCommand } from '@/commands/run-many/run-many.command' | ||
import { ShowCommand } from '@/commands/show/show.command' | ||
|
||
export const Commands = [MigrateCommand, RunCommand, ShowCommand] | ||
export const Commands = [ | ||
RunCommand, | ||
MigrateCommand, | ||
ShowCommand, | ||
RunManyCommand | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
import { | ||
compact, | ||
concurrent, | ||
concurrently, | ||
isEmpty, | ||
isTypeOfBoolean | ||
} from '@neodx/std' | ||
import { Inject } from '@nestjs/common' | ||
import { | ||
CliUtilityService, | ||
Command, | ||
CommandRunner, | ||
Option | ||
} from 'nest-commander' | ||
import { cpus } from 'node:os' | ||
import { RunCommand } from '@/commands/run/run.command' | ||
import { LoggerService } from '@/logger' | ||
import type { AbstractPackageManager } from '@/pkg-manager' | ||
import { InjectPackageManager } from '@/pkg-manager/pkg-manager.decorator' | ||
import type { WorkspaceProject } from '@/pkg-manager/pkg-manager.types' | ||
import { invariant } from '@/shared/misc' | ||
|
||
export interface RunManyCommandOptions { | ||
all?: boolean | ||
exclude?: string[] | ||
parallel?: number | ||
projects?: string[] | ||
targets?: string[] | ||
} | ||
|
||
@Command({ | ||
name: 'run-many', | ||
// TODO: add descriptions | ||
description: '' | ||
}) | ||
export class RunManyCommand extends CommandRunner { | ||
constructor( | ||
@InjectPackageManager() private readonly manager: AbstractPackageManager, | ||
@Inject(LoggerService) private readonly logger: LoggerService, | ||
@Inject(RunCommand) private readonly runner: RunCommand, | ||
private readonly utilityService: CliUtilityService | ||
) { | ||
super() | ||
} | ||
|
||
private readonly DEFAULT_CONCURRENT_THREADS = 1 as const | ||
|
||
public async run(_: string[], opts: RunManyCommandOptions) { | ||
await this.manager.computeWorkspaceProjects() | ||
|
||
const timeEnd = this.logger.time() | ||
const { projects } = this.manager | ||
|
||
const computeProjectsToRun = (): WorkspaceProject[] => { | ||
if (opts.all) return projects | ||
|
||
const isExcluded = ({ name }: WorkspaceProject) => | ||
!opts.exclude?.includes(name) | ||
const isIncluded = ({ name }: WorkspaceProject) => | ||
opts.projects?.includes(name) | ||
|
||
if (opts.exclude) return projects.filter(isExcluded) | ||
|
||
if (opts.projects) return projects.filter(isIncluded) | ||
|
||
this.logger.error( | ||
'Invalid options provided: Either "all", "exclude", or "projects" should be specified.' | ||
) | ||
process.exit(1) | ||
} | ||
const projectsToRun = computeProjectsToRun() | ||
|
||
invariant(opts.targets && !isEmpty(opts.targets), 'Targets are required.') | ||
|
||
const targetsToRun = opts.targets as string[] | ||
const threadsConcurrency = isTypeOfBoolean(opts.parallel) | ||
? cpus().length | ||
: opts.parallel ?? this.DEFAULT_CONCURRENT_THREADS | ||
|
||
await concurrently( | ||
projectsToRun, | ||
async (projectMeta) => { | ||
const targetsConcurrency = Math.min(targetsToRun.length, cpus().length) | ||
|
||
const splitTargets = concurrent<string, void>(async (target) => { | ||
const project = projectMeta.name | ||
this.logger.log( | ||
`${this.logger.greaterSignPrefix} gx run ${project}:${target}\n` | ||
) | ||
|
||
this.logger.mute() | ||
try { | ||
await this.runner.run([target, project], {}) | ||
} catch {} | ||
|
||
this.logger.unmute() | ||
}, targetsConcurrency) | ||
|
||
await splitTargets(targetsToRun) | ||
}, | ||
threadsConcurrency | ||
) | ||
|
||
const projectNames = projectsToRun.map(({ name }) => name) | ||
|
||
const joinCommas = (arr: string[]) => arr.join(', ') | ||
|
||
timeEnd( | ||
`Successfully ran targets ${joinCommas(targetsToRun)} for projects ${joinCommas(projectNames)}` | ||
) | ||
} | ||
|
||
@Option({ | ||
flags: '--all [boolean]', | ||
name: 'all' | ||
}) | ||
public parseAll(all: string) { | ||
return this.utilityService.parseBoolean(all) | ||
} | ||
|
||
@Option({ | ||
flags: '--parallel [boolean]', | ||
name: 'parallel' | ||
}) | ||
public parseParallel(parallel: string) { | ||
return this.utilityService.parseInt(parallel) | ||
} | ||
|
||
@Option({ | ||
flags: '-e --exclude [string]', | ||
name: 'exclude' | ||
}) | ||
public parseExclude(exclude: string) { | ||
return serializeCommas(exclude) | ||
} | ||
|
||
@Option({ | ||
flags: '-p --projects [string]', | ||
name: 'projects' | ||
}) | ||
public parseProjects(projects: string) { | ||
return serializeCommas(projects) | ||
} | ||
|
||
@Option({ | ||
flags: '-t --targets [string]', | ||
name: 'targets' | ||
}) | ||
public parseTargets(targets: string) { | ||
return serializeCommas(targets) | ||
} | ||
} | ||
|
||
const serializeCommas = (str: string) => compact(str.split(', ')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.