element.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 ElementGenerator {
  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 custom element?')
  13. .then(name => {
  14. let fileName = this.project.makeFileName(name);
  15. let className = this.project.makeClassName(name);
  16. this.project.elements.add(
  17. ProjectItem.text(`${fileName}.js`, this.generateJSSource(className)),
  18. ProjectItem.text(`${fileName}.html`, this.generateHTMLSource(className))
  19. );
  20. return this.project.commitChanges()
  21. .then(() => this.ui.log(`Created ${fileName}.`));
  22. });
  23. }
  24. generateJSSource(className) {
  25. return `import {bindable} from 'aurelia-framework';
  26. export class ${className} {
  27. @bindable value;
  28. valueChanged(newValue, oldValue) {
  29. }
  30. }
  31. `;
  32. }
  33. generateHTMLSource(className) {
  34. return `<template>
  35. <h1>\${value}</h1>
  36. </template>`;
  37. }
  38. }