diff --git a/lib/index.js b/lib/index.js index 02d8dc3..0a64a9a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -12,25 +12,34 @@ Promise.promisifyAll(dir); Promise.promisifyAll(SwaggerParser); var constraints = { + requireCurrentVersion: { + inclusion: { + within: [true, false], + message: "'%{value}' is not allowed" + } + }, visibilityFilter: { - exclusion: { + inclusion: { within: ["EXTERNAL", "INTERNAL"], message: "'%{value}' is not allowed" } }, statusFilter: { - exclusion: { + inclusion: { within: ["PROPOSED", "IN_DEVELOPMENT", "RELEASED"], message: "'%{value}' is not allowed" } - }, - stripExtensions: { - } }; exports.preparePromise = function(specDir, specFile, infoFile, host, schemes, basePath, options) { - validate(options, constraints); + var validationResult = validate(options, constraints); + if (validationResult) { + return Promise.reject("*** Invalid options:\n " + _.reduce(Object.keys(validationResult), (result, key) => { + return result + (key + ":\n " + validationResult[key].join("\n ")); + }, "")) + } + return dir.promiseFiles(specDir) .then((files) => { return files.filter(function(file) { @@ -61,39 +70,63 @@ exports.preparePromise = function(specDir, specFile, infoFile, host, schemes, ba unsorted.tags = _.sortBy(unsorted.tags, 'name'); return unsorted; }) - .then((sorted) => { - if (options.visibilityFilter && options.statusFilter) { + .then((sorted) => { + if (options.requireCurrentVersion) { paths = sorted.paths; + pathsWithoutVersion = []; _.forIn(paths, function(value, path) { + version = _.get(value, "x-current-version", null); + if (!version) { + pathsWithoutVersion.push(path); + } + }) + + if (pathsWithoutVersion) { + return Promise.reject("*** Missing 'x-current-version'. Paths affected:\n" + pathsWithoutVersion.join("\n")); + } + } + + return sorted; + }) + .then((sorted) => { + paths = sorted.paths; + _.forIn(paths, function(value, path) { + // check visibility + if (options.visibilityFilter) { visibility = _.get(value, "x-visibility", ""); - stat = _.get(value, "x-status", ""); if (visibility !== options.visibilityFilter || visibility === "") { delete sorted.paths[path]; } - if (stat !== options.statusFilter || stat === "") { + } + + // check status + if (options.statusFilter) { + status = _.get(value, "x-status", ""); + + if (status !== options.statusFilter || status === "") { delete sorted.paths[path]; } - }); - } + } + }); return sorted; }) .catch(e => { - Promise.reject(e); + return Promise.reject(e); }); } exports.prepare = function(specDir, specFile, infoFile, host, schemes, basePath, options, callback) { this.preparePromise(specDir, specFile, infoFile, host, schemes, basePath, options) .then((apis) => { - callback(null, apis); + callback(apis, null); }) .catch(e => { - callback(e, null); + callback(null, e); }); } exports.write = function(specDir, specFile, infoFile, host, schemes, basePath, outputFile, options, callback) { - this.preparePromise(specDir, specFile, infoFile, host, schemes, basePath, outputFile, options) + this.preparePromise(specDir, specFile, infoFile, host, schemes, basePath, options) .then((apis) => { fs.writeFileAsync(outputFile, JSON.stringify(apis), {}) .then(() => { @@ -101,6 +134,6 @@ exports.write = function(specDir, specFile, infoFile, host, schemes, basePath, o }); }) .catch(e => { - callback(e, null); + callback(e); }); }