|
| 1 | +import * as path from 'path'; |
| 2 | +import * as fs from 'fs'; |
| 3 | +import { exec } from 'child_process'; |
| 4 | + |
| 5 | +const RONN_COMMAND = process.env.RONN_COMMAND || 'ronn'; |
| 6 | +const COMMANDS: Record<string, { optionsFile?: string }> = { |
| 7 | + auth: {}, |
| 8 | + test: { |
| 9 | + optionsFile: '_SNYK_COMMAND_OPTIONS', |
| 10 | + }, |
| 11 | + monitor: { |
| 12 | + optionsFile: '_SNYK_COMMAND_OPTIONS', |
| 13 | + }, |
| 14 | + container: {}, |
| 15 | + iac: {}, |
| 16 | + config: {}, |
| 17 | + protect: {}, |
| 18 | + policy: {}, |
| 19 | + ignore: {}, |
| 20 | + wizard: {}, |
| 21 | + help: {}, |
| 22 | + woof: {}, |
| 23 | +}; |
| 24 | + |
| 25 | +const GENERATED_MARKDOWN_FOLDER = './help/commands-md'; |
| 26 | +const GENERATED_MAN_FOLDER = './help/commands-man'; |
| 27 | +const GENERATED_TXT_FOLDER = './help/commands-txt'; |
| 28 | + |
| 29 | +function execShellCommand(cmd): Promise<string> { |
| 30 | + return new Promise((resolve) => { |
| 31 | + exec(cmd, (error, stdout, stderr) => { |
| 32 | + if (error) { |
| 33 | + console.warn(error); |
| 34 | + } |
| 35 | + return resolve(stdout ? stdout : stderr); |
| 36 | + }); |
| 37 | + }); |
| 38 | +} |
| 39 | + |
| 40 | +async function generateRoff(inputFile): Promise<string> { |
| 41 | + return await execShellCommand( |
| 42 | + `cat ${inputFile} | ${RONN_COMMAND} --roff --pipe --organization=Snyk.io`, |
| 43 | + ); |
| 44 | +} |
| 45 | + |
| 46 | +async function printRoff2Txt(inputFile) { |
| 47 | + return await execShellCommand(`cat ${inputFile} | ${RONN_COMMAND} -m`); |
| 48 | +} |
| 49 | + |
| 50 | +async function processMarkdown(markdownDoc, commandName) { |
| 51 | + const markdownFilePath = `${GENERATED_MARKDOWN_FOLDER}/${commandName}.md`; |
| 52 | + const roffFilePath = `${GENERATED_MAN_FOLDER}/${commandName}.1`; |
| 53 | + const txtFilePath = `${GENERATED_TXT_FOLDER}/${commandName}.txt`; |
| 54 | + |
| 55 | + console.info(`Generating markdown version ${commandName}.md`); |
| 56 | + fs.writeFileSync(markdownFilePath, markdownDoc); |
| 57 | + |
| 58 | + console.info(`Generating roff version ${commandName}.1`); |
| 59 | + const roffDoc = await generateRoff(markdownFilePath); |
| 60 | + |
| 61 | + fs.writeFileSync(roffFilePath, roffDoc); |
| 62 | + |
| 63 | + console.info(`Generating txt version ${commandName}.txt`); |
| 64 | + const txtDoc = (await printRoff2Txt(markdownFilePath)) as string; |
| 65 | + |
| 66 | + const formattedTxtDoc = txtDoc |
| 67 | + .replace(/(.)[\b](.)/gi, (match, firstChar, actualletter) => { |
| 68 | + if (firstChar === '_' && actualletter !== '_') { |
| 69 | + return `\x1b[4m${actualletter}\x1b[0m`; |
| 70 | + } |
| 71 | + return `\x1b[1m${actualletter}\x1b[0m`; |
| 72 | + }) |
| 73 | + .split('\n') |
| 74 | + .slice(4, -4) |
| 75 | + .join('\n'); |
| 76 | + console.log(formattedTxtDoc); |
| 77 | + |
| 78 | + fs.writeFileSync(txtFilePath, formattedTxtDoc); |
| 79 | +} |
| 80 | + |
| 81 | +async function run() { |
| 82 | + // Ensure folders exists |
| 83 | + [ |
| 84 | + GENERATED_MAN_FOLDER, |
| 85 | + GENERATED_MARKDOWN_FOLDER, |
| 86 | + GENERATED_TXT_FOLDER, |
| 87 | + ].forEach((path) => { |
| 88 | + if (!fs.existsSync(path)) { |
| 89 | + fs.mkdirSync(path); |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + const getMdFilePath = (filename: string) => |
| 94 | + path.resolve(__dirname, `./../commands-docs/${filename}.md`); |
| 95 | + |
| 96 | + const readFile = (filename: string) => |
| 97 | + fs.readFileSync(getMdFilePath(filename), 'utf8'); |
| 98 | + |
| 99 | + const readFileIfExists = (filename: string) => |
| 100 | + fs.existsSync(getMdFilePath(filename)) ? readFile(filename) : ''; |
| 101 | + |
| 102 | + const _snykHeader = readFile('_SNYK_COMMAND_HEADER'); |
| 103 | + const _snykOptions = readFile('_SNYK_COMMAND_OPTIONS'); |
| 104 | + const _snykGlobalOptions = readFile('_SNYK_GLOBAL_OPTIONS'); |
| 105 | + const _environment = readFile('_ENVIRONMENT'); |
| 106 | + const _examples = readFile('_EXAMPLES'); |
| 107 | + const _exitCodes = readFile('_EXIT_CODES'); |
| 108 | + const _notices = readFile('_NOTICES'); |
| 109 | + |
| 110 | + for (const [name, { optionsFile }] of Object.entries(COMMANDS)) { |
| 111 | + const commandDoc = readFile(name); |
| 112 | + |
| 113 | + // Piece together a help file for each command |
| 114 | + const doc = `${commandDoc} |
| 115 | +
|
| 116 | +${optionsFile ? readFileIfExists(optionsFile) : ''} |
| 117 | +
|
| 118 | +${_snykGlobalOptions} |
| 119 | +
|
| 120 | +${readFileIfExists(`${name}-examples`)} |
| 121 | +
|
| 122 | +${_exitCodes} |
| 123 | +
|
| 124 | +${_environment} |
| 125 | +
|
| 126 | +${_notices} |
| 127 | +`; |
| 128 | + |
| 129 | + await processMarkdown(doc, 'snyk-' + name); |
| 130 | + } |
| 131 | + |
| 132 | + // This just slaps strings together for the global snyk help doc |
| 133 | + const globalDoc = `${_snykHeader} |
| 134 | +
|
| 135 | +${_snykOptions} |
| 136 | +${_snykGlobalOptions} |
| 137 | +
|
| 138 | +${_examples} |
| 139 | +
|
| 140 | +${_exitCodes} |
| 141 | +
|
| 142 | +${_environment} |
| 143 | +
|
| 144 | +${_notices} |
| 145 | +`; |
| 146 | + await processMarkdown(globalDoc, 'snyk'); |
| 147 | +} |
| 148 | +run(); |
0 commit comments