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