Skip to content

Commit

Permalink
feature: support refactored schematics package
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Nov 5, 2018
1 parent 5a8920a commit 1085850
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 29 deletions.
10 changes: 9 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
.idea/
.gitignore
.travis.yml
Makefile
Makefile

# source
**/*.ts
*.ts

# definitions
!**/*.d.ts
!*.d.ts
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ publish-npm-package: prepublish
@docker run -w /nestjs/cli nestjs/cli:$$ARTEFACT_ID \
/bin/sh -c "\
echo //registry.npmjs.org/:_authToken=$$NPM_TOKEN >> .npmrc && \
npm publish \
npm publish --tag next \
"

prepublish:
Expand Down
5 changes: 4 additions & 1 deletion actions/abstract.action.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Input } from '../commands';

export abstract class AbstractAction {
public abstract async handle(inputs?: Input[], options?: Input[]): Promise<void>;
public abstract async handle(
inputs?: Input[],
options?: Input[],
): Promise<void>;
}
13 changes: 9 additions & 4 deletions actions/add.action.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { Input } from '../commands';
import { AbstractPackageManager, PackageManagerFactory } from '../lib/package-managers';
import {
AbstractPackageManager,
PackageManagerFactory,
} from '../lib/package-managers';
import { AbstractAction } from './abstract.action';

export class AddAction extends AbstractAction {
public async handle(inputs: Input[]) {
const manager: AbstractPackageManager = await PackageManagerFactory.find();
const libraryInput: Input = inputs.find((input) => input.name === 'library') as Input;
if (!!libraryInput) {
const libraryInput: Input = inputs.find(
input => input.name === 'library',
) as Input;
if (libraryInput) {
const library: string = libraryInput.value as string;
await manager.addProduction([ library ], 'latest');
await manager.addProduction([library], 'latest');
}
}
}
36 changes: 26 additions & 10 deletions commands/generate.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,31 @@ export class GenerateCommand extends AbstractCommand {
.command('generate <schematic> <name> [path]')
.alias('g')
.description('Generate a Nest element.')
.option('--dry-run', 'Allow to test changes before execute command')
.action(async (schematic: string, name: string, path: string, command: Command) => {
const options: Input[] = [];
options.push({ name: 'dry-run', value: !!command.dryRun });
const inputs: Input[] = [];
inputs.push({ name: 'schematic', value: schematic });
inputs.push({ name: 'name', value: name });
inputs.push({ name: 'path', value: path });
await this.action.handle(inputs, options);
});
.option('--dry-run', 'Allow to test changes before command execution')
.option('--flat', 'Enforce flat structure of generated element')
.option('--no-spec', 'Disable spec files generation')
.action(
async (
schematic: string,
name: string,
path: string,
command: Command,
) => {
const options: Input[] = [];
options.push({ name: 'dry-run', value: !!command.dryRun });
options.push({ name: 'flat', value: command.flat });
options.push({
name: 'spec',
value: command.spec,
});

const inputs: Input[] = [];
inputs.push({ name: 'schematic', value: schematic });
inputs.push({ name: 'name', value: name });
inputs.push({ name: 'path', value: path });

await this.action.handle(inputs, options);
},
);
}
}
9 changes: 5 additions & 4 deletions lib/schematics/nest.collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export class NestCollection extends AbstractCollection {
{ name: 'configuration', alias: 'config' },
{ name: 'controller', alias: 'co' },
{ name: 'decorator', alias: 'd' },
{ name: 'exception', alias: 'e' },
{ name: 'filter', alias: 'f' },
{ name: 'gateway', alias: 'ga' },
{ name: 'guard', alias: 'gu' },
Expand All @@ -24,6 +23,7 @@ export class NestCollection extends AbstractCollection {
{ name: 'pipe', alias: 'pi' },
{ name: 'provider', alias: 'pr' },
{ name: 'service', alias: 's' },
{ name: 'library', alias: 'lib' },
];

constructor(runner: AbstractRunner) {
Expand All @@ -36,12 +36,13 @@ export class NestCollection extends AbstractCollection {
}

private validate(name: string) {
const schematic = this.schematics.find((s) => s.name === name || s.alias === name);
const schematic = this.schematics.find(
s => s.name === name || s.alias === name,
);

if (schematic === undefined || schematic === null) {
throw new Error(`Invalid schematic ${ name }`);
throw new Error(`Invalid schematic ${name}`);
}

return schematic.name;
}
}
15 changes: 9 additions & 6 deletions lib/schematics/schematic.option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ export class SchematicOption {
public toCommandString(): string {
if (typeof this.value === 'string') {
if (this.name === 'name') {
return `--${ this.name }=${ this.format() }`;
return `--${this.name}=${this.format()}`;
} else if (this.name === 'version' || this.name === 'path') {
return `--${ this.name }=${ this.value }`;
return `--${this.name}=${this.value}`;
} else {
return `--${ this.name }="${ this.value }"`;
return `--${this.name}="${this.value}"`;
}
} else if (typeof this.value === 'boolean') {
const str = strings.dasherize(this.name);
return this.value ? `--${str}` : `--no-${str}`;
} else {
return `--${ strings.dasherize(this.name) }=${ this.value }`;
return `--${strings.dasherize(this.name)}=${this.value}`;
}
}

Expand All @@ -23,9 +26,9 @@ export class SchematicOption {
.split('')
.reduce((content, char) => {
if (char === '(' || char === ')' || char === '[' || char === ']') {
return `${ content }\\${ char }`;
return `${content}\\${char}`;
}
return `${ content }${ char }`;
return `${content}${char}`;
}, '');
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nestjs/cli",
"version": "5.5.0",
"version": "5.5.0-next",
"description": "Nest CLI",
"publishConfig": {
"access": "public"
Expand Down Expand Up @@ -33,7 +33,7 @@
"dependencies": {
"@angular-devkit/core": "^0.8.1",
"@angular-devkit/schematics-cli": "^0.8.1",
"@nestjs/schematics": "^5.8.0",
"@nestjs/schematics": "^5.8.0-next",
"@nuxtjs/opencollective": "^0.1.0",
"@types/jest": "^22.2.3",
"chalk": "^2.4.1",
Expand Down

0 comments on commit 1085850

Please sign in to comment.