Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "monux-cli",
"version": "2.0.5",
"version": "2.0.6",
"license": "MIT",
"main": "index.js",
"engines": {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/add/add-ts-library/add-ts-library.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class AddTsLibraryCommand extends AddCommand<TsLibraryConfiguration> {

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);
}

Expand Down
13 changes: 4 additions & 9 deletions src/commands/run/run.command.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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}`);
}
91 changes: 56 additions & 35 deletions src/commands/validate-input.function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -20,40 +21,57 @@ export async function validateInput(args: string[]): Promise<void> {
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;
}
}
}

Expand All @@ -74,10 +92,6 @@ async function validateRunInput(...args: string[]): Promise<void> {
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<PackageJson>(getPath(packageJson.parentPath, packageJson.name));

Expand All @@ -93,4 +107,11 @@ async function validateInsideWorkspace(): Promise<void> {
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);
}
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async function main(): Promise<void> {
const command: string = args[0];

if (!isCommand(command)) {
await runRun(...args);
runRun(...args);
return;
}

Expand Down
8 changes: 3 additions & 5 deletions src/npm/npm.utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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}`);
}

/**
Expand Down