Skip to content

Commit

Permalink
update installModule
Browse files Browse the repository at this point in the history
  • Loading branch information
killmenot committed Sep 26, 2018
1 parent 83bc41d commit 514a5dd
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 26 deletions.
25 changes: 15 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const { spawn, execSync } = require('child_process');
const fs = require('fs-extra');
const sudo = require('sudo');
const clc = require('cli-color');
const moduleExists = require('module-exists');
const debug = require('debug')('browl-util');

const utils = {};
Expand Down Expand Up @@ -178,18 +177,24 @@ utils.copy = function (source, destination, callback) {
return promise;
};

utils.installModule = function (moduleName) {
debug('installing %s module', moduleName);
utils.installModule = function (moduleName, options) {
debug('installing %s module; options = %j', moduleName, options);

const index = moduleName.indexOf('/');
const moduleNameToCheck = index > -1 ?
moduleName.slice(index + 1) :
moduleName;
options = options || {};

const cmd = ['npm install'];

if (options.cwd) {
cmd.push('--prefix', options.cwd);
}

if (!moduleExists(moduleNameToCheck)) {
utils.log(`Installing ${moduleName}...`);
execSync(`npm install --no-save ${moduleName}`, {stdio:[0,1,2]});
if (options.global) {
cmd.push('--global');
}

cmd.push(moduleName);

execSync(cmd.join(' '), { stdio:[0, 1, 2] });
};

module.exports = utils;
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"debug": "^4.0.1",
"fs-extra": "^7.0.0",
"lodash": "^4.17.11",
"module-exists": "^0.2.1",
"sudo": "^1.0.3"
},
"devDependencies": {
Expand Down
62 changes: 47 additions & 15 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ describe('browl-util', () => {

it('should accept sudo, name, args, options and callback arguments', (done) => {
utils.run(true, 'make', ['install'], options, () => {
expect(sudoStub).be.calledWith(['make', 'install'], {
expect(sudoStub).calledWith(['make', 'install'], {
spawnOptions: {
foo: 'bar'
}
Expand All @@ -95,7 +95,7 @@ describe('browl-util', () => {

it('should accept name, args, options and callback arguments', (done) => {
utils.run('make', ['install'], options, () => {
expect(spawnStub).be.calledWith('make', ['install'], {
expect(spawnStub).calledWith('make', ['install'], {
foo: 'bar'
});
done();
Expand All @@ -104,21 +104,21 @@ describe('browl-util', () => {

it('should accept sudo, name, args and callback arguments', (done) => {
utils.run(true, 'make', ['install'], () => {
expect(sudoStub).be.calledWith(['make', 'install'], {});
expect(sudoStub).calledWith(['make', 'install'], {});
done();
});
});

it('should accept name, args and callback arguments', (done) => {
utils.run('make', ['install'], () => {
expect(spawnStub).be.calledWith('make', ['install'], {});
expect(spawnStub).calledWith('make', ['install'], {});
done();
});
});

it('should accept sudo name, options and callback arguments', (done) => {
utils.run(true, 'make', options, () => {
expect(sudoStub).be.calledWith(['make'], {
expect(sudoStub).calledWith(['make'], {
spawnOptions: {
foo: 'bar'
}
Expand All @@ -129,7 +129,7 @@ describe('browl-util', () => {

it('should accept name, options and callback arguments', (done) => {
utils.run('make', options, () => {
expect(spawnStub).be.calledWith('make', [], {
expect(spawnStub).calledWith('make', [], {
foo: 'bar'
});
done();
Expand All @@ -138,17 +138,17 @@ describe('browl-util', () => {

it('should accept sudo, name and args arguments', () => {
utils.run(true, 'make', ['install']);
expect(sudoStub).be.calledWith(['make', 'install'], {});
expect(sudoStub).calledWith(['make', 'install'], {});
});

it('should accept name and args arguments', () => {
utils.run('make', ['install']);
expect(spawnStub).be.calledWith('make', ['install'], {});
expect(spawnStub).calledWith('make', ['install'], {});
});

it('should accept sudo, name and options arguments', () => {
utils.run(true, 'make', options);
expect(sudoStub).be.calledWith(['make'], {
expect(sudoStub).calledWith(['make'], {
spawnOptions: {
foo: 'bar'
}
Expand All @@ -157,35 +157,35 @@ describe('browl-util', () => {

it('should accept name and options arguments', () => {
utils.run('make', options);
expect(spawnStub).be.calledWith('make', [], {
expect(spawnStub).calledWith('make', [], {
foo: 'bar'
});
});

it('should accept sudo, name and callback arguments', (done) => {
utils.run(true, 'make', () => {
expect(sudoStub).be.calledWith(['make'], {});
expect(sudoStub).calledWith(['make'], {});
done();
});
});

it('should accept name and callback arguments', (done) => {
utils.run('make', () => {
expect(spawnStub).be.calledWith('make', [], {});
expect(spawnStub).calledWith('make', [], {});
done();
});
});

it('should accept sudo and name arguments', (done) => {
utils.run(true, 'make', () => {
expect(sudoStub).be.calledWith(['make'], {});
expect(sudoStub).calledWith(['make'], {});
done();
});
});

it('should name argument', (done) => {
utils.run('make', () => {
expect(spawnStub).be.calledWith('make', [], {});
expect(spawnStub).calledWith('make', [], {});
done();
});
});
Expand All @@ -198,7 +198,39 @@ describe('browl-util', () => {

it('should call utils.run as sudo', () => {
utils.sudo('arg1', 'arg2', 'arg3');
expect(utils.run).be.calledWith(true, 'arg1', 'arg2', 'arg3');
expect(utils.run).calledWith(true, 'arg1', 'arg2', 'arg3');
});
});

describe('#installModule', () => {
let execSyncStub;
let revert;

beforeEach(() => {
execSyncStub = sandbox.stub();

revert = utils.__set__({
execSync: execSyncStub
});
});

afterEach(() => {
revert();
});

it('should install module', () => {
utils.installModule('sinon');
expect(execSyncStub).calledWith('npm install sinon', { stdio: [0, 1, 2] });
});

it('should install module in specific directory', () => {
utils.installModule('sinon', { cwd: '/path/to/dir' });
expect(execSyncStub).calledWith('npm install --prefix /path/to/dir sinon', { stdio: [0, 1, 2] });
});

it('should install module globally', () => {
utils.installModule('sinon', { global: true });
expect(execSyncStub).calledWith('npm install --global sinon', { stdio: [0, 1, 2] });
});
});
});

0 comments on commit 514a5dd

Please sign in to comment.