From bac7d7ee8a23ca4ad07ded1ca72354d5e9c8f8a0 Mon Sep 17 00:00:00 2001 From: NullVoxPopuli Date: Mon, 23 Nov 2020 07:55:35 -0500 Subject: [PATCH] feat: support --blueprint arg for specifying a path to a blueprint, rather than relying on blueprint lookup/resolver rules --- lib/commands/generate.js | 17 ++++++++++------- lib/models/blueprint.js | 17 +++++++++++++++++ tests/acceptance/generate-test.js | 10 ++++++++++ tests/unit/commands/generate-test.js | 7 ++----- 4 files changed, 39 insertions(+), 12 deletions(-) diff --git a/lib/commands/generate.js b/lib/commands/generate.js index 6d488abe52e..d650885afa0 100644 --- a/lib/commands/generate.js +++ b/lib/commands/generate.js @@ -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.', @@ -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 = { @@ -165,3 +164,7 @@ module.exports = Command.extend({ } }, }); + +module.exports.ERRORS = { + UNKNOWN_BLUEPRINT_ERROR, +}; diff --git a/lib/models/blueprint.js b/lib/models/blueprint.js index 0736c9753f9..731bb77e51a 100644 --- a/lib/models/blueprint.js +++ b/lib/models/blueprint.js @@ -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; diff --git a/tests/acceptance/generate-test.js b/tests/acceptance/generate-test.js index bbf16bb516d..9ed8a7eba5a 100755 --- a/tests/acceptance/generate-test.js +++ b/tests/acceptance/generate-test.js @@ -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(); diff --git a/tests/unit/commands/generate-test.js b/tests/unit/commands/generate-test.js index 6a978151c85..1c131633781 100644 --- a/tests/unit/commands/generate-test.js +++ b/tests/unit/commands/generate-test.js @@ -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; @@ -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); }); });