|
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as path from 'path'; |
| 3 | +import * as jsonc from 'jsonc-parser'; |
| 4 | +import { Log, LogLevel } from '../../spec-utils/log'; |
| 5 | + |
| 6 | +const FEATURES_README_TEMPLATE = ` |
| 7 | +# #{Name} |
| 8 | +
|
| 9 | +#{Description} |
| 10 | +
|
| 11 | +## Example Usage |
| 12 | +
|
| 13 | +\`\`\`json |
| 14 | +"features": { |
| 15 | + "#{Registry}/#{Namespace}/#{Id}:#{Version}": {} |
| 16 | +} |
| 17 | +\`\`\` |
| 18 | +
|
| 19 | +#{OptionsTable} |
| 20 | +#{Customizations} |
| 21 | +#{Notes} |
| 22 | +
|
| 23 | +--- |
| 24 | +
|
| 25 | +_Note: This file was auto-generated from the [devcontainer-feature.json](#{RepoUrl}). Add additional notes to a \`NOTES.md\`._ |
| 26 | +`; |
| 27 | + |
| 28 | +const TEMPLATE_README_TEMPLATE = ` |
| 29 | +# #{Name} |
| 30 | +
|
| 31 | +#{Description} |
| 32 | +
|
| 33 | +#{OptionsTable} |
| 34 | +
|
| 35 | +#{Notes} |
| 36 | +
|
| 37 | +--- |
| 38 | +
|
| 39 | +_Note: This file was auto-generated from the [devcontainer-template.json](#{RepoUrl}). Add additional notes to a \`NOTES.md\`._ |
| 40 | +`; |
| 41 | + |
| 42 | +export async function generateFeaturesDocumentation(basePath: string, ociRegistry: string, namespace: string, output: Log) { |
| 43 | + await _generateDocumentation(basePath, FEATURES_README_TEMPLATE, 'devcontainer-feature.json', ociRegistry, namespace, output); |
| 44 | +} |
| 45 | + |
| 46 | +export async function generateTemplatesDocumentation(basePath: string, output: Log) { |
| 47 | + await _generateDocumentation(basePath, TEMPLATE_README_TEMPLATE, 'devcontainer-template.json', '', '', output); |
| 48 | +} |
| 49 | + |
| 50 | +async function _generateDocumentation( |
| 51 | + basePath: string, |
| 52 | + readmeTemplate: string, |
| 53 | + metadataFile: string, |
| 54 | + ociRegistry: string = '', |
| 55 | + namespace: string = '', |
| 56 | + output: Log |
| 57 | +) { |
| 58 | + const directories = fs.readdirSync(basePath); |
| 59 | + |
| 60 | + await Promise.all( |
| 61 | + directories.map(async (f: string) => { |
| 62 | + if (!f.startsWith('.')) { |
| 63 | + const readmePath = path.join(basePath, f, 'README.md'); |
| 64 | + output.write(`Generating ${readmePath}...`, LogLevel.Info); |
| 65 | + |
| 66 | + const jsonPath = path.join(basePath, f, metadataFile); |
| 67 | + |
| 68 | + if (!fs.existsSync(jsonPath)) { |
| 69 | + output.write(`(!) Warning: ${metadataFile} not found at path '${jsonPath}'. Skipping...`, LogLevel.Warning); |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + let parsedJson: any | undefined = undefined; |
| 74 | + try { |
| 75 | + parsedJson = jsonc.parse(fs.readFileSync(jsonPath, 'utf8')); |
| 76 | + } catch (err) { |
| 77 | + output.write(`Failed to parse ${jsonPath}: ${err}`, LogLevel.Error); |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + if (!parsedJson || !parsedJson?.id) { |
| 82 | + output.write(`${metadataFile} for '${f}' does not contain an 'id'`, LogLevel.Error); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + // Add version |
| 87 | + let version = 'latest'; |
| 88 | + const parsedVersion: string = parsedJson?.version; |
| 89 | + if (parsedVersion) { |
| 90 | + // example - 1.0.0 |
| 91 | + const splitVersion = parsedVersion.split('.'); |
| 92 | + version = splitVersion[0]; |
| 93 | + } |
| 94 | + |
| 95 | + const generateOptionsMarkdown = () => { |
| 96 | + const options = parsedJson?.options; |
| 97 | + if (!options) { |
| 98 | + return ''; |
| 99 | + } |
| 100 | + |
| 101 | + const keys = Object.keys(options); |
| 102 | + const contents = keys |
| 103 | + .map(k => { |
| 104 | + const val = options[k]; |
| 105 | + |
| 106 | + const desc = val.description || '-'; |
| 107 | + const type = val.type || '-'; |
| 108 | + const def = val.default !== '' ? val.default : '-'; |
| 109 | + |
| 110 | + return `| ${k} | ${desc} | ${type} | ${def} |`; |
| 111 | + }) |
| 112 | + .join('\n'); |
| 113 | + |
| 114 | + return '## Options\n\n' + '| Options Id | Description | Type | Default Value |\n' + '|-----|-----|-----|-----|\n' + contents; |
| 115 | + }; |
| 116 | + |
| 117 | + const generateNotesMarkdown = () => { |
| 118 | + const notesPath = path.join(basePath, f, 'NOTES.md'); |
| 119 | + return fs.existsSync(notesPath) ? fs.readFileSync(path.join(notesPath), 'utf8') : ''; |
| 120 | + }; |
| 121 | + |
| 122 | + let header; |
| 123 | + const isDeprecated = parsedJson?.deprecated; |
| 124 | + const hasLegacyIds = parsedJson?.legacyIds && parsedJson?.legacyIds.length > 0; |
| 125 | + |
| 126 | + if (isDeprecated || hasLegacyIds) { |
| 127 | + header = '### **IMPORTANT NOTE**\n'; |
| 128 | + |
| 129 | + if (isDeprecated) { |
| 130 | + header += `- **This Feature is deprecated, and will no longer receive any further updates/support.**\n`; |
| 131 | + } |
| 132 | + |
| 133 | + if (hasLegacyIds) { |
| 134 | + const formattedLegacyIds = parsedJson.legacyIds.map((legacyId: string) => `'${legacyId}'`); |
| 135 | + header += `- **Ids used to publish this Feature in the past - ${formattedLegacyIds.join(', ')}**\n`; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + let extensions = ''; |
| 140 | + if (parsedJson?.customizations?.vscode?.extensions) { |
| 141 | + const extensionsList = parsedJson.customizations.vscode.extensions; |
| 142 | + if (extensionsList && extensionsList.length > 0) { |
| 143 | + extensions = |
| 144 | + '\n## Customizations\n\n### VS Code Extensions\n\n' + extensionsList.map((ext: string) => `- \`${ext}\``).join('\n') + '\n'; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + let newReadme = readmeTemplate |
| 149 | + // Templates & Features |
| 150 | + .replace('#{Id}', parsedJson.id) |
| 151 | + .replace('#{Name}', parsedJson.name ? `${parsedJson.name} (${parsedJson.id})` : `${parsedJson.id}`) |
| 152 | + .replace('#{Description}', parsedJson.description ?? '') |
| 153 | + .replace('#{OptionsTable}', generateOptionsMarkdown()) |
| 154 | + .replace('#{Notes}', generateNotesMarkdown()) |
| 155 | + // Features Only |
| 156 | + .replace('#{Registry}', ociRegistry) |
| 157 | + .replace('#{Namespace}', namespace) |
| 158 | + .replace('#{Version}', version) |
| 159 | + .replace('#{Customizations}', extensions); |
| 160 | + |
| 161 | + if (header) { |
| 162 | + newReadme = header + newReadme; |
| 163 | + } |
| 164 | + |
| 165 | + // Remove previous readme |
| 166 | + if (fs.existsSync(readmePath)) { |
| 167 | + fs.unlinkSync(readmePath); |
| 168 | + } |
| 169 | + |
| 170 | + // Write new readme |
| 171 | + fs.writeFileSync(readmePath, newReadme); |
| 172 | + } |
| 173 | + }) |
| 174 | + ); |
| 175 | +} |
0 commit comments