Skip to content
Snippets Groups Projects
Commit c2ecfdc5 authored by Justin Ehlert's avatar Justin Ehlert
Browse files

Basic flatten step for swagger

parent 687e255f
No related branches found
No related tags found
No related merge requests found
'use strict';
var loadTasks = require('load-grunt-tasks');
module.exports = function(grunt) {
require('load-grunt-tasks')(grunt);
grunt.task.loadTasks('tasks/');
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
/**
* Copy schema files into build. Once we fully transition to swagger,
* the schema files should live in this subdirectory permanently.
*/
copy: {
schema: {
files: [
{ expand: true, cwd: '../raml/schemas', src: ['**'], dest: 'build/schemas' }
]
}
},
/**
* Flatten the swagger file into an intermediate JSON file.
* Schemas will not be resolved during this step
*/
flattenSwagger: {
core: {
apiFile: 'index.yaml',
dest: 'build/swagger-flat.json'
}
}
});
grunt.registerTask('default', ['copy', 'flattenSwagger']);
};
This diff is collapsed.
......@@ -9,5 +9,10 @@
"author": "Justin Ehlert <justinehlert@flywheel.io>",
"license": "MIT",
"devDependencies": {
"grunt": "^1.0.1",
"grunt-contrib-copy": "^1.0.0",
"js-yaml": "^3.10.0",
"json-refs": "^3.0.2",
"load-grunt-tasks": "^3.5.2"
}
}
......@@ -3,7 +3,7 @@ get:
operationId: 'getAllGroups'
responses:
200:
description: OK
description: 'OK'
schema:
$ref: "../schemas/output/groups-list.json"
'use strict';
module.exports = function(grunt) {
var path = require('path');
var fs = require('fs');
var yaml = require('js-yaml');
var resolve = require('json-refs').resolveRefs;
// Use YAML to load nested content
function resolveContent(res, callback) {
callback(undefined, yaml.safeLoad(res.text));
}
/**
* This task flattens the nested swagger yaml into a single flat file.
* It does not resolve the JSON schema links.
* @param {object} options
* @param {string} options.format The output format, either 'yaml' or 'json' (default)
* @param {object} data Task data
* @param {string} data.apiFile The input file (root level swagger file)
* @param {string} data.dest The destination file (the flattened output file)
*/
grunt.registerMultiTask('flattenSwagger', 'Resolve references in swagger YAML files', function() {
var apiFile = this.data.apiFile||'swagger.yml';
var destFile = this.data.dest||'swagger.json';
// See: http://azimi.me/2015/07/16/split-swagger-into-smaller-files.html
// and the corresponding repo: https://github.com/mohsen1/multi-file-swagger-example
if(!fs.existsSync(apiFile)) {
grunt.log.writeln('Could not find:', apiFile);
return false;
}
var options = this.options({
format: 'json'
});
var root = yaml.safeLoad(fs.readFileSync(apiFile).toString());
var resolveOpts = {
filter: ['relative'],
loaderOptions: {
processContent: resolveContent
}
};
var done = this.async();
resolve(root, resolveOpts).then(function(results) {
var data;
if( options.format === 'yaml' ) {
data = yaml.safeDump(results.resolved);
} else if( options.format === 'json' ) {
data = JSON.stringify(results.resolved, null, 2);
}
fs.writeFileSync(destFile, data);
done();
});
});
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment