Last active
January 12, 2017 09:52
-
-
Save domjtalbot/4146e7e82367175b02d242b231e186c7 to your computer and use it in GitHub Desktop.
Convert XML comments to Json.Specially intended for C# XML documentation files.
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
import { green, yellow, magenta, red } from 'ansicolors'; | |
import { readFileSync, writeFile } from 'fs'; | |
import mkdirp from 'mkdirp'; | |
import Ora from 'ora'; | |
import { resolve, basename } from 'path'; | |
import { parseString } from 'xml2js'; | |
import '../../Scripts/env.es6'; | |
import { Debug, Error } from '../../Scripts/debug.es6'; | |
const namespace = 'csharptoxml'; | |
const debug = new Debug(namespace); | |
const error = new Error(namespace); | |
const XMLCommentsInputPath = process.env.XMLCOMMENTSINPUTPATH; | |
const XMLCommentsInput = resolve(__dirname, `../../${XMLCommentsInputPath}`); | |
const XMLCommentsOutputPath = process.env.XMLCOMMENTSOUTPUTPATH; | |
const XMLCommentsOutputName = basename(XMLCommentsOutputPath); | |
const XMLCommentsOutputDir = XMLCommentsOutputPath.replace(XMLCommentsOutputName, ''); | |
const XMLCommentsOutput = resolve(__dirname, `../../${XMLCommentsOutputPath}`); | |
debug(`${magenta(XMLCommentsInputPath)} ${yellow('-->')} ${green(XMLCommentsOutputPath)}`); | |
const parseXMLFile = inputFile => ( | |
new Promise((res, reject) => { | |
const parseXMLFileSpinner = new Ora('parsing XML file as JSON'); | |
parseXMLFileSpinner.start(); | |
const XMLComments = readFileSync(inputFile); | |
parseString(XMLComments, { | |
attrkey: 'attr', | |
charkey: 'description', | |
trim: true, | |
normalizeTags: true, | |
normalize: true, | |
childKey: 'child', | |
mergeAttrs: true, | |
}, (err, data) => { | |
if (err) { | |
parseXMLFileSpinner.fail(); | |
reject(new Error(err)); | |
} else { | |
parseXMLFileSpinner.succeed(); | |
const tmp = { | |
comments: [...data.doc.members[0].member], | |
}; | |
res(tmp); | |
} | |
}); | |
}) | |
); | |
const mkdripAsync = outputDir => ( | |
new Promise((res, reject) => { | |
const mkdirpSpinner = new Ora(`Creating ${outputDir}`); | |
mkdirpSpinner.start(); | |
mkdirp(outputDir, (mkdirpError) => { | |
if (mkdirpError) { | |
mkdirpSpinner.fail(); | |
reject(new Error(error(red(`mkdirpError: ${mkdirpError}`)))); | |
} else { | |
mkdirpSpinner.succeed(); | |
res(true); | |
} | |
}); | |
}) | |
); | |
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 xml2json() { | |
const XMLCommentsJSON = await parseXMLFile(XMLCommentsInput); | |
await mkdripAsync(XMLCommentsOutputDir); | |
if (XMLCommentsJSON !== null) { | |
await writeFileAsync(XMLCommentsOutput, XMLCommentsJSON); | |
const commentCount = XMLCommentsJSON.comments.length || 0; | |
let componentCount = 0; | |
let unknownComponentCount = 0; | |
XMLCommentsJSON.comments.forEach((comment) => { | |
if (comment.component) { | |
componentCount += 1; | |
} else { | |
unknownComponentCount += 1; | |
} | |
}); | |
debug(yellow(`${commentCount} comment${(commentCount > 1) ? 's' : ''}`)); | |
debug(yellow(`${componentCount} component${(componentCount > 1) ? 's' : ''}`)); | |
if (unknownComponentCount > 0) { | |
debug(red(`${unknownComponentCount} unknown component${(unknownComponentCount > 1) ? 's' : ''}`)); | |
} | |
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