Skip to content

Commit

Permalink
Merge 331adc2 into 767ba80
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanKingston committed Apr 13, 2015
2 parents 767ba80 + 331adc2 commit dd0b0aa
Show file tree
Hide file tree
Showing 2 changed files with 211 additions and 0 deletions.
54 changes: 54 additions & 0 deletions lib/models/blueprint.js
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,60 @@ Blueprint.prototype.addPackagesToProject = function(packages) {
});
};

/**
Used to remove a package from the projects `package.json`.
Generally, this would be done from the `afterInstall` hook, to
ensure that any package conflicts can be resolved before the
addon is used.
@method removePackageFromProject
@param {String} packageName
@return {Promise}
*/
Blueprint.prototype.removePackageFromProject = function(packageName) {
var packageObject = {name: packageName};

return this.removePackagesFromProject([packageObject]);
};

/**
Used to remove multiple packages from the projects `package.json`.
Generally, this would be done from the `afterInstall` hook, to
ensure that a package that is required by a given blueprint is
available.
Expects each array item to be an object with a `name`. Each object
may optionally have a `target` to specify a specific version.
@method removePackagesFromProject
@param {Array} packages
@return {Promise}
*/
Blueprint.prototype.removePackagesFromProject = function(packages) {
var task = this.taskFor('npm-uninstall');
var install = (packages.length > 1) ? 'uninstall packages' : 'uninstall package';
var packageNames = [];
var packageArray = [];

for (var i = 0; i < packages.length; i++) {
packageNames.push(packages[i].name);

var packageNameAndVersion = packages[i].name;

packageArray.push(packageNameAndVersion);
}

this._writeStatusToUI(chalk.green, install, packageNames.join(', '));

return task.run({
'save-dev': true,
verbose: false,
packages: packageArray
});
};

/**
Used to add a package to the projects `bower.json`.
Expand Down
157 changes: 157 additions & 0 deletions tests/unit/models/blueprint-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,163 @@ describe('Blueprint', function() {
});
});

describe('removePackageFromProject', function() {
var blueprint;
var ui;
var tmpdir;
var NpmUninstallTask;
var taskNameLookedUp;

beforeEach(function() {
tmpdir = tmp.in(tmproot);
blueprint = new Blueprint(basicBlueprint);
ui = new MockUI();

blueprint.taskFor = function(name) {
taskNameLookedUp = name;

return new NpmUninstallTask();
};
});

afterEach(function() {
return remove(tmproot);
});

it('looks up the `npm-uninstall` task', function() {
NpmUninstallTask = Task.extend({
run: function() {}
});

blueprint.removePackageFromProject({name: 'foo-bar'});

expect(taskNameLookedUp).to.equal('npm-uninstall');
});

});

describe('removePackagesFromProject', function() {
var blueprint;
var ui;
var tmpdir;
var NpmUninstallTask;
var taskNameLookedUp;

beforeEach(function() {
tmpdir = tmp.in(tmproot);
blueprint = new Blueprint(basicBlueprint);
ui = new MockUI();

blueprint.taskFor = function(name) {
taskNameLookedUp = name;

return new NpmUninstallTask();
};
});

afterEach(function() {
return remove(tmproot);
});

it('looks up the `npm-uninstall` task', function() {
NpmUninstallTask = Task.extend({
run: function() {}
});

blueprint.removePackagesFromProject([{name: 'foo-bar'}]);

expect(taskNameLookedUp).to.equal('npm-uninstall');
});

it('calls the task with package names', function() {
var packages;

NpmUninstallTask = Task.extend({
run: function(options) {
packages = options.packages;
}
});

blueprint.removePackagesFromProject([
{name: 'foo-bar'},
{name: 'bar-foo'}
]);

expect(packages).to.deep.equal(['foo-bar', 'bar-foo']);
});

it('writes information to the ui log for a single package', function() {
blueprint.ui = ui;

blueprint.removePackagesFromProject([
{name: 'foo-bar'}
]);

var output = ui.output.trim();

expect(output).to.match(/uninstall package.*foo-bar/);
});

it('writes information to the ui log for multiple packages', function() {
blueprint.ui = ui;

blueprint.removePackagesFromProject([
{name: 'foo-bar'},
{name: 'bar-foo'}
]);

var output = ui.output.trim();

expect(output).to.match(/uninstall packages.*foo-bar, bar-foo/);
});

it('does not error if ui is not present', function() {
delete blueprint.ui;

blueprint.removePackagesFromProject([
{name: 'foo-bar'}
]);

var output = ui.output.trim();

expect(output).to.not.match(/uninstall package.*foo-bar/);
});

it('runs task with --save-dev', function() {
var saveDev;

NpmUninstallTask = Task.extend({
run: function(options) {
saveDev = options['save-dev'];
}
});

blueprint.removePackagesFromProject([
{name: 'foo-bar'},
{name: 'bar-foo'}
]);

expect(!!saveDev).to.equal(true);
});

it('does not use verbose mode with the task', function() {
var verbose;

NpmUninstallTask = Task.extend({
run: function(options) {
verbose = options.verbose;
}
});

blueprint.removePackagesFromProject([
{name: 'foo-bar'},
{name: 'bar-foo'}
]);

expect(verbose).to.equal(false);
});
});

describe('addBowerPackageToProject', function() {
var blueprint;
var ui;
Expand Down

0 comments on commit dd0b0aa

Please sign in to comment.