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

fix: display custom schematics in help #1911

Merged
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
6 changes: 5 additions & 1 deletion actions/generate.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ const generateFiles = async (inputs: Input[]) => {
specValue,
specOptions.passedAsInput,
);
generateFlat = shouldGenerateFlat(configuration, answers.appNames, flatValue);
generateFlat = shouldGenerateFlat(
configuration,
answers.appNames,
flatValue,
);
}
}

Expand Down
8 changes: 4 additions & 4 deletions bin/nest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
localBinExists,
} from '../lib/utils/local-binaries';

const bootstrap = () => {
const bootstrap = async () => {
const program: CommanderStatic = commander;
program
.version(
Expand All @@ -20,11 +20,11 @@ const bootstrap = () => {

if (localBinExists()) {
const localCommandLoader = loadLocalBinCommandLoader();
localCommandLoader.load(program);
await localCommandLoader.load(program);
} else {
CommandLoader.load(program);
await CommandLoader.load(program);
}
commander.parse(process.argv);
commander.parseAsync(process.argv);

if (!process.argv.slice(2).length) {
program.outputHelp();
Expand Down
4 changes: 2 additions & 2 deletions commands/command.loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ import { InfoCommand } from './info.command';
import { NewCommand } from './new.command';
import { StartCommand } from './start.command';
export class CommandLoader {
public static load(program: CommanderStatic): void {
public static async load(program: CommanderStatic): Promise<void> {
new NewCommand(new NewAction()).load(program);
new BuildCommand(new BuildAction()).load(program);
new StartCommand(new StartAction()).load(program);
new InfoCommand(new InfoAction()).load(program);
new AddCommand(new AddAction()).load(program);
new GenerateCommand(new GenerateAction()).load(program);
await new GenerateCommand(new GenerateAction()).load(program);

this.handleInvalidCommand(program);
}
Expand Down
31 changes: 23 additions & 8 deletions commands/generate.command.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import * as chalk from 'chalk';
import * as Table from 'cli-table3';
import { Command, CommanderStatic } from 'commander';
import { NestCollection } from '../lib/schematics/nest.collection';
import { AbstractCollection, CollectionFactory } from '../lib/schematics';
import { Schematic } from '../lib/schematics/nest.collection';
import { loadConfiguration } from '../lib/utils/load-configuration';
import { AbstractCommand } from './abstract.command';
import { Input } from './command.input';

export class GenerateCommand extends AbstractCommand {
public load(program: CommanderStatic) {
public async load(program: CommanderStatic): Promise<void> {
program
.command('generate <schematic> [name] [path]')
.alias('g')
.description(this.buildDescription())
.description(await this.buildDescription())
.option(
'-d, --dry-run',
'Report actions that would be taken without writing out results.',
Expand Down Expand Up @@ -93,17 +95,18 @@ export class GenerateCommand extends AbstractCommand {
);
}

private buildDescription(): string {
private async buildDescription(): Promise<string> {
const collection = await this.getCollection();
return (
'Generate a Nest element.\n' +
` Schematics available on ${chalk.bold(
'@nestjs/schematics',
collection,
)} collection:\n` +
this.buildSchematicsListAsTable()
this.buildSchematicsListAsTable(await this.getSchematics(collection))
);
}

private buildSchematicsListAsTable(): string {
private buildSchematicsListAsTable(schematics: Schematic[]): Promise<string> {
const leftMargin = ' ';
const tableConfig = {
head: ['name', 'alias', 'description'],
Expand All @@ -118,7 +121,7 @@ export class GenerateCommand extends AbstractCommand {
},
};
const table: any = new Table(tableConfig);
for (const schematic of NestCollection.getSchematics()) {
for (const schematic of schematics) {
table.push([
chalk.green(schematic.name),
chalk.cyan(schematic.alias),
Expand All @@ -127,4 +130,16 @@ export class GenerateCommand extends AbstractCommand {
}
return table.toString();
}

private async getCollection(): Promise<string> {
const configuration = await loadConfiguration();
return configuration.collection;
}

private async getSchematics(collection: string): Promise<Schematic[]> {
const abstractCollection: AbstractCollection = CollectionFactory.create(
collection,
);
return abstractCollection.getSchematics();
}
}
5 changes: 4 additions & 1 deletion lib/schematics/abstract.collection.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { AbstractRunner } from '../runners';
import { Schematic } from './nest.collection';
import { SchematicOption } from './schematic.option';

export class AbstractCollection {
export abstract class AbstractCollection {
constructor(protected collection: string, protected runner: AbstractRunner) {}

public async execute(
Expand All @@ -14,6 +15,8 @@ export class AbstractCollection {
await this.runner.run(command);
}

public abstract getSchematics(): Schematic[];

private buildCommandLine(name: string, options: SchematicOption[]): string {
return `${this.collection}:${name}${this.buildOptions(options)}`;
}
Expand Down
28 changes: 27 additions & 1 deletion lib/schematics/custom.collection.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
import { description } from 'commander';
import { readFileSync } from 'fs';
import { dirname, join } from 'path';
import { AbstractCollection } from './abstract.collection';
import { Schematic } from './nest.collection';

export class CustomCollection extends AbstractCollection {}
export interface CollectionSchematic {
schema: string;
description: string;
aliases: string[];
}

export class CustomCollection extends AbstractCollection {
public getSchematics(): Schematic[] {
const collectionPackagePath = dirname(require.resolve(this.collection));
const collectionPath = join(collectionPackagePath, 'collection.json');
const collection = JSON.parse(readFileSync(collectionPath, 'utf8'));
const schematics = Object.entries(collection.schematics).map(
([name, value]) => {
const schematic = value as CollectionSchematic;
const description = schematic.description;
const alias = schematic?.aliases?.length ? schematic.aliases[0] : '';
return { name, description, alias };
},
);

return schematics;
}
}
2 changes: 1 addition & 1 deletion lib/schematics/nest.collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export class NestCollection extends AbstractCollection {
await super.execute(schematic, options);
}

public static getSchematics(): Schematic[] {
public getSchematics(): Schematic[] {
return NestCollection.schematics.filter(
(item) => item.name !== 'angular-app',
);
Expand Down