|
| 1 | +module.exports.command = 'update-docs'; |
| 2 | +module.exports.desc = 'Update the project README with current list of transforms'; |
| 3 | + |
| 4 | +function updateProjectREADME() { |
| 5 | + const fs = require('fs-extra'); |
| 6 | + |
| 7 | + let TRANSFORMS_PLACE_HOLDER = /<!--TRANSFORMS_START-->[\s\S]*<!--TRANSFORMS_END-->/; |
| 8 | + |
| 9 | + let transforms = fs |
| 10 | + .readdirSync('transforms') |
| 11 | + .filter(file => fs.lstatSync(`transforms/${file}`).isDirectory()); |
| 12 | + |
| 13 | + let readmeContent = transforms |
| 14 | + .map(name => `* [${name}](transforms/${name}/README.md)`) |
| 15 | + .join('\n'); |
| 16 | + |
| 17 | + fs.writeFileSync( |
| 18 | + 'README.md', |
| 19 | + fs |
| 20 | + .readFileSync('README.md', 'utf8') |
| 21 | + .replace( |
| 22 | + TRANSFORMS_PLACE_HOLDER, |
| 23 | + `<!--TRANSFORMS_START-->\n${readmeContent}\n<!--TRANSFORMS_END-->` |
| 24 | + ) |
| 25 | + ); |
| 26 | +} |
| 27 | + |
| 28 | +function updateTransformREADME(transformName) { |
| 29 | + const fs = require('fs-extra'); |
| 30 | + const path = require('path'); |
| 31 | + |
| 32 | + let toc = []; |
| 33 | + let details = []; |
| 34 | + |
| 35 | + let fixtureDir = `transforms/${transformName}/__testfixtures__`; |
| 36 | + |
| 37 | + if (!fs.existsSync(fixtureDir)) { |
| 38 | + // project does not include fixtures (perhaps using different testing |
| 39 | + // setup) |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + fs.readdirSync(fixtureDir) |
| 44 | + .filter(filename => /\.input$/.test(path.basename(filename, path.extname(filename)))) |
| 45 | + .forEach(filename => { |
| 46 | + let extension = path.extname(filename); |
| 47 | + let testName = filename.replace(`.input${extension}`, ''); |
| 48 | + let inputPath = path.join(fixtureDir, `${testName}.input${extension}`); |
| 49 | + let outputPath = path.join(fixtureDir, `${testName}.output${extension}`); |
| 50 | + |
| 51 | + toc.push(`* [${testName}](#${testName})`); |
| 52 | + details.push( |
| 53 | + '---', |
| 54 | + `<a id="${testName}"></a>`, |
| 55 | + `**Input** (<small>[${testName}.input${extension}](${inputPath})</small>):`, |
| 56 | + fs.readFileSync(inputPath), |
| 57 | + `**Output** (<small>[${testName}.input${extension}](${outputPath})</small>):`, |
| 58 | + fs.readFileSync(outputPath) |
| 59 | + ); |
| 60 | + }); |
| 61 | + |
| 62 | + let transformREADMEPath = `transforms/${transformName}/README.md`; |
| 63 | + |
| 64 | + let FIXTURES_TOC_PLACE_HOLDER = /<!--FIXTURES_TOC_START-->[\s\S]*<!--FIXTURES_TOC_END-->/; |
| 65 | + let FIXTURES_CONTENTS_PLACE_HOLDER = /<!--FIXTURES_CONTENT_START-->[\s\S]*<!--FIXTURES_CONTENT_END-->/; |
| 66 | + |
| 67 | + fs.writeFileSync( |
| 68 | + transformREADMEPath, |
| 69 | + fs |
| 70 | + .readFileSync(transformREADMEPath, 'utf8') |
| 71 | + .replace( |
| 72 | + FIXTURES_TOC_PLACE_HOLDER, |
| 73 | + `<!--FIXTURES_TOC_START-->\n${toc.join('\n')}\n<!--FIXTURES_TOC_END-->` |
| 74 | + ) |
| 75 | + .replace( |
| 76 | + FIXTURES_CONTENTS_PLACE_HOLDER, |
| 77 | + `<!--FIXTURES_CONTENTS_START-->\n${details.join('\n')}\n<!--FIXTURES_CONTENTS_END-->` |
| 78 | + ) |
| 79 | + ); |
| 80 | +} |
| 81 | + |
| 82 | +function updateTransformREADMEs() { |
| 83 | + const fs = require('fs-extra'); |
| 84 | + |
| 85 | + fs.readdirSync('transforms') |
| 86 | + .filter(file => fs.lstatSync(`transforms/${file}`).isDirectory()) |
| 87 | + .forEach(updateTransformREADME); |
| 88 | +} |
| 89 | + |
| 90 | +module.exports.handler = function handler() { |
| 91 | + updateProjectREADME(); |
| 92 | + updateTransformREADMEs(); |
| 93 | +}; |
0 commit comments