Skip to content

Commit

Permalink
Hacked remote references.
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeLenny committed Mar 3, 2017
1 parent c716b47 commit e5e4622
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
29 changes: 23 additions & 6 deletions app/lib/common.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
var fs = require('fs');
var path = require('path');
var cheerio = require('cheerio');
var marked = require('marked');
var highlight = require('highlight.js');
var _ = require('lodash');
var request = require('sync-request');

var common = {
highlight: function(code, name) {
Expand Down Expand Up @@ -97,16 +100,30 @@ var common = {

resolveSchemaReference: function(reference, json) {
reference = reference.trim();
if (reference.lastIndexOf('#', 0) < 0) {
console.warn('Remote references not supported yet. Reference must start with "#" (but was ' + reference + ')')
return {};
}
var components = reference.split('#');
var url = components[0];
var hash = components[1];
if(!hash) {
hash = '';
}
var hashParts = hash.split('/');
// TODO : Download remote json from url if url not empty
var current = json; //options.data.root
var current = null;
if(url && url !== "") {
if(url.indexOf("://") < 0) {
url = path.resolve(json.filePath, url);
options = json.spectacleOptions;
current = require(path.resolve(options.appDir + '/lib/preprocessor'))(options, require(url));
}
else {
//TODO: cache results for performance
current = JSON.parse(request('GET', url));
}
current.filePath = url;
current.spectacleOptions = json.spectacleOptions;
}
else {
current = json;
}
hashParts.forEach(function(hashPart) {
// Traverse schema from root along the path
if (hashPart.trim().length > 0) {
Expand Down
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ module.exports = function (options) {
function loadData() {
var specPath = path.resolve(opts.specFile);
delete require.cache[specPath];
return require(path.resolve(opts.appDir + '/lib/preprocessor'))(
var data = require(path.resolve(opts.appDir + '/lib/preprocessor'))(
options, require(specPath));
data.filePath = path.dirname(specPath);
data.spectacleOptions = options;
return data;
}

var config = require(path.resolve(opts.configFile))(grunt, opts, loadData());
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"lodash": "^4.2.1",
"marked": "^0.3.5",
"tmp": "0.0.31",
"sync-request": "^4.0.1",
"trace": "^1.1.0"
}
}

0 comments on commit e5e4622

Please sign in to comment.