|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2022 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +/** |
| 19 | + * Forked of https://github.com/firebase/firebase-js-sdk/blob/5ce06766303b92fea969c58172a7c1ab8695e21e/repo-scripts/api-documenter/src/toc.ts. |
| 20 | + */ |
| 21 | +import { writeFileSync } from 'fs'; |
| 22 | +import { resolve } from 'path'; |
| 23 | + |
| 24 | +import yargs from 'yargs'; |
| 25 | +import * as yaml from 'js-yaml'; |
| 26 | +import { FileSystem } from '@rushstack/node-core-library'; |
| 27 | + |
| 28 | +export interface TocGenerationOptions { |
| 29 | + inputFolder: string; |
| 30 | + outputFolder: string; |
| 31 | + g3Path: string; |
| 32 | +} |
| 33 | + |
| 34 | +interface TocItem { |
| 35 | + title: string; |
| 36 | + path: string; |
| 37 | + section?: TocItem[]; |
| 38 | + status?: 'experimental'; |
| 39 | +} |
| 40 | + |
| 41 | +function fileExt(f: string) { |
| 42 | + const parts = f.split('.'); |
| 43 | + if (parts.length < 2) { |
| 44 | + return ''; |
| 45 | + } |
| 46 | + return parts.pop(); |
| 47 | +} |
| 48 | + |
| 49 | +export function generateToc({ |
| 50 | + inputFolder, |
| 51 | + g3Path, |
| 52 | + outputFolder, |
| 53 | +}: TocGenerationOptions) { |
| 54 | + const asObj = FileSystem.readFolder(inputFolder) |
| 55 | + .filter((f) => fileExt(f) === 'md') |
| 56 | + .reduce((acc, f) => { |
| 57 | + const parts = f.split('.'); |
| 58 | + parts.pop(); // Get rid of file extenion (.md) |
| 59 | + |
| 60 | + let cursor = acc; |
| 61 | + for (const p of parts) { |
| 62 | + cursor[p] = cursor[p] || {}; |
| 63 | + cursor = cursor[p]; |
| 64 | + } |
| 65 | + return acc; |
| 66 | + }, {} as any); |
| 67 | + |
| 68 | + function toToc(obj, prefix = ''): TocItem[] { |
| 69 | + const toc: TocItem[] = []; |
| 70 | + for (const key of Object.keys(obj)) { |
| 71 | + const path = prefix?.length ? `${prefix}.${key}` : key; |
| 72 | + const section = toToc(obj[key], path); |
| 73 | + const tic: TocItem = { |
| 74 | + title: key, |
| 75 | + path: `${g3Path}/${path}.md`, |
| 76 | + }; |
| 77 | + if (section.length > 0) { |
| 78 | + tic.section = section; |
| 79 | + } |
| 80 | + toc.push(tic); |
| 81 | + } |
| 82 | + return toc; |
| 83 | + } |
| 84 | + |
| 85 | + const toc: TocItem[] = [ |
| 86 | + { |
| 87 | + title: 'firebase-functions', |
| 88 | + status: 'experimental', |
| 89 | + path: `${g3Path}/firebase-functions.md`, |
| 90 | + }, |
| 91 | + ...toToc(asObj['firebase-functions'], 'firebase-functions'), |
| 92 | + ]; |
| 93 | + |
| 94 | + writeFileSync( |
| 95 | + resolve(outputFolder, 'toc.yaml'), |
| 96 | + yaml.dump( |
| 97 | + { toc }, |
| 98 | + { |
| 99 | + quotingType: '"', |
| 100 | + } |
| 101 | + ) |
| 102 | + ); |
| 103 | +} |
| 104 | + |
| 105 | +const { input, output, path } = yargs(process.argv.slice(2)) |
| 106 | + .option('input', { |
| 107 | + alias: 'i', |
| 108 | + describe: 'input folder containing the *.api.json files to be processed.', |
| 109 | + default: './input', |
| 110 | + }) |
| 111 | + .option('output', { |
| 112 | + alias: 'o', |
| 113 | + describe: 'destination for the generated toc content.', |
| 114 | + default: './toc', |
| 115 | + }) |
| 116 | + .option('path', { |
| 117 | + alias: 'p', |
| 118 | + describe: 'specifies the path where the reference docs resides (e.g. g3)', |
| 119 | + default: '/', |
| 120 | + }) |
| 121 | + .help().argv; |
| 122 | + |
| 123 | +FileSystem.ensureFolder(output); |
| 124 | +generateToc({ inputFolder: input, g3Path: path, outputFolder: output }); |
0 commit comments