Skip to content

Commit 41072ff

Browse files
TCourtneyOwenakrantz
authored andcommitted
VSO. 3341632: Code to make template target a single host needs to be in the templates themselves (#33)
`npm run convert-to-single-host {host}` will modify the project so it only targets a single host. This will be used by generator-office so that the code to do this resides in the template itself.
1 parent 9537021 commit 41072ff

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

convertToSingleHost.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
});

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"scripts": {
1515
"build": "webpack -p --mode production",
1616
"build-dev": "webpack --mode development",
17+
"convert-to-single-host": "node convertToSingleHost.js",
1718
"dev-server": "webpack-dev-server --mode development",
1819
"sideload": "echo Please specify which app using 'npm run sideload:app'.",
1920
"sideload:excel": "office-toolbox sideload -m manifest.xml -a excel",

0 commit comments

Comments
 (0)