Skip to content

Commit

Permalink
Merge pull request #686 from opentable/npm
Browse files Browse the repository at this point in the history
[DX-185] Removed NPM as dependency
  • Loading branch information
nickbalestra authored Oct 3, 2017
2 parents 85465c8 + a97ac4d commit 51fa02c
Show file tree
Hide file tree
Showing 6 changed files with 230 additions and 114 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
"multer": "^1.3.0",
"nice-cache": "0.0.5",
"node-dir": "0.1.17",
"npm": "5.3.0",
"oc-client": "2.1.31",
"oc-client-browser": "1.0.4",
"oc-get-unix-utc-timestamp": "1.0.1",
Expand Down
15 changes: 12 additions & 3 deletions src/cli/domain/load-dependencies.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
'use strict';

const format = require('stringformat');
const path = require('path');
const _ = require('lodash');

const getComponentsDependencies = require('./get-components-deps');
const getMissingDeps = require('./get-missing-deps');
const npmInstaller = require('./npm-installer');
const npm = require('../../utils/npm-utils');
const strings = require('../../resources/index');
const wrapCliCallback = require('../wrap-cli-callback');

module.exports = function loadDependencies({ components, logger }, cb) {
logger.warn(strings.messages.cli.CHECKING_DEPENDENCIES, true);
Expand Down Expand Up @@ -35,7 +37,14 @@ function installMissingDeps({ missing, logger }, cb) {
}

logger.warn(format(strings.messages.cli.INSTALLING_DEPS, missing.join(', ')));
npmInstaller(missing, err => {

const options = {
dependencies: missing,
installPath: path.resolve('.'),
save: false
};

npm.installDependencies(options, err => {
if (err) {
logger.err(err.toString());
throw err;
Expand Down
13 changes: 0 additions & 13 deletions src/cli/domain/npm-installer.js

This file was deleted.

71 changes: 53 additions & 18 deletions src/utils/npm-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,66 @@
const path = require('path');
const spawn = require('cross-spawn');

module.exports = {
installDependency: (options, callback) => {
const { dependency, installPath, isDev, save } = options;
const buildInstallCommand = options => {
const args = ['install'];

if (options.save) {
args.push('--save-exact');
args.push(options.isDev ? '--save-dev' : '--save');
}

const args = ['install'];
return args;
};

const executeCommand = (options, callback) => {
const cmd = spawn('npm', options.command, {
cwd: options.installPath,
stdio: 'inherit'
});

cmd.on('error', () => callback('error'));
cmd.on('close', code => callback(code !== 0 ? code : null));
};

if (save) {
args.push('--save-exact');
args.push(isDev ? '--save-dev' : '--save');
}
const moduleName = dependency => dependency.split('@')[0];

const cmd = spawn('npm', [...args, dependency], {
cwd: installPath,
stdio: 'inherit'
});
module.exports = {
installDependencies: (options, callback) => {
const { dependencies, installPath } = options;
const npmi = buildInstallCommand(options);
const cmdOptions = { installPath, command: [...npmi, ...dependencies] };

executeCommand(cmdOptions, err =>
callback(
err,
err
? null
: {
dest: dependencies.map(dependency =>
path.join(installPath, 'node_modules', moduleName(dependency))
)
}
)
);
},
installDependency: (options, callback) => {
const { dependency, installPath } = options;
const npmi = buildInstallCommand(options);
const cmdOptions = { installPath, command: [...npmi, dependency] };

cmd.on('error', () => callback('error'));
cmd.on('close', code => {
const err = code !== 0 ? code : null;
executeCommand(cmdOptions, err =>
callback(
err,
err
? null
: { dest: path.join(installPath, 'node_modules', dependency) }
);
});
: {
dest: path.join(
installPath,
'node_modules',
moduleName(dependency)
)
}
)
);
}
};
31 changes: 11 additions & 20 deletions test/unit/cli-facade-dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,46 @@ const expect = require('chai').expect;
const sinon = require('sinon');

describe('cli : facade : dev', () => {

const logSpy = {},
DevFacade = require('../../src/cli/facade/dev'),
Local = require('../../src/cli/domain/local'),
local = new Local(),
npm = require('npm'),
devFacade = new DevFacade({ local: local, logger: logSpy });
devFacade = new DevFacade({ local, logger: logSpy });

const execute = function(dirName, port){
const execute = function(dirName, port) {
logSpy.err = sinon.spy();
logSpy.warn = () => {};
devFacade({ dirName: dirName, port: port }, () => {});
devFacade({ dirName, port }, () => {});
};

describe('when running a dev version of the registry', () => {

describe('when the directory is not found', () => {

beforeEach(() => {
sinon.stub(npm, 'load').yields(undefined);
sinon.stub(local, 'getComponentsByDir').yields(null, []);
execute();
});

afterEach(() => {
npm.load.restore();
local.getComponentsByDir.restore();
});
afterEach(() => local.getComponentsByDir.restore());

it('should show an error', () => {
expect(logSpy.err.args[0][0]).to.equal('An error happened when initialising the dev runner: no components found in specified path');
expect(logSpy.err.args[0][0]).to.equal(
'An error happened when initialising the dev runner: no components found in specified path'
);
});
});

describe('when the directory does not contain any valid component', () => {

beforeEach(() => {
sinon.stub(npm, 'load').yields(undefined);
sinon.stub(local, 'getComponentsByDir').yields(null, []);
execute();
});

afterEach(() => {
npm.load.restore();
local.getComponentsByDir.restore();
});
afterEach(() => local.getComponentsByDir.restore());

it('should show an error', () => {
expect(logSpy.err.args[0][0]).to.equal('An error happened when initialising the dev runner: no components found in specified path');
expect(logSpy.err.args[0][0]).to.equal(
'An error happened when initialising the dev runner: no components found in specified path'
);
});
});
});
Expand Down
Loading

0 comments on commit 51fa02c

Please sign in to comment.