forked from OfficeDev/Excel-Custom-Functions-JS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convertToSingleHost.js
235 lines (200 loc) · 6.81 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
/* global require, process, console */
const fs = require("fs");
const path = require("path");
const util = require("util");
const childProcess = require("child_process");
const manifestType = process.argv[2];
const projectName = process.argv[3];
let appId = process.argv[4];
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 removeTestInfraStructure() {
// 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`));
await updatePackageJsonFile();
await updateLaunchJsonFile();
// delete this script
await unlinkFileAsync("./convertToSingleHost.js");
await deleteSupportFiles();
}
async function updatePackageJsonFile() {
const packageJson = `./package.json`;
const data = await readFileAsync(packageJson, "utf8");
let content = JSON.parse(data);
// remove scripts that are unrelated to testing or this file
Object.keys(content.scripts).forEach(function (key) {
if (key === "convert-to-single-host" || 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(".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);
// Remove scripts that are only used with JSON manifest
delete content.scripts["signin"];
delete content.scripts["signout"];
// 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();
}
/**
* Remove test infrastructure and repo support files from project.
*/
removeTestInfraStructure().catch((err) => {
console.error(`Error: ${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) {
Promise.reject(stdout);
} else {
Promise.resolve();
}
});
}