-
Notifications
You must be signed in to change notification settings - Fork 64
/
convertToSingleHost.js
310 lines (266 loc) · 9.61 KB
/
convertToSingleHost.js
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
// NOTE: This script is generally in sync with other template repos (with minor differences) even though this template doesn't support different hosts.
// It's easier to maintain the script in one place and copy it over to other repos than to maintain multiple versions of the script.
/* global require, process, console */
const fs = require("fs");
const path = require("path");
const util = require("util");
const childProcess = require("child_process");
const hosts = ["excel"];
if (process.argv.length <= 2) {
const hostList = hosts.map((host) => `'${host}'`).join(", ");
console.log("SYNTAX: convertToSingleHost.js <host> <manifestType> <projectName> <appId>");
console.log();
console.log(` host (required): Specifies which Office app will host the add-in: ${hostList}`);
console.log(` manifestType: Specify the type of manifest to use: 'xml' or 'json'. Defaults to 'xml'`);
console.log(` projectName: The name of the project (use quotes when there are spaces in the name). Defaults to 'My Office Add-in'`);
console.log(` appId: The id of the project or 'random' to generate one. Defaults to 'random'`);
console.log();
process.exit(1);
}
const host = process.argv[2];
const manifestType = process.argv[3];
const projectName = process.argv[4];
let appId = process.argv[5];
const testPackages = [
"@types/mocha",
"@types/node",
"mocha",
"office-addin-mock",
"office-addin-test-helpers",
"office-addin-test-server",
"ts-node",
];
const readFileAsync = util.promisify(fs.readFile);
const unlinkFileAsync = util.promisify(fs.unlink);
const writeFileAsync = util.promisify(fs.writeFile);
async function modifyProjectForSingleHost(host) {
if (!host) {
throw new Error("The host was not provided.");
}
if (!hosts.includes(host)) {
throw new Error(`'${host}' is not a supported host.`);
}
await convertProjectToSingleHost(host);
await updatePackageJsonForSingleHost(host);
await updateLaunchJsonFile(host);
}
async function convertProjectToSingleHost(host) {
// NOTE: This template only supports Excel, so we don't need to deal with host specific files.
// Copy host-specific manifest over manifest.xml
// const manifestContent = await readFileAsync(`./manifest.${host}.xml`, "utf8");
// await writeFileAsync(`./manifest.xml`, manifestContent);
// Copy over host-specific taskpane code to taskpane.ts
// const srcContent = await readFileAsync(`./src/taskpane/${host}.ts`, "utf8");
// await writeFileAsync(`./src/taskpane/taskpane.ts`, srcContent);
// Delete all host-specific files
// hosts.forEach(async function (host) {
// await unlinkFileAsync(`./manifest.${host}.xml`);
// await unlinkFileAsync(`./src/taskpane/${host}.ts`);
// });
// Delete test folder
deleteFolder(path.resolve(`./test`));
// Delete the .github folder
deleteFolder(path.resolve(`./.github`));
// Delete CI/CD pipeline files
deleteFolder(path.resolve(`./.azure-devops`));
// Delete repo support files
await deleteSupportFiles();
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async function updatePackageJsonForSingleHost(host) {
// Update package.json to reflect selected host
const packageJson = `./package.json`;
const data = await readFileAsync(packageJson, "utf8");
let content = JSON.parse(data);
// Update 'config' section in package.json to use selected host
//content.config["app_to_debug"] = host;
// Remove 'engines' section
delete content.engines;
// Remove scripts that are unrelated to the selected host
Object.keys(content.scripts).forEach(function (key) {
if (key === "convert-to-single-host") {
delete content.scripts[key];
}
});
// Remove test-related scripts
Object.keys(content.scripts).forEach(function (key) {
if (key.includes("test")) {
delete content.scripts[key];
}
});
// Remove test-related packages
Object.keys(content.devDependencies).forEach(function (key) {
if (testPackages.includes(key)) {
delete content.devDependencies[key];
}
});
// Write updated JSON to file
await writeFileAsync(packageJson, JSON.stringify(content, null, 2));
}
async function updateLaunchJsonFile(host) {
// Remove 'Debug Tests' configuration from launch.json
const launchJson = `.vscode/launch.json`;
const launchJsonContent = await readFileAsync(launchJson, "utf8");
let content = JSON.parse(launchJsonContent);
content.configurations = content.configurations.filter(function (config) {
return config.name.startsWith(getHostName(host));
});
await writeFileAsync(launchJson, JSON.stringify(content, null, 2));
}
function getHostName(host) {
switch (host) {
case "excel":
return "Excel";
case "onenote":
return "OneNote";
case "outlook":
return "Outlook";
case "powerpoint":
return "PowerPoint";
case "project":
return "Project";
case "word":
return "Word";
default:
throw new Error(`'${host}' is not a supported host.`);
}
}
function deleteFolder(folder) {
try {
if (fs.existsSync(folder)) {
fs.readdirSync(folder).forEach(function (file) {
const curPath = `${folder}/${file}`;
if (fs.lstatSync(curPath).isDirectory()) {
deleteFolder(curPath);
} else {
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(folder);
}
} catch (err) {
throw new Error(`Unable to delete folder "${folder}".\n${err}`);
}
}
async function deleteSupportFiles() {
await unlinkFileAsync("CONTRIBUTING.md");
await unlinkFileAsync("LICENSE");
await unlinkFileAsync("README.md");
await unlinkFileAsync("SECURITY.md");
await unlinkFileAsync("./convertToSingleHost.js");
await unlinkFileAsync(".npmrc");
await unlinkFileAsync("package-lock.json");
}
async function deleteJSONManifestRelatedFiles() {
await unlinkFileAsync("manifest.json");
await unlinkFileAsync("assets/color.png");
await unlinkFileAsync("assets/outline.png");
}
async function deleteXMLManifestRelatedFiles() {
await unlinkFileAsync("manifest.xml");
}
async function updatePackageJsonForXMLManifest() {
const packageJson = `./package.json`;
const data = await readFileAsync(packageJson, "utf8");
let content = JSON.parse(data);
// Write updated JSON to file
await writeFileAsync(packageJson, JSON.stringify(content, null, 2));
}
async function updatePackageJsonForJSONManifest() {
const packageJson = `./package.json`;
const data = await readFileAsync(packageJson, "utf8");
let content = JSON.parse(data);
// Remove special start scripts
Object.keys(content.scripts).forEach(function (key) {
if (key.includes("start:")) {
delete content.scripts[key];
}
});
// Change manifest file name extension
content.scripts.start = "office-addin-debugging start manifest.json";
content.scripts.stop = "office-addin-debugging stop manifest.json";
content.scripts.validate = "office-addin-manifest validate manifest.json";
// Write updated JSON to file
await writeFileAsync(packageJson, JSON.stringify(content, null, 2));
}
async function updateTasksJsonFileForJSONManifest() {
const tasksJson = `.vscode/tasks.json`;
const data = await readFileAsync(tasksJson, "utf8");
let content = JSON.parse(data);
content.tasks.forEach(function (task) {
if (task.label.startsWith("Build")) {
task.dependsOn = ["Install"];
}
if (task.label === "Debug: Outlook Desktop") {
task.script = "start";
task.dependsOn = ["Check OS", "Install"];
}
});
const checkOSTask = {
label: "Check OS",
type: "shell",
windows: {
command: "echo 'Sideloading in Outlook on Windows is supported'",
},
linux: {
command: "echo 'Sideloading on Linux is not supported' && exit 1",
},
osx: {
command: "echo 'Sideloading in Outlook on Mac is not supported' && exit 1",
},
presentation: {
clear: true,
panel: "dedicated",
},
};
content.tasks.push(checkOSTask);
await writeFileAsync(tasksJson, JSON.stringify(content, null, 2));
}
async function updateWebpackConfigForJSONManifest() {
const webPack = `webpack.config.js`;
const webPackContent = await readFileAsync(webPack, "utf8");
const updatedContent = webPackContent.replace(".xml", ".json");
await writeFileAsync(webPack, updatedContent);
}
async function modifyProjectForJSONManifest() {
await updatePackageJsonForJSONManifest();
await updateWebpackConfigForJSONManifest();
await updateTasksJsonFileForJSONManifest();
await deleteXMLManifestRelatedFiles();
}
/**
* Modify the project so that it only supports a single host.
* @param host The host to support.
*/
modifyProjectForSingleHost(host).catch((err) => {
console.error(`Error modifying for single host: ${err instanceof Error ? err.message : err}`);
process.exitCode = 1;
});
let manifestPath = "manifest.xml";
// Uncomment when template supports JSON manifest
// if (host !== "outlook" || manifestType !== "json") {
// Remove things that are only relevant to JSON manifest
deleteJSONManifestRelatedFiles();
updatePackageJsonForXMLManifest();
// } else {
// manifestPath = "manifest.json";
// modifyProjectForJSONManifest().catch((err) => {
// console.error(`Error modifying for JSON manifest: ${err instanceof Error ? err.message : err}`);
// process.exitCode = 1;
// });
// }
if (projectName) {
if (!appId) {
appId = "random";
}
// Modify the manifest to include the name and id of the project
const cmdLine = `npx office-addin-manifest modify ${manifestPath} -g ${appId} -d "${projectName}"`;
childProcess.exec(cmdLine, (error, stdout) => {
if (error) {
console.error(`Error updating the manifest: ${error}`);
process.exitCode = 1;
} else {
console.log(stdout);
}
});
}