|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +import { mkdir, writeFile } from 'node:fs/promises'; |
| 4 | +import { join } from 'node:path'; |
| 5 | + |
| 6 | +import { visit } from 'unist-util-visit'; |
| 7 | + |
| 8 | +import { updateFilesForBuild } from './utils/updateFilesForBuild.mjs'; |
| 9 | +import { EXTRACT_CODE_FILENAME_COMMENT } from './constants.mjs'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Normalizes a section name. |
| 13 | + * |
| 14 | + * @param {string} sectionName Section name |
| 15 | + * @returns {string} |
| 16 | + */ |
| 17 | +export function normalizeSectionName(sectionName) { |
| 18 | + return sectionName.toLowerCase().replace(/\s/g, '_').replace(/\W/g, ''); |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * This generator generates a file list from code blocks extracted from |
| 23 | + * `doc/api/addons.md` to facilitate C++ compilation and JavaScript runtime |
| 24 | + * validations. |
| 25 | + * |
| 26 | + * @typedef {Array<ApiDocMetadataEntry>} Input |
| 27 | + * |
| 28 | + * @type {import('../types.d.ts').GeneratorMetadata<Input, string>} |
| 29 | + */ |
| 30 | +export default { |
| 31 | + name: 'addon-verify', |
| 32 | + |
| 33 | + version: '1.0.0', |
| 34 | + |
| 35 | + description: |
| 36 | + 'Generates a file list from code blocks extracted from `doc/api/addons.md` to facilitate C++ compilation and JavaScript runtime validations', |
| 37 | + |
| 38 | + dependsOn: 'ast', |
| 39 | + |
| 40 | + /** |
| 41 | + * Generates a file list from code blocks. |
| 42 | + * |
| 43 | + * @param {Input} input |
| 44 | + * @param {Partial<GeneratorOptions>} options |
| 45 | + */ |
| 46 | + async generate(input, { output }) { |
| 47 | + const sectionsCodeBlocks = input.reduce((addons, node) => { |
| 48 | + const sectionName = node.heading.data.name; |
| 49 | + |
| 50 | + const content = node.content; |
| 51 | + |
| 52 | + visit(content, childNode => { |
| 53 | + if (childNode.type === 'code') { |
| 54 | + const filename = childNode.value.match(EXTRACT_CODE_FILENAME_COMMENT); |
| 55 | + |
| 56 | + if (filename === null) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + if (!addons[sectionName]) { |
| 61 | + addons[sectionName] = []; |
| 62 | + } |
| 63 | + |
| 64 | + addons[sectionName].push({ |
| 65 | + name: filename[1], |
| 66 | + content: childNode.value, |
| 67 | + }); |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + return addons; |
| 72 | + }, {}); |
| 73 | + |
| 74 | + const files = await Promise.all( |
| 75 | + Object.entries(sectionsCodeBlocks) |
| 76 | + .filter(([, files]) => { |
| 77 | + // Must have a .cc and a .js to be a valid test. |
| 78 | + return ( |
| 79 | + files.some(file => file.name.endsWith('.cc')) && |
| 80 | + files.some(file => file.name.endsWith('.js')) |
| 81 | + ); |
| 82 | + }) |
| 83 | + .flatMap(async ([sectionName, files], index) => { |
| 84 | + const newFiles = updateFilesForBuild(files); |
| 85 | + |
| 86 | + if (output) { |
| 87 | + const normalizedSectionName = normalizeSectionName(sectionName); |
| 88 | + |
| 89 | + const identifier = String(index + 1).padStart(2, '0'); |
| 90 | + |
| 91 | + const folder = `${identifier}_${normalizedSectionName}`; |
| 92 | + |
| 93 | + await mkdir(join(output, folder), { recursive: true }); |
| 94 | + |
| 95 | + newFiles.forEach(async ({ name, content }) => { |
| 96 | + await writeFile(join(output, folder, name), content); |
| 97 | + }); |
| 98 | + } |
| 99 | + |
| 100 | + return newFiles; |
| 101 | + }) |
| 102 | + ); |
| 103 | + |
| 104 | + return files; |
| 105 | + }, |
| 106 | +}; |
0 commit comments