This repository has been archived by the owner on Jan 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
util.ts
526 lines (452 loc) · 18.2 KB
/
util.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/*
* Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
* See LICENSE in the project root for license information.
*/
import * as appInsights from 'applicationinsights';
import * as fs from 'fs-extra';
import * as jszip from 'jszip';
import * as junk from 'junk';
import * as shell from 'node-powershell';
import * as officeAddinValidator from 'office-addin-validator';
import * as opn from 'opn';
import * as os from 'os';
import * as path from 'path';
import * as xml2js from 'xml2js';
export const appInsightsClient = new appInsights.TelemetryClient('7695b3c1-32c5-4458-99d6-5d0e3208c9c2');
appInsightsClient.config.maxBatchIntervalMs = 500;
const office16RegistryPath = 'HKCU:\\Software\\Microsoft\\Office\\16.0';
const wefFolder = '\\WEF';
const developerFolder = '\\Developer';
export const applicationProperties = {
word: {
TaskPaneApp: {
webExtensionPath: 'word/webextensions/webextension.xml',
templateName: 'DocumentWithTaskPane.docx'
},
sideloadingDirectory: path.join(os.homedir(), 'Library/Containers/com.microsoft.Word/Data/Documents/wef'),
documentationLink: "https://docs.microsoft.com/en-us/office/dev/add-ins/testing/create-a-network-shared-folder-catalog-for-task-pane-and-content-add-ins",
canSideload: true
},
excel: {
TaskPaneApp: {
webExtensionPath: 'xl/webextensions/webextension.xml',
templateName: 'BookWithTaskPane.xlsx'
},
ContentApp: {
webExtensionPath: 'xl/webextensions/webextension.xml',
templateName: 'BookWithContent.xlsx'
},
sideloadingDirectory: path.join(os.homedir(), 'Library/Containers/com.microsoft.Excel/Data/Documents/wef'),
documentationLink: "https://docs.microsoft.com/en-us/office/dev/add-ins/testing/create-a-network-shared-folder-catalog-for-task-pane-and-content-add-ins",
canSideload: true
},
powerpoint: {
TaskPaneApp: {
webExtensionPath: 'ppt/webextensions/webextension.xml',
templateName: 'PresentationWithTaskPane.pptx'
},
ContentApp: {
webExtensionPath: 'ppt/slides/udata/data.xml',
templateName: 'PresentationWithContent.pptx'
},
sideloadingDirectory: path.join(os.homedir(), 'Library/Containers/com.microsoft.Powerpoint/Data/Documents/wef'),
documentationLink: "https://docs.microsoft.com/en-us/office/dev/add-ins/testing/create-a-network-shared-folder-catalog-for-task-pane-and-content-add-ins",
canSideload: true
},
outlook: {
documentationLink: "https://docs.microsoft.com/en-us/outlook/add-ins/sideload-outlook-add-ins-for-testing",
canSideload: false
},
onenote: {
documentationLink: "https://docs.microsoft.com/en-us/office/dev/add-ins/onenote/onenote-add-ins-getting-started",
canSideload: false
},
project: {
documentationLink: "https://docs.microsoft.com/en-us/office/dev/add-ins/project/project-add-ins",
canSideload: false
}
};
// TOP-LEVEL COMMANDS //
export function sideload(application: string, manifestPath: string): Promise<any> {
appInsightsClient.trackEvent({ name: 'sideload' });
return sideloadManifest(application, manifestPath);
}
export function list(): Promise<Array<[string, string, string]>> {
appInsightsClient.trackEvent({ name: 'list'});
return getAllIdsAndManifests();
}
export function remove(application: string, manifestPath: string): Promise<any> {
appInsightsClient.trackEvent({ name: 'remove' });
return removeManifest(application, manifestPath);
}
export function validate(manifestPath: string): Promise<string> {
appInsightsClient.trackEvent({ name: 'validate' });
return validateManifest(manifestPath);
}
// DISAMBIGUATING COMMANDS //
export function getManifests(application: string): Promise<string[]> {
return (process.platform === 'win32')
? getManifestsFromRegistry()
: getManifestsFromSideloadingDirectory(application);
}
function addManifest(application: string, manifestPath: string): Promise<any> {
return (process.platform === 'win32')
? addManifestToRegistry(manifestPath)
: addManifestToSideloadingDirectory(application, manifestPath);
}
async function getAllManifests(): Promise<string[]> {
if (process.platform === 'win32') {
return getManifestsFromRegistry();
}
else {
let manifests = [];
for (const application of Object.keys(applicationProperties)) {
manifests = [...manifests, await getManifests(application)];
}
return Promise.resolve(manifests);
}
}
function removeManifest(application: string, manifestPath: string): Promise<any> {
if (fs.existsSync(manifestPath)) {
manifestPath = fs.realpathSync(manifestPath);
}
return (process.platform === 'win32'
? removeManifestFromRegistry(manifestPath)
: removeManifestFromSideloadingDirectory(application, manifestPath));
}
// NON-WIN32 COMMANDS //
function addManifestToSideloadingDirectory(application: string, manifestPath: string): Promise<any> {
return new Promise ((resolve, reject) => {
const sideloadingDirectory = applicationProperties[application].sideloadingDirectory;
fs.ensureDirSync(sideloadingDirectory);
const sideloadingManifestPath = path.join(sideloadingDirectory, path.basename(manifestPath));
if (fs.existsSync(sideloadingManifestPath)) {
const stat = fs.statSync(manifestPath);
const sideloadingStat = fs.statSync(sideloadingManifestPath);
if (stat.ino !== sideloadingStat.ino && stat.dev !== sideloadingStat.dev) {
return reject(['Remove the manifest with matching name before adding this one. ', fs.realpathSync(sideloadingManifestPath)]);
}
}
fs.ensureLinkSync(manifestPath, sideloadingManifestPath);
resolve();
});
}
function getManifestsFromSideloadingDirectory(inputApplication: string): Promise<string[]> {
return new Promise((resolve, reject) => {
let manifestPaths = [];
for (let application of Object.keys(applicationProperties)) {
if (!inputApplication || application === inputApplication) {
const sideloadingDirectory = applicationProperties[application].sideloadingDirectory;
if (!fs.existsSync(sideloadingDirectory)) {
continue;
}
fs.readdirSync(sideloadingDirectory).filter(junk.not).forEach(manifestName => {
manifestPaths.push(fs.realpathSync(path.join(sideloadingDirectory, manifestName)));
});
}
}
resolve(manifestPaths);
});
}
function removeManifestFromSideloadingDirectory(inputApplication: string, manifestPathToRemove: string): Promise<any> {
return new Promise(async (resolve, reject) => {
try {
let manifestRemoved = false;
for (let application of Object.keys(applicationProperties)) {
if (!inputApplication || application === inputApplication) {
const sideloadingDirectory = applicationProperties[application].sideloadingDirectory;
if (!fs.existsSync(sideloadingDirectory)) {
continue;
}
fs.readdirSync(sideloadingDirectory).forEach(manifestName => {
const realManifestPath = (fs.realpathSync(path.join(sideloadingDirectory, manifestName)));
if (manifestPathToRemove === realManifestPath) {
console.log(`Removing ${manifestPathToRemove} for application ${application}`);
fs.unlinkSync(manifestPathToRemove);
manifestRemoved = true;
}
});
}
}
if (!manifestRemoved) {
return reject('No manifests were found to remove. Use "list" to show manifests that have been added.');
}
} catch (err) {
reject(err);
}
});
}
// WIN32 SPECIFIC COMMANDS //
function querySideloadingRegistry(commands: string[]): Promise<string> {
return new Promise(async (resolve, reject) => {
let ps = new shell({'debugMsg': false});
try {
// Ensure that the registry path exists
ps.addCommand('$RegistryPath = "' + office16RegistryPath + '"');
ps.addCommand('if(!(Test-Path $RegistryPath)) { Throw "NO-OFFICE-16" } ');
ps.addCommand('$RegistryPath = "' + office16RegistryPath + wefFolder + '"');
ps.addCommand('if(!(Test-Path $RegistryPath)) { New-Item -Path $RegistryPath } ');
ps.addCommand('$RegistryPath = "' + office16RegistryPath + wefFolder + developerFolder + '"');
ps.addCommand('if(!(Test-Path $RegistryPath)) { New-Item -Path $RegistryPath } ');
await ps.invoke();
ps.dispose();
ps = new shell({'debugMsg': false});
ps.addCommand('$RegistryPath = "' + office16RegistryPath + wefFolder + developerFolder + '"');
for (const command of commands) {
ps.addCommand(command);
}
const output = await ps.invoke();
ps.dispose();
resolve(output);
} catch (err) {
ps.dispose();
if (err.indexOf('NO-OFFICE-16') > -1) {
reject(office16RegistryPath + ' could not be found in the registry. Make sure Microsoft Office is installed.');
}
else {
reject(err);
}
}
});
}
function addManifestToRegistry(manifestPath: string): Promise<any> {
return querySideloadingRegistry(['Set-ItemProperty -LiteralPath $RegistryPath -Name "' + manifestPath + '" -Value "' + manifestPath + '"']);
}
function getManifestsFromRegistry(): Promise<string[]> {
return new Promise(async(resolve, reject) => {
try {
const registryOutput = await querySideloadingRegistry(['Get-ItemProperty -LiteralPath $RegistryPath | ConvertTo-Json -Compress']);
if (!registryOutput || registryOutput.indexOf('{') === -1) {
resolve([]);
}
const registryJSON = JSON.parse(registryOutput);
let manifestPaths = [];
for (const name in registryJSON) {
// Manifests are inserted in the registry with matching name and value
if (registryJSON[name].toString().toLowerCase() === name.toString().toLowerCase()) {
manifestPaths.push(name);
}
}
resolve(manifestPaths);
} catch (err) {
reject(err);
}
});
}
function removeManifestFromRegistry(manifestPath: string): Promise<any> {
if (!manifestPath) {
return new Promise((resolve, reject) => {
return reject('No manifest was specified');
});
}
console.log(`Removing ${manifestPath}`);
return querySideloadingRegistry(['Remove-ItemProperty -LiteralPath $RegistryPath -Name "' + manifestPath + '" -ErrorAction SilentlyContinue']);
}
// GENERIC HELPER FUNCTIONS //
function isGuid (text: string): boolean {
const guidRegex = /^[0-9A-F]{8}[-]?([0-9A-F]{4}[-]?){3}[0-9A-F]{12}?/i;
return guidRegex.test(text);
}
function sideloadManifest(application: string, manifestPath: string): Promise<any> {
return new Promise(async (resolve, reject) => {
try {
if (fs.existsSync(manifestPath) && fs.lstatSync(manifestPath).isFile()) {
manifestPath = fs.realpathSync(manifestPath);
}
else {
return reject(['The manifest to sideload could not be found: ', manifestPath]);
}
const [parsedType, parsedGuid, parsedVersion] = await parseManifest(manifestPath);
await addManifest(application, manifestPath);
const templateFile = await generateTemplateFile(application, parsedType, parsedGuid, parsedVersion);
appInsightsClient.trackEvent({ name: 'open', properties: { guid: parsedGuid, version: parsedVersion }});
console.log(`Opening file ${templateFile}`);
opn(templateFile, { wait: false});
resolve();
}
catch (err) {
return reject(err);
}
});
}
function getAllIdsAndManifests(): Promise<Array<[string, string, string]>> {
let applications = [];
if (process.platform === 'win32') {
applications = [null];
}
else {
applications = Object.keys(applicationProperties);
}
return new Promise(async (resolve, reject) => {
try {
let allIdsAndManifests = [];
for (const application of applications) {
const idsAndManifests = await getIdsAndManifests(application);
for (const [id, manifest] of idsAndManifests) {
allIdsAndManifests.push([id, manifest, application]);
}
}
resolve(allIdsAndManifests);
} catch (err) {
return reject(err);
}
});
}
function validateManifest(manifestPath: string): Promise<string> {
return new Promise(async (resolve, reject) => {
try {
if (!fs.existsSync(manifestPath)) {
return reject(['The manifest to validate could not be found: ', manifestPath]);
}
const result = await officeAddinValidator.validateManifest(manifestPath);
resolve(result);
}
catch (err) {
return reject(err);
}
});
}
function getIdsAndManifests(application: string): Promise<Array<[string, string]>> {
return new Promise(async (resolve, reject) => {
try {
const manifests = await getManifests(application);
const ids = await getInfoForManifests(manifests);
let idsAndManifests = [];
for (let i = 0; i < manifests.length; i++) {
idsAndManifests.push([ids[i], manifests[i]]);
}
resolve(idsAndManifests);
} catch (err) {
return reject(err);
}
});
}
function parseManifest(manifestPath: string): Promise<[string, string, string]> {
return new Promise(async (resolve, reject) => {
try {
const parser = new xml2js.Parser();
let manifestBuffer;
// Parse the manifest and get the id and version
try {
manifestBuffer = await fs.readFile(manifestPath);
} catch (err) {
return reject(['Failed to read the manifest file: ', manifestPath]);
}
parser.parseString(manifestBuffer, (err, manifestXml) => {
if (!manifestXml || typeof(manifestXml) !== 'object') {
return reject(['Failed to parse the manifest file: ', manifestPath]);
}
else if (!('OfficeApp' in manifestXml)) {
return reject(['OfficeApp missing in manifest file: ', manifestPath]);
}
else if (!('$' in manifestXml['OfficeApp'] &&
typeof(manifestXml['OfficeApp']['$'] === 'object') &&
'xsi:type' in manifestXml['OfficeApp']['$'] &&
typeof(manifestXml['OfficeApp']['$']['xsi:type'] === 'string'))) {
return reject(['xsi:type missing in manifest file: ', manifestPath]);
}
else if (!('Id' in manifestXml['OfficeApp'] && manifestXml['OfficeApp']['Id'] instanceof Array)) {
return reject(['Id missing in in manifest file: ', manifestPath]);
}
else if (!('Version' in manifestXml['OfficeApp'] && manifestXml['OfficeApp']['Version'] instanceof Array)) {
return reject(['Version missing in in manifest file: ', manifestPath]);
}
const type = manifestXml['OfficeApp']['$']['xsi:type'];
const id = manifestXml['OfficeApp']['Id'][0];
const version = manifestXml['OfficeApp']['Version'][0];
if (!isGuid(id)) {
return reject(['Invalid Id ' + id + ' in manifest file: ', manifestPath]);
}
else if (type === 'MailApp') {
return reject('The manifest specified an Outlook add-in. Outlook Add-ins are not supported by this tool');
}
else if (type !== 'ContentApp' && type !== 'TaskPaneApp') {
return reject('The manifest must have xsi:type set to ContentApp or TaskPaneApp');
}
resolve ([type, id, version]);
});
}
catch (err) {
return reject(err);
}
});
}
function getInfoForManifests(manifestPaths: string[]): Promise<any> {
return new Promise(async (resolve, reject) => {
let ids = [];
for (let manifestPath of manifestPaths) {
try {
const [type, id, version] = await parseManifest(manifestPath);
ids.push(id);
}
catch (err) {
console.log(err);
ids.push(null);
}
}
resolve(ids);
});
}
/**
* makePathUnique
* @description Given a file path, returns a unique file path where the file doesn't exist by
* appending a period and a numeric suffix, starting from 2.
* @param tryToDelete If true, first try to delete the file if it exists.
*/
function makePathUnique(originalPath: string, tryToDelete: boolean = false): string {
let currentPath = originalPath;
let parsedPath = null;
let suffix = 1;
while (fs.existsSync(currentPath)) {
let deleted: boolean = false;
if (tryToDelete) {
try {
fs.removeSync(currentPath);
deleted = true;
console.log(`Deleted file: ${currentPath}`);
} catch (err) {
console.log(`File is in use: ${currentPath}`);
}
}
if (!deleted) {
++suffix;
if (parsedPath == null) {
parsedPath = path.parse(originalPath);
}
currentPath = path.join(parsedPath.dir, `${parsedPath.name}.${suffix}${parsedPath.ext}`);
}
}
return currentPath;
}
function generateTemplateFile(application: string, type: string, id: string, version: string): Promise<any> {
return new Promise(async (resolve, reject) => {
try {
if (Object.keys(applicationProperties).indexOf(application) < 0 ||
Object.keys(applicationProperties[application]).indexOf(type) < 0) {
return reject('The Add-in type ' + type + ' specified in the manifest is not supported for ' + application);
}
const defaultTemplateName = applicationProperties[application][type].templateName;
const webExtensionPath = applicationProperties[application][type].webExtensionPath;
const extension = path.extname(defaultTemplateName);
const templatePath = makePathUnique(path.join(os.tmpdir(), `${application} add-in ${id}${extension}`), true);
fs.ensureDirSync(path.dirname(templatePath));
console.log(`Generating file ${templatePath}`);
// Read the template
const templateBuffer = await fs.readFile(path.join(__filename, '..', '..', 'templates', defaultTemplateName));
const zip = await jszip.loadAsync(templateBuffer);
// Replace the placeholder ID and version
let webExtensionXml = await zip.file(webExtensionPath).async("text");
webExtensionXml = webExtensionXml.replace(/00000000-0000-0000-0000-000000000000/g, id);
webExtensionXml = webExtensionXml.replace(/1.0.0.0/g, version);
zip.file(webExtensionPath, webExtensionXml);
// Write the file
zip.generateNodeStream({type: 'nodebuffer', streamFiles: true})
.pipe(fs.createWriteStream(templatePath))
.on('finish', () => {
resolve(templatePath);
});
} catch (err) {
return reject(err);
}
});
}