Skip to content

Commit 6948977

Browse files
committed
Merge pull request #46 from opentable/preview
Added preview functionality
2 parents faeff6c + 85dc137 commit 6948977

File tree

6 files changed

+193
-2
lines changed

6 files changed

+193
-2
lines changed

cli/commands.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@ module.exports = {
4747

4848
ls: { help: 'Shows the list of the available components for a linked oc registry' },
4949

50+
preview: {
51+
help: 'Runs a test page consuming a component',
52+
options: {
53+
componentHref: {
54+
help: 'The name of the component to preview'
55+
},
56+
port: {
57+
help: 'The port where to start the server. Default 3000',
58+
required: false
59+
}
60+
}
61+
},
62+
5063
publish: {
5164
help: 'Publish a component',
5265
options: {

cli/facade/preview.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
3+
var colors = require('colors');
4+
var format = require('stringformat');
5+
var http = require('http');
6+
var opn = require('opn');
7+
var strings = require('../../resources/index');
8+
var urlParser = require('../../registry/domain/url-parser');
9+
10+
module.exports = function(dependencies){
11+
12+
var logger = dependencies.logger;
13+
14+
return function(opts){
15+
16+
var port = opts.port || 3000;
17+
18+
urlParser.parse(opts.componentHref, function(err, parsed){
19+
if(err){ return logger.log(strings.errors.cli.COMPONENT_HREF_NOT_FOUND.red); }
20+
21+
http.createServer(function(req, res){
22+
res.writeHead(200, {'Content-Type': 'text/html'});
23+
res.end(format('<html><body><oc-component href="{0}"></oc-component><script src="{1}"></script></body></html>',
24+
opts.componentHref, parsed.clientHref));
25+
26+
}).listen(port, function(){
27+
var previewUrl = 'http://localhost:' + port;
28+
logger.log(format(strings.messages.cli.PREVIEW_STARTED_AT_URL, previewUrl).green);
29+
opn(previewUrl);
30+
});
31+
});
32+
33+
};
34+
};

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,14 @@
6161
"nice-cache": "0.0.5",
6262
"node-dir": "0.1.5",
6363
"nomnom": "1.8.1",
64+
"opn": "^1.0.1",
6465
"performance-now": "0.2.0",
6566
"read": "1.0.5",
6667
"semver": "4.0.3",
68+
"stringformat": "0.0.5",
6769
"tar.gz": "0.1.1",
6870
"uglify-js": "2.4.13",
6971
"underscore": "1.8.1",
70-
"watch": "0.13.0",
71-
"stringformat": "0.0.5"
72+
"watch": "0.13.0"
7273
}
7374
}

registry/domain/url-parser.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
3+
var querystring = require('querystring');
4+
var request = require('../../utils/request');
5+
var semver = require('semver');
6+
var url = require('url');
7+
8+
var removeFinalSlashes = function(s){
9+
while(s.slice(-1) === '/'){
10+
s = s.slice(0, -1);
11+
}
12+
return s;
13+
};
14+
15+
module.exports = {
16+
parse: function(componentHref, callback){
17+
request(componentHref, function(err, res){
18+
if(err){ return callback(err); }
19+
20+
var parsed = JSON.parse(res),
21+
requestedVersion = parsed.requestVersion,
22+
href = url.parse(componentHref),
23+
relativePath = removeFinalSlashes(href.pathname),
24+
withoutVersion = removeFinalSlashes(relativePath.replace(requestedVersion, '')),
25+
componentName = withoutVersion.substr(withoutVersion.lastIndexOf('/') + 1),
26+
withoutComponent = removeFinalSlashes(withoutVersion.replace(componentName, '')),
27+
registryUrl = href.protocol + '//' + href.host + withoutComponent + '/';
28+
29+
callback(null, {
30+
clientHref: registryUrl + 'oc-client/client.js',
31+
componentName: componentName,
32+
parameters: querystring.parse(href.query),
33+
registryUrl: registryUrl,
34+
version: requestedVersion
35+
});
36+
});
37+
}
38+
};

resources/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module.exports = {
3232
PARAMETER_WRONG_FORMAT_CODE: 'wrong type'
3333
},
3434
cli: {
35+
COMPONENT_HREF_NOT_FOUND: 'The specified path is not a valid component\'s url',
3536
COMPONENTS_LINKED_NOT_FOUND: 'No components linked in the project',
3637
COMPONENTS_NOT_FOUND: 'no components found in specified path',
3738
DEV_FAIL: 'An error happened when initialising the dev runner: {0}',
@@ -63,6 +64,7 @@ module.exports = {
6364
ENTER_PASSWORD: 'Enter password:',
6465
ENTER_USERNAME: 'Enter username:',
6566
PACKAGING: 'Packaging -> {0}',
67+
PREVIEW_STARTED_AT_URL: 'Component\'s preview started at url: {0}',
6668
PUBLISHED: 'Published -> {0}',
6769
PUBLISHING: 'Publishing -> {0}',
6870
REGISTRY_ADDED: 'oc registry added',
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
'use strict';
2+
3+
var expect = require('chai').expect;
4+
var injectr = require('injectr');
5+
var sinon = require('sinon');
6+
7+
describe('registry : domain : url-parser', function(){
8+
9+
var parsed;
10+
var execute = function(url, returnVersion, callback){
11+
var urlParser = injectr('../../registry/domain/url-parser.js', {
12+
'../../utils/request': sinon.stub().yields(null, JSON.stringify({
13+
requestVersion: returnVersion
14+
}))
15+
});
16+
17+
urlParser.parse(url, function(err, res){
18+
parsed = res;
19+
callback();
20+
});
21+
};
22+
23+
describe('when parsing http://www.registry.com/api/v2/component-name', function(){
24+
25+
before(function(done){
26+
execute('http://www.registry.com/api/v2/component-name', '', done);
27+
});
28+
29+
it('componentName should be component-name', function(){
30+
expect(parsed.componentName).to.equal('component-name');
31+
});
32+
33+
it('version should be blank', function(){
34+
expect(parsed.version).to.equal('');
35+
});
36+
37+
it('registryUrl should be http://www.registry.com/api/v2/', function(){
38+
expect(parsed.registryUrl).to.equal('http://www.registry.com/api/v2/');
39+
});
40+
41+
it('parameters should be {}', function(){
42+
expect(parsed.parameters).to.eql({});
43+
});
44+
45+
it('clientHref should be http://www.registry.com/api/v2/oc-client/client.js', function(){
46+
expect(parsed.clientHref).to.equal('http://www.registry.com/api/v2/oc-client/client.js');
47+
});
48+
});
49+
50+
describe('when parsing http://www.registry.com/component-name/~1.0.0/?hello=world', function(){
51+
52+
before(function(done){
53+
execute('http://www.registry.com/component-name/~1.0.0/?hello=world', '~1.0.0', done);
54+
});
55+
56+
it('componentName should be component-name', function(){
57+
expect(parsed.componentName).to.equal('component-name');
58+
});
59+
60+
it('version should be blank', function(){
61+
expect(parsed.version).to.equal('~1.0.0');
62+
});
63+
64+
it('registryUrl should be http://www.registry.com/', function(){
65+
expect(parsed.registryUrl).to.equal('http://www.registry.com/');
66+
});
67+
68+
it('parameters should be { hello: \'world\'}', function(){
69+
expect(parsed.parameters).to.eql({ hello: 'world' });
70+
});
71+
72+
it('clientHref should be http://www.registry.com/oc-client/client.js', function(){
73+
expect(parsed.clientHref).to.equal('http://www.registry.com/oc-client/client.js');
74+
});
75+
});
76+
77+
describe('when parsing http://www.registry.com/12345/~1.0.0?hello=world', function(){
78+
79+
before(function(done){
80+
execute('http://www.registry.com/12345/~1.0.0?hello=world', '~1.0.0', done);
81+
});
82+
83+
it('componentName should be 12345', function(){
84+
expect(parsed.componentName).to.equal('12345');
85+
});
86+
87+
it('version should be blank', function(){
88+
expect(parsed.version).to.equal('~1.0.0');
89+
});
90+
91+
it('registryUrl should be http://www.registry.com/', function(){
92+
expect(parsed.registryUrl).to.equal('http://www.registry.com/');
93+
});
94+
95+
it('parameters should be { hello: \'world\'}', function(){
96+
expect(parsed.parameters).to.eql({ hello: 'world' });
97+
});
98+
99+
it('clientHref should be http://www.registry.com/oc-client/client.js', function(){
100+
expect(parsed.clientHref).to.equal('http://www.registry.com/oc-client/client.js');
101+
});
102+
});
103+
});

0 commit comments

Comments
 (0)