|
| 1 | +import {inject} from 'aurelia-dependency-injection'; |
| 2 | +import {Project, ProjectItem, CLIOptions, UI} from 'aurelia-cli'; |
| 3 | + |
| 4 | +@inject(Project, CLIOptions, UI) |
| 5 | +export default class GeneratorGenerator { |
| 6 | + constructor(private project: Project, private options: CLIOptions, private ui: UI) { } |
| 7 | + |
| 8 | + async execute() { |
| 9 | + const name = await this.ui.ensureAnswer( |
| 10 | + this.options.args[0], |
| 11 | + 'What would you like to call the generator?' |
| 12 | + ); |
| 13 | + |
| 14 | + let fileName = this.project.makeFileName(name); |
| 15 | + let className = this.project.makeClassName(name); |
| 16 | + |
| 17 | + this.project.generators.add( |
| 18 | + ProjectItem.text(`${fileName}.ts`, this.generateSource(className)) |
| 19 | + ); |
| 20 | + |
| 21 | + await this.project.commitChanges() |
| 22 | + await this.ui.log(`Created ${fileName}.`); |
| 23 | + } |
| 24 | + |
| 25 | + generateSource(className) { |
| 26 | + return `import {inject} from 'aurelia-dependency-injection'; |
| 27 | +import {Project, ProjectItem, CLIOptions, UI} from 'aurelia-cli'; |
| 28 | +
|
| 29 | +@inject(Project, CLIOptions, UI) |
| 30 | +export default class ${className}Generator { |
| 31 | + constructor(project, options, ui) { |
| 32 | + this.project = project; |
| 33 | + this.options = options; |
| 34 | + this.ui = ui; |
| 35 | + } |
| 36 | +
|
| 37 | + execute() { |
| 38 | + return this.ui |
| 39 | + .ensureAnswer(this.options.args[0], 'What would you like to call the new item?') |
| 40 | + .then(name => { |
| 41 | + let fileName = this.project.makeFileName(name); |
| 42 | + let className = this.project.makeClassName(name); |
| 43 | +
|
| 44 | + this.project.elements.add( |
| 45 | + ProjectItem.text(\`\${fileName}.ts\`, this.generateSource(className)) |
| 46 | + ); |
| 47 | +
|
| 48 | + return this.project.commitChanges() |
| 49 | + .then(() => this.ui.log(\`Created \${fileName}.\`)); |
| 50 | + }); |
| 51 | + } |
| 52 | +
|
| 53 | + generateSource(className) { |
| 54 | +return \`import {bindable} from 'aurelia-framework'; |
| 55 | +
|
| 56 | +export class \${className} { |
| 57 | + @bindable value; |
| 58 | +
|
| 59 | + valueChanged(newValue, oldValue) { |
| 60 | + // |
| 61 | + } |
| 62 | +} |
| 63 | +\` |
| 64 | + } |
| 65 | +} |
| 66 | +`; |
| 67 | + } |
| 68 | +} |
0 commit comments