Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(wip): support new options #37

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
12,457 changes: 12,456 additions & 1 deletion package-lock.json

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion packages/@guidesmiths/cuckoojs-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,18 @@
"dependencies": {
"@angular-devkit/schematics-cli": "^14.2.3",
"@guidesmiths/cuckoojs-schematics": "^0.0.1",
"@nestjs/cli": "^9.1.5",
"@nestjs/schematics": "^9.0.3",
"@types/jasmine": "~4.3.0",
"@types/node": "^18.7.18",
"commander": "^9.4.0",
"loading-cli": "^1.1.0"
"loading-cli": "^1.1.0",
"inquirer": "^8.2.5"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To remove

},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.38.0",
"@typescript-eslint/parser": "^5.38.0",
"@types/inquirer": "^9.0.3",
"eslint": "^8.23.1",
"eslint-config-xo": "^0.42.0",
"eslint-config-xo-typescript": "^0.53.0",
Expand Down
40 changes: 25 additions & 15 deletions packages/@guidesmiths/cuckoojs-cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,27 @@
import {Command} from 'commander';
import {version} from '../package.json';
import {NewCommand, GenerateCommand} from './commands';
import {removeDashes} from './lib/utils/formatArgs';

const init = (): void => {
const cuckoo = new Command('cuckoo');

cuckoo
.version(
version,
'-v, --version',
'Output the current version.',
);
const originalArgs = process.argv;

cuckoo
.helpOption('-h, --help', 'Output usage information.')
.showHelpAfterError()
.usage('<command> [options]');

cuckoo
.command('new <name>')
.command('new')
.argument('[name]', 'Name of your Nest App')
.argument('[options...]', 'List of options. Please, refer to Nest documentation for the available options: https://docs.nestjs.com/cli/usages#nest-new')
.alias('n')
.description('Generate Nest application with basic tooling.')
.action(async (name: string, _options: any) => {
await new NewCommand(name).execute();
.hook('preAction', (_, actionCommand) => {
const commandArgs = originalArgs.slice(3);
const isOption = (a: string) => /^--|^-/.test(a);
actionCommand.processedArgs[0] = commandArgs.find(a => !isOption(a));
actionCommand.processedArgs[1] = commandArgs.filter(a => isOption(a));
})
.action(async (name: string, options: string[]) => {
await new NewCommand(name, options).execute();
});

cuckoo
Expand All @@ -35,7 +34,18 @@ const init = (): void => {
});

cuckoo
.parse(process.argv);
.version(
version,
'-v, --version',
'Output the current version.',
);

cuckoo
.helpOption('-h, --help', 'Output usage information.')
.showHelpAfterError();

cuckoo
.parse(removeDashes(process.argv));
};

init();
79 changes: 40 additions & 39 deletions packages/@guidesmiths/cuckoojs-cli/src/commands/new.command.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import * as fs from 'fs';
import {join} from 'path';

import {AbstractCommand} from './abstract.command';
import {SchematicRunner} from '../lib/runners/schematic.runner';
import {GitRunner} from '../lib/runners/git.runner';
// import {GitRunner} from '../lib/runners/git.runner';
import type {PackageEntry, ScriptEntry} from '../lib/runners/npm.runner';
import {NpmRunner} from '../lib/runners/npm.runner';
import {messages} from '../lib/ui/ui';
import * as fs from 'fs';
import {join} from 'path';
import {BashRunnerHusky} from '../lib/runners/bash.runner.husky';
import {AbstractCommand} from './abstract.command';
import {NestJsRunner} from '../lib/runners/nestjs.runner';

import Printer from '../lib/printer/printer';
import {messages} from '../lib/ui/ui';

export class NewCommand extends AbstractCommand {
private readonly schematicRunner: SchematicRunner = new SchematicRunner();
private readonly gitRunner: GitRunner = new GitRunner();
// private readonly gitRunner: GitRunner = new GitRunner();
private readonly npmRunner: NpmRunner = new NpmRunner();
private readonly bashRunnerHusky: BashRunnerHusky = new BashRunnerHusky();
private readonly nestJsRunner: NestJsRunner = new NestJsRunner();

private readonly initialPackages: PackageEntry[] = [
{name: 'husky', version: '^8.0.1', section: 'devDependencies'},
Expand All @@ -26,6 +30,7 @@ export class NewCommand extends AbstractCommand {

constructor(
private readonly name: string,
private readonly options: string[],
) {
super();
}
Expand All @@ -34,48 +39,44 @@ export class NewCommand extends AbstractCommand {
const printer = new Printer({total: 8, step: 1});
this.printSuccess(messages.banner);

if (this.checkFileExists()) {
this.printError(`Error generating new project: Folder ${this.name} already exists`);
NewCommand.endProcess(1);
}

try {
this.checkFileExists();

printer.startStep('Generating NestJS application scaffolding');
await this.schematicRunner.generateNestApplication(this.name);
printer.endStep();

printer.startStep('Initializing Git repository');
await this.gitRunner.init(this.name);
await this.gitRunner.createBranch({folderName: this.name});
printer.endStep();

printer.startStep('Adding additional packages');
// await this.retrieveMissingArguments();

// printer.startStep('Generating NestJS application scaffolding');
await this.nestJsRunner.generateNestApplication(this.name, this.options);
// printer.endStep();

// // printer.startStep('Initializing Git repository');
// // await this.gitRunner.init(this.name);
// // await this.gitRunner.createBranch({folderName: this.name});
// // printer.endStep();
// //
// printer.startStep('Adding additional packages');
await this.npmRunner.addPackages(this.name, this.initialPackages);
printer.endStep();
// printer.endStep();

printer.startStep('Adding additional npm scripts');
// // printer.startStep('Adding additional npm scripts');
await this.npmRunner.addScripts(this.name, this.initialScripts);
printer.endStep();
// // printer.endStep();

printer.startStep('Adding commitlint config file');
// // printer.startStep('Adding commitlint config file');
await this.schematicRunner.addCommitlint(this.name);
printer.endStep();
// // printer.endStep();

printer.startStep('Adding .gitignore file');
// // printer.startStep('Adding .gitignore file');
await this.schematicRunner.addGitignoreFile(this.name);
printer.endStep();
// // printer.endStep();

printer.startStep('Installing dependencies');
// // printer.startStep('Installing dependencies');
await this.npmRunner.install(this.name);
printer.endStep();
// // printer.endStep();

printer.startStep('Creating husky files');
// // printer.startStep('Creating husky files');
// Only possible ig --skip-git not present
await this.bashRunnerHusky.runHuskyCommit(this.name);
printer.endStep();
// // printer.endStep();

this.printSuccess(`\n 🐦 Your CuckooJS nest "${this.name}" is generated and ready to use 🐦`);
this.printSuccess('\n 🐦 Your CuckooJS NestJS app is generated and ready to use 🐦');
} catch (error: unknown) {
printer.load.fail(`Error generating new project: ${(error as Error).message}`);
this.removeFolder();
Expand All @@ -91,8 +92,8 @@ export class NewCommand extends AbstractCommand {
}
}

private checkFileExists() {
const path = join(process.cwd(), this.name);
return fs.existsSync(path);
}
// private checkFileExists() {
// const path = join(process.cwd(), this.name);
// return fs.existsSync(path);
// }
}
13 changes: 13 additions & 0 deletions packages/@guidesmiths/cuckoojs-cli/src/lib/inquirer/inquirer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as inquirer from 'inquirer';

export async function promptInput({
name, message, defaultAnswer,
}: {name: string; message: string; defaultAnswer: string}): Promise<inquirer.Answers> {
return inquirer.createPromptModule()([{type: 'input', name, message, default: defaultAnswer}]);
}

export async function promptSelect({
name, message, choices, defaultAnswer,
}: {name: string; message: string; choices: string[]; defaultAnswer: string}): Promise<inquirer.Answers> {
return inquirer.createPromptModule({})([{type: 'select', name, message, choices, default: defaultAnswer}]);
}
Comment on lines +1 to +13
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To remove

Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ export class GenericRunner {
);

child.on('error', error => {
console.log('eeeee', error);
reject(new Error(`Child process filed with error: ${error.message}`));
reject(new Error(`Child process failed with error: ${error.message}`));
});

child.on('close', code => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {GenericRunner} from './generic.runner';

export class NestJsRunner extends GenericRunner {
private static getNestJsCliPath(): string {
return require.resolve(
'@nestjs/cli/bin/nest.js',
{paths: module.paths},
);
}

// private static formatNewOptions(options: string[]): string {
// const formattedOptions = options.filter(Boolean).join(' ');
// if (options.includes('--skip-install')) {
// return formattedOptions;
// }
//
// return `${formattedOptions} --skip-install`;
// }
//
Comment on lines +11 to +19
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To remove

constructor() {
super('node');
}

public async generateNestApplication(name: string, options: string[]) {
if (!options.includes('--skip-install')) {
options.push('--skip-install');
}

const args = ['new', name, ...options].filter(Boolean);
console.log(args);
await super.run({command: NestJsRunner.getNestJsCliPath(), args, stdio: 'inherit'});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ export class NpmRunner extends GenericRunner {

public async install(name: string) {
const args = [`--prefix ${name}`];
await super.run({command: 'install', args, stdio: 'pipe'});
console.log('args :>> ', args);
await super.run({command: 'install', args, stdio: 'inherit'});
}

public async addPackages(name: string, packageEntries: PackageEntry[]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ export class SchematicRunner extends GenericRunner {
}

public async addGitignoreFile(name: string) {
const args = [`@guidesmiths/cuckoojs-schematics:gitignore --directory=${name}`];
const args = ['@guidesmiths/cuckoojs-schematics:gitignore', `--directory=${name}`];
await super.run({command: SchematicRunner.getSchematicsCliPath(), args, stdio: ['pipe', 'pipe', 'inherit']});
}

public async addCommitlint(name: string) {
const args = [`@guidesmiths/cuckoojs-schematics:commitlint --directory=${name}`];
await super.run({command: SchematicRunner.getSchematicsCliPath(), args, stdio: ['pipe', 'pipe', 'inherit']});
const args = ['@guidesmiths/cuckoojs-schematics:commitlint', `--directory=${name}`];
await super.run({command: SchematicRunner.getSchematicsCliPath(), args, stdio: ['inherit', 'inherit', 'inherit']});
}
}
30 changes: 30 additions & 0 deletions packages/@guidesmiths/cuckoojs-cli/src/lib/utils/formatArgs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
function removeDashesFrom(args: string[], initialIndex: number): string[] {
return args.map((arg, index) => {
if (index < initialIndex) {
return arg;
}

return arg.replace(/^--|^-/, '');
});
}

export function removeDashes(args: string[]): string[] {
const command = args[2];
const parsingStrategies: Record<string, () => string[]> = {
new() {
return removeDashesFrom(args, 2);
},
generate() {
return removeDashesFrom(args, 5);
},
default() {
return args;
},
};

return (parsingStrategies[command] || parsingStrategies.default)();
}

export function addDashes(args: string[]): string[] {
return args.map(arg => `--${arg}`);
}
Comment on lines +28 to +30
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To remove