Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement component co-location blueprints #18203

Merged
merged 2 commits into from
Jul 31, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions blueprints/component/files/__root__/__path__/__name__.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Component from '@ember/component';
<%= importComponent %>
<%= importTemplate %>
export default Component.extend({<%= contents %>
});
export default <%= defaultExport %>
195 changes: 170 additions & 25 deletions blueprints/component/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
'use strict';

const path = require('path');
const SilentError = require('silent-error');
const stringUtil = require('ember-cli-string-utils');
const pathUtil = require('ember-cli-path-utils');
const getPathOption = require('ember-cli-get-component-path-option');
const normalizeEntityName = require('ember-cli-normalize-entity-name');
const useEditionDetector = require('../edition-detector');
const isModuleUnificationProject = require('../module-unification').isModuleUnificationProject;
const EOL = require('os').EOL;
const { isModuleUnificationProject } = require('../module-unification');
const { EOL } = require('os');

const OCTANE = process.env.EMBER_VERSION === 'octane';

// TODO: this should be reading from the @ember/canary-features module
// need to refactor broccoli/features.js to be able to work more similarly
// to https://github.com/emberjs/data/pull/6231
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Started work in #18218 and should be good to go.

const EMBER_GLIMMER_SET_COMPONENT_TEMPLATE = OCTANE;

// intentionally avoiding use-edition-detector
module.exports = {
EMBER_GLIMMER_SET_COMPONENT_TEMPLATE,

module.exports = useEditionDetector({
description: 'Generates a component.',

availableOptions: [
Expand All @@ -19,9 +29,66 @@ module.exports = useEditionDetector({
default: 'components',
aliases: [{ 'no-path': '' }],
},
{
name: 'component-class',
type: ['@ember/component', '@glimmer/component', '@ember/component/template-only', ''],
default: OCTANE ? '--no-component-class' : '@ember/component',
aliases: [
{ cc: '@ember/component' },
{ gc: '@glimmer/component' },
{ tc: '@ember/component/template-only' },
{ nc: '' },
{ 'no-component-class': '' },
],
},
{
name: 'component-structure',
type: ['flat', 'nested', 'classic'],
default: OCTANE ? 'flat' : 'classic',
aliases: [{ fs: 'flat' }, { ns: 'nested' }, { cs: 'classic' }],
},
],

fileMapTokens: function() {
init() {
this._super && this._super.init.apply(this, arguments);
let isOctane = process.env.EMBER_VERSION === 'OCTANE';

this.availableOptions.forEach(option => {
if (option.name === 'component-class') {
if (isOctane) {
option.default = '--no-component-class';
} else {
option.default = '@ember/component';
}
} else if (option.name === 'component-structure') {
option.default = isOctane ? 'flat' : 'classic';
}
});

this.EMBER_GLIMMER_SET_COMPONENT_TEMPLATE = EMBER_GLIMMER_SET_COMPONENT_TEMPLATE || isOctane;
},

install(options) {
if (!this.EMBER_GLIMMER_SET_COMPONENT_TEMPLATE) {
if (options.componentClass !== '@ember/component') {
throw new SilentError(
'Usage of --component-class argument to `ember generate component` is only available on canary'
);
}

if (options.componentStructure !== 'classic') {
throw new SilentError(
'Usage of --component-structure argument to `ember generate component` is only available on canary'
);
}
}

return this._super.install.apply(this, arguments);
},

fileMapTokens(options) {
let commandOptions = this.options;

if (isModuleUnificationProject(this.project)) {
return {
__name__: function() {
Expand All @@ -46,40 +113,96 @@ module.exports = useEditionDetector({
return 'template';
},
};
} else {
} else if (commandOptions.pod) {
return {
__path__: function(options) {
if (options.pod) {
return path.join(options.podPath, options.locals.path, options.dasherizedModuleName);
} else {
return 'components';
}
__path__() {
return path.join(options.podPath, options.locals.path, options.dasherizedModuleName);
},
__templatepath__: function(options) {
if (options.pod) {
return path.join(options.podPath, options.locals.path, options.dasherizedModuleName);
}
__templatepath__() {
return path.join(options.podPath, options.locals.path, options.dasherizedModuleName);
},
__templatename__() {
return 'template';
},
};
} else if (
!this.EMBER_GLIMMER_SET_COMPONENT_TEMPLATE ||
commandOptions.componentStructure === 'classic'
) {
return {
__path__() {
return 'components';
},
__templatepath__() {
return 'templates/components';
},
__templatename__: function(options) {
if (options.pod) {
return 'template';
}
__templatename__() {
return options.dasherizedModuleName;
},
};
} else if (
this.EMBER_GLIMMER_SET_COMPONENT_TEMPLATE &&
commandOptions.componentStructure === 'flat'
) {
return {
__path__() {
return 'components';
},
__templatepath__() {
return 'components';
},
__templatename__() {
return options.dasherizedModuleName;
},
};
} else if (
this.EMBER_GLIMMER_SET_COMPONENT_TEMPLATE &&
commandOptions.componentStructure === 'nested'
) {
return {
__path__() {
return `components/${options.dasherizedModuleName}`;
},
__name__() {
return 'index';
},
__templatepath__() {
return `components/${options.dasherizedModuleName}`;
},
__templatename__() {
return `index`;
},
};
}
},

normalizeEntityName: function(entityName) {
files() {
let files = this._super.files.apply(this, arguments);

if (
this.EMBER_GLIMMER_SET_COMPONENT_TEMPLATE &&
(this.options.componentClass === '' || this.options.componentClass === '--no-component-class')
) {
files = files.filter(file => !file.endsWith('.js'));
}

return files;
},

normalizeEntityName(entityName) {
return normalizeEntityName(
entityName.replace(/\.js$/, '') //Prevent generation of ".js.js" files
);
},

locals: function(options) {
locals(options) {
let sanitizedModuleName = options.entity.name.replace(/\//g, '-');
let classifiedModuleName = stringUtil.classify(sanitizedModuleName);

let templatePath = '';
let importComponent = '';
let importTemplate = '';
let defaultExport = '';
let contents = '';

if (!isModuleUnificationProject(this.project)) {
Expand All @@ -98,10 +221,32 @@ module.exports = useEditionDetector({
}
}

let componentClass = this.EMBER_GLIMMER_SET_COMPONENT_TEMPLATE
? options.componentClass
: '@ember/component';

switch (componentClass) {
case '@ember/component':
importComponent = `import Component from '@ember/component';`;
defaultExport = `Component.extend({${contents}\n});`;
break;
case '@glimmer/component':
importComponent = `import Component from '@glimmer/component';`;
defaultExport = `class ${classifiedModuleName}Component extends Component {\n}`;
break;
case '':
case '@ember/component/template-only':
importComponent = `import templateOnly from '@ember/component/template-only';`;
defaultExport = `templateOnly();`;
break;
}

return {
importTemplate: importTemplate,
contents: contents,
importTemplate,
importComponent,
defaultExport,
path: getPathOption(options),
componentClass: options.componentClass,
};
},
});
};

This file was deleted.

This file was deleted.

Loading