UNPKG

ember-source

Version:

A JavaScript framework for creating ambitious web applications

107 lines (94 loc) 3.15 kB
import stringUtil from 'ember-cli-string-utils'; import getPathOption from 'ember-cli-get-component-path-option'; import normalizeEntityName from 'ember-cli-normalize-entity-name'; import { generateComponentSignature } from '../-utils.js'; import typescriptBlueprintPolyfill from 'ember-cli-typescript-blueprint-polyfill'; // intentionally avoiding use-edition-detector export default { description: 'Generates a component class.', shouldTransformTypeScript: true, availableOptions: [ { name: 'path', type: String, default: 'components', aliases: [{ 'no-path': '' }], }, { name: 'component-class', type: ['@ember/component', '@glimmer/component', '@ember/component/template-only'], default: '@glimmer/component', aliases: [ { cc: '@ember/component' }, { gc: '@glimmer/component' }, { tc: '@ember/component/template-only' }, ], }, { name: 'component-structure', type: ['flat', 'nested'], default: 'flat', aliases: [{ fs: 'flat' }, { ns: 'nested' }], }, ], init() { this._super && this._super.init.apply(this, arguments); typescriptBlueprintPolyfill(this); }, fileMapTokens(options) { let commandOptions = this.options; if (commandOptions.componentStructure === 'flat') { return { __path__() { return 'components'; }, }; } else if (commandOptions.componentStructure === 'nested') { return { __path__() { return `components/${options.dasherizedModuleName}`; }, __name__() { return 'index'; }, }; } }, normalizeEntityName(entityName) { return normalizeEntityName( entityName.replace(/\.js$/, '') //Prevent generation of ".js.js" files ); }, locals(options) { let sanitizedModuleName = options.entity.name.replace(/\//g, '-'); let classifiedModuleName = stringUtil.classify(sanitizedModuleName); let importComponent = ''; let importTemplate = ''; let defaultExport = ''; let componentSignature = ''; switch (options.componentClass) { case '@ember/component': importComponent = `import Component from '@ember/component';`; defaultExport = `class extends Component {}`; break; case '@glimmer/component': importComponent = `import Component from '@glimmer/component';`; componentSignature = generateComponentSignature(classifiedModuleName); defaultExport = `class ${classifiedModuleName} extends Component<${classifiedModuleName}Signature> {}`; break; case '@ember/component/template-only': importComponent = `import templateOnly from '@ember/component/template-only';`; componentSignature = generateComponentSignature(classifiedModuleName); defaultExport = `templateOnly<${classifiedModuleName}Signature>();`; break; } return { importTemplate, importComponent, componentSignature, defaultExport, path: getPathOption(options), componentClass: options.componentClass, }; }, };