|
4 | 4 | * |
5 | 5 | */ |
6 | 6 | import * as _ from 'lodash'; |
| 7 | +import * as path from 'path'; |
| 8 | +import * as inquirer from 'inquirer'; |
| 9 | + |
7 | 10 | import { writeTemplate } from './template'; |
8 | | -import { askFileName, buildFilePath, existsFile, parseName, updateTargets } from './utils'; |
| 11 | +import { existsFile, parseName, updateTargets, inputIsRequired } from './utils'; |
9 | 12 |
|
10 | 13 | export interface MakeCommand { |
11 | 14 | context: any; |
@@ -51,14 +54,49 @@ export class AbstractMakeCommand { |
51 | 54 | } |
52 | 55 |
|
53 | 56 | 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); |
55 | 58 | } |
56 | 59 |
|
57 | 60 | 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); |
59 | 62 | await existsFile(filePath, true, this.isTest); |
60 | 63 | this.context.name = parseName(this.context.name, this.suffix); |
61 | 64 | await writeTemplate(this.template, filePath, this.context); |
62 | 65 | } |
63 | 66 |
|
| 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 | + |
64 | 102 | } |
0 commit comments