Skip to content
This repository was archived by the owner on Nov 21, 2024. It is now read-only.

Commit ef1c21d

Browse files
committed
feat(schematics): Add support use external and project schematics collections
1 parent 41f6a7b commit ef1c21d

10 files changed

Lines changed: 199 additions & 58 deletions

File tree

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,7 @@ node_modules
2424
/.travis.yml
2525

2626
/test/fixtures/new
27-
/test/fixtures/entity
27+
/test/fixtures/entity
28+
/test/fixtures/new-with-entity
29+
/test/fixtures/angular-new
30+
/test/fixtures/nestjs-entity

src/commands/angular-entity.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
import { flags, Command } from '@oclif/command';
2-
import { resolve as resolvePath, sep } from 'path';
1+
import { Command, flags } from '@oclif/command';
2+
import { sep } from 'path';
3+
import { schematicsCommandBuilder } from '../utils/schematics-command-builder';
34
const npmRun = require('npm-run');
45

56
export class AngularEntity extends Command {
67
static aliases = ['entity:angular', 'angular-entity'];
78
static description = 'generate frontend model, service, grid, lookup input, modal for edit row in grid, modal for select items from grid with items for Angular 6+ application based on Rucken template';
89
static flags = {
910
help: flags.help({ char: 'h' }),
11+
template: flags.string({ char: 't', description: 'template', default: '@rucken/schematics:angular-entity' }),
1012
name: flags.string({ char: 'n', description: 'entity singular name on ke-bab case', required: true }),
1113
fields: flags.string({ char: 'f', description: 'entity fields', default: '[name]' }),
1214
username: flags.string({ char: 'u', description: 'username' }),
@@ -19,6 +21,7 @@ export class AngularEntity extends Command {
1921
async run() {
2022
const { args, flags } = this.parse(AngularEntity);
2123
const folder = args.folder;
24+
const template = flags.template || '';
2225
const name = flags.name;
2326
const fields = flags.fields;
2427
const username = flags.username;
@@ -29,6 +32,7 @@ export class AngularEntity extends Command {
2932

3033
try {
3134
await this.runEntity(
35+
template,
3236
folder,
3337
name,
3438
fields,
@@ -43,6 +47,7 @@ export class AngularEntity extends Command {
4347
}
4448
}
4549
private runEntity(
50+
template: string,
4651
folder: string,
4752
name: string,
4853
fields?: string,
@@ -53,6 +58,7 @@ export class AngularEntity extends Command {
5358
web?: string
5459
) {
5560
this.debug('Start', {
61+
template: template,
5662
folder: folder,
5763
name: name,
5864
fields: fields,
@@ -64,18 +70,20 @@ export class AngularEntity extends Command {
6470
});
6571
return new Promise((resolve, reject) => {
6672
const inputFolder = folder ? folder.replace(new RegExp('\\' + sep, 'g'), '/').split(sep).join('/') : undefined;
67-
const command = 'node ' +
68-
resolvePath(__dirname, '../../node_modules/@angular-devkit/schematics-cli/bin/schematics.js') +
69-
' @rucken/schematics:angular-entity ' +
70-
(inputFolder ? ('--root=' + inputFolder + ' ') : ' ') +
71-
(name ? ('--name=' + name + ' ') : ' ') +
72-
(fields ? ('--fields=' + fields + ' ') : ' ') +
73-
(username ? ('--username=' + username + ' ') : ' ') +
74-
(email ? ('--email=' + email + ' ') : ' ') +
75-
(app ? ('--app=' + app + ' ') : ' ') +
76-
(core ? ('--core=' + core + ' ') : ' ') +
77-
(web ? ('--web=' + web + ' ') : ' ') +
78-
'--dry-run=false --force';
73+
const command = schematicsCommandBuilder(
74+
inputFolder as string,
75+
template,
76+
{
77+
root: inputFolder,
78+
name: name,
79+
fields: fields,
80+
username: username,
81+
email: email,
82+
app: app,
83+
core: core,
84+
web: web
85+
}
86+
);
7987
this.debug('command', command);
8088
npmRun.exec(
8189
command,

src/commands/angular-new.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { flags, Command } from '@oclif/command';
2-
import { resolve as resolvePath, sep } from 'path';
2+
import { sep } from 'path';
3+
import { schematicsCommandBuilder } from '../utils/schematics-command-builder';
34
const npmRun = require('npm-run');
45

56
export class AngularNew extends Command {
67
static aliases = ['new:angular', 'angular-new'];
78
static description = 'generate frontend web empty Angular 6+ application based on Rucken template';
89
static flags = {
910
help: flags.help({ char: 'h' }),
11+
template: flags.string({ char: 't', description: 'template', default: '@rucken/schematics:angular-new' }),
1012
name: flags.string({ char: 'n', description: 'application name on ke-bab case' }),
1113
username: flags.string({ char: 'u', description: 'username' }),
1214
email: flags.string({ char: 'e', description: 'email' })
@@ -15,12 +17,14 @@ export class AngularNew extends Command {
1517
async run() {
1618
const { args, flags } = this.parse(AngularNew);
1719
const folder = args.folder;
20+
const template = flags.template || '';
1821
const name = flags.name;
1922
const username = flags.username;
2023
const email = flags.email;
2124

2225
try {
2326
await this.runNew(
27+
template,
2428
folder,
2529
name,
2630
username,
@@ -31,12 +35,14 @@ export class AngularNew extends Command {
3135
}
3236
}
3337
private runNew(
38+
template: string,
3439
folder: string,
3540
name?: string,
3641
username?: string,
3742
email?: string
3843
) {
3944
this.debug('Start', {
45+
template: template,
4046
folder: folder,
4147
name: name,
4248
username: username,
@@ -47,14 +53,16 @@ export class AngularNew extends Command {
4753
if (inputFolder && inputFolder.indexOf('/') === -1 && !name) {
4854
name = inputFolder;
4955
}
50-
const command = 'node ' +
51-
resolvePath(__dirname, '../../node_modules/@angular-devkit/schematics-cli/bin/schematics.js') +
52-
' @rucken/schematics:angular-new ' +
53-
(inputFolder ? ('--root=' + inputFolder + ' ') : ' ') +
54-
(name ? ('--name=' + name + ' ') : ' ') +
55-
(username ? ('--username=' + username + ' ') : ' ') +
56-
(email ? ('--email=' + email + ' ') : ' ') +
57-
'--dry-run=false --force';
56+
const command = schematicsCommandBuilder(
57+
inputFolder as string,
58+
template,
59+
{
60+
root: inputFolder,
61+
name: name,
62+
username: username,
63+
email: email
64+
}
65+
);
5866
this.debug('command', command);
5967
npmRun.exec(
6068
command,

src/commands/entity.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { flags, Command } from '@oclif/command';
1+
import { Command, flags } from '@oclif/command';
22
import { readFileSync } from 'fs';
33
import { resolve as resolvePath } from 'path';
44
import { AngularEntity } from './angular-entity';
@@ -29,14 +29,27 @@ export class Entity extends Command {
2929
const core = flags.core;
3030
const web = flags.web;
3131
const errorMessage = 'Before create entity, you must create project: rucken new -n demo -u demo -e demo@demo.demo';
32+
let templateBackend = '';
33+
let templateFrontend = '';
3234

3335
try {
3436
const ruckenJsonFile = resolvePath(folder, 'rucken.json');
3537
const ruckenJson: any = JSON.parse(readFileSync(ruckenJsonFile, 'utf8').toString());
36-
if (ruckenJson.backend !== 'nestjs' && ruckenJson.frontend !== 'angular') {
38+
if (
39+
!ruckenJson.backend ||
40+
!ruckenJson.frontend
41+
) {
3742
console.error(errorMessage);
3843
return Promise.resolve();
3944
}
45+
if (ruckenJson.backend === 'nestjs') {
46+
ruckenJson.backend = '@rucken/schematics:nestjs';
47+
}
48+
if (ruckenJson.frontend === 'angular') {
49+
ruckenJson.frontend = '@rucken/schematics:angular';
50+
}
51+
templateBackend = ruckenJson.backend + '-entity';
52+
templateFrontend = ruckenJson.frontend + '-entity';
4053
// tslint:disable-next-line:no-unused
4154
} catch (error) {
4255
console.error(errorMessage);
@@ -45,6 +58,7 @@ export class Entity extends Command {
4558

4659
try {
4760
await this.runNestJSEntity(
61+
templateBackend,
4862
folder,
4963
name,
5064
fields,
@@ -58,6 +72,7 @@ export class Entity extends Command {
5872
}
5973
try {
6074
await this.runAngularEntity(
75+
templateFrontend,
6176
frontendFolder,
6277
name,
6378
fields,
@@ -72,6 +87,7 @@ export class Entity extends Command {
7287
}
7388
}
7489
private runNestJSEntity(
90+
templateBackend: string,
7591
folder: string,
7692
name: string,
7793
fields?: string,
@@ -82,7 +98,7 @@ export class Entity extends Command {
8298
) {
8399
return new Promise(async (resolve, reject) => {
84100
try {
85-
const args = [folder, '--name', name];
101+
const args = [folder, '--name', name, '--template', templateBackend];
86102
if (fields) {
87103
args.push('--fields');
88104
args.push(fields);
@@ -111,6 +127,7 @@ export class Entity extends Command {
111127
});
112128
}
113129
private runAngularEntity(
130+
templateFrontend: string,
114131
folder: string,
115132
name: string,
116133
fields?: string,
@@ -122,7 +139,7 @@ export class Entity extends Command {
122139
) {
123140
return new Promise(async (resolve, reject) => {
124141
try {
125-
const args = [folder, '--name', name];
142+
const args = [folder, '--name', name, '--template', templateFrontend];
126143
if (fields) {
127144
args.push('--fields');
128145
args.push(fields);

src/commands/nestjs-entity.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { flags, Command } from '@oclif/command';
2-
import { resolve as resolvePath, sep } from 'path';
2+
import { sep } from 'path';
3+
import { schematicsCommandBuilder } from '../utils/schematics-command-builder';
34
const npmRun = require('npm-run');
45

56
export class NestJSEntity extends Command {
67
static aliases = ['entity:nestjs', 'nestjs-entity'];
78
static description = 'generate dto, entity, service and controller for NestJS backend';
89
static flags = {
910
help: flags.help({ char: 'h' }),
11+
template: flags.string({ char: 't', description: 'template', default: '@rucken/schematics:nestjs-entity' }),
1012
name: flags.string({ char: 'n', description: 'entity singular name on ke-bab case', required: true }),
1113
fields: flags.string({ char: 'f', description: 'entity fields', default: '[name]' }),
1214
username: flags.string({ char: 'u', description: 'username' }),
@@ -18,6 +20,7 @@ export class NestJSEntity extends Command {
1820
async run() {
1921
const { args, flags } = this.parse(NestJSEntity);
2022
const folder = args.folder;
23+
const template = flags.template || '';
2124
const name = flags.name;
2225
const fields = flags.fields;
2326
const username = flags.username;
@@ -27,6 +30,7 @@ export class NestJSEntity extends Command {
2730

2831
try {
2932
await this.runEntity(
33+
template,
3034
folder,
3135
name,
3236
fields,
@@ -40,6 +44,7 @@ export class NestJSEntity extends Command {
4044
}
4145
}
4246
private runEntity(
47+
template: string,
4348
folder: string,
4449
name: string,
4550
fields?: string,
@@ -49,6 +54,7 @@ export class NestJSEntity extends Command {
4954
core?: string
5055
) {
5156
this.debug('Start', {
57+
template: template,
5258
folder: folder,
5359
name: name,
5460
fields: fields,
@@ -59,17 +65,19 @@ export class NestJSEntity extends Command {
5965
});
6066
return new Promise((resolve, reject) => {
6167
const inputFolder = folder ? folder.replace(new RegExp('\\' + sep, 'g'), '/').split(sep).join('/') : undefined;
62-
const command = 'node ' +
63-
resolvePath(__dirname, '../../node_modules/@angular-devkit/schematics-cli/bin/schematics.js') +
64-
' @rucken/schematics:nestjs-entity ' +
65-
(inputFolder ? ('--root=' + inputFolder + ' ') : ' ') +
66-
(name ? ('--name=' + name + ' ') : ' ') +
67-
(fields ? ('--fields=' + fields + ' ') : ' ') +
68-
(username ? ('--username=' + username + ' ') : ' ') +
69-
(email ? ('--email=' + email + ' ') : ' ') +
70-
(app ? ('--app=' + app + ' ') : ' ') +
71-
(core ? ('--core=' + core + ' ') : ' ') +
72-
'--dry-run=false --force';
68+
const command = schematicsCommandBuilder(
69+
inputFolder as string,
70+
template,
71+
{
72+
root: inputFolder,
73+
name: name,
74+
fields: fields,
75+
username: username,
76+
email: email,
77+
app: app,
78+
core: core
79+
}
80+
);
7381
this.debug('command', command);
7482
npmRun.exec(
7583
command,

src/commands/nestjs-new.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { flags, Command } from '@oclif/command';
2-
import { resolve as resolvePath, sep } from 'path';
2+
import { sep } from 'path';
3+
import { schematicsCommandBuilder } from '../utils/schematics-command-builder';
34
const npmRun = require('npm-run');
45

56
export class NestJSNew extends Command {
67
static aliases = ['new:nestjs', 'nestjs-new'];
78
static description = 'generate empty NestJS backend application';
89
static flags = {
910
help: flags.help({ char: 'h' }),
11+
template: flags.string({ char: 't', description: 'template', default: '@rucken/schematics:nestjs-new' }),
1012
name: flags.string({ char: 'n', description: 'application name on ke-bab case' }),
1113
username: flags.string({ char: 'u', description: 'username' }),
1214
email: flags.string({ char: 'e', description: 'email' })
@@ -15,12 +17,14 @@ export class NestJSNew extends Command {
1517
async run() {
1618
const { args, flags } = this.parse(NestJSNew);
1719
const folder = args.folder;
20+
const template = flags.template || '';
1821
const name = flags.name;
1922
const username = flags.username;
2023
const email = flags.email;
2124

2225
try {
2326
await this.runNew(
27+
template,
2428
folder,
2529
name,
2630
username,
@@ -31,12 +35,14 @@ export class NestJSNew extends Command {
3135
}
3236
}
3337
private runNew(
38+
template: string,
3439
folder: string,
3540
name?: string,
3641
username?: string,
3742
email?: string
3843
) {
3944
this.debug('Start', {
45+
template: template,
4046
folder: folder,
4147
name: name,
4248
username: username,
@@ -47,14 +53,16 @@ export class NestJSNew extends Command {
4753
if (inputFolder && inputFolder.indexOf('/') === -1 && !name) {
4854
name = inputFolder;
4955
}
50-
const command = 'node ' +
51-
resolvePath(__dirname, '../../node_modules/@angular-devkit/schematics-cli/bin/schematics.js') +
52-
' @rucken/schematics:nestjs-new ' +
53-
(inputFolder ? ('--root=' + inputFolder + ' ') : ' ') +
54-
(name ? ('--name=' + name + ' ') : ' ') +
55-
(username ? ('--username=' + username + ' ') : ' ') +
56-
(email ? ('--email=' + email + ' ') : ' ') +
57-
'--dry-run=false --force';
56+
const command = schematicsCommandBuilder(
57+
inputFolder as string,
58+
template,
59+
{
60+
root: inputFolder,
61+
name: name,
62+
username: username,
63+
email: email
64+
}
65+
);
5866
this.debug('command', command);
5967
npmRun.exec(
6068
command,

0 commit comments

Comments
 (0)