1+ // NOTE: This script is generally in sync with other template repos (with minor differences) even though this template doesn't support different hosts.
2+ // It's easier to maintain the script in one place and copy it over to other repos than to maintain multiple versions of the script.
3+
14/* global require, process, console */
25
36const fs = require ( "fs" ) ;
47const path = require ( "path" ) ;
58const util = require ( "util" ) ;
69const childProcess = require ( "child_process" ) ;
710
8- const manifestType = process . argv [ 2 ] ;
9- const projectName = process . argv [ 3 ] ;
10- let appId = process . argv [ 4 ] ;
11+ const host = process . argv [ 2 ] ;
12+ const manifestType = process . argv [ 3 ] ;
13+ const projectName = process . argv [ 4 ] ;
14+ let appId = process . argv [ 5 ] ;
15+ const hosts = [ "excel" ] ;
1116const testPackages = [
1217 "@types/mocha" ,
1318 "@types/node" ,
14- "current-processes" ,
1519 "mocha" ,
1620 "office-addin-mock" ,
1721 "office-addin-test-helpers" ,
@@ -22,7 +26,35 @@ const readFileAsync = util.promisify(fs.readFile);
2226const unlinkFileAsync = util . promisify ( fs . unlink ) ;
2327const writeFileAsync = util . promisify ( fs . writeFile ) ;
2428
25- async function removeTestInfraStructure ( ) {
29+ async function modifyProjectForSingleHost ( host ) {
30+ if ( ! host ) {
31+ throw new Error ( "The host was not provided." ) ;
32+ }
33+ if ( ! hosts . includes ( host ) ) {
34+ throw new Error ( `'${ host } ' is not a supported host.` ) ;
35+ }
36+ await convertProjectToSingleHost ( host ) ;
37+ await updatePackageJsonForSingleHost ( host ) ;
38+ await updateLaunchJsonFile ( host ) ;
39+ }
40+
41+ async function convertProjectToSingleHost ( host ) {
42+ // NOTE: This template only supports Excel, so we don't need to deal with host specific files.
43+
44+ // Copy host-specific manifest over manifest.xml
45+ // const manifestContent = await readFileAsync(`./manifest.${host}.xml`, "utf8");
46+ // await writeFileAsync(`./manifest.xml`, manifestContent);
47+
48+ // Copy over host-specific taskpane code to taskpane.ts
49+ // const srcContent = await readFileAsync(`./src/taskpane/${host}.ts`, "utf8");
50+ // await writeFileAsync(`./src/taskpane/taskpane.ts`, srcContent);
51+
52+ // Delete all host-specific files
53+ // hosts.forEach(async function (host) {
54+ // await unlinkFileAsync(`./manifest.${host}.xml`);
55+ // await unlinkFileAsync(`./src/taskpane/${host}.ts`);
56+ // });
57+
2658 // Delete test folder
2759 deleteFolder ( path . resolve ( `./test` ) ) ;
2860
@@ -32,21 +64,33 @@ async function removeTestInfraStructure() {
3264 // Delete CI/CD pipeline files
3365 deleteFolder ( path . resolve ( `./.azure-devops` ) ) ;
3466
35- await updatePackageJsonFile ( ) ;
36- await updateLaunchJsonFile ( "excel" ) ;
37- // delete this script
38- await unlinkFileAsync ( "./convertToSingleHost.js" ) ;
67+ // Delete repo support files
3968 await deleteSupportFiles ( ) ;
4069}
4170
42- async function updatePackageJsonFile ( ) {
71+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
72+ async function updatePackageJsonForSingleHost ( host ) {
73+ // Update package.json to reflect selected host
4374 const packageJson = `./package.json` ;
4475 const data = await readFileAsync ( packageJson , "utf8" ) ;
4576 let content = JSON . parse ( data ) ;
4677
47- // remove scripts that are unrelated to testing or this file
78+ // Update 'config' section in package.json to use selected host
79+ //content.config["app_to_debug"] = host;
80+
81+ // Remove 'engines' section
82+ delete content . engines ;
83+
84+ // Remove scripts that are unrelated to the selected host
4885 Object . keys ( content . scripts ) . forEach ( function ( key ) {
49- if ( key === "convert-to-single-host" || key . includes ( "test" ) ) {
86+ if ( key === "convert-to-single-host" ) {
87+ delete content . scripts [ key ] ;
88+ }
89+ } ) ;
90+
91+ // Remove test-related scripts
92+ Object . keys ( content . scripts ) . forEach ( function ( key ) {
93+ if ( key . includes ( "test" ) ) {
5094 delete content . scripts [ key ] ;
5195 }
5296 } ) ;
@@ -116,6 +160,7 @@ async function deleteSupportFiles() {
116160 await unlinkFileAsync ( "LICENSE" ) ;
117161 await unlinkFileAsync ( "README.md" ) ;
118162 await unlinkFileAsync ( "SECURITY.md" ) ;
163+ await unlinkFileAsync ( "./convertToSingleHost.js" ) ;
119164 await unlinkFileAsync ( ".npmrc" ) ;
120165 await unlinkFileAsync ( "package-lock.json" ) ;
121166}
@@ -211,12 +256,12 @@ async function modifyProjectForJSONManifest() {
211256 await deleteXMLManifestRelatedFiles ( ) ;
212257}
213258
214-
215259/**
216- * Remove test infrastructure and repo support files from project.
260+ * Modify the project so that it only supports a single host.
261+ * @param host The host to support.
217262 */
218- removeTestInfraStructure ( ) . catch ( ( err ) => {
219- console . error ( `Error: ${ err instanceof Error ? err . message : err } ` ) ;
263+ modifyProjectForSingleHost ( host ) . catch ( ( err ) => {
264+ console . error ( `Error modifying for single host : ${ err instanceof Error ? err . message : err } ` ) ;
220265 process . exitCode = 1 ;
221266} ) ;
222267
@@ -241,7 +286,7 @@ if (projectName) {
241286 }
242287
243288 // Modify the manifest to include the name and id of the project
244- const cmdLine = `npx office-addin-manifest modify ${ manifestPath } -g ${ appId } -d ${ projectName } ` ;
289+ const cmdLine = `npx office-addin-manifest modify ${ manifestPath } -g ${ appId } -d " ${ projectName } " ` ;
245290 childProcess . exec ( cmdLine , ( error , stdout ) => {
246291 if ( error ) {
247292 console . error ( `Error updating the manifest: ${ error } ` ) ;
0 commit comments