Skip to content

Commit

Permalink
Implicitly add an npm dev dependency on bower
Browse files Browse the repository at this point in the history
For scenarios that
  1. do not already have a dependency or dev dependency on bower and
  2. have bower dependencies

Also assert that
  1. npm is ran before bower
  2. if bower is run, a local bower is found
  • Loading branch information
hjdivad committed Nov 8, 2017
1 parent 83529e8 commit 2a56af3
Show file tree
Hide file tree
Showing 6 changed files with 291 additions and 111 deletions.
11 changes: 0 additions & 11 deletions lib/dependency-manager-adapters/bower.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,6 @@ module.exports = CoreObject.extend({
return adapter._findBowerPath(adapter.cwd).then(function(bowerPath) {
debug('Run bower install using bower at %s', bowerPath);
return adapter.run('node', [].concat([bowerPath], commandParts, options), { cwd: adapter.cwd });
}).catch(function() {
debug('Local bower not found');
return adapter._checkForGlobalBower().then(function() {
debug('Using global bower');
return adapter.run('bower', [].concat(commandParts, options), { cwd: adapter.cwd });
});
});
},
_checkForGlobalBower: function() {
return this.run('bower', ['-v'], { stdio: 'ignore' }).catch(function() {
throw new Error('Bower must be installed either locally or globally to have bower scenarios');
});
},
_bowerJSONForDependencySet: function(bowerJSON, depSet) {
Expand Down
64 changes: 59 additions & 5 deletions lib/utils/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ var RSVP = require('rsvp');
var findByName = require('./find-by-name');
var debug = require('debug')('ember-try:utils:config');

function config(options) {
var IMPLICIT_BOWER_VERSION = '^1.8.2';

function getBaseConfig(options) {
var relativePath = options.configPath || path.join('config', 'ember-try.js');
var configFile = path.join(options.project.root, relativePath);
var configData;
Expand Down Expand Up @@ -39,8 +41,39 @@ function config(options) {
}
}

function config(options) {
return getBaseConfig(options).then(function(configData) {
return addImplicitBowerToScenarios(configData);
});
}

module.exports = config;

function addImplicitBowerToScenarios(configData) {
configData.scenarios.forEach(function(scenario) {
if (!('bower' in scenario)) {
// Don't do anything for scenarios that don't include bower
return;
}

if ('npm' in scenario) {
var npm = scenario.npm;
if ((npm.dependencies && npm.dependencies.bower) ||
(npm.devDependencies && npm.devDependencies.bower)) {
// Dont' do anything for scenarios that already include bower in npm,
// either as a dependency or a dev dependency
return;
}
}

// add an implicit bower dev dependency to npm for this scenario
scenario.npm = scenario.npm || {};
scenario.npm.devDependencies = scenario.npm.devDependencies || {};
scenario.npm.devDependencies.bower = IMPLICIT_BOWER_VERSION;
});
return configData;
}

function mergeAutoConfigAndConfigFileData(autoConfig, configData) {
configData = configData || {};
configData.scenarios = configData.scenarios || [];
Expand Down Expand Up @@ -77,35 +110,56 @@ function defaultConfig() {
dependencies: { } /* No dependencies needed as the
default is already specified in
the consuming app's bower.json */
}
},
npm: {
devDependencies: {
bower: IMPLICIT_BOWER_VERSION,
}
},
},
{
name: 'ember-release',
bower: {
dependencies: {
ember: 'release'
}
}
},
npm: {
devDependencies: {
bower: IMPLICIT_BOWER_VERSION,
}
},
},
{
name: 'ember-beta',
bower: {
dependencies: {
ember: 'beta'
}
}
},
npm: {
devDependencies: {
bower: IMPLICIT_BOWER_VERSION,
}
},
},
{
name: 'ember-canary',
bower: {
dependencies: {
ember: 'canary'
}
}
},
npm: {
devDependencies: {
bower: IMPLICIT_BOWER_VERSION,
}
},
}
]
};
}

// Used for internal testing purposes.
module.exports._defaultConfig = defaultConfig;
module.exports._addImplicitBowerToScenarios = addImplicitBowerToScenarios;
52 changes: 7 additions & 45 deletions test/dependency-manager-adapters/bower-adapter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,33 +121,23 @@ describe('bowerAdapter', function() {
return new BowerAdapter({ cwd: tmpdir, run: stubbedRun })._install();
});

it('runs bower install with global bower if local bower is not found', function() {
var actualCommand, actualArgs, actualOpts;

it('rejects if local bower is not found', function() {
var doNotFindLocalBower = function() {
return RSVP.reject();
};

var findGlobalBower = function() {
return RSVP.resolve();
return RSVP.reject('no local bower found');
};

var stubbedRun = function(command, args, opts) {
actualCommand = command;
actualArgs = args;
actualOpts = opts;
return RSVP.resolve();
var stubbedRun = function() {
return RSVP.reject();
};

return new BowerAdapter({
cwd: tmpdir,
_findBowerPath: doNotFindLocalBower,
_checkForGlobalBower: findGlobalBower,
run: stubbedRun
})._install().then(function() {
expect(actualCommand).to.equal('bower');
expect(actualArgs).to.eql(['install', '--config.interactive=false']);
expect(actualOpts).to.have.property('cwd', tmpdir);
expect.fail(true, false, 'unreachable: _install promise rejects');
}, function(error) {
expect(error).to.equal('no local bower found');
});
});

Expand Down Expand Up @@ -283,34 +273,6 @@ describe('bowerAdapter', function() {
});
});
});

describe('#_checkForGlobalBower()', function() {
it('runs bower -v to see if bower exists', function() {
let actualCommand, actualArgs, actualOpts;
var stubbedRun = function(command, args, opts) {
actualCommand = command;
actualArgs = args;
actualOpts = opts;
return RSVP.resolve();
};

return new BowerAdapter({ cwd: tmpdir, run: stubbedRun })._checkForGlobalBower().then(function() {
expect(actualCommand).to.equal('bower');
expect(actualArgs).to.eql(['-v']);
expect(actualOpts.stdio).to.equal('ignore');
});
});

it('throws if running bower -v fails', function() {
var stubbedRun = function() {
return RSVP.reject();
};

return new BowerAdapter({ cwd: tmpdir, run: stubbedRun })._checkForGlobalBower().catch(function(err) {
expect(err).to.match(/Bower must be installed either locally or globally to have bower scenarios/);
});
});
});
});

function assertFileContainsJSON(filename, expectedObj) {
Expand Down
Loading

0 comments on commit 2a56af3

Please sign in to comment.