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

Add cli argument to skip dependencies packaging #1191

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
"semi": true,
"singleQuote": true,
"useTabs": false,
"arrowParens": "always"
"arrowParens": "avoid"
}
5 changes: 5 additions & 0 deletions src/cli/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ module.exports = {
username: {
description:
'username used to authenticate when publishing to registry'
},
skipPackage: {
boolean: true,
description: 'Skip packaging step',
default: false
}
},
description: 'Publish a component',
Expand Down
1 change: 0 additions & 1 deletion src/cli/domain/local.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const fs = require('fs-extra');
const path = require('path');
const targz = require('targz');
const _ = require('lodash');

Expand Down
115 changes: 71 additions & 44 deletions src/cli/facade/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const async = require('async');
const colors = require('colors/safe');
const format = require('stringformat');
const path = require('path');
const fs = require('fs-extra');
const read = require('read');
const _ = require('lodash');

Expand All @@ -18,13 +19,17 @@ module.exports = function(dependencies) {

return function(opts, callback) {
const componentPath = opts.componentPath,
skipPackage = opts.skipPackage,
packageDir = path.resolve(componentPath, '_package'),
compressedPackagePath = path.resolve(componentPath, 'package.tar.gz');

let errorMessage;

callback = wrapCliCallback(callback);

const readPackageJson = cb =>
fs.readJson(path.join(packageDir, 'package.json'), cb);

const getCredentials = function(cb) {
if (opts.username && opts.password) {
logger.ok(strings.messages.cli.USING_CREDS);
Expand All @@ -42,12 +47,17 @@ module.exports = function(dependencies) {
});
};

const compress = cb => {
local.compress(packageDir, compressedPackagePath, cb);
};

const packageAndCompress = function(cb) {
logger.warn(format(strings.messages.cli.PACKAGING, packageDir));
const packageOptions = {
production: true,
componentPath: path.resolve(componentPath)
};

local.package(packageOptions, (err, component) => {
if (err) {
return cb(err);
Expand All @@ -57,7 +67,7 @@ module.exports = function(dependencies) {
format(strings.messages.cli.COMPRESSING, compressedPackagePath)
);

local.compress(packageDir, compressedPackagePath, err => {
compress(err => {
if (err) {
return cb(err);
}
Expand Down Expand Up @@ -129,61 +139,78 @@ module.exports = function(dependencies) {
});
};

const publishToRegistries = (registryLocations, component) => {
async.eachSeries(
registryLocations,
(registryUrl, next) => {
const registryNormalised = registryUrl.replace(/\/$/, ''),
componentRoute = `${registryNormalised}/${component.name}/${component.version}`;
putComponentToRegistry(
{ route: componentRoute, path: compressedPackagePath },
next
);
},
err => {
local.cleanup(compressedPackagePath, (err2, res) => {
if (err) {
return callback(err);
}
callback(err2, res);
});
}
);
};

registry.get((err, registryLocations) => {
if (err) {
logger.err(err);
return callback(err);
}

handleDependencies(
{ components: [path.resolve(componentPath)], logger },
(err, dependencies) => {
if (err) {
logger.err(err);
return callback(err);
}
packageAndCompress((err, component) => {
if (!skipPackage) {
handleDependencies(
{ components: [path.resolve(componentPath)], logger },
err => {
if (err) {
errorMessage = format(
strings.errors.cli.PACKAGE_CREATION_FAIL,
err
);
logger.err(errorMessage);
return callback(errorMessage);
logger.err(err);
return callback(err);
}

async.eachSeries(
registryLocations,
(registryUrl, next) => {
const registryLength = registryUrl.length,
registryNormalised =
registryUrl.slice(registryLength - 1) === '/'
? registryUrl.slice(0, registryLength - 1)
: registryUrl,
componentRoute = format(
'{0}/{1}/{2}',
registryNormalised,
component.name,
component.version
);

putComponentToRegistry(
{ route: componentRoute, path: compressedPackagePath },
next
packageAndCompress((err, component) => {
if (err) {
errorMessage = format(
strings.errors.cli.PACKAGE_CREATION_FAIL,
err
);
},
err => {
local.cleanup(compressedPackagePath, (err2, res) => {
if (err) {
return callback(err);
}
callback(err2, res);
});
logger.err(errorMessage);
return callback(errorMessage);
}
);

publishToRegistries(registryLocations, component);
});
}
);
} else {
if (fs.existsSync(packageDir)) {
readPackageJson((err, component) => {
if (err) {
logger.err(err);
return callback(err);
}
compress(err => {
if (err) {
logger.err(err);
return callback(err);
}

publishToRegistries(registryLocations, component);
});
});
} else {
errorMessage = strings.errors.cli.PACKAGE_FOLDER_MISSING;
logger.err(errorMessage);
return callback(errorMessage);
}
);
}
});
};
};
3 changes: 2 additions & 1 deletion src/resources/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ ${yellow(validMockObject)}`;

const initSuccess = (componentName, componentPath) => {
const success = `Success! Created ${componentName} at ${componentPath}`;
return `${green(success)}
return `${green(success)}

From here you can run several commands

Expand Down Expand Up @@ -173,6 +173,7 @@ module.exports = {
'the version of used OC CLI is invalid. Try to upgrade OC CLI running {0}',
PACKAGE_CREATION_FAIL: 'An error happened when creating the package: {0}',
PACKAGING_FAIL: 'an error happened while packaging {0}: {1}',
PACKAGE_FOLDER_MISSING: 'Could not find a _package folder to publish',
PLUGIN_MISSING_FROM_REGISTRY:
'Looks like you are trying to use a plugin in the dev mode ({0}).\nYou need to mock it doing {1}',
PORT_IS_BUSY:
Expand Down
89 changes: 71 additions & 18 deletions test/unit/cli-facade-publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,41 @@ describe('cli : facade : publish', () => {
Local = require('../../src/cli/domain/local'),
local = new Local(),
readStub = sinon.stub().yields(null, 'test'),
PublishFacade = injectr('../../src/cli/facade/publish.js', {
read: readStub
}),
publishFacade = new PublishFacade({
registry,
local,
logger: logSpy
});

const execute = function(cb, creds) {
creds = creds || {};
mockComponent = {
name: 'hello-world',
version: '1.0.0'
};

const execute = function(
cb,
{ creds = {}, skipPackage = false, fs = {} } = {}
) {
logSpy.err = sinon.stub();
logSpy.log = sinon.stub();
logSpy.ok = sinon.stub();
logSpy.warn = sinon.stub();
const fsMock = Object.assign(
{
existsSync: sinon.stub().returns(true),
readJson: sinon.stub().yields(null, mockComponent)
},
fs
),
PublishFacade = injectr('../../src/cli/facade/publish.js', {
'fs-extra': fsMock,
read: readStub
}),
publishFacade = new PublishFacade({
registry,
local,
logger: logSpy
});
publishFacade(
{
componentPath: 'test/fixtures/components/hello-world/',
username: creds.username,
password: creds.password
password: creds.password,
skipPackage
},
() => {
cb();
Expand Down Expand Up @@ -106,10 +121,7 @@ describe('cli : facade : publish', () => {

describe('when a component is valid', () => {
beforeEach(() => {
sinon.stub(local, 'package').yields(null, {
name: 'hello-world',
version: '1.0.0'
});
sinon.stub(local, 'package').yields(null, mockComponent);
});

afterEach(() => {
Expand Down Expand Up @@ -279,8 +291,10 @@ describe('cli : facade : publish', () => {
beforeEach(done => {
sinon.stub(registry, 'putComponent').yields('Unauthorized');
execute(done, {
username: 'myuser',
password: 'password'
creds: {
username: 'myuser',
password: 'password'
}
});
});

Expand Down Expand Up @@ -323,6 +337,45 @@ describe('cli : facade : publish', () => {
});
});
});
describe('when skipping packaging', () => {
beforeEach(() => {
sinon.stub(local, 'compress').yields(null);
});

afterEach(() => {
local.compress.restore();
});

it('should publish the package to all registries', done => {
sinon.stub(registry, 'putComponent').yields(null, 'ok');
execute(
() => {
registry.putComponent.restore();

expect(logSpy.ok.args[0][0]).to.include('http://www.api.com');
expect(logSpy.ok.args[1][0]).to.include('http://www.api2.com');
done();
},
{ skipPackage: true }
);
});
it('should show an error message if the package folder does not exist', done => {
execute(
() => {
expect(logSpy.err.args[0][0]).to.equal(
'Could not find a _package folder to publish'
);
done();
},
{
skipPackage: true,
fs: {
existsSync: sinon.stub().returns(false)
}
}
);
});
});
});
});
});