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

Add support for specifying a path for the generate command #9387

Merged
merged 1 commit into from Jan 26, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 10 additions & 7 deletions lib/commands/generate.js
Expand Up @@ -9,6 +9,11 @@ const _ = require('ember-cli-lodash-subset');
const EOL = require('os').EOL;
const SilentError = require('silent-error');

const UNKNOWN_BLUEPRINT_ERROR =
'The `ember generate` command requires a ' +
'blueprint name to be specified. ' +
'For more details, use `ember help`';

module.exports = Command.extend({
name: 'generate',
description: 'Generates new code from blueprints.',
Expand Down Expand Up @@ -40,13 +45,7 @@ module.exports = Command.extend({
let blueprintName = rawArgs[0];

if (!blueprintName) {
return Promise.reject(
new SilentError(
'The `ember generate` command requires a ' +
'blueprint name to be specified. ' +
'For more details, use `ember help`.'
)
);
return Promise.reject(new SilentError(UNKNOWN_BLUEPRINT_ERROR));
}

let taskArgs = {
Expand Down Expand Up @@ -166,3 +165,7 @@ module.exports = Command.extend({
}
},
});

module.exports.ERRORS = {
UNKNOWN_BLUEPRINT_ERROR,
};
17 changes: 17 additions & 0 deletions lib/models/blueprint.js
Expand Up @@ -1318,6 +1318,23 @@ let Blueprint = CoreObject.extend({
Blueprint.lookup = function (name, options) {
options = options || {};

if (name.includes(path.sep)) {
let blueprintPath = path.resolve(name);
Copy link
Contributor

Choose a reason for hiding this comment

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

could we add check for relative/absolute paths, is cwd = curren project dir?

let isNameAPath = Boolean(blueprintPath);

if (isNameAPath) {
if (Blueprint._existsSync(blueprintPath)) {
return Blueprint.load(blueprintPath);
}

if (!options.ignoreMissing) {
throw new SilentError(`Unknown blueprint: ${name}`);
}

return;
}
}

let lookupPaths = generateLookupPaths(options.paths);

let lookupPath;
Expand Down
7 changes: 7 additions & 0 deletions tests/acceptance/destroy-test.js
Expand Up @@ -101,6 +101,13 @@ describe('Acceptance: ember destroy', function () {
return assertDestroyAfterGenerate(commandArgs, files);
});

it('deletes files generated using blueprint paths', function () {
let commandArgs = [`${__dirname}/../../blueprints/http-proxy`, 'foo', 'bar'];
let files = ['server/proxies/foo.js'];

return assertDestroyAfterGenerate(commandArgs, files);
});

it('deletes files generated using blueprints from the project directory', async function () {
let commandArgs = ['foo', 'bar'];
let files = ['app/foos/bar.js'];
Expand Down
10 changes: 10 additions & 0 deletions tests/acceptance/generate-test.js
Expand Up @@ -144,6 +144,16 @@ describe('Acceptance: ember generate', function () {
expect(file('app/controllers/foo.js')).to.contain('custom: true');
});

it('allows a path to be specified to a blueprint', async function () {
await outputFile(
'blueprints/http-proxy/files/server/proxies/__name__.js',
"import Ember from 'ember';\n" + 'export default Ember.Object.extend({ foo: true });\n'
);
await generate([path.resolve(`${__dirname}/../../blueprints/http-proxy`), 'foo', 'http://localhost:5000']);

expect(file('server/index.js')).to.not.contain('foo: true', 'the local blueprint is not used');
});

it('passes custom cli arguments to blueprint options', async function () {
await initApp();

Expand Down
7 changes: 2 additions & 5 deletions tests/unit/commands/generate-test.js
Expand Up @@ -11,6 +11,7 @@ const GenerateCommand = require('../../../lib/commands/generate');
const td = require('testdouble');
const ROOT = process.cwd();
const { createTempDir } = require('broccoli-test-helper');
const { ERRORS } = GenerateCommand;

describe('generate command', function () {
let input, options, command;
Expand Down Expand Up @@ -98,11 +99,7 @@ describe('generate command', function () {

it('complains if no blueprint name is given', function () {
return expect(command.validateAndRun([])).to.be.rejected.then((error) => {
expect(error.message).to.equal(
'The `ember generate` command requires a ' +
'blueprint name to be specified. ' +
'For more details, use `ember help`.'
);
expect(error.message).to.equal(ERRORS.UNKNOWN_BLUEPRINT_ERROR);
});
});

Expand Down