-
Notifications
You must be signed in to change notification settings - Fork 0
/
convertToSingleHost.js
181 lines (157 loc) · 5.93 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
/* global require, process, console */
const convertTest = process.argv[3] === "convert-test";
const fs = require("fs");
const host = process.argv[2];
const hosts = ["excel", "onenote", "outlook", "powerpoint", "project", "word"];
const path = require("path");
const util = require("util");
const testPackages = [
"@types/mocha",
"@types/node",
"current-processes",
"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);
if (!convertTest) {
await updateLaunchJsonFile();
}
}
async function convertProjectToSingleHost(host) {
// 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/app/${host}.app.component.ts`, "utf8");
await writeFileAsync(`./src/taskpane/app/app.component.ts`, srcContent);
// delete all test files by default for now - eventually we want to convert the tests by default
if (convertTest && (host === "excel" || host === "word")) {
// copy over host-specific taskpane test code to test-taskpane.ts
const testTaskpaneContent = await readFileAsync(`./test/src/test.${host}.app.component.ts`, "utf8");
const updatedTestTaskpaneContent = testTaskpaneContent.replace(
`../../src/taskpane/app/${host}.app.component`,
`../../src/taskpane/app/app.component`
);
await writeFileAsync(`./test/src/test.app.component.ts`, updatedTestTaskpaneContent);
// update ui-test.ts to only run against specified host
const testContent = await readFileAsync(`./test/ui-test.ts`, "utf8");
const updatedTestContent = testContent.replace(`const hosts = ["Excel", "Word"]`, `const hosts = ["${host == "excel" ? "Excel" : "Word"}"]`);
await writeFileAsync(`./test/ui-test.ts`, updatedTestContent);
// delete all host-specific test files after converting to single host
hosts.forEach(async function (host) {
if (host == "excel" || host == "word") {
await unlinkFileAsync(`./test/src/test.${host}.app.component.ts`);
}
});
} else {
deleteFolder(path.resolve(`./test`));
}
// delete all host-specific files
hosts.forEach(async function (host) {
await unlinkFileAsync(`./manifest.${host}.xml`);
await unlinkFileAsync(`./src/taskpane/app/${host}.app.component.ts`);
});
// delete the .github folder
deleteFolder(path.resolve(`./.github`));
// delete CI/CD pipeline files
deleteFolder(path.resolve(`./.azure-devops`));
// delete repo support files
await deleteSupportFiles();
}
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;
// update sideload and unload scripts to use selected host.
["sideload", "unload"].forEach((key) => {
content.scripts[key] = content.scripts[`${key}:${host}`];
});
// remove scripts that are unrelated to the selected host
Object.keys(content.scripts).forEach(function (key) {
if (
key.startsWith("sideload:") ||
key.startsWith("unload:") ||
key === "convert-to-single-host" ||
key === "start:desktop:outlook"
) {
delete content.scripts[key];
}
});
if (!convertTest) {
// 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() {
// remove 'Debug Tests' configuration from launch.json
const launchJson = `.vscode/launch.json`;
const launchJsonContent = await readFileAsync(launchJson, "utf8");
const regex = /(.+{\r?\n.*"name": "Debug (?:UI|Unit) Tests",\r?\n(?:.*\r?\n)*?.*},.*\r?\n)/gm;
const updatedContent = launchJsonContent.replace(regex, "");
await writeFileAsync(launchJson, updatedContent);
}
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");
}
/**
* Modify the project so that it only supports a single host.
* @param host The host to support.
*/
modifyProjectForSingleHost(host).catch((err) => {
console.error(`Error: ${err instanceof Error ? err.message : err}`);
process.exitCode = 1;
});