generator.js 1.8 KB

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