-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathmidprocessor.ts
367 lines (322 loc) · 16.3 KB
/
midprocessor.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
import { fetchAndThrowOnError } from './util';
import * as fsx from 'fs-extra';
import * as path from "path";
import yaml = require('js-yaml');
import * as colors from 'colors';
const CURRENT_EXCEL_RELEASE = 18;
const OLDEST_EXCEL_RELEASE_WITH_CUSTOM_FUNCTIONS = 9;
const CURRENT_OUTLOOK_RELEASE = 15;
const CURRENT_WORD_RELEASE = 9;
const CURRENT_POWERPOINT_RELEASE = 7;
tryCatch(async () => {
// ----
// Clean up Office and Outlook json cross-referencing.
// ----
console.log("\nCleaning up Office json cross-referencing...");
const officeJsonPaths: string[] = [path.resolve("../json/office"), path.resolve("../json/office_release")];
const officeFilename = "office.api.json";
officeJsonPaths.forEach((officeJsonPath) => {
fsx.writeFileSync(
officeJsonPath + '/' + officeFilename,
fsx.readFileSync(officeJsonPath + '/' + officeFilename)
.toString()
.replace(/office\!~Office_2\.Mailbox/g, "outlook!Office.Mailbox")
.replace(/office\!~Office_2\.RoamingSettings/g, "outlook!Office.RoamingSettings")
.replace(/office\!~Office_2\.SensitivityLabelsCatalog/g, "outlook!Office.SensitivityLabelsCatalog"));
});
console.log("\nCompleted Office json cross-referencing cleanup");
cleanUpJson("outlook");
cleanUpJson("excel");
cleanUpJson("word");
cleanUpJson("powerpoint");
cleanUpJson("onenote");
cleanUpJson("visio");
// ----
// Process Snippets
// ----
console.log("\nRemoving old snippets input files...");
const scriptInputsPath = path.resolve("../script-inputs");
fsx.readdirSync(scriptInputsPath)
.filter(filename => filename.indexOf("snippets") > 0)
.forEach(filename => fsx.removeSync(scriptInputsPath + '/' + filename));
console.log("\nCreating snippets file...");
console.log("\nReading from: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/snippet-extractor-output/snippets.yaml");
fsx.writeFileSync("../script-inputs/script-lab-snippets.yaml", await fetchAndThrowOnError("https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/snippet-extractor-output/snippets.yaml", "text"));
console.log("\nReading from files: " + path.resolve("../../docs/code-snippets"));
const snippetsSourcePath = path.resolve("../../docs/code-snippets");
let localCodeSnippetsString : string = "";
fsx.readdirSync(path.resolve(snippetsSourcePath))
.filter(name => name.endsWith('.yaml') || name.endsWith('.yml'))
.forEach((filename, index) => {
localCodeSnippetsString += fsx.readFileSync(`${snippetsSourcePath}/${filename}`).toString() + "\r\n";
});
fsx.writeFileSync("../script-inputs/local-repo-snippets.yaml", localCodeSnippetsString);
// Parse the YAML into an object/hash set.
let allSnippets: Object = yaml.load(localCodeSnippetsString);
// If a duplicate key exists, merge the Script Lab examples into the item with the existing key.
let scriptLabSnippets: Object = yaml.load(fsx.readFileSync(`../script-inputs/script-lab-snippets.yaml`).toString());
for (const key of Object.keys(scriptLabSnippets)) {
scriptLabSnippets[key] = consolidateMappedSnippets(scriptLabSnippets[key]);
if (allSnippets[key]) {
console.warn(`${key} has both local and Script Lab snippets. Combining, but these should be evaluated.`);
allSnippets[key] = allSnippets[key].concat(scriptLabSnippets[key]);
} else {
allSnippets[key] = scriptLabSnippets[key];
}
}
console.log("\nWriting snippets to: " + path.resolve("../json/snippets.yaml"));
fsx.writeFileSync("../json/snippets.yaml", yaml.dump(
allSnippets,
{sortKeys: <any>((a: string, b: string) => {
if (a < b) {
return -1;
} else if (a > b) {
return 1;
} else {
return 0;
}
})}
));
console.log("Copying snippets file to subfolders");
const snippetPath = path.resolve("../json/snippets.yaml");
allSnippets = yaml.load(fsx.readFileSync(snippetPath).toString());
let commonSnippetKeys = [];
let excelSnippetKeys = [];
let onenoteSnippetKeys = [];
let outlookSnippetKeys = [];
let powerpointSnippetKeys = [];
let visioSnippetKeys = [];
let wordSnippetKeys = [];
let officeRuntimeSnippetKeys = [];
let commonText = fsx.readFileSync(path.resolve("../json/office/office.api.json")).toString();
for (const key of Object.keys(allSnippets)) {
if (key.startsWith("Excel") || key.startsWith("CustomFunctions")) {
excelSnippetKeys.push(key);
} else if (key.startsWith("OneNote")) {
onenoteSnippetKeys.push(key);
} else if (key.startsWith("PowerPoint")) {
powerpointSnippetKeys.push(key);
} else if (key.startsWith("Visio")) {
visioSnippetKeys.push(key);
} else if (key.startsWith("Word")) {
wordSnippetKeys.push(key);
} else if (key.startsWith("OfficeRuntime")) {
officeRuntimeSnippetKeys.push(key);
} else if (key.startsWith("Office")) {
// Any key that's defined in the office.api.json is common. Otherwise, it's Outlook.
let reg = new RegExp(`"kind": ".*",[\\s]*"canonicalReference": [^\\s]*${key.substring(0, key.indexOf("(") == -1 ? key.length : key.indexOf("("))}`, "gm");
let match = commonText.match(reg)
if (match != null && match.length > 0) {
commonSnippetKeys.push(key);
} else {
outlookSnippetKeys.push(key);
}
} else {
console.error(colors.red("Unknown snippet key prefix: " + key));
}
}
let commonSnippets = {};
let excelSnippets = {};
let onenoteSnippets = {};
let outlookSnippets = {};
let powerpointSnippets = {};
let visioSnippets = {};
let wordSnippets = {};
let officeRuntimeSnippets = {};
commonSnippetKeys.forEach(key => {
commonSnippets[key] = allSnippets[key];
delete allSnippets[key];
});
excelSnippetKeys.forEach(key => {
excelSnippets[key] = allSnippets[key];
delete allSnippets[key];
});
onenoteSnippetKeys.forEach(key => {
onenoteSnippets[key] = allSnippets[key];
delete allSnippets[key];
});
outlookSnippetKeys.forEach(key => {
outlookSnippets[key] = allSnippets[key];
delete allSnippets[key];
});
powerpointSnippetKeys.forEach(key => {
powerpointSnippets[key] = allSnippets[key];
delete allSnippets[key];
});
visioSnippetKeys.forEach(key => {
visioSnippets[key] = allSnippets[key];
delete allSnippets[key];
});
wordSnippetKeys.forEach(key => {
wordSnippets[key] = allSnippets[key];
delete allSnippets[key];
});
officeRuntimeSnippetKeys.forEach(key => {
officeRuntimeSnippets[key] = allSnippets[key];
delete allSnippets[key];
});
writeSnippetFileAndClearYamlIfNew("../json/excel/snippets.yaml", yaml.dump(excelSnippets), "excel");
writeSnippetFileAndClearYamlIfNew("../json/excel_online/snippets.yaml", yaml.dump(excelSnippets), "excel");
for (let i = CURRENT_EXCEL_RELEASE; i > 0; i--) {
writeSnippetFileAndClearYamlIfNew(`../json/excel_1_${i}/snippets.yaml`, yaml.dump(excelSnippets), "excel");
}
writeSnippetFileAndClearYamlIfNew("../json/office/snippets.yaml", yaml.dump(commonSnippets), "office");
writeSnippetFileAndClearYamlIfNew("../json/office_release/snippets.yaml", yaml.dump(commonSnippets), "office");
writeSnippetFileAndClearYamlIfNew("../json/office-runtime/snippets.yaml", yaml.dump(officeRuntimeSnippets), "office-runtime");
writeSnippetFileAndClearYamlIfNew("../json/onenote/snippets.yaml", yaml.dump(onenoteSnippets), "onenote");
writeSnippetFileAndClearYamlIfNew("../json/outlook/snippets.yaml", yaml.dump(outlookSnippets), "outlook");
for (let i = CURRENT_OUTLOOK_RELEASE; i > 0; i--) {
writeSnippetFileAndClearYamlIfNew(`../json/outlook_1_${i}/snippets.yaml`, yaml.dump(outlookSnippets), "outlook");
}
writeSnippetFileAndClearYamlIfNew("../json/powerpoint/snippets.yaml", yaml.dump(powerpointSnippets), "powerpoint");
for (let i = CURRENT_POWERPOINT_RELEASE; i > 0; i--) {
writeSnippetFileAndClearYamlIfNew(`../json/powerpoint_1_${i}/snippets.yaml`, yaml.dump(powerpointSnippets), "powerpoint");
}
writeSnippetFileAndClearYamlIfNew("../json/visio/snippets.yaml", yaml.dump(visioSnippets), "visio");
writeSnippetFileAndClearYamlIfNew("../json/word/snippets.yaml", yaml.dump(wordSnippets), "word");
writeSnippetFileAndClearYamlIfNew("../json/word_online/snippets.yaml", yaml.dump(wordSnippets), "word");
writeSnippetFileAndClearYamlIfNew("../json/word_desktop_1_2/snippets.yaml", yaml.dump(wordSnippets), "word");
writeSnippetFileAndClearYamlIfNew("../json/word_desktop_1_1/snippets.yaml", yaml.dump(wordSnippets), "word");
writeSnippetFileAndClearYamlIfNew("../json/word_1_5_hidden_document/snippets.yaml", yaml.dump(wordSnippets), "word");
writeSnippetFileAndClearYamlIfNew("../json/word_1_4_hidden_document/snippets.yaml", yaml.dump(wordSnippets), "word");
writeSnippetFileAndClearYamlIfNew("../json/word_1_3_hidden_document/snippets.yaml", yaml.dump(wordSnippets), "word");
for (let i = CURRENT_WORD_RELEASE; i > 0; i--) {
writeSnippetFileAndClearYamlIfNew(`../json/word_1_${i}/snippets.yaml`, yaml.dump(wordSnippets), "word");
}
console.log("Moving Custom Functions APIs to correct versions of Excel");
const customFunctionsJson = path.resolve("../json/custom-functions-runtime.api.json");
fsx.copySync(customFunctionsJson, "../json/excel/custom-functions-runtime.api.json");
for (let i = CURRENT_EXCEL_RELEASE; i >= OLDEST_EXCEL_RELEASE_WITH_CUSTOM_FUNCTIONS; i--) {
fsx.copySync(customFunctionsJson, `../json/excel_1_${i}/custom-functions-runtime.api.json`);
}
fsx.copySync(customFunctionsJson, `../json/excel_online/custom-functions-runtime.api.json`);
console.log("Cleaning up What's New markdown files.");
let filePath = `../../docs/includes/outlook-preview.md`;
fsx.writeFileSync(filePath, cleanUpOutlookMarkdown(fsx.readFileSync(filePath).toString()));
for (let i = CURRENT_OUTLOOK_RELEASE; i > 0; i--) {
filePath = `../../docs/includes/outlook-1_${i}.md`;
fsx.writeFileSync(filePath, cleanUpOutlookMarkdown(fsx.readFileSync(filePath).toString()));
}
});
function cleanUpJson(host: string) {
console.log(`\nCleaning up ${host} json cross-referencing...`);
const jsonPath = path.resolve(`../json/${host}`);
const fileName = `${host}.api.json`;
console.log(`\nStarting ${host}...`);
let json = fsx.readFileSync(`${jsonPath}/${fileName}`).toString();
let cleanJson;
if (host === "outlook") {
cleanJson = cleanUpOutlookJson(json);
} else {
cleanJson = cleanUpRichApiJson(json);
}
fsx.writeFileSync(`${jsonPath}/${fileName}`, cleanJson);
console.log(`\nCompleted ${host}`);
let currentRelease;
if (host === "outlook") {
currentRelease = CURRENT_OUTLOOK_RELEASE;
} else if (host === "excel") {
currentRelease = CURRENT_EXCEL_RELEASE;
// Handle ExcelApiOnline corner case.
console.log(`\nStarting ${host}_online...`);
json = fsx.readFileSync(`${jsonPath}_online/${fileName}`).toString();
fsx.writeFileSync(`${jsonPath}_online/${fileName}`, cleanUpRichApiJson(json));
console.log(`\nCompleted ${host}_online`);
} else if (host === "word") {
currentRelease = CURRENT_WORD_RELEASE;
// Handle WordApiOnline corner case.
console.log(`\nStarting ${host}_online...`);
json = fsx.readFileSync(`${jsonPath}_online/${fileName}`).toString();
fsx.writeFileSync(`${jsonPath}_online/${fileName}`, cleanUpRichApiJson(json));
console.log(`\nCompleted ${host}_online`);
// Handle WordApiDesktop 1.2 case.
console.log(`\nStarting ${host}_desktop_1_2...`);
json = fsx.readFileSync(`${jsonPath}_desktop_1_2/${fileName}`).toString();
fsx.writeFileSync(`${jsonPath}_desktop_1_2/${fileName}`, cleanUpRichApiJson(json));
console.log(`\nCompleted ${host}_desktop_1_2`);
// Handle WordApiDesktop 1.1 case.
console.log(`\nStarting ${host}_desktop_1_1...`);
json = fsx.readFileSync(`${jsonPath}_desktop_1_1/${fileName}`).toString();
fsx.writeFileSync(`${jsonPath}_desktop_1_1/${fileName}`, cleanUpRichApiJson(json));
console.log(`\nCompleted ${host}_desktop_1_1`);
// Handle WordApiHiddenDocument 1.5 case.
console.log(`\nStarting ${host}_1_5_hidden_document...`);
json = fsx.readFileSync(`${jsonPath}_1_5_hidden_document/${fileName}`).toString();
fsx.writeFileSync(`${jsonPath}_1_5_hidden_document/${fileName}`, cleanUpRichApiJson(json));
console.log(`\nCompleted ${host}_1_5_hidden_document`);
// Handle WordApiHiddenDocument 1.4 case.
console.log(`\nStarting ${host}_1_4_hidden_document...`);
json = fsx.readFileSync(`${jsonPath}_1_4_hidden_document/${fileName}`).toString();
fsx.writeFileSync(`${jsonPath}_1_4_hidden_document/${fileName}`, cleanUpRichApiJson(json));
console.log(`\nCompleted ${host}_1_4_hidden_document`);
// Handle WordApiHiddenDocument 1.3 case.
console.log(`\nStarting ${host}_1_3_hidden_document...`);
json = fsx.readFileSync(`${jsonPath}_1_3_hidden_document/${fileName}`).toString();
fsx.writeFileSync(`${jsonPath}_1_3_hidden_document/${fileName}`, cleanUpRichApiJson(json));
console.log(`\nCompleted ${host}_1_3_hidden_document`);
} else if (host === "powerpoint") {
currentRelease = CURRENT_POWERPOINT_RELEASE;
} else {
currentRelease = 0;
}
if (currentRelease > 0) {
for (let i = currentRelease; i > 0; i--) {
console.log(`\nStarting ${host}_1_${i}...`);
json = fsx.readFileSync(`${jsonPath}_1_${i}/${fileName}`).toString();
if (host === "outlook") {
cleanJson = cleanUpOutlookJson(json);
} else {
cleanJson = cleanUpRichApiJson(json);
}
fsx.writeFileSync(`${jsonPath}_1_${i}/${fileName}`, cleanJson);
console.log(`Completed ${host}_1_${i}`);
}
}
console.log(`\nCompleted ${host} json cross-referencing cleanup`);
}
function cleanUpOutlookJson(jsonString : string) {
return jsonString.replace(/(\"CommonAPI\.\w+",[\s]+"canonicalReference": ")outlook!~Office_2/gm, "$1office!Office")
.replace(/("kind": "EnumMember",((?!kind)[\s\S])+"docComment":.*)@remarks\\n/gm, `$1`);
}
function cleanUpRichApiJson(jsonString : string) {
return jsonString.replace(/(excel|word|visio|onenote|powerpoint)\!~OfficeExtension/g, "office!OfficeExtension")
.replace(/("kind": "EnumMember",((?!kind)[\s\S])+"docComment":.*)@remarks\\n/gm, `$1`);
}
function cleanUpOutlookMarkdown(markdownString : string) {
return markdownString.replace(/CommonAPI/gm, "Office");
}
function writeSnippetFileAndClearYamlIfNew(snippetsFilePath: string, snippetsContent: string, keyword: string) {
const yamlRoot = "../yaml";
let existingSnippets = "";
if (fsx.existsSync(snippetsFilePath)) {
existingSnippets = fsx.readFileSync(snippetsFilePath).toString();
}
if (existingSnippets !== snippetsContent) {
fsx.writeFileSync(snippetsFilePath, snippetsContent);
fsx.readdirSync(yamlRoot).forEach((yamlFolder) => {
if (yamlFolder.indexOf(keyword) >= 0) {
console.log(`Removing ${yamlRoot}/${yamlFolder}`);
fsx.removeSync(`${yamlRoot}/${yamlFolder}`);
}
});
}
}
function consolidateMappedSnippets(snippets: string[]): string[] {
let consolidatedSnippets = [snippets[0]];
if (snippets.length > 1) {
snippets.forEach((snippet, index) => {
if (index > 0) {
consolidatedSnippets[0] = consolidatedSnippets[0] + snippet.replace(/\/\/ Link to full sample: https:\/\/.*.yaml/gm, "\n\n...");
}
});
}
return consolidatedSnippets;
}
async function tryCatch(call: () => Promise<void>) {
try {
await call();
} catch (e) {
console.error(e);
process.exit(1);
}
}