Skip to content

Commit 1325eb6

Browse files
author
hirsch88
committed
Refactor console commands structure
1 parent 688bd9c commit 1325eb6

File tree

3 files changed

+51
-25
lines changed

3 files changed

+51
-25
lines changed

src/console/MakeSeedCommand.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
* -------------------------------------
44
*
55
*/
6+
import * as _ from 'lodash';
67
import { AbstractMakeCommand } from './lib/AbstractMakeCommand';
78

8-
99
export class MakeSeedCommand extends AbstractMakeCommand {
1010

1111
public static command = 'make:seed';
@@ -17,4 +17,10 @@ export class MakeSeedCommand extends AbstractMakeCommand {
1717
public template = 'seed.hbs';
1818
public updateTargets = false;
1919

20+
public parseName(suffix: string = '', prefix: string = ''): (name: string) => string {
21+
return (name: string) => {
22+
return _.snakeCase(name);
23+
};
24+
}
25+
2026
}

src/console/lib/AbstractMakeCommand.ts

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
*
55
*/
66
import * as _ from 'lodash';
7+
import * as path from 'path';
8+
import * as inquirer from 'inquirer';
9+
710
import { writeTemplate } from './template';
8-
import { askFileName, buildFilePath, existsFile, parseName, updateTargets } from './utils';
11+
import { existsFile, parseName, updateTargets, inputIsRequired } from './utils';
912

1013
export interface MakeCommand {
1114
context: any;
@@ -51,14 +54,49 @@ export class AbstractMakeCommand {
5154
}
5255

5356
public async run(): Promise<void> {
54-
this.context = await askFileName(this.context, this.type, this.suffix, this.prefix);
57+
this.context = await this.askFileName(this.context, this.type, this.suffix, this.prefix);
5558
}
5659

5760
public async write(): Promise<void> {
58-
const filePath = buildFilePath(this.target, this.context.name, this.isTest);
61+
const filePath = this.buildFilePath(this.target, this.context.name, this.isTest);
5962
await existsFile(filePath, true, this.isTest);
6063
this.context.name = parseName(this.context.name, this.suffix);
6164
await writeTemplate(this.template, filePath, this.context);
6265
}
6366

67+
public buildFilePath = (targetPath: string, fileName: string, isTest = false, extension = '.ts') => {
68+
return path.join(__dirname, `/../../${targetPath}`, `${fileName}${extension}`);
69+
}
70+
71+
public parseName(suffix: string = '', prefix: string = ''): (name: string) => string {
72+
return (name: string) => {
73+
let ns = name.split('/');
74+
ns = ns.map((v) => _.camelCase(v));
75+
ns[ns.length - 1] = _.capitalize(ns[ns.length - 1]);
76+
return (ns.join('/')) + prefix + suffix;
77+
};
78+
}
79+
80+
public async askFileName(context: any, name: string, suffix: string, prefix: string): Promise<any> {
81+
const nameParser = this.parseName(suffix, prefix);
82+
if (context === undefined || context.name === undefined) {
83+
const prompt = inquirer.createPromptModule();
84+
context = await prompt([
85+
{
86+
type: 'input',
87+
name: 'name',
88+
message: `Enter the name of the ${name}:`,
89+
filter: nameParser,
90+
validate: inputIsRequired
91+
}
92+
]);
93+
const amount = context.name.split('/').length - 1;
94+
context.deepness = '';
95+
_.times(amount, () => context.deepness += '../');
96+
} else {
97+
context.name = nameParser(context.name);
98+
}
99+
return context;
100+
}
101+
64102
}

src/console/lib/utils.ts

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ export const removeSufix = (suffix: string, value: string) => {
2121
};
2222

2323
export const filterInput = (suffix: string, prefix = '') => (value: string) => {
24+
if (value.indexOf('/') < 0) {
25+
return value;
26+
}
2427
let vs = value.split('/');
2528
vs = vs.map((v) => _.camelCase(v));
2629
vs[vs.length - 1] = _.capitalize(vs[vs.length - 1]);
@@ -37,27 +40,6 @@ export const buildFilePath = (targetPath: string, fileName: string, isTest = fal
3740

3841
export const inputIsRequired = (value: any) => !!value;
3942

40-
export const askFileName = async (context: any, name: string, suffix: string, prefix: string) => {
41-
if (context === undefined || context.name === undefined) {
42-
const prompt = inquirer.createPromptModule();
43-
context = await prompt([
44-
{
45-
type: 'input',
46-
name: 'name',
47-
message: `Enter the name of the ${name}:`,
48-
filter: filterInput(suffix, prefix),
49-
validate: inputIsRequired
50-
}
51-
]);
52-
const amount = context.name.split('/').length - 1;
53-
context.deepness = '';
54-
_.times(amount, () => context.deepness += '../');
55-
} else {
56-
context.name = filterInput(suffix, prefix)(context.name);
57-
}
58-
return context;
59-
};
60-
6143
export const existsFile = async (path: string, stop: boolean = false, isTest = false) => {
6244
const prompt = inquirer.createPromptModule();
6345
return new Promise((resolve, reject) => {

0 commit comments

Comments
 (0)