forked from OfficeDev/Excel-Custom-Functions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvertToSingleHost.js
81 lines (70 loc) · 2.69 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
const fs = require("fs");
const path = require("path");
const util = require("util");
const testPackages = ["@types/mocha", "@types/node", "current-processes", "mocha", "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() {
deleteFolder(path.resolve(`./test`));
// delete the .github folder
deleteFolder(path.resolve(`./.github`));
await updatePackageJsonFile();
await updateLaunchJsonFile();
// delete this script
await unlinkFileAsync("./convertToSingleHost.js");
}
async function updatePackageJsonFile() {
// update package.json to reflect selected host
const packageJson = `./package.json`;
const data = await readFileAsync(packageJson, "utf8");
let content = JSON.parse(data);
// remove scripts that are unrelated to the selected host
Object.keys(content.scripts).forEach(function (key) {
if (key === "convert-to-single-host" || key === "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 = /"configurations": \[\r?\n(.*{(.*\r?\n)*?.*"name": "Debug Tests",\r?\n(.*\r?\n)*?.*},)/gm;
const updatedContent = launchJsonContent.replace(regex, `"configurations": [`);
await writeFileAsync(launchJson, updatedContent);
}
function deleteFolder(folder) {
try {
if (fs.existsSync(folder)) {
fs.readdirSync(folder).forEach(function (file, index) {
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}`);
}
}
/**
* Remove test infrastructure from project.
*/
removeTestInfraStructure().catch(err => {
console.error(`Error: ${err instanceof Error ? err.message : err}`);
process.exitCode = 1;
});