|
| 1 | +/* |
| 2 | + * Copyright 2016 Palantir Technologies, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the 'License'); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an 'AS IS' BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/* |
| 18 | + * This TS script reads the metadata from each TSLint built-in rule |
| 19 | + * and serializes it in a format appropriate for the docs website. |
| 20 | + * |
| 21 | + * This script expects there to be a tslint-gh-pages directory |
| 22 | + * parallel to the main tslint directory. The tslint-gh-pages should |
| 23 | + * have the gh-pages branch of the TSLint repo checked out. |
| 24 | + * One easy way to do this is with the following Git command: |
| 25 | + * |
| 26 | + * ``` |
| 27 | + * git worktree add -b gh-pages ../tslint-gh-pages origin/gh-pages |
| 28 | + * ``` |
| 29 | + * |
| 30 | + * See http://palantir.github.io/tslint/develop/docs/ for more info |
| 31 | + * |
| 32 | + */ |
| 33 | + |
| 34 | +import * as fs from 'fs'; |
| 35 | +import * as glob from 'glob'; |
| 36 | +import stringify = require('json-stringify-pretty-compact'); |
| 37 | +import * as yaml from 'js-yaml'; |
| 38 | +import * as path from 'path'; |
| 39 | + |
| 40 | +import {IFormatterMetadata, IRuleMetadata} from 'tslint'; |
| 41 | + |
| 42 | +type Metadata = IRuleMetadata | IFormatterMetadata; |
| 43 | + |
| 44 | +interface Documented { |
| 45 | + metadata: Metadata; |
| 46 | +} |
| 47 | + |
| 48 | +interface IDocumentation { |
| 49 | + /** |
| 50 | + * File name for the json data file listing. |
| 51 | + */ |
| 52 | + dataFileName: string; |
| 53 | + |
| 54 | + /** |
| 55 | + * Exported item name from each file. |
| 56 | + */ |
| 57 | + exportName: string; |
| 58 | + |
| 59 | + /** |
| 60 | + * Pattern matching files to be documented. |
| 61 | + */ |
| 62 | + globPattern: string; |
| 63 | + |
| 64 | + /** |
| 65 | + * Key of the item's name within the metadata object. |
| 66 | + */ |
| 67 | + nameMetadataKey: string; |
| 68 | + |
| 69 | + /** |
| 70 | + * Function to generate individual documentation pages. |
| 71 | + */ |
| 72 | + pageGenerator: (metadata: any) => string; |
| 73 | + |
| 74 | + /** |
| 75 | + * Documentation subdirectory to output to. |
| 76 | + */ |
| 77 | + subDirectory: string; |
| 78 | +} |
| 79 | + |
| 80 | +const DOCS_DIR = '../docs'; |
| 81 | + |
| 82 | +process.chdir('./build'); |
| 83 | + |
| 84 | +/** |
| 85 | + * Documentation definition for rule modules. |
| 86 | + */ |
| 87 | +const ruleDocumentation: IDocumentation = { |
| 88 | + dataFileName: 'rules.json', |
| 89 | + exportName: 'Rule', |
| 90 | + globPattern: '../dist/src/*Rule.js', |
| 91 | + nameMetadataKey: 'ruleName', |
| 92 | + pageGenerator: generateRuleFile, |
| 93 | + subDirectory: path.join(DOCS_DIR, 'rules'), |
| 94 | +}; |
| 95 | + |
| 96 | +/** |
| 97 | + * Documentation definition for formatter modules. |
| 98 | + */ |
| 99 | +const formatterDocumentation: IDocumentation = { |
| 100 | + dataFileName: 'formatters.json', |
| 101 | + exportName: 'Formatter', |
| 102 | + globPattern: '../lib/formatters/*Formatter.js', |
| 103 | + nameMetadataKey: 'formatterName', |
| 104 | + pageGenerator: generateFormatterFile, |
| 105 | + subDirectory: path.join(DOCS_DIR, 'formatters'), |
| 106 | +}; |
| 107 | + |
| 108 | +/** |
| 109 | + * Builds complete documentation. |
| 110 | + */ |
| 111 | +function buildDocumentation(documentation: IDocumentation) { |
| 112 | + // Create each module's documentation file. |
| 113 | + const paths = glob.sync(documentation.globPattern); |
| 114 | + const metadataJson = paths.map((path: string) => { |
| 115 | + return buildSingleModuleDocumentation(documentation, path); |
| 116 | + }); |
| 117 | + |
| 118 | + // Create a data file with details of every module. |
| 119 | + buildDocumentationDataFile(documentation, metadataJson); |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * Produces documentation for a single file/module. |
| 124 | + */ |
| 125 | +function buildSingleModuleDocumentation(documentation: IDocumentation, modulePath: string): Metadata { |
| 126 | + // Load the module. |
| 127 | + // tslint:disable-next-line:no-var-requires |
| 128 | + const module = require(modulePath); |
| 129 | + const DocumentedItem = module[documentation.exportName] as Documented; |
| 130 | + if (DocumentedItem !== null && DocumentedItem.metadata !== null) { |
| 131 | + // Build the module's page. |
| 132 | + const { metadata } = DocumentedItem; |
| 133 | + const fileData = documentation.pageGenerator(metadata); |
| 134 | + |
| 135 | + // Ensure a directory exists and write the module's file. |
| 136 | + const moduleName = (metadata as any)[documentation.nameMetadataKey]; |
| 137 | + const fileDirectory = path.join(documentation.subDirectory, moduleName); |
| 138 | + if (!fs.existsSync(documentation.subDirectory)) { |
| 139 | + fs.mkdirSync(documentation.subDirectory); |
| 140 | + } |
| 141 | + if (!fs.existsSync(fileDirectory)) { |
| 142 | + fs.mkdirSync(fileDirectory); |
| 143 | + } |
| 144 | + fs.writeFileSync(path.join(fileDirectory, 'index.html'), fileData); |
| 145 | + |
| 146 | + return metadata; |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +function buildDocumentationDataFile(documentation: IDocumentation, metadataJson: any[]) { |
| 151 | + const dataJson = JSON.stringify(metadataJson, undefined, 2); |
| 152 | + fs.writeFileSync(path.join(DOCS_DIR, '_data', documentation.dataFileName), dataJson); |
| 153 | +} |
| 154 | + |
| 155 | +/** |
| 156 | + * Generates Jekyll data from any item's metadata. |
| 157 | + */ |
| 158 | +function generateJekyllData(metadata: any, layout: string, type: string, name: string): any { |
| 159 | + return { |
| 160 | + ...metadata, |
| 161 | + layout, |
| 162 | + title: `${type}: ${name}`, |
| 163 | + }; |
| 164 | +} |
| 165 | + |
| 166 | +/** |
| 167 | + * Based off a rule's metadata, generates a Jekyll 'HTML' file |
| 168 | + * that only consists of a YAML front matter block. |
| 169 | + */ |
| 170 | +function generateRuleFile(metadata: IRuleMetadata): string { |
| 171 | + if (metadata.optionExamples) { |
| 172 | + metadata = { ...metadata }; |
| 173 | + metadata.optionExamples = (metadata.optionExamples as any[]).map((example) => |
| 174 | + typeof example === 'string' ? example : stringify(example)); |
| 175 | + } |
| 176 | + |
| 177 | + const yamlData = generateJekyllData(metadata, 'rule', 'Rule', metadata.ruleName); |
| 178 | + yamlData.optionsJSON = JSON.stringify(metadata.options, undefined, 2); |
| 179 | + return `---\n${yaml.safeDump(yamlData, {lineWidth: 140} as any)}---`; |
| 180 | +} |
| 181 | + |
| 182 | +/** |
| 183 | + * Based off a formatter's metadata, generates a Jekyll 'HTML' file |
| 184 | + * that only consists of a YAML front matter block. |
| 185 | + */ |
| 186 | +function generateFormatterFile(metadata: IFormatterMetadata): string { |
| 187 | + const yamlData = generateJekyllData(metadata, 'formatter', 'TSLint formatter', metadata.formatterName); |
| 188 | + return `---\n${yaml.safeDump(yamlData, {lineWidth: 140} as any)}---`; |
| 189 | +} |
| 190 | + |
| 191 | +buildDocumentation(ruleDocumentation); |
| 192 | +buildDocumentation(formatterDocumentation); |
0 commit comments