Skip to content

Commit

Permalink
feat: support --blueprint arg for specifying a path to a blueprint,
Browse files Browse the repository at this point in the history
rather than relying on blueprint lookup/resolver rules
  • Loading branch information
NullVoxPopuli committed Dec 17, 2020
1 parent 7c0e785 commit bac7d7e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 12 deletions.
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 @@ -39,13 +44,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 @@ -165,3 +164,7 @@ module.exports = Command.extend({
}
},
});

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

if (name.includes(path.sep)) {
let blueprintPath = path.resolve(name);
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
10 changes: 10 additions & 0 deletions tests/acceptance/generate-test.js
Expand Up @@ -268,6 +268,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

0 comments on commit bac7d7e

Please sign in to comment.