From 7e8de8197ee5b94d863b74afd764c30b9c41b270 Mon Sep 17 00:00:00 2001 From: Matteo Figus Date: Thu, 26 Nov 2015 10:30:12 +0000 Subject: [PATCH 1/2] removing info, ls, link, unlink --- cli/commands.js | 34 ------- cli/domain/link.js | 48 ---------- cli/domain/local.js | 11 +-- cli/domain/registry.js | 30 ------ cli/domain/unlink.js | 19 ---- cli/facade/info.js | 95 ------------------- cli/facade/link.js | 21 ----- cli/facade/ls.js | 77 ---------------- cli/facade/unlink.js | 21 ----- package.json | 1 - resources/index.js | 7 +- test/unit/cli-domain-link.js | 162 --------------------------------- test/unit/cli-domain-unlink.js | 65 ------------- test/unit/cli-facade-info.js | 137 ---------------------------- test/unit/cli-facade-link.js | 54 ----------- test/unit/cli-facade-ls.js | 108 ---------------------- test/unit/cli-facade-unlink.js | 51 ----------- 17 files changed, 2 insertions(+), 939 deletions(-) delete mode 100644 cli/domain/link.js delete mode 100644 cli/domain/unlink.js delete mode 100644 cli/facade/info.js delete mode 100644 cli/facade/link.js delete mode 100644 cli/facade/ls.js delete mode 100644 cli/facade/unlink.js delete mode 100644 test/unit/cli-domain-link.js delete mode 100644 test/unit/cli-domain-unlink.js delete mode 100644 test/unit/cli-facade-info.js delete mode 100644 test/unit/cli-facade-link.js delete mode 100644 test/unit/cli-facade-ls.js delete mode 100644 test/unit/cli-facade-unlink.js diff --git a/cli/commands.js b/cli/commands.js index 84cffaf09..b0f7defbf 100644 --- a/cli/commands.js +++ b/cli/commands.js @@ -16,8 +16,6 @@ module.exports = { } }, - info: { help: 'Shows installed components on the current project' }, - init: { help: 'Creates a new empty component in the current folder', options: { @@ -32,29 +30,6 @@ module.exports = { } }, - link: { - help: 'Links a component in the current project', - options: { - componentName: { - help: 'The name of the component to link. to see the list of available components' - }, - componentVersion: { - help: 'The specific version of the component to link. Default is the latest', - required: false - } - } - }, - - ls: { - help: 'Shows the list of the available components for a linked oc registry', - options: { - registry: { - help: 'Specify registry to query', - required: false - } - } - }, - mock: { help: 'Allows to mock configuration in order to facilitate local development', options: { @@ -101,15 +76,6 @@ module.exports = { } }, - unlink: { - help: 'Unlinks a component from the current project', - options: { - componentName: { - help: 'The name of the component to unlink. to see the list of linked components' - } - } - }, - version: { help: 'Shows the cli version', flag: true diff --git a/cli/domain/link.js b/cli/domain/link.js deleted file mode 100644 index 5da242795..000000000 --- a/cli/domain/link.js +++ /dev/null @@ -1,48 +0,0 @@ -'use strict'; - -var fs = require('fs-extra'); -var format = require('stringformat'); -var request = require('../../utils/request'); -var settings = require('../../resources/settings'); - -module.exports = function(){ - return function(componentName, componentVersion, callback){ - - fs.readJson(settings.configFile.src, function(err, localConfig){ - if(err){ - return callback(err); - } - - if(!localConfig || !localConfig.registries || localConfig.registries.length === 0){ - return callback(new Error('Registry configuration not found. Add a registry reference to the project first')); - } - - localConfig.components = localConfig.components || {}; - - if(!!localConfig.components[componentName]){ - return callback(new Error('Component already linked in the project')); - } - - var componentHref = format('{0}/{1}/{2}', localConfig.registries[0], componentName, componentVersion); - - request(componentHref, function(err, res){ - if(err || !res){ - return callback(new Error('Component not available')); - } - - try { - var apiResponse = JSON.parse(res); - - if(apiResponse.type !== 'oc-component'){ - return callback(new Error('not a valid oc Component')); - } - } catch(e){ - return callback(new Error('not a valid oc Component')); - } - - localConfig.components[componentName] = componentVersion; - fs.writeJson(settings.configFile.src, localConfig, callback); - }); - }); - }; -}; diff --git a/cli/domain/local.js b/cli/domain/local.js index 00212aec5..3fe1ddc04 100644 --- a/cli/domain/local.js +++ b/cli/domain/local.js @@ -1,19 +1,15 @@ 'use strict'; var fs = require('fs-extra'); -var colors = require('colors'); var path = require('path'); var Targz = require('tar.gz'); var _ = require('underscore'); var getComponentsByDir = require('./get-components-by-dir'); var getLocalNpmModules = require('./get-local-npm-modules'); -var link = require('./link'); var packageComponents = require('./package-components'); var mock = require('./mock'); -var settings = require('../../resources/settings'); var validator = require('../../registry/domain/validators'); -var unlink = require('./unlink'); module.exports = function(dependencies){ var logger = dependencies.logger; @@ -28,9 +24,6 @@ module.exports = function(dependencies){ }, getComponentsByDir: getComponentsByDir(dependencies), getLocalNpmModules: getLocalNpmModules(), - info: function(callback){ - return fs.readJson(settings.configFile.src, callback); - }, init: function(componentName, templateType, callback){ if(!validator.validateComponentName(componentName)){ @@ -63,9 +56,7 @@ module.exports = function(dependencies){ return callback(e); } }, - link: link(), mock: mock(), - package: packageComponents(), - unlink: unlink() + package: packageComponents() }); }; diff --git a/cli/domain/registry.js b/cli/domain/registry.js index 251125ca9..aa0a863cc 100644 --- a/cli/domain/registry.js +++ b/cli/domain/registry.js @@ -1,13 +1,11 @@ 'use strict'; -var async = require('async'); var format = require('stringformat'); var fs = require('fs-extra'); var path = require('path'); var _ = require('underscore'); var put = require('../../utils/put'); -var querystring = require('querystring'); var request = require('../../utils/request'); var settings = require('../../resources/settings'); var urlBuilder = require('../../registry/domain/url-builder'); @@ -105,34 +103,6 @@ module.exports = function(opts){ callback(null, previewUrl); }); }, - getRegistryComponentsByRegistry: function(registry, callback){ - - request(registry, requestsOptions, function(err, res){ - if(err || !res){ - return callback('not components found for registry: ' + registry, null); - } - - var components; - - try { - components = JSON.parse(res).components; - } catch(e){ - return callback('Registry api responded in an unexpected way', null); - } - - if(components.length === 0){ - return callback('no components found in oc registry', null); - } - - async.map(components, function(component, cb){ - request(component + settings.registry.componentInfoPath, requestsOptions, function(err, res){ - cb(err, _.extend(JSON.parse(res), { - href: component - })); - }); - }, callback); - }); - }, putComponent: function(options, callback){ var headers = requestsOptions.headers; diff --git a/cli/domain/unlink.js b/cli/domain/unlink.js deleted file mode 100644 index 5d02d6cc9..000000000 --- a/cli/domain/unlink.js +++ /dev/null @@ -1,19 +0,0 @@ -'use strict'; - -var fs = require('fs-extra'); -var settings = require('../../resources/settings'); - -module.exports = function(){ - return function(componentName, callback){ - fs.readJson(settings.configFile.src, function(err, localConfig){ - - localConfig = localConfig || {}; - - if(!!localConfig.components[componentName]){ - delete localConfig.components[componentName]; - } - - fs.writeJson(settings.configFile.src, localConfig, callback); - }); - }; -}; diff --git a/cli/facade/info.js b/cli/facade/info.js deleted file mode 100644 index 33cf42092..000000000 --- a/cli/facade/info.js +++ /dev/null @@ -1,95 +0,0 @@ -'use strict'; - -var async = require('async'); -var colors = require('colors'); -var Table = require('cli-table'); -var _ = require('underscore'); - -var strings = require('../../resources/index'); - -module.exports = function(dependencies){ - - var registry = dependencies.registry, - local = dependencies.local, - logger = dependencies.logger; - - return function(){ - local.info(function(err, res){ - if(err){ - return logger.log(err.red); - } else { - if(res.registries.length === 0){ - return logger.log(strings.errors.cli.REGISTRY_NOT_FOUND.red); - } - if(_.keys(res.components).length === 0){ - return logger.log(strings.errors.cli.COMPONENTS_LINKED_NOT_FOUND.red); - } - - logger.log(strings.messages.cli.COMPONENTS_LINKED_LIST.yellow); - - var componentUrls = _.map(res.components, function(componentVersion, componentName){ - return [res.registries[0] + '/' + componentName + '/' + componentVersion]; - }); - - async.map(componentUrls, function(componentUrl, cb){ - registry.getApiComponentByHref(componentUrl, function(errInfo, resInfo){ - cb(null, [errInfo, resInfo]); - }); - }, function(poo, callbacks){ - - var rows = [], - margin = 2, - namesLength = 0, - versionsLength = 0, - urlsLength = 0; - - _.forEach(callbacks, function(callback, i){ - - var error = callback[0], - nameColumn, - versionColumn, - descriptionColumn, - urlColumn = componentUrls[i][0]; - - if(!!error){ - nameColumn = 'Not available'.red; - versionColumn = descriptionColumn = '-'.red; - } else { - var component = callback[1]; - - nameColumn = component.name, - versionColumn = (component.version === component.requestVersion ? component.version : component.requestVersion + ' => ' + component.version), - descriptionColumn = component.description; - } - - if(nameColumn.length > namesLength){ - namesLength = nameColumn.length; - } - - if(versionColumn.length > versionsLength){ - versionsLength = versionColumn.length; - } - - if(urlColumn.length > urlsLength){ - urlsLength = urlColumn.length; - } - - rows.push([nameColumn, versionColumn, descriptionColumn, urlColumn]); - - }, this); - - var table = new Table({ - head: ['name', 'version', 'description', 'href'], - colWidths: [namesLength + margin, versionsLength + margin, (100 - namesLength - urlsLength - versionsLength), urlsLength + margin] - }); - - _.forEach(rows, function(row){ - table.push(row); - }); - - logger.log(table.toString()); - }); - } - }); - }; -}; \ No newline at end of file diff --git a/cli/facade/link.js b/cli/facade/link.js deleted file mode 100644 index baceaf424..000000000 --- a/cli/facade/link.js +++ /dev/null @@ -1,21 +0,0 @@ -'use strict'; - -var colors = require('colors'); - -var strings = require('../../resources/index'); - -module.exports = function(dependencies){ - - var local = dependencies.local, - logger = dependencies.logger; - - return function(opts){ - local.link(opts.componentName, opts.componentVersion || '', function(err, res){ - if(err){ - return logger.log(err.red); - } else { - return logger.log(strings.messages.cli.COMPONENT_LINKED.green); - } - }); - }; -}; \ No newline at end of file diff --git a/cli/facade/ls.js b/cli/facade/ls.js deleted file mode 100644 index a68afee87..000000000 --- a/cli/facade/ls.js +++ /dev/null @@ -1,77 +0,0 @@ -'use strict'; - -var colors = require('colors'); -var format = require('stringformat'); -var Table = require('cli-table'); -var _ = require('underscore'); - -var strings = require('../../resources/index'); - -module.exports = function(dependencies){ - - var registry = dependencies.registry, - logger = dependencies.logger; - - var printComponents = function (err, components) { - - if(err){ - return logger.log(err.red); - } - - var rows = [], - margin = 2, - namesLength = 0, - versionsLength = 0, - urlsLength = 0; - - _.forEach(components, function(component){ - - if(component.name.length > namesLength){ - namesLength = component.name.length; - } - - if(component.version.length > versionsLength){ - versionsLength = component.version.length; - } - - if(component.href.length > urlsLength){ - urlsLength = component.href.length; - } - - rows.push([component.name, component.version, component.description, component.href]); - }, this); - - var table = new Table({ - head: ['name', 'version', 'description', 'href'], - colWidths: [namesLength + margin, versionsLength + margin, (100 - namesLength - urlsLength - versionsLength), urlsLength + margin] - }); - - _.forEach(rows, function(row){ - table.push(row); - }); - - logger.log(table.toString()); - - }; - - return function(opts){ - - opts = opts || {}; - - if (!opts.registry) { - registry.get(function(err, registryLocations){ - if(!registryLocations || registryLocations.length === 0){ - return logger.log(strings.errors.cli.REGISTRY_NOT_FOUND.red); - } else { - logger.log(format(strings.messages.cli.COMPONENTS_LIST, registryLocations[0]).yellow); - - registry.getRegistryComponentsByRegistry(registryLocations[0], printComponents); - } - }); - } else { - - registry.getRegistryComponentsByRegistry(opts.registry, printComponents); - - } - }; -}; \ No newline at end of file diff --git a/cli/facade/unlink.js b/cli/facade/unlink.js deleted file mode 100644 index 4cb66cd90..000000000 --- a/cli/facade/unlink.js +++ /dev/null @@ -1,21 +0,0 @@ -'use strict'; - -var colors = require('colors'); - -var strings = require('../../resources/index'); - -module.exports = function(dependencies){ - - var local = dependencies.local, - logger = dependencies.logger; - - return function(opts){ - local.unlink(opts.componentName, function(err, res){ - if(err){ - return logger.log(err.red); - } else { - return logger.log(strings.messages.cli.COMPONENT_UNLINKED.green); - } - }); - }; -}; \ No newline at end of file diff --git a/package.json b/package.json index b0fd2415f..344836ee9 100644 --- a/package.json +++ b/package.json @@ -51,7 +51,6 @@ "async": "0.9.0", "aws-sdk": "2.1.43", "clean-css": "3.4.5", - "cli-table": "0.3.1", "colors": "1.0.3", "dependency-graph": "^0.4.1", "detective": "4.0.0", diff --git a/resources/index.js b/resources/index.js index 4a5d4be52..e27ce2cc8 100644 --- a/resources/index.js +++ b/resources/index.js @@ -48,7 +48,6 @@ module.exports = { }, cli: { COMPONENT_HREF_NOT_FOUND: 'The specified path is not a valid component\'s url', - COMPONENTS_LINKED_NOT_FOUND: 'No components linked in the project', COMPONENTS_NOT_FOUND: 'no components found in specified path', FOLDER_IS_NOT_A_FOLDER: '"{0}" must be a directory', FOLDER_NOT_FOUND: '"{0}" not found', @@ -61,7 +60,7 @@ module.exports = { OC_CLI_VERSION_NEEDS_UPGRADE: '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}', - 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}', + 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}', PLUGIN_MISSING_FROM_COMPONENT: 'Looks like you are trying to use a plugin you haven\'t registered ({0}).' + '\nYou need to register it editing your component\'s package.json', PORT_IS_BUSY: 'The port {0} is already in use. Specify the optional port parameter to use another port.', @@ -87,10 +86,6 @@ module.exports = { CHANGES_DETECTED: 'Changes detected on file: {0}', CHECKING_DEPENDENCIES: 'Ensuring dependencies are loaded...', COMPONENT_INITED: 'Component "{0}" created', - COMPONENT_LINKED: 'oc Component linked', - COMPONENT_UNLINKED: 'oc Component unlinked', - COMPONENTS_LINKED_LIST: 'Components linked in project:', - COMPONENTS_LIST: 'Components available in oc registry: {0}', COMPRESSING: 'Compressing -> {0}', ENTER_PASSWORD: 'Enter password:', ENTER_USERNAME: 'Enter username:', diff --git a/test/unit/cli-domain-link.js b/test/unit/cli-domain-link.js deleted file mode 100644 index a76abd406..000000000 --- a/test/unit/cli-domain-link.js +++ /dev/null @@ -1,162 +0,0 @@ -'use strict'; - -var expect = require('chai').expect; -var injectr = require('injectr'); -var path = require('path'); -var sinon = require('sinon'); -var _ = require('underscore'); - -var initialise = function(){ - - var fsMock = { - existsSync: sinon.stub(), - lstatSync: sinon.stub(), - mkdirSync: sinon.spy(), - readdirSync: sinon.stub(), - readFileSync: sinon.stub(), - readJson: sinon.stub(), - readJsonSync: sinon.stub(), - writeFile: sinon.stub().yields(null, 'ok'), - writeJson: sinon.stub().yields(null, 'ok') - }; - - var requestMock = sinon.stub().yields(null, {}); - - var Link = injectr('../../cli/domain/link.js', { - 'fs-extra': fsMock, - '../../utils/request': requestMock - }); - - var local = new Link(); - - return { local: local, fs: fsMock, request: requestMock }; -}; - -var executeLink = function(local, callback){ - return local('my-component', '1.x.x', callback); -}; - -describe('cli : domain : link', function(){ - - describe('when linking a component', function(){ - - var error, result; - beforeEach(function(done){ - - var data = initialise(); - - data.fs.readJson.onCall(0).yields(null, { - registries: [ 'http://my-registry.com'] - }); - - data.request.onCall(0).yields(null, JSON.stringify({ - type: 'oc-component' - })); - - data.fs.writeJson.onCall(0).yields(null); - - executeLink(data.local, function(err, res){ - error = err; - result = data.fs.writeJson.firstCall.args; - done(err); - }); - }); - - it('should add linked component to config file', function(){ - expect(result[1].components).to.eql({ 'my-component': '1.x.x' }); - }); - }); - - describe('when the component is already linked', function(){ - - var error, result; - beforeEach(function(done){ - - var data = initialise(); - - data.fs.readJson.onCall(0).yields(null, { - registries: [ 'http://my-registry.com'], - components: { - 'my-component': '1.x.x' - } - }); - - executeLink(data.local, function(err, res){ - error = err; - done(); - }); - }); - - it('should return an error', function(){ - expect(error.toString()).to.eql('Error: Component already linked in the project'); - }); - }); - - describe('when the component doesn\'t exist', function(){ - - var error, result; - beforeEach(function(done){ - - var data = initialise(); - - data.fs.readJson.onCall(0).yields(null, { - registries: [ 'http://my-registry.com'] - }); - - data.request.onCall(0).yields(new Error('component not found')); - - data.fs.writeJson.onCall(0).yields(null); - - executeLink(data.local, function(err, res){ - error = err; - done(); - }); - }); - - it('should return an error', function(){ - expect(error.toString()).to.eql('Error: Component not available'); - }); - }); - - describe('when the registry is not configured', function(){ - - var error, result; - beforeEach(function(done){ - - var data = initialise(); - - data.fs.readJson.onCall(0).yields(null, { - registries: [] - }); - - executeLink(data.local, function(err, res){ - error = err; - done(); - }); - }); - - it('should return an error', function(){ - expect(error.toString()).to.eql('Error: Registry configuration not found. Add a registry reference to the project first'); - }); - }); - - describe('when the oc.json is missing', function(){ - - var error, result; - beforeEach(function(done){ - - var data = initialise(); - - data.fs.readJson.onCall(0).yields(new Error('file not found')); - - executeLink(data.local, function(err, res){ - error = err; - done(); - }); - }); - - it('should return an error', function(){ - expect(error.toString()).to.eql('Error: file not found'); - }); - }); -}); diff --git a/test/unit/cli-domain-unlink.js b/test/unit/cli-domain-unlink.js deleted file mode 100644 index c62309e89..000000000 --- a/test/unit/cli-domain-unlink.js +++ /dev/null @@ -1,65 +0,0 @@ -'use strict'; - -var expect = require('chai').expect; -var injectr = require('injectr'); -var path = require('path'); -var sinon = require('sinon'); -var _ = require('underscore'); - -var initialise = function(){ - - var fsMock = { - existsSync: sinon.stub(), - lstatSync: sinon.stub(), - mkdirSync: sinon.spy(), - readdirSync: sinon.stub(), - readFileSync: sinon.stub(), - readJson: sinon.stub(), - readJsonSync: sinon.stub(), - writeFile: sinon.stub().yields(null, 'ok'), - writeJson: sinon.stub().yields(null, 'ok') - }; - - var Unlink = injectr('../../cli/domain/unlink.js', { - 'fs-extra': fsMock - }); - - var local = new Unlink(); - - return { local: local, fs: fsMock }; -}; - -var executeUnlink = function(local, callback){ - return local('my-component', callback); -}; - -describe('cli : domain : link', function(){ - - describe('when unlinking a component', function(){ - - var error, result; - beforeEach(function(done){ - - var data = initialise(); - - data.fs.readJson.onCall(0).yields(null, { - registries: [ 'http://my-registry.com'], - components: { - 'my-component': '1.x.x' - } - }); - - data.fs.writeJson.onCall(0).yields(null); - - executeUnlink(data.local, function(err, res){ - error = err; - result = data.fs.writeJson.firstCall.args; - done(err); - }); - }); - - it('should remove linked component from config file', function(){ - expect(result[1].components).to.eql({}); - }); - }); -}); diff --git a/test/unit/cli-facade-info.js b/test/unit/cli-facade-info.js deleted file mode 100644 index 78f133f08..000000000 --- a/test/unit/cli-facade-info.js +++ /dev/null @@ -1,137 +0,0 @@ -'use strict'; - -var colors = require('colors'); -var expect = require('chai').expect; -var sinon = require('sinon'); -var _ = require('underscore'); - -describe('cli : facade : info', function(){ - - var logSpy = {}, - Registry = require('../../cli/domain/registry'), - registry = new Registry(), - Local = require('../../cli/domain/local'), - local = new Local({ logger: { log: function(){} } }), - InfoFacade = require('../../cli/facade/info'), - infoFacade = new InfoFacade({ registry: registry, local: local, logger: logSpy }); - - var setup = function(stubs){ - _.forEach(stubs, function(stubInfo){ - var stub = sinon.stub(stubInfo.obj, stubInfo.method); - _.forEach(stubInfo.responses, function(response, i){ - stub.onCall(i).yields(response.err, response.res); - }); - }); - }; - - var execute = function(){ - logSpy.log = sinon.spy(); - infoFacade(); - }; - - describe('when showing project info', function(){ - - describe('when the information can\'t be retrieved', function(){ - - beforeEach(function(){ - setup([{ obj: local, method: 'info', responses: [{err: 'something bad happened'}]}]); - execute(); - }); - - afterEach(function(){ - local.info.restore(); - }); - - it('should show an error', function(){ - expect(logSpy.log.args[0][0]).to.be.equal('something bad happened'.red); - }); - }); - - describe('when the information is retrieved', function(){ - - describe('when the registry list is blank', function(){ - - beforeEach(function(){ - setup([{ obj: local, method: 'info', responses: [{ res: { registries: [] }}]}]); - execute(); - }); - - afterEach(function(){ - local.info.restore(); - }); - - it('should show an error', function(){ - expect(logSpy.log.args[0][0]).to.be.equal('oc registries not found. Run "oc registry add "'.red); - }); - }); - - describe('when there are no components linked in the project', function(){ - - beforeEach(function(){ - setup([{ obj: local, method: 'info', responses: [{ res: { registries: ['http://registry.com'], components: {}}}]}]); - execute(); - }); - - afterEach(function(){ - local.info.restore(); - }); - - it('should show an error', function(){ - expect(logSpy.log.args[0][0]).to.be.equal('No components linked in the project'.red); - }); - }); - - describe('when there are components linked in the project', function(){ - - beforeEach(function(){ - setup([{ - obj: local, - method: 'info', - responses: [{ - res: { registries: ['http://registry.com'], components: { 'ghost': '1.X.X', 'hello': '~1.0.0' }} - }] - }, { - obj: registry, - method: 'getApiComponentByHref', - responses: [{ - err: 'not_found' - }, { - res: { - name: 'hello', - version: '1.0.1', - requestVersion: '~1.0.0', - description: 'the best component ever', - href: 'http://registry.com/hello/~1.0.0' - } - }] - }]); - - execute(); - }); - - afterEach(function(){ - local.info.restore(); - registry.getApiComponentByHref.restore(); - }); - - it('should list the components', function(){ - expect(logSpy.log.args[0][0]).to.be.equal('Components linked in project:'.yellow); - }); - - it('should show a message when a component is not found on the registry', function(){ - expect(logSpy.log.args[1][0]).to.include('Not available'.red); - }); - - it('should show the component\'s details when it is found on the registry', function(){ - expect(logSpy.log.args[1][0]).to.include('hello'); - expect(logSpy.log.args[1][0]).to.include('the best component ever'); - expect(logSpy.log.args[1][0]).to.include('http://registry.com/hello/~1.0.0'); - }); - - it('should show the component\'s resolved version', function(){ - expect(logSpy.log.args[1][0]).to.include('~1.0.0 => 1.0.1'); - }); - }); - }); - }); -}); diff --git a/test/unit/cli-facade-link.js b/test/unit/cli-facade-link.js deleted file mode 100644 index 82bacf63f..000000000 --- a/test/unit/cli-facade-link.js +++ /dev/null @@ -1,54 +0,0 @@ -'use strict'; - -var colors = require('colors'); -var expect = require('chai').expect; -var sinon = require('sinon'); - -describe('cli : facade : link', function(){ - - var logSpy = {}, - LinkFacade = require('../../cli/facade/link'), - Local = require('../../cli/domain/local'), - local = new Local({ logger: { log: function(){} } }), - linkFacade = new LinkFacade({ local: local, logger: logSpy }); - - var execute = function(){ - logSpy.log = sinon.spy(); - linkFacade({ componentName: 'hello' }); - }; - - describe('when linking component', function(){ - - describe('when an error happens', function(){ - - beforeEach(function(){ - sinon.stub(local, 'link').yields('an error!'); - execute(); - }); - - afterEach(function(){ - local.link.restore(); - }); - - it('should show the error', function(){ - expect(logSpy.log.args[0][0]).to.equal('an error!'.red); - }); - }); - - describe('when it succeeds', function(){ - - beforeEach(function(){ - sinon.stub(local, 'link').yields(null, 'yay'); - execute(); - }); - - afterEach(function(){ - local.link.restore(); - }); - - it('should show a confirmation message', function(){ - expect(logSpy.log.args[0][0]).to.equal('oc Component linked'.green); - }); - }); - }); -}); diff --git a/test/unit/cli-facade-ls.js b/test/unit/cli-facade-ls.js deleted file mode 100644 index c9ef458c3..000000000 --- a/test/unit/cli-facade-ls.js +++ /dev/null @@ -1,108 +0,0 @@ -'use strict'; - -var colors = require('colors'); -var expect = require('chai').expect; -var sinon = require('sinon'); - -describe('cli : facade : ls', function(){ - - var logSpy = {}, - Registry = require('../../cli/domain/registry'), - registry = new Registry(), - LsFacade = require('../../cli/facade/ls'), - lsFacade = new LsFacade({ registry: registry, logger: logSpy }); - - var execute = function(opts){ - logSpy.log = sinon.spy(); - lsFacade(opts); - }; - - describe('when no registries linked to the app', function(){ - - beforeEach(function(){ - sinon.stub(registry, 'get').yields(null, []); - execute(); - }); - - afterEach(function(){ - registry.get.restore(); - }); - - it('should show an error', function(){ - expect(logSpy.log.args[0][0]).to.equal('oc registries not found. Run "oc registry add "'.red); - }); - }); - - describe('when registry is passed on command line', function(){ - - beforeEach(function(){ - sinon.stub(registry, 'get').yields(null, []); - sinon.stub(registry, 'getRegistryComponentsByRegistry').yields(null, []); - execute({registry: 'http://www.registry.com'}); - }); - - afterEach(function(){ - registry.getRegistryComponentsByRegistry.restore(); - registry.get.restore(); - }); - - it('should use the supplied registry', function(){ - expect(registry.get.called).to.equal(false); - }); - }); - - describe('when registry linked to the app', function(){ - - beforeEach(function(){ - sinon.stub(registry, 'get').yields(null, ['http://www.registry.com']); - execute(); - }); - - afterEach(function(){ - registry.get.restore(); - }); - - it('should show the list of components for given registry', function(){ - sinon.stub(registry, 'getRegistryComponentsByRegistry').yields(null, []); - expect(logSpy.log.args[0][0]).to.equal('Components available in oc registry: http://www.registry.com'.yellow); - registry.getRegistryComponentsByRegistry.restore(); - }); - - describe('when no components found', function(){ - - beforeEach(function(){ - sinon.stub(registry, 'getRegistryComponentsByRegistry').yields(null, []); - execute(); - }); - - afterEach(function(){ - registry.getRegistryComponentsByRegistry.restore(); - }); - - it('should show an empty list of components', function(){ - expect(logSpy.log.args[1][0]).not.to.include('http://www.registry.com'); - }); - }); - - describe('when components found', function(){ - - beforeEach(function(){ - sinon.stub(registry, 'getRegistryComponentsByRegistry').yields(null, [{ - name: 'hello', description: 'a description', version: '1.0.0', href: 'http://www.api.com/hello' - }]); - execute(); - }); - - afterEach(function(){ - registry.getRegistryComponentsByRegistry.restore(); - }); - - it('should list the components', function(){ - expect(logSpy.log.args[1][0]).to.include('hello'); - expect(logSpy.log.args[1][0]).to.include('a description'); - expect(logSpy.log.args[1][0]).to.include('1.0.0'); - expect(logSpy.log.args[1][0]).to.include('http://www.api.com/hello'); - }); - }); - }); -}); \ No newline at end of file diff --git a/test/unit/cli-facade-unlink.js b/test/unit/cli-facade-unlink.js deleted file mode 100644 index 77b228679..000000000 --- a/test/unit/cli-facade-unlink.js +++ /dev/null @@ -1,51 +0,0 @@ -'use strict'; - -var colors = require('colors'); -var expect = require('chai').expect; -var sinon = require('sinon'); - -describe('cli : facade : unlink', function(){ - - var logSpy = {}, - Local = require('../../cli/domain/local'), - local = new Local({ logger: { log: function(){} } }), - UnlinkFacade = require('../../cli/facade/unlink'), - unlinkFacade = new UnlinkFacade({ local:local, logger: logSpy }); - - describe('when unlinking component', function(){ - - describe('when an error happens', function(){ - - beforeEach(function(){ - logSpy.log = sinon.spy(); - sinon.stub(local, 'unlink').yields('an error!'); - unlinkFacade({ componentName: 'a-component' }); - }); - - afterEach(function(){ - local.unlink.restore(); - }); - - it('should show the error', function(){ - expect(logSpy.log.args[0][0]).to.equal('an error!'.red); - }); - }); - - describe('when it succeeds', function(){ - - beforeEach(function(){ - logSpy.log = sinon.spy(); - sinon.stub(local, 'unlink').yields(null, 'yay'); - unlinkFacade({ componentName: 'a-component' }); - }); - - afterEach(function(){ - local.unlink.restore(); - }); - - it('should show a confirmation message', function(){ - expect(logSpy.log.args[0][0]).to.equal('oc Component unlinked'.green); - }); - }); - }); -}); From 7161038278aa8f59245bfe1e6923012d6fa2d3ae Mon Sep 17 00:00:00 2001 From: Matteo Figus Date: Thu, 26 Nov 2015 10:39:03 +0000 Subject: [PATCH 2/2] Client cleanup --- client/index.js | 92 +++++++++------------------------------------- client/settings.js | 4 -- 2 files changed, 17 insertions(+), 79 deletions(-) diff --git a/client/index.js b/client/index.js index 676acbc8b..c0a1845fa 100644 --- a/client/index.js +++ b/client/index.js @@ -17,47 +17,6 @@ var _ = require('./utils/helpers'); var isLocal = function(apiResponse){ return apiResponse.type === 'oc-component-local'; }; - -var readJson = function(file, callback){ - fs.readFile(file, {}, function(err, data) { - if(err){ return callback(err); } - - var obj = null; - - try { - obj = JSON.parse(data); - } catch (err2) { - return callback(err2); - } - - callback(null, obj); - }); -}; - -var loadConfig = function(callback){ - var mainModule = process.mainModule.filename, - currentFolder = path.resolve(mainModule, '..'); - - var checkConfigInFolder = function(baseDir, callback){ - var configInFolder = path.resolve(baseDir, settings.configFile.src); - - fs.exists(configInFolder, function(exists){ - if(exists){ - readJson(configInFolder, callback); - } else { - var nextConfigFolder = path.resolve(baseDir, '..'); - - if(nextConfigFolder === baseDir){ - return callback(settings.messages.fileNotFound); - } else { - return checkConfigInFolder(nextConfigFolder, callback); - } - } - }); - }; - - return checkConfigInFolder(currentFolder, callback); -}; var getRenderedComponent = function(data){ @@ -69,7 +28,6 @@ var getRenderedComponent = function(data){ data.href, data.key, random, data.version, data.html); }; - var getUnrenderedComponent = function(href, options){ if(!options || !options.ie8){ @@ -123,45 +81,29 @@ module.exports = function(conf){ options.headers = options.headers || {}; options.timeout = options.timeout || 5; - var getConfig = function(callback){ - if(!!self.config){ - return callback(null, self.config); - } - - loadConfig(function(err, content){ - if(!!content){ - self.config = content; - } - - callback(err, content); - }); - }; - - getConfig(function(err, config){ - if(!!err){ return callback(err); } + var config = self.config || {}; - config.registries = _.toArray(config.registries); + config.registries = _.toArray(config.registries); - if(_.isEmpty(config.registries)){ - return callback(settings.messages.registryUrlMissing); - } + if(_.isEmpty(config.registries)){ + return callback(settings.messages.registryUrlMissing); + } - if(!_.has(config.components, componentName)){ - return callback(settings.messages.componentMissing); - } + if(!_.has(config.components, componentName)){ + return callback(settings.messages.componentMissing); + } - var version = config.components[componentName], - versionSegment = !!version ? (version + '/') : '', - registryUrl = config.registries[0], - registrySegment = registryUrl.slice(-1) === '/' ? registryUrl : (registryUrl + '/'), - href = url.resolve(registrySegment, componentName + '/') + versionSegment; + var version = config.components[componentName], + versionSegment = !!version ? (version + '/') : '', + registryUrl = config.registries[0], + registrySegment = registryUrl.slice(-1) === '/' ? registryUrl : (registryUrl + '/'), + href = url.resolve(registrySegment, componentName + '/') + versionSegment; - if(!!options.params){ - href += '?' + querystring.stringify(options.params); - } + if(!!options.params){ + href += '?' + querystring.stringify(options.params); + } - self.render(href, options, callback); - }); + self.render(href, options, callback); }; diff --git a/client/settings.js b/client/settings.js index 75c2a3a25..e5e4745e4 100644 --- a/client/settings.js +++ b/client/settings.js @@ -1,12 +1,8 @@ 'use strict'; module.exports = { - configFile: { - src: './oc.json' - }, messages: { componentMissing: 'Configuration is not valid - Component not found', - fileNotFound: 'File not found', registryUrlMissing: 'Configuration is not valid - Registry location not found', serverSideRenderingFail: 'Server-side rendering failed' }