|
| 1 | +import { ensureFile } from '@neodx/fs' |
| 2 | +import { hasOwn, isNil } from '@neodx/std' |
| 3 | +import chalk from 'chalk' |
| 4 | +import { Command, CommandRunner, Option } from 'nest-commander' |
| 5 | +import { dirname, resolve } from 'node:path' |
| 6 | +import type { AbstractPackageManager } from '@/pkg-manager' |
| 7 | +import { InjectPackageManager } from '@/pkg-manager' |
| 8 | +import { PackageManager } from '@/pkg-manager/pkg-manager.consts' |
| 9 | +import type { PackageJson } from '@/shared/json' |
| 10 | +import { readJson } from '@/shared/json' |
| 11 | +import { addLibraryPrefix } from '@/shared/misc' |
| 12 | + |
| 13 | +export interface BaseInitOptions { |
| 14 | + args?: string |
| 15 | +} |
| 16 | + |
| 17 | +@Command({ |
| 18 | + name: 'run', |
| 19 | + options: { |
| 20 | + isDefault: true |
| 21 | + }, |
| 22 | + description: '' |
| 23 | +}) |
| 24 | +export class RunCommand extends CommandRunner { |
| 25 | + constructor( |
| 26 | + @InjectPackageManager() private readonly manager: AbstractPackageManager |
| 27 | + ) { |
| 28 | + super() |
| 29 | + } |
| 30 | + |
| 31 | + public async run(params: string[], options: BaseInitOptions) { |
| 32 | + const [target, project = null] = params |
| 33 | + |
| 34 | + if (!target) { |
| 35 | + console.log('no target') |
| 36 | + return |
| 37 | + } |
| 38 | + |
| 39 | + const startTime = Date.now() |
| 40 | + |
| 41 | + let packageJsonPath = resolve(process.cwd(), 'package.json') |
| 42 | + |
| 43 | + if (project) { |
| 44 | + const workspaces = await this.manager.getWorkspaces() |
| 45 | + |
| 46 | + const projectMeta = workspaces.find( |
| 47 | + (workspace) => workspace.name === project |
| 48 | + ) |
| 49 | + |
| 50 | + if (!projectMeta) { |
| 51 | + // TODO improve errors rewrite to class |
| 52 | + throw new Error( |
| 53 | + `Project ${chalk.cyan(project)} not found. Please ensure it exists.` |
| 54 | + ) |
| 55 | + } |
| 56 | + |
| 57 | + packageJsonPath = resolve(projectMeta.location, 'package.json') |
| 58 | + } |
| 59 | + |
| 60 | + await ensureFile(packageJsonPath) |
| 61 | + |
| 62 | + const pkg = (await readJson(packageJsonPath)) as PackageJson |
| 63 | + |
| 64 | + const projectName = project ?? pkg.name ?? 'root' |
| 65 | + |
| 66 | + if (isNil(pkg.scripts) || !hasOwn(pkg.scripts, target)) { |
| 67 | + // TODO improve errors |
| 68 | + throw new Error( |
| 69 | + `Could not find target ${chalk.cyan(target)} in project ${chalk.white(projectName)}.` |
| 70 | + ) |
| 71 | + } |
| 72 | + |
| 73 | + const command = this.manager.createRunCommand({ |
| 74 | + target, |
| 75 | + project, |
| 76 | + packageJsonPath, |
| 77 | + args: options.args |
| 78 | + }) |
| 79 | + |
| 80 | + try { |
| 81 | + // https://github.com/oven-sh/bun/issues/6386 |
| 82 | + const cwd = |
| 83 | + this.manager.agent === PackageManager.BUN |
| 84 | + ? dirname(packageJsonPath) |
| 85 | + : process.cwd() |
| 86 | + |
| 87 | + await this.manager.exec(command, { stdio: 'inherit', cwd }) |
| 88 | + } catch (error) { |
| 89 | + console.log(error) |
| 90 | + } |
| 91 | + |
| 92 | + // TODO: improve logger |
| 93 | + console.info( |
| 94 | + addLibraryPrefix( |
| 95 | + `Successfully ran target ${chalk.cyan(target)} for project ${chalk.white(project ?? pkg.name)} (${Date.now() - startTime} ms)` |
| 96 | + ) |
| 97 | + ) |
| 98 | + } |
| 99 | + |
| 100 | + @Option({ |
| 101 | + flags: '-args, --args [string]', |
| 102 | + name: 'args' |
| 103 | + }) |
| 104 | + public parseArgs(args: string) { |
| 105 | + if (!args) return |
| 106 | + |
| 107 | + const isValid = args.match(/^--\w+=\w+$/) |
| 108 | + |
| 109 | + if (!isValid) { |
| 110 | + throw new Error('not valid') |
| 111 | + } |
| 112 | + |
| 113 | + return args |
| 114 | + } |
| 115 | +} |
0 commit comments