Created
July 12, 2016 18:13
-
-
Save kristelteng/345f1c39a5bd573bc719f9bea203d4f6 to your computer and use it in GitHub Desktop.
Docs folder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let tar = require('tar-stream'); | |
let _ = require('lodash'); | |
let assert = require('assert'); | |
let fs = require('mz/fs'); | |
let path = require('path'); | |
let recursiveReadSync = require('recursive-readdir-sync'); | |
async function documenter(options) { | |
options = _.defaults({}, options, { | |
schemas: [], | |
docsFolder: null, | |
}); | |
assert(options.schemas, 'options.schemas must be given'); | |
assert(options.schemas instanceof Array, 'options.schemas must be an array'); | |
let schemas = options.schemas; | |
let tarball = tar.pack(); | |
// add schemas to tarball | |
schemas.forEach(schema => { | |
let data = JSON.stringify(schema, null, 2); | |
tarball.entry({name: 'schema/' + schema.id}, data); | |
}); | |
if (options.docsFolder !== null) { | |
// add docs to tarball | |
let docs = options.docsFolder; | |
let files = recursiveReadSync(options.docsFolder); | |
await Promise.all(files.map(async (file) => { | |
//remove the path | |
let relativePath = path.basename(file); | |
let data = await fs.readFile(file, {encoding: 'utf8'}); | |
tarball.entry({name: 'docs/' + relativePath}, data); | |
})); | |
} | |
tarball.finalize(); | |
tarball.pipe(process.stdout); | |
let output = { | |
tarball, | |
}; | |
return output; | |
} | |
module.exports = documenter; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment