Skip to content

Instantly share code, notes, and snippets.

@domjtalbot
Last active December 15, 2016 16:12
Show Gist options
  • Save domjtalbot/51a27904ba6eaaa187070dd9f1ab7ab8 to your computer and use it in GitHub Desktop.
Save domjtalbot/51a27904ba6eaaa187070dd9f1ab7ab8 to your computer and use it in GitHub Desktop.
Use documentation.js to get comments from a glob of javascript files then output to a json file.
import { magenta, yellow, red } from 'ansicolors';
import { buildSync, formats } from 'documentation';
import { writeFile } from 'fs';
import glob from 'glob';
import Ora from 'ora';
import { resolve, basename } from 'path';
import '../../Scripts/env.es6';
import { Debug, Error } from '../../Scripts/debug.es6';
const namespace = 'js2json';
const debug = new Debug(namespace);
const error = new Error(namespace);
const jsCommentsInput = process.env.JSCOMMENTSINPUT.split(', ');
const jsCommentsInputArray = jsCommentsInput.map(file => resolve(__dirname, `../../${file}`));
const jsonOutputPath = process.env.JSCOMMENTSOUTPUTPATH;
const jsonOutputName = basename(jsonOutputPath);
const jsonOutputDir = jsonOutputPath.replace(jsonOutputName, '');
const jsonOutput = resolve(__dirname, `../../${jsonOutputPath}`);
debug(yellow(`Javascript comments being compiled to ${jsonOutputName}`));
debug(yellow(jsCommentsInputArray));
const globAsync = file => (
new Promise((res, reject) => {
glob(file, {}, (globError, globFiles) => {
if (globError) {
reject(new Error(error(red(`globAsync: ${globError}`))));
} else {
res(globFiles);
}
});
})
);
const getGlob = async inputGlob => {
const getGlobSpinner = new Ora('Getting files from glob');
getGlobSpinner.start();
let allFiles = [];
await Promise.all(inputGlob.map(async file => {
const glob = await globAsync(file);
allFiles = [...allFiles, ...glob];
}));
getGlobSpinner.succeed();
debug(yellow(`${allFiles.length} file${(allFiles.length > 1) ? 's' : ''}`));
return allFiles;
};
const formatJSON = comments => (
new Promise((res, reject) => {
const formatJSONSpinner = new Ora('Formatting comments to JSON');
formatJSONSpinner.start();
formats.json(comments, {}, (formatError, output) => {
if (formatError) {
formatJSONSpinner.fail();
reject(new Error(error(red(`formatJSON: ${formatError}`))));
} else {
formatJSONSpinner.succeed();
res(JSON.parse(output));
}
});
})
);
const writeFileAsync = (outputPath, outputContent) => (
new Promise((res, reject) => {
const writeFileSpinner = new Ora('Writing comments JSON to file');
writeFileSpinner.start();
writeFile(outputPath, JSON.stringify(outputContent), (writeError) => {
if (writeError) {
writeFileSpinner.fail();
reject(new Error(error(red(`writeError: ${writeError}`))));
} else {
writeFileSpinner.succeed();
res(true);
}
});
})
);
(async function js2json() {
const inputFiles = await getGlob(jsCommentsInputArray);
const inputResultsSpinner = new Ora('Reading Javascript files');
inputResultsSpinner.start();
const inputResults = buildSync(inputFiles);
inputResultsSpinner.succeed();
const jsCommentsJSON = await formatJSON(inputResults);
if (jsCommentsJSON !== null) {
await writeFileAsync(jsonOutput, jsCommentsJSON);
debug(green('Done.'));
} else {
debug(red('No comments found.'));
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment