diff --git a/package.json b/package.json index d0aa97b..baab705 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "monux-cli", - "version": "2.0.5", + "version": "2.0.6", "license": "MIT", "main": "index.js", "engines": { diff --git a/src/commands/add/add-ts-library/add-ts-library.command.ts b/src/commands/add/add-ts-library/add-ts-library.command.ts index 5282c0d..e20cd93 100644 --- a/src/commands/add/add-ts-library/add-ts-library.command.ts +++ b/src/commands/add/add-ts-library/add-ts-library.command.ts @@ -55,7 +55,7 @@ export class AddTsLibraryCommand extends AddCommand { await FsUtilities.createFile(getPath(root, 'src', 'index.ts'), ''); await NpmUtilities.install(config.name, [NpmPackage.VITE_PLUGIN_DTS], true); - await NpmUtilities.run(config.name, 'build'); + NpmUtilities.run(config.name, 'build'); await this.installInProjects(config); } diff --git a/src/commands/run/run.command.ts b/src/commands/run/run.command.ts index 1ffea8c..a0840e0 100644 --- a/src/commands/run/run.command.ts +++ b/src/commands/run/run.command.ts @@ -1,24 +1,19 @@ -import { Dirent } from 'fs'; - import { CPUtilities } from '../../encapsulation'; import { NativeNpmCommands, NpmUtilities } from '../../npm'; -import { getPath } from '../../utilities'; -import { WorkspaceUtilities } from '../../workspace'; /** * Runs the run cli command. * @param args - The passed cli commands. */ -export async function runRun(...args: string[]): Promise { +export function runRun(...args: string[]): void { const projectName: string = args[0]; const npmScript: string = args[1]; const nativeCommand: boolean = Object.values(NativeNpmCommands).includes(npmScript as NativeNpmCommands); + const commands: string = args.slice(1).join(' '); if (!nativeCommand) { - await NpmUtilities.run(projectName, npmScript); + NpmUtilities.run(projectName, commands); } - const project: Dirent = await WorkspaceUtilities.findProjectOrFail(projectName); - const projectPath: string = getPath(project.parentPath, project.name); - CPUtilities.execSync(`cd ${projectPath} && npm ${args.slice(1).join(' ')}`); + CPUtilities.execSync(`npm ${commands} --workspace=${projectName}`); } \ No newline at end of file diff --git a/src/commands/validate-input.function.ts b/src/commands/validate-input.function.ts index 2ca71b4..047464a 100644 --- a/src/commands/validate-input.function.ts +++ b/src/commands/validate-input.function.ts @@ -7,8 +7,9 @@ import { exitWithError } from './exit-with-error.function'; import { PACKAGE_JSON_FILE_NAME } from '../constants'; import { NativeNpmCommands, PackageJson } from '../npm'; import { getPath } from '../utilities'; +import { isCommand } from './is-command.function'; -const allKnownCommands: Command[] = Object.values(Command); +const TOO_MANY_ARGUMENTS_ERROR_MESSAGE: string = 'Error parsing the command: Too many arguments.'; /** * Validates the user input. @@ -20,40 +21,57 @@ export async function validateInput(args: string[]): Promise { return; } - const command: Command = args[0] as Command; - if (!allKnownCommands.includes(command) && args.length > 1) { - await validateRunInput(...args); - return; - } + const command: string = args[0]; - if (!allKnownCommands.includes(command)) { - exitWithError(`Error: Unknown command ${command}.`); - return; - } - - if (args.length > 1 && !args[1].startsWith('-')) { - exitWithError('Error parsing the command: Too many arguments.'); + if (!isCommand(command)) { + if (args.length === 1) { + exitWithError(`Error: Unknown command ${command}.`); + } + await validateRunInput(...args); return; } - if ([ - Command.ADD, - Command.A, - Command.PREPARE, - Command.P, - Command.D, - Command.DOWN, - Command.DD, - Command.DOWN_DEV, - Command.U, - Command.UP, - Command.UD, - Command.UP_DEV, - Command.GENERATE_PAGE, - Command.UP_LOCAL, - Command.UL - ].includes(command)) { - await validateInsideWorkspace(); + switch (command) { + case Command.HELP: + case Command.H: + case Command.VERSION: + case Command.V: + case Command.INIT: + case Command.I: { + validateMaxLength(args.length, 1); + return; + } + case Command.A: + case Command.ADD: + case Command.PREPARE: + case Command.P: + case Command.UP: + case Command.U: + case Command.DOWN: + case Command.D: + case Command.UP_DEV: + case Command.UD: + case Command.DOWN_DEV: + case Command.DD: + case Command.UP_LOCAL: + case Command.UL: + case Command.DOWN_LOCAL: + case Command.DL: + case Command.GENERATE_PAGE: + case Command.GP: { + await validateInsideWorkspace(); + validateMaxLength(args.length, 1); + return; + } + case Command.RUN_MANY: + case Command.RUN_ALL: + case Command.RA: { + if (args.length === 1) { + exitWithError('Error: No npm script specified to run in all projects.'); + } + await validateInsideWorkspace(); + return; + } } } @@ -74,10 +92,6 @@ async function validateRunInput(...args: string[]): Promise { return; } - if (args.length > 2) { - exitWithError('Error parsing the command: Too many arguments.'); - return; - } const npmScript: string = args[1]; const file: PackageJson = await FsUtilities.parseFileAs(getPath(packageJson.parentPath, packageJson.name)); @@ -93,4 +107,11 @@ async function validateInsideWorkspace(): Promise { if (!config?.isWorkspace) { exitWithError('This command can only be run inside a workspace'); } +} + +// eslint-disable-next-line jsdoc/require-jsdoc +function validateMaxLength(value: number, maxLength: number): void { + if (value > maxLength) { + exitWithError(TOO_MANY_ARGUMENTS_ERROR_MESSAGE); + } } \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index bf52409..82b7252 100644 --- a/src/index.ts +++ b/src/index.ts @@ -16,7 +16,7 @@ async function main(): Promise { const command: string = args[0]; if (!isCommand(command)) { - await runRun(...args); + runRun(...args); return; } diff --git a/src/npm/npm.utilities.ts b/src/npm/npm.utilities.ts index 95f14e1..c7f6ac6 100644 --- a/src/npm/npm.utilities.ts +++ b/src/npm/npm.utilities.ts @@ -61,12 +61,10 @@ export abstract class NpmUtilities { /** * Runs the given npm script in the project with the provided name. * @param projectName - The project to run the npm script in. - * @param npmScript - The npm script to run. + * @param commands - The npm script to run. */ - static async run(projectName: string, npmScript: NpmScript): Promise { - const project: Dirent = await WorkspaceUtilities.findProjectOrFail(projectName); - const projectPath: string = getPath(project.parentPath, project.name); - CPUtilities.execSync(`cd ${projectPath} && npm run ${npmScript}`); + static run(projectName: string, commands: string): void { + CPUtilities.execSync(`npm run ${commands} --workspace=${projectName}`); } /**