-
Notifications
You must be signed in to change notification settings - Fork 61
/
postprocessor.ts
427 lines (373 loc) · 19.2 KB
/
postprocessor.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#!/usr/bin/env node --harmony
import { generateEnumList } from './util';
import * as fsx from 'fs-extra';
import * as jsyaml from "js-yaml";
import * as path from "path";
const OLDEST_EXCEL_RELEASE_WITH_CUSTOM_FUNCTIONS = 9;
interface Toc {
items: [{
name: string,
href?: string,
items?: (ApplicationTocNode | ManifestItem)[]
}]
}
interface ApplicationTocNode {
name: string,
href?: string,
uid?: string
items: [
{
name: string,
uid: string,
items: [
{
name: string,
uid?: string,
items: IMembers
}
]
}
]
}
interface ManifestItem {
name: string,
href?: string,
items: [
{
name: string,
href: string
}
]
}
interface IMembers {
items: [
{
name: string,
uid?: string
}
]
}
const docsSource = path.resolve("../yaml");
const docsDestination = path.resolve("../../docs/docs-ref-autogen");
const manifestRefPath = path.resolve("../../docs/manifest");
const requirementSetRefPath = path.resolve("../../docs/requirement-sets");
tryCatch(async () => {
console.log("\nStarting postprocessor script...");
console.log(`Deleting old docs at: ${docsDestination}`);
// delete everything except the 'overview' folder from the /docs folder
fsx.readdirSync(docsDestination)
.filter(filename => filename !== "overview" && filename !== "images")
.forEach(filename => fsx.removeSync(docsDestination + '/' + filename));
console.log(`Creating global TOC`);
let globalToc = <Toc>{items: [{name: "API reference"}]};
globalToc.items[0].items = [{"name": "API reference overview", "href": "/javascript/api/overview"},
{"name": "Excel", "href": "/javascript/api/excel"},
{"name": "OneNote", "href": "/javascript/api/onenote"},
{"name": "Outlook", "href": "/javascript/api/outlook"},
{"name": "PowerPoint", "href": "/javascript/api/powerpoint"},
{"name": "Visio", "href": "/javascript/api/visio"},
{"name": "Word", "href": "/javascript/api/word"},
{"name": "Common APIs", "href": "/javascript/api/office"}] as any;
console.log(`Copying docs output files to: ${docsDestination}`);
// copy docs output to /docs/docs-ref-autogen folder
fsx.readdirSync(docsSource)
.forEach(filename => {
fsx.copySync(
docsSource + '/' + filename,
docsDestination + '/' + filename
);
});
// fix all the individual TOC files
(globalToc.items[0].items[0] as ApplicationTocNode).href = "../overview/overview.md"; // Stay within a moniker
const tocWithPreviewCommon = scrubAndWriteToc(docsDestination + "/office", globalToc);
const tocWithReleaseCommon = scrubAndWriteToc(docsDestination + "/office_release", globalToc);
const hostVersionMap = [{host: "excel", versions: 18}, /*not including online*/
{host: "onenote", versions: 1},
{host: "outlook", versions: 15},
{host: "powerpoint", versions: 6},
{host: "visio", versions: 1},
{host: "word", versions: 8}]; /* not including online or desktop*/
hostVersionMap.forEach(category => {
if (category.versions > 1) {
scrubAndWriteToc(path.resolve(`${docsDestination}/${category.host}`), category.host === "visio" ? globalToc : tocWithPreviewCommon, category.host, category.versions);
for (let i = 1; i < category.versions; i++) {
scrubAndWriteToc(path.resolve(`${docsDestination}/${category.host}_1_${i}`), category.host === "visio" ? globalToc : tocWithReleaseCommon, category.host, i);
}
} else {
// This assumes the single version of the application's docs is not a preview version.
scrubAndWriteToc(path.resolve(`${docsDestination}/${category.host}`), category.host === "visio" ? globalToc : tocWithReleaseCommon, category.host, category.versions);
}
});
// Special case for ExcelApi Online
scrubAndWriteToc(path.resolve(`${docsDestination}/excel_online`), tocWithReleaseCommon, "excel", 99);
// Special case for WordApi Online
scrubAndWriteToc(path.resolve(`${docsDestination}/word_online`), tocWithReleaseCommon, "word", 99);
// Special case for WordApi Desktop
scrubAndWriteToc(path.resolve(`${docsDestination}/word_1_5_hidden_document`), tocWithReleaseCommon, "word", 5.5);
scrubAndWriteToc(path.resolve(`${docsDestination}/word_1_4_hidden_document`), tocWithReleaseCommon, "word", 4.5);
scrubAndWriteToc(path.resolve(`${docsDestination}/word_1_3_hidden_document`), tocWithReleaseCommon, "word", 3.5);
console.log(`Namespace pass on Outlook docs`);
// replace Outlook/CommonAPI namespace references with Office
fsx.readdirSync(docsDestination)
.filter(filename => filename.indexOf("outlook") >= 0 && filename.indexOf(".yml") < 0)
.forEach(filename => {
let subfolder = docsDestination + '/' + filename + "/outlook";
fsx.readdirSync(subfolder)
.forEach(subfilename => {
fsx.writeFileSync(subfolder + '/' + subfilename, fsx.readFileSync(subfolder + '/' + subfilename).toString().replace(/CommonAPI/g, "Office"));
});
});
console.log(`Namespace pass on Office docs`);
const officeFolders: string[] = [docsDestination + "/office/office", docsDestination + "/office_release/office"];
officeFolders.forEach((officeFolder) => {
console.log(officeFolder);
fsx.readdirSync(officeFolder)
.forEach(filename => {
fsx.writeFileSync(officeFolder + '/' + filename, fsx.readFileSync(officeFolder + '/' + filename).toString().replace(/Outlook\.Mailbox/g, "Office.Mailbox").replace(/Outlook\.RoamingSettings/g, "Office.RoamingSettings").replace(/Outlook\.SensitivityLabelsCatalog/g, "Office.SensitivityLabelsCatalog"));
});
});
console.log(`Custom Functions API requirement set link pass`);
fsx.readdirSync(docsDestination)
.filter(filename => filename.indexOf("excel") >= 0 && filename.indexOf(".yml") < 0)
.forEach(filename => {
let subfolder = docsDestination + '/' + filename + "/custom-functions-runtime";
if (fsx.existsSync(subfolder)) {
fsx.readdirSync(subfolder)
.forEach(subfilename => {
fsx.writeFileSync(subfolder + '/' + subfilename,
fsx.readFileSync(subfolder + '/' + subfilename).toString()
.replace(/\/office\/dev\/add-ins\/reference\/javascript-api-for-office/g, "/javascript/api/requirement-sets/excel/custom-functions-requirement-sets")
.replace(/\/office\/dev\/add-ins\/reference\/overview\/visio-javascript-reference-overview/g, "/javascript/api/requirement-sets/excel/custom-functions-requirement-sets"));
});
}
});
console.log(`Fixing top href`);
fsx.readdirSync(docsDestination)
.filter(filename => filename.indexOf(".yml") < 0)
.forEach(filename => {
let subfolder = docsDestination + '/' + filename;
fsx.readdirSync(subfolder).forEach(subfilename => {
if (subfilename.indexOf("toc") >= 0) {
// Update overview HREF.
fsx.writeFileSync(subfolder + '/' + subfilename, fsx.readFileSync(subfolder + '/' + subfilename).toString().replace("~/docs-ref-autogen/overview/office.md", "overview.md"));
} else if (subfilename.indexOf(".") < 0) {
let packageFolder = subfolder + '/' + subfilename;
fsx.readdirSync(packageFolder).filter(packageFileName => packageFileName.indexOf(".yml") > 0).forEach(packageFileName => {
fsx.writeFileSync(packageFolder + '/' + packageFileName, fsx.readFileSync(packageFolder + '/' + packageFileName).toString()
.replace(/^\s*example: \[\]\s*$/gm, "") // Remove example field from yml as the OPS schema does not support it.
.replace(/\\\*/gm, "*")); // Fix asterisk protection.
});
} else if (subfilename.indexOf(".yml") > 0) {
fsx.writeFileSync(subfolder + '/' + subfilename, fsx.readFileSync(subfolder + '/' + subfilename).toString()
.replace(/^\s*example: \[\]\s*$/gm, "") // Remove example field from yml as the OPS schema does not support it.
.replace(/\\\*/gm, "*")); // Fix asterisk protection.
}
});
});
console.log(`Moving common TOC to its own folder`);
fsx.copySync(docsDestination + "/office/toc.yml", docsDestination + "/common_preview/toc.yml");
fsx.copySync(docsDestination + "/office_release/toc.yml", docsDestination + "/common/toc.yml");
// remove to prevent build errors
fsx.removeSync(docsDestination + "/office/overview.md");
fsx.removeSync(docsDestination + "/office/toc.yml");
fsx.removeSync(docsDestination + "/office_release/toc.yml");
console.log("\nPostprocessor script complete!\n");
process.exit(0);
});
async function tryCatch(call: () => Promise<void>) {
try {
await call();
} catch (e) {
console.error(e);
process.exit(1);
}
}
function scrubAndWriteToc(versionFolder: string, globalToc: Toc, hostName?: string, versionNumber?: number): Toc {
const tocPath = versionFolder + "/toc.yml";
let latestToc;
if (!hostName) {
latestToc = fixCommonToc(tocPath, globalToc);
} else {
latestToc = fixToc(tocPath, globalToc, hostName, versionNumber, hostName === "visio");
}
fsx.writeFileSync(tocPath, jsyaml.safeDump(latestToc));
return latestToc;
}
function fixToc(tocPath: string, globalToc: Toc, hostName: string, versionNumber: number, addNonApiRef: boolean): Toc {
console.log(`Updating the structure of the TOC file: ${tocPath}`);
let origToc = (jsyaml.safeLoad(fsx.readFileSync(tocPath).toString()) as Toc);
let newTocNode = <ApplicationTocNode>{};
let membersToMove = <IMembers>{};
let generalFilter: string[] = ["Interfaces"];
// create custom folders
let excelIconSetFilter : string [] = ["FiveArrowsGraySet", "FiveArrowsSet", "FiveBoxesSet", "FiveQuartersSet", "FiveRatingSet", "FourArrowsGraySet", "FourArrowsSet", "FourRatingSet", "FourRedToBlackSet", "FourTrafficLightsSet", "IconCollections", "ThreeArrowsGraySet", "ThreeArrowsSet", "ThreeFlagsSet", "ThreeSignsSet", "ThreeStarsSet", "ThreeSymbols2Set", "ThreeSymbolsSet", "ThreeTrafficLights1Set", "ThreeTrafficLights2Set", "ThreeTrianglesSet"];
let customFunctionsRoot = {"name": "Custom Functions", "uid": "", "items": [] as any};
// create filter lists for types we shouldn't expose
if (hostName === "excel") {
generalFilter = generalFilter.concat(excelIconSetFilter);
} else if (hostName === "outlook") {
generalFilter = generalFilter.concat(['Appointment', 'AppointmentForm', 'ItemCompose', 'ItemRead', 'Message']);
}
origToc.items.forEach((rootItem, rootIndex) => {
rootItem.items.forEach((packageItem: ApplicationTocNode, packageIndex) => {
// fix host capitalization
let packageName;
if (packageItem.name === 'onenote') {
packageName = 'OneNote';
} else if (packageItem.name === 'powerpoint') {
packageName = 'PowerPoint';
} else {
packageName = (packageItem.name.substr(0, 1).toUpperCase() + packageItem.name.substr(1)).replace(/\-/g, ' ');
}
// get items in the namespace for the new TOC
membersToMove.items = packageItem.items;
if (packageName.toLocaleLowerCase().includes('custom functions runtime')) {
customFunctionsRoot.items.push({
"name": packageName,
"uid": packageItem.uid,
"items": membersToMove.items as any
});
} else {
let primaryList = [] as any;
if (membersToMove.items) {
let enumList = membersToMove.items.filter(item => {
return item.uid.indexOf("enum") >= 0;
});
primaryList = membersToMove.items.filter(item => {
// Remove previous chosen items and anything with the "Interfaces" namespace (those are Rich API duplicates for load/set).
return generalFilter.indexOf(item.name) < 0 && item.uid.indexOf(".Interfaces.") < 0 && item.uid.indexOf("enum") < 0;
});
if (enumList) {
const enumRootName = packageName.toLocaleLowerCase().includes("outlook") ? "MailboxEnums" : "Enums";
let enumRoot = {"name": enumRootName, "uid": "", "items": enumList};
if (packageName.toLocaleLowerCase().includes("excel")) {
// Excel has also has subfolders for icon sets and custom functions. They need to be correctly ordered.
let iconSetList = membersToMove.items.filter(item => {
return excelIconSetFilter.indexOf(item.name) >= 0;
});
if (iconSetList.length > 0) {
let excelIconSetRoot = {"name": "Icon Sets", "uid": "", "items": iconSetList};
primaryList.unshift(excelIconSetRoot);
}
primaryList.unshift(enumRoot);
if (versionNumber >= OLDEST_EXCEL_RELEASE_WITH_CUSTOM_FUNCTIONS) {
primaryList.unshift(customFunctionsRoot);
}
} else {
primaryList.unshift(enumRoot);
}
}
// Address any nested namespaces
primaryList.forEach((namespaceItem, namespaceIndex) => {
// Scan UID for namespace to add to name.
if (namespaceItem.uid) {
let regex = /\w+\.(\w+\.\w+)/g;
let matchResults = regex.exec(namespaceItem.uid);
if (matchResults) {
namespaceItem.name = matchResults[1];
}
}
});
}
newTocNode = {
name: packageName,
uid: packageItem.uid,
items: primaryList
};
}
});
});
const newToc = <Toc>{items: [] as any};
globalToc.items.forEach((topLevel, topLevelIndex) => {
newToc.items.push({name: topLevel.name, items: []});
topLevel.items.forEach((applicationNode) => {
if (applicationNode.name === newTocNode.name) {
newToc.items[topLevelIndex].items.push(newTocNode);
} else {
newToc.items[topLevelIndex].items.push(applicationNode);
}
});
});
if (addNonApiRef) {
addNonRefTocInformation(newToc);
}
return newToc;
}
function fixCommonToc(tocPath: string, globalToc: Toc): Toc {
console.log(`\nUpdating the structure of the Common TOC file: ${tocPath}`);
let origToc = (jsyaml.safeLoad(fsx.readFileSync(tocPath).toString()) as Toc);
let membersToMove = <IMembers>{};
// Create roots for items we want to reorder.
let newTocNode = {
name: 'Common APIs',
uid: "office!",
items: [] as any
};
// create folders for common (shared) API subcategories
let sharedEnumFilter = generateEnumList(fsx.readFileSync("../api-extractor-inputs-office/office.d.ts").toString());
// process 'office' (Common "Shared" API) package
origToc.items.forEach((rootItem, rootIndex) => {
rootItem.items.forEach((packageItem: ApplicationTocNode, packageIndex) => {
membersToMove.items = packageItem.items;
if (packageItem.name.toLocaleLowerCase() === 'office') {
membersToMove.items.forEach((namespaceItem, namespaceIndex) => {
// Scan UID for namespace to add to name.
if (namespaceItem.uid) {
let regex = /\w+\.(\w+\.\w+)/g;
let matchResults = regex.exec(namespaceItem.uid);
if (matchResults) {
namespaceItem.name = matchResults[1];
}
}
});
let enumList = membersToMove.items.filter(item => {
return sharedEnumFilter.indexOf(item.name) >= 0;
});
let officeExtensionList = membersToMove.items.filter(item => {
return item.uid.indexOf("office!OfficeExtension.") >= 0;
});
let primaryList = membersToMove.items.filter(item => {
return sharedEnumFilter.indexOf(item.name) < 0 && item.uid.indexOf("office!OfficeExtension.") < 0;
});
let sharedEnumRoot = {"name": "Enums", "uid": "", "items": enumList};
primaryList.unshift(sharedEnumRoot);
newTocNode.items.push({
"name": 'Office',
"uid": packageItem.uid,
"items": primaryList
});
newTocNode.items.push({
"name": 'OfficeExtension',
"items": officeExtensionList
});
} else if (packageItem.name === 'office-runtime') {
newTocNode.items.push({
"name": 'OfficeRuntime',
"uid": packageItem.uid,
"items": packageItem.items
});
}
});
});
const newToc = <Toc>{items: [] as any};
globalToc.items.forEach((topLevel, topLevelIndex) => {
newToc.items.push({name: topLevel.name, items: []});
topLevel.items.forEach((applicationNode) => {
if (applicationNode.name === newTocNode.name) {
newToc.items[topLevelIndex].items.push(newTocNode);
} else {
newToc.items[topLevelIndex].items.push(applicationNode);
}
});
});
addNonRefTocInformation(newToc);
return newToc;
}
function addNonRefTocInformation(toc: Toc) {
// Add manifest TOC
let manifestYml = jsyaml.safeLoad(fsx.readFileSync(`${manifestRefPath}/toc.yml`).toString());
toc.items.push(manifestYml[0]);
// Add requirement sets TOC
let requirementSetYml = jsyaml.safeLoad(fsx.readFileSync(`${requirementSetRefPath}/toc.yml`).toString());
toc.items.push(requirementSetYml[0]);
}