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

[Cleanup] Component blueprint cleanup #20773

Merged
merged 2 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
55 changes: 8 additions & 47 deletions blueprints/component-class/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
'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 { EOL } = require('os');
const { has } = require('@ember/edition-utils');
const { generateComponentSignature } = require('../-utils');

Expand Down Expand Up @@ -44,9 +41,9 @@ module.exports = {
},
{
name: 'component-structure',
type: OCTANE ? ['flat', 'nested', 'classic'] : ['classic'],
default: OCTANE ? 'flat' : 'classic',
aliases: OCTANE ? [{ fs: 'flat' }, { ns: 'nested' }, { cs: 'classic' }] : [{ cs: 'classic' }],
type: ['flat', 'nested'],
default: 'flat',
aliases: [{ fs: 'flat' }, { ns: 'nested' }],
},
],

Expand All @@ -71,15 +68,9 @@ module.exports = {
option.default = '@ember/component';
}
} else if (option.name === 'component-structure') {
if (isOctane) {
option.type = ['flat', 'nested', 'classic'];
option.default = 'flat';
option.aliases = [{ fs: 'flat' }, { ns: 'nested' }, { cs: 'classic' }];
} else {
option.type = ['classic'];
option.default = 'classic';
option.aliases = [{ cs: 'classic' }];
}
option.type = ['flat', 'nested'];
option.default = 'flat';
option.aliases = [{ fs: 'flat' }, { ns: 'nested' }];
}
});

Expand All @@ -99,19 +90,7 @@ module.exports = {
fileMapTokens(options) {
let commandOptions = this.options;

if (commandOptions.pod) {
return {
__path__() {
return path.join(options.podPath, options.locals.path, options.dasherizedModuleName);
},
__name__() {
return 'component';
},
};
} else if (
commandOptions.componentStructure === 'classic' ||
commandOptions.componentStructure === 'flat'
) {
if (commandOptions.componentStructure === 'flat') {
return {
__path__() {
return 'components';
Expand Down Expand Up @@ -139,35 +118,17 @@ module.exports = {
let sanitizedModuleName = options.entity.name.replace(/\//g, '-');
let classifiedModuleName = stringUtil.classify(sanitizedModuleName);

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

// if we're in an addon, build import statement
if (options.project.isEmberCLIAddon() || (options.inRepoAddon && !options.inDummy)) {
if (options.pod) {
templatePath = './template';
} else {
templatePath =
pathUtil.getRelativeParentPath(options.entity.name) +
'templates/components/' +
stringUtil.dasherize(options.entity.name);
}
}

let componentClass = options.componentClass;

switch (componentClass) {
case '@ember/component':
importComponent = `import Component from '@ember/component';`;
if (templatePath) {
importTemplate = `import layout from '${templatePath}';${EOL}`;
defaultExport = `Component.extend({${EOL} layout${EOL}});`;
} else {
defaultExport = `Component.extend({});`;
}
defaultExport = `Component.extend({});`;
break;
case '@glimmer/component':
importComponent = `import Component from '@glimmer/component';`;
Expand Down
79 changes: 8 additions & 71 deletions blueprints/component/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
'use strict';

const chalk = require('chalk');
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 { EOL } = require('os');
const { has } = require('@ember/edition-utils');
const { generateComponentSignature } = require('../-utils');

Expand Down Expand Up @@ -48,9 +45,9 @@ module.exports = {
},
{
name: 'component-structure',
type: OCTANE ? ['flat', 'nested', 'classic'] : ['classic'],
default: OCTANE ? 'flat' : 'classic',
aliases: OCTANE ? [{ fs: 'flat' }, { ns: 'nested' }, { cs: 'classic' }] : [{ cs: 'classic' }],
type: ['flat', 'nested'],
default: 'flat',
aliases: [{ fs: 'flat' }, { ns: 'nested' }],
},
],

Expand All @@ -75,15 +72,9 @@ module.exports = {
option.default = '@ember/component';
}
} else if (option.name === 'component-structure') {
if (isOctane) {
option.type = ['flat', 'nested', 'classic'];
option.default = 'flat';
option.aliases = [{ fs: 'flat' }, { ns: 'nested' }, { cs: 'classic' }];
} else {
option.type = ['classic'];
option.default = 'classic';
option.aliases = [{ cs: 'classic' }];
}
option.type = ['flat', 'nested'];
option.default = 'flat';
option.aliases = [{ fs: 'flat' }, { ns: 'nested' }];
}
});

Expand All @@ -107,12 +98,6 @@ module.exports = {
'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);
Expand Down Expand Up @@ -149,37 +134,7 @@ module.exports = {
fileMapTokens(options) {
let commandOptions = this.options;

if (commandOptions.pod) {
return {
__path__() {
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__() {
return options.dasherizedModuleName;
},
};
} else if (
this.EMBER_GLIMMER_SET_COMPONENT_TEMPLATE &&
commandOptions.componentStructure === 'flat'
) {
if (this.EMBER_GLIMMER_SET_COMPONENT_TEMPLATE && commandOptions.componentStructure === 'flat') {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the EMBER_GLIMMER_SET_COMPONENT_TEMPLATE code can also be removed but that's unrelated to this PR.

return {
__path__() {
return 'components';
Expand Down Expand Up @@ -236,19 +191,6 @@ module.exports = {
},

locals(options) {
// if we're in an addon, build import statement
let templatePath = '';
if (options.project.isEmberCLIAddon() || (options.inRepoAddon && !options.inDummy)) {
if (options.pod) {
templatePath = './template';
} else {
templatePath =
pathUtil.getRelativeParentPath(options.entity.name) +
'templates/components/' +
stringUtil.dasherize(options.entity.name);
}
}

let componentClass = this.EMBER_GLIMMER_SET_COMPONENT_TEMPLATE
? options.componentClass
: '@ember/component';
Expand All @@ -264,12 +206,7 @@ module.exports = {
switch (componentClass) {
case '@ember/component':
importComponent = `import Component from '@ember/component';`;
if (templatePath) {
importTemplate = `import layout from '${templatePath}';${EOL}`;
defaultExport = `Component.extend({${EOL} layout${EOL}});`;
} else {
defaultExport = `Component.extend({});`;
}
defaultExport = `Component.extend({});`;
break;
case '@glimmer/component':
importComponent = `import Component from '@glimmer/component';`;
Expand Down
121 changes: 0 additions & 121 deletions node-tests/blueprints/component-class-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ const blueprintHelpers = require('ember-cli-blueprint-test-helpers/helpers');
const setupTestHooks = blueprintHelpers.setupTestHooks;
const emberNew = blueprintHelpers.emberNew;
const emberGenerateDestroy = blueprintHelpers.emberGenerateDestroy;
const setupPodConfig = blueprintHelpers.setupPodConfig;
const modifyPackages = blueprintHelpers.modifyPackages;

const chai = require('ember-cli-blueprint-test-helpers/chai');
const expect = chai.expect;

const generateFakePackageManifest = require('../helpers/generate-fake-package-manifest');
const fixture = require('../helpers/fixture');

const setupTestEnvironment = require('../helpers/setup-test-environment');
const enableOctane = setupTestEnvironment.enableOctane;
Expand Down Expand Up @@ -54,23 +52,6 @@ describe('Blueprint: component-class', function () {
});
});

// classic default
it('component-class foo --component-structure=classic --component-class=@ember/component', function () {
return emberGenerateDestroy(
[
'component-class',
'foo',
'--component-structure',
'classic',
'--component-class',
'@ember/component',
],
(_file) => {
expect(_file('app/components/foo.js')).to.equal(emberComponentContents);
}
);
});

// Octane default
it('component-class foo --component-structure=flat --component-class=@glimmer/component', function () {
return emberGenerateDestroy(
Expand Down Expand Up @@ -106,15 +87,6 @@ describe('Blueprint: component-class', function () {
);
});

it('component-class foo --component-structure=classic', function () {
return emberGenerateDestroy(
['component-class', '--component-structure', 'classic', 'foo'],
(_file) => {
expect(_file('app/components/foo.js')).to.equal(glimmerComponentContents);
}
);
});

it('component-class foo --component-class=@ember/component', function () {
return emberGenerateDestroy(
['component-class', '--component-class', '@ember/component', 'foo'],
Expand Down Expand Up @@ -177,99 +149,6 @@ describe('Blueprint: component-class', function () {
}
);
});

describe('with podModulePrefix', function () {
beforeEach(function () {
setupPodConfig({ podModulePrefix: true });
});

it('component-class foo --pod', function () {
return emberGenerateDestroy(['component-class', 'foo', '--pod'], (_file) => {
expect(_file('app/pods/components/foo/component.js')).to.equal(
fixture('component/component.js')
);
});
});

it('component-class x-foo --pod', function () {
return emberGenerateDestroy(['component-class', 'x-foo', '--pod'], (_file) => {
expect(_file('app/pods/components/x-foo/component.js')).to.equal(
fixture('component/component-dash.js')
);
});
});

it('component-class foo/x-foo --pod', function () {
return emberGenerateDestroy(['component-class', 'foo/x-foo', '--pod'], (_file) => {
expect(_file('app/pods/components/foo/x-foo/component.js')).to.equal(
fixture('component/component-nested.js')
);
});
});

it('component-class x-foo --pod --path foo', function () {
return emberGenerateDestroy(
['component-class', 'x-foo', '--pod', '--path', 'foo'],
(_file) => {
expect(_file('app/pods/foo/x-foo/component.js')).to.equal(
fixture('component/component-dash.js')
);
}
);
});

it('component-class foo/x-foo --pod --path bar', function () {
return emberGenerateDestroy(
['component-class', 'foo/x-foo', '--pod', '--path', 'bar'],
(_file) => {
expect(_file('app/pods/bar/foo/x-foo/component.js')).to.equal(
fixture('component/component-nested.js')
);
}
);
});

it('component-class x-foo --pod --path bar/foo', function () {
return emberGenerateDestroy(
['component-class', 'x-foo', '--pod', '--path', 'bar/foo'],
(_file) => {
expect(_file('app/pods/bar/foo/x-foo/component.js')).to.equal(
fixture('component/component-dash.js')
);
}
);
});

it('component-class foo/x-foo --pod --path bar/baz', function () {
return emberGenerateDestroy(
['component-class', 'foo/x-foo', '--pod', '--path', 'bar/baz'],
(_file) => {
expect(_file('app/pods/bar/baz/foo/x-foo/component.js')).to.equal(
fixture('component/component-nested.js')
);
}
);
});

it('component-class x-foo --pod -no-path', function () {
return emberGenerateDestroy(['component-class', 'x-foo', '--pod', '-no-path'], (_file) => {
expect(_file('app/pods/x-foo/component.js')).to.equal(
fixture('component/component-dash.js')
);
});
});

it('component-class foo/x-foo --pod -no-path', function () {
return emberGenerateDestroy(
['component-class', 'foo/x-foo', '--pod', '-no-path'],
(_file) => {
expect(_file('app/pods/foo/x-foo/component.js')).to.equal(
fixture('component/component-nested.js')
);
}
);
});
});
});

describe('in addon - octane', function () {
Expand Down
Loading