|
| 1 | +import { assertDir, ensureFile } from '@neodx/fs' |
| 2 | +import { mapValues } from '@neodx/std' |
| 3 | +import { Inject } from '@nestjs/common' |
| 4 | +import { |
| 5 | + CliUtilityService, |
| 6 | + Command, |
| 7 | + CommandRunner, |
| 8 | + Option |
| 9 | +} from 'nest-commander' |
| 10 | +import { resolve } from 'node:path' |
| 11 | +import { ConfigService } from '@/config' |
| 12 | +import { LoggerService } from '@/logger' |
| 13 | +import type { AbstractPackageManager } from '@/pkg-manager' |
| 14 | +import { InjectPackageManager } from '@/pkg-manager' |
| 15 | +import { ROOT_PROJECT } from '@/pkg-manager/pkg-manager.consts' |
| 16 | +import type { WorkspaceProject } from '@/pkg-manager/pkg-manager.types' |
| 17 | +import type { TargetOptions } from '@/resolver/targets/targets-resolver.schema' |
| 18 | +import type { PackageJson, PackageScripts } from '@/shared/json' |
| 19 | +import { writeJson } from '@/shared/json' |
| 20 | +import { invariant } from '@/shared/misc' |
| 21 | + |
| 22 | +export interface MigrateCommandOptions { |
| 23 | + all?: boolean |
| 24 | +} |
| 25 | + |
| 26 | +@Command({ |
| 27 | + name: 'migrate', |
| 28 | + // TODO: add descriptions |
| 29 | + description: '' |
| 30 | +}) |
| 31 | +export class MigrateCommand extends CommandRunner { |
| 32 | + constructor( |
| 33 | + @InjectPackageManager() private readonly manager: AbstractPackageManager, |
| 34 | + @Inject(LoggerService) private readonly logger: LoggerService, |
| 35 | + @Inject(ConfigService) private readonly cfg: ConfigService, |
| 36 | + private readonly utilityService: CliUtilityService |
| 37 | + ) { |
| 38 | + super() |
| 39 | + } |
| 40 | + |
| 41 | + public async run(params: string[], options: MigrateCommandOptions) { |
| 42 | + await this.manager.computeWorkspaceProjects() |
| 43 | + |
| 44 | + const { projects: workspaceProjects } = this.manager |
| 45 | + |
| 46 | + if (options.all) { |
| 47 | + await Promise.all( |
| 48 | + workspaceProjects.map(async (projectMeta) => |
| 49 | + this.migrateProjectCommands(projectMeta, options) |
| 50 | + ) |
| 51 | + ) |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + const [projectName = ROOT_PROJECT] = params |
| 56 | + |
| 57 | + const projectMeta = workspaceProjects.find( |
| 58 | + ({ name }) => name === projectName |
| 59 | + ) |
| 60 | + |
| 61 | + invariant(projectMeta, `Project '${projectName}' not found.`) |
| 62 | + |
| 63 | + await this.migrateProjectCommands(projectMeta) |
| 64 | + } |
| 65 | + |
| 66 | + private async migrateProjectCommands( |
| 67 | + projectMeta: WorkspaceProject, |
| 68 | + options?: MigrateCommandOptions |
| 69 | + ): Promise<void> { |
| 70 | + const { location: projectCwd, type: targetType } = projectMeta |
| 71 | + |
| 72 | + await assertDir(projectMeta.location) |
| 73 | + |
| 74 | + if (targetType === 'package-scripts') { |
| 75 | + const commandsFilePath = resolve( |
| 76 | + projectMeta.location, |
| 77 | + this.cfg.commandsFile |
| 78 | + ) |
| 79 | + const pkgScripts = projectMeta.targets as NonNullable< |
| 80 | + PackageJson['scripts'] |
| 81 | + > |
| 82 | + |
| 83 | + await ensureFile(commandsFilePath) |
| 84 | + |
| 85 | + const serializedTargets = mapValues<PackageScripts, TargetOptions>( |
| 86 | + pkgScripts, |
| 87 | + (command) => ({ command }) |
| 88 | + ) |
| 89 | + |
| 90 | + await writeJson(commandsFilePath, serializedTargets) |
| 91 | + |
| 92 | + this.logger.info(`Generated ${this.cfg.commandsFile} in ${projectCwd}`) |
| 93 | + return |
| 94 | + } |
| 95 | + |
| 96 | + if (!options?.all) { |
| 97 | + this.logger.error( |
| 98 | + `The specified target type '${targetType}' is not allowed. |
| 99 | + It seems you may already have a '${this.cfg.commandsFile}' file in your project.` |
| 100 | + ) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Option({ |
| 105 | + flags: '--all [boolean]', |
| 106 | + name: 'all' |
| 107 | + }) |
| 108 | + public parseAll(all: string) { |
| 109 | + return this.utilityService.parseBoolean(all) |
| 110 | + } |
| 111 | +} |
0 commit comments