|
| 1 | +const fs = require("fs"); |
| 2 | +const host = process.argv[2]; |
| 3 | +const hosts = ["excel", "onenote", "outlook", "powerpoint", "project", "word"]; |
| 4 | +const util = require("util"); |
| 5 | +const readFileAsync = util.promisify(fs.readFile); |
| 6 | +const unlinkFileAsync = util.promisify(fs.unlink); |
| 7 | +const writeFileAsync = util.promisify(fs.writeFile); |
| 8 | + |
| 9 | +async function modifyProjectForSingleHost(host) { |
| 10 | + if (!host) { |
| 11 | + throw new Error("The host was not provided."); |
| 12 | + } |
| 13 | + if (!hosts.includes(host)) { |
| 14 | + throw new Error(`'${host}' is not a supported host.`); |
| 15 | + } |
| 16 | + await convertProjectToSingleHost(host); |
| 17 | + await updatePackageJsonForSingleHost(host); |
| 18 | +} |
| 19 | + |
| 20 | +async function convertProjectToSingleHost(host) { |
| 21 | + // copy host-specific manifest over manifest.xml |
| 22 | + const manifestContent = await readFileAsync(`./manifest.${host}.xml`, "utf8"); |
| 23 | + await writeFileAsync(`./manifest.xml`, manifestContent); |
| 24 | + |
| 25 | + // copy over host-specific taskpane code to taskpane.ts |
| 26 | + const srcContent = await readFileAsync(`./src/taskpane/${host}.ts`, "utf8"); |
| 27 | + await writeFileAsync(`./src/taskpane/taskpane.ts`, srcContent); |
| 28 | + |
| 29 | + // delete all host-specific files |
| 30 | + hosts.forEach(async function(host) { |
| 31 | + await unlinkFileAsync(`./manifest.${host}.xml`); |
| 32 | + await unlinkFileAsync(`./src/taskpane/${host}.ts`); |
| 33 | + }); |
| 34 | + |
| 35 | + // delete this script |
| 36 | + await unlinkFileAsync("./convertToSingleHost.js"); |
| 37 | +} |
| 38 | + |
| 39 | +async function updatePackageJsonForSingleHost(host) { |
| 40 | + // update package.json to reflect selected host |
| 41 | + const packageJson = `./package.json`; |
| 42 | + const data = await readFileAsync(packageJson, "utf8"); |
| 43 | + let content = JSON.parse(data); |
| 44 | + |
| 45 | + // update 'config' section in package.json to use selected host |
| 46 | + content.config["app-to-debug"] = host; |
| 47 | + |
| 48 | + // update sideload and unload scripts to use selected host. |
| 49 | + ["sideload", "unload"].forEach(key => { |
| 50 | + content.scripts[key] = content.scripts[`${key}:${host}`]; |
| 51 | + }); |
| 52 | + |
| 53 | + // remove scripts that are unrelated to the selected host |
| 54 | + Object.keys(content.scripts).forEach(function(key) { |
| 55 | + if (key.startsWith("sideload:") |
| 56 | + || key.startsWith("unload:") |
| 57 | + || key === "convert-to-single-host" |
| 58 | + ) { |
| 59 | + delete content.scripts[key]; |
| 60 | + } |
| 61 | + }); |
| 62 | + |
| 63 | + // write updated json to file |
| 64 | + await writeFileAsync(packageJson, JSON.stringify(content, null, 2)); |
| 65 | +} |
| 66 | + |
| 67 | + |
| 68 | +/** |
| 69 | + * Modify the project so that it only supports a single host. |
| 70 | + * @param host The host to support. |
| 71 | + */ |
| 72 | +modifyProjectForSingleHost(host).catch(err => { |
| 73 | + console.error(`Error: ${err instanceof Error ? err.message : err}`); |
| 74 | + process.exitCode = 1; |
| 75 | +}); |
0 commit comments