33const fs = require ( "fs" ) ;
44const path = require ( "path" ) ;
55const util = require ( "util" ) ;
6+ const childProcess = require ( "child_process" ) ;
7+
8+ const manifestType = process . argv [ 2 ] ;
9+ const projectName = process . argv [ 3 ] ;
10+ let appId = process . argv [ 4 ] ;
611const testPackages = [
712 "@types/mocha" ,
813 "@types/node" ,
@@ -18,12 +23,13 @@ const unlinkFileAsync = util.promisify(fs.unlink);
1823const writeFileAsync = util . promisify ( fs . writeFile ) ;
1924
2025async function removeTestInfraStructure ( ) {
26+ // Delete test folder
2127 deleteFolder ( path . resolve ( `./test` ) ) ;
2228
23- // delete the .github folder
29+ // Delete the .github folder
2430 deleteFolder ( path . resolve ( `./.github` ) ) ;
2531
26- // delete CI/CD pipeline files
32+ // Delete CI/CD pipeline files
2733 deleteFolder ( path . resolve ( `./.azure-devops` ) ) ;
2834
2935 await updatePackageJsonFile ( ) ;
@@ -45,19 +51,19 @@ async function updatePackageJsonFile() {
4551 }
4652 } ) ;
4753
48- // remove test-related packages
54+ // Remove test-related packages
4955 Object . keys ( content . devDependencies ) . forEach ( function ( key ) {
5056 if ( testPackages . includes ( key ) ) {
5157 delete content . devDependencies [ key ] ;
5258 }
5359 } ) ;
5460
55- // write updated json to file
61+ // Write updated JSON to file
5662 await writeFileAsync ( packageJson , JSON . stringify ( content , null , 2 ) ) ;
5763}
5864
5965async function updateLaunchJsonFile ( ) {
60- // remove 'Debug Tests' configuration from launch.json
66+ // Remove 'Debug Tests' configuration from launch.json
6167 const launchJson = `.vscode/launch.json` ;
6268 const launchJsonContent = await readFileAsync ( launchJson , "utf8" ) ;
6369 const regex = / ( .+ { \r ? \n .* " n a m e " : " D e b u g (?: U I | U n i t ) T e s t s " , \r ? \n (?: .* \r ? \n ) * ?.* } , .* \r ? \n ) / gm;
@@ -93,10 +99,137 @@ async function deleteSupportFiles() {
9399 await unlinkFileAsync ( "package-lock.json" ) ;
94100}
95101
102+ async function deleteJSONManifestRelatedFiles ( ) {
103+ await unlinkFileAsync ( "manifest.json" ) ;
104+ await unlinkFileAsync ( "assets/color.png" ) ;
105+ await unlinkFileAsync ( "assets/outline.png" ) ;
106+ }
107+
108+ async function deleteXMLManifestRelatedFiles ( ) {
109+ await unlinkFileAsync ( "manifest.xml" ) ;
110+ }
111+
112+ async function updatePackageJsonForXMLManifest ( ) {
113+ const packageJson = `./package.json` ;
114+ const data = await readFileAsync ( packageJson , "utf8" ) ;
115+ let content = JSON . parse ( data ) ;
116+
117+ // Remove scripts that are only used with JSON manifest
118+ delete content . scripts [ "signin" ] ;
119+ delete content . scripts [ "signout" ] ;
120+
121+ // Write updated JSON to file
122+ await writeFileAsync ( packageJson , JSON . stringify ( content , null , 2 ) ) ;
123+ }
124+
125+ async function updatePackageJsonForJSONManifest ( ) {
126+ const packageJson = `./package.json` ;
127+ const data = await readFileAsync ( packageJson , "utf8" ) ;
128+ let content = JSON . parse ( data ) ;
129+
130+ // Remove special start scripts
131+ Object . keys ( content . scripts ) . forEach ( function ( key ) {
132+ if ( key . includes ( "start:" ) ) {
133+ delete content . scripts [ key ] ;
134+ }
135+ } ) ;
136+
137+ // Change manifest file name extension
138+ content . scripts . start = "office-addin-debugging start manifest.json" ;
139+ content . scripts . stop = "office-addin-debugging stop manifest.json" ;
140+ content . scripts . validate = "office-addin-manifest validate manifest.json" ;
141+
142+ // Write updated JSON to file
143+ await writeFileAsync ( packageJson , JSON . stringify ( content , null , 2 ) ) ;
144+ }
145+
146+ async function updateTasksJsonFileForJSONManifest ( ) {
147+ const tasksJson = `.vscode/tasks.json` ;
148+ const data = await readFileAsync ( tasksJson , "utf8" ) ;
149+ let content = JSON . parse ( data ) ;
150+
151+ content . tasks . forEach ( function ( task ) {
152+ if ( task . label . startsWith ( "Build" ) ) {
153+ task . dependsOn = [ "Install" ] ;
154+ }
155+ if ( task . label === "Debug: Outlook Desktop" ) {
156+ task . script = "start" ;
157+ task . dependsOn = [ "Check OS" , "Install" ] ;
158+ }
159+ } ) ;
160+
161+ const checkOSTask = {
162+ label : "Check OS" ,
163+ type : "shell" ,
164+ windows : {
165+ command : "echo 'Sideloading in Outlook on Windows is supported'" ,
166+ } ,
167+ linux : {
168+ command : "echo 'Sideloading on Linux is not supported' && exit 1" ,
169+ } ,
170+ osx : {
171+ command : "echo 'Sideloading in Outlook on Mac is not supported' && exit 1" ,
172+ } ,
173+ presentation : {
174+ clear : true ,
175+ panel : "dedicated" ,
176+ } ,
177+ } ;
178+
179+ content . tasks . push ( checkOSTask ) ;
180+ await writeFileAsync ( tasksJson , JSON . stringify ( content , null , 2 ) ) ;
181+ }
182+
183+ async function updateWebpackConfigForJSONManifest ( ) {
184+ const webPack = `webpack.config.js` ;
185+ const webPackContent = await readFileAsync ( webPack , "utf8" ) ;
186+ const updatedContent = webPackContent . replace ( ".xml" , ".json" ) ;
187+ await writeFileAsync ( webPack , updatedContent ) ;
188+ }
189+
190+ async function modifyProjectForJSONManifest ( ) {
191+ await updatePackageJsonForJSONManifest ( ) ;
192+ await updateWebpackConfigForJSONManifest ( ) ;
193+ await updateTasksJsonFileForJSONManifest ( ) ;
194+ await deleteXMLManifestRelatedFiles ( ) ;
195+ }
196+
197+
96198/**
97199 * Remove test infrastructure and repo support files from project.
98200 */
99201removeTestInfraStructure ( ) . catch ( ( err ) => {
100202 console . error ( `Error: ${ err instanceof Error ? err . message : err } ` ) ;
101203 process . exitCode = 1 ;
102204} ) ;
205+
206+ let manifestPath = "manifest.xml" ;
207+
208+ // Uncomment when template supports JSON manifest
209+ // if (host !== "outlook" || manifestType !== "json") {
210+ // Remove things that are only relevant to JSON manifest
211+ deleteJSONManifestRelatedFiles ( ) ;
212+ updatePackageJsonForXMLManifest ( ) ;
213+ // } else {
214+ // manifestPath = "manifest.json";
215+ // modifyProjectForJSONManifest().catch((err) => {
216+ // console.error(`Error modifying for JSON manifest: ${err instanceof Error ? err.message : err}`);
217+ // process.exitCode = 1;
218+ // });
219+ // }
220+
221+ if ( projectName ) {
222+ if ( ! appId ) {
223+ appId = "random" ;
224+ }
225+
226+ // Modify the manifest to include the name and id of the project
227+ const cmdLine = `npx office-addin-manifest modify ${ manifestPath } -g ${ appId } -d ${ projectName } ` ;
228+ childProcess . exec ( cmdLine , ( error , stdout ) => {
229+ if ( error ) {
230+ Promise . reject ( stdout ) ;
231+ } else {
232+ Promise . resolve ( ) ;
233+ }
234+ } ) ;
235+ }
0 commit comments