Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DX-185] Removed NPM as dependency #686

Merged
merged 2 commits into from
Oct 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool here we could hook with yarn support easily in the future

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 =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems like a lot of duplication just to handle a single vs multiple deps install, coudln't we make into a single method?

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