1
+ const convertTest = process . argv [ 3 ] === "convert-test" ;
1
2
const fs = require ( "fs" ) ;
2
3
const host = process . argv [ 2 ] ;
3
4
const hosts = [ "excel" , "onenote" , "outlook" , "powerpoint" , "project" , "word" ] ;
5
+ const path = require ( "path" ) ;
4
6
const util = require ( "util" ) ;
7
+ const testPackages = [ "@types/mocha" , "@types/node" , "current-processes" , "mocha" , "office-addin-test-helpers" ,
8
+ "office-addin-test-server" , "ts-node" ] ;
5
9
const readFileAsync = util . promisify ( fs . readFile ) ;
6
10
const unlinkFileAsync = util . promisify ( fs . unlink ) ;
7
11
const writeFileAsync = util . promisify ( fs . writeFile ) ;
@@ -15,6 +19,9 @@ async function modifyProjectForSingleHost(host) {
15
19
}
16
20
await convertProjectToSingleHost ( host ) ;
17
21
await updatePackageJsonForSingleHost ( host ) ;
22
+ if ( ! convertTest ) {
23
+ await updateLaunchJsonFile ( ) ;
24
+ }
18
25
}
19
26
20
27
async function convertProjectToSingleHost ( host ) {
@@ -26,8 +33,31 @@ async function convertProjectToSingleHost(host) {
26
33
const srcContent = await readFileAsync ( `./src/taskpane/${ host } .ts` , "utf8" ) ;
27
34
await writeFileAsync ( `./src/taskpane/taskpane.ts` , srcContent ) ;
28
35
36
+ // delete all test files by default for now - eventually we want to convert the tests by default
37
+ if ( convertTest && ( host === "excel" || host === "word" ) ) {
38
+ // copy over host-specific taskpane test code to test-taskpane.ts
39
+ const testTaskpaneContent = await readFileAsync ( `./test/src/${ host } -test-taskpane.ts` , "utf8" ) ;
40
+ const updatedTestTaskpaneContent = testTaskpaneContent . replace ( `../../src/taskpane/${ host } ` , `../../src/taskpane/taskpane` ) ;
41
+ await writeFileAsync ( `./test/src/test-taskpane.ts` , updatedTestTaskpaneContent ) ;
42
+
43
+ // update ui-test.ts to only run against specified host
44
+ const testContent = await readFileAsync ( `./test/ui-test.ts` , "utf8" ) ;
45
+ const updatedTestContent = testContent . replace ( `const hosts = ["Excel", "Word"]` , `const hosts = ["${ host } "]` ) ;
46
+ await writeFileAsync ( `./test/ui-test.ts` , updatedTestContent ) ;
47
+
48
+ // delete all host-specific test files after converting to single host
49
+ hosts . forEach ( async function ( host ) {
50
+ if ( host == "excel" || host == "word" ) {
51
+ await unlinkFileAsync ( `./test/src/${ host } -test-taskpane.ts` ) ;
52
+ }
53
+ } ) ;
54
+ }
55
+ else {
56
+ deleteFolder ( path . resolve ( `${ process . cwd ( ) } /test` ) ) ;
57
+ }
58
+
29
59
// delete all host-specific files
30
- hosts . forEach ( async function ( host ) {
60
+ hosts . forEach ( async function ( host ) {
31
61
await unlinkFileAsync ( `./manifest.${ host } .xml` ) ;
32
62
await unlinkFileAsync ( `./src/taskpane/${ host } .ts` ) ;
33
63
} ) ;
@@ -51,25 +81,69 @@ async function updatePackageJsonForSingleHost(host) {
51
81
} ) ;
52
82
53
83
// 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:" )
84
+ Object . keys ( content . scripts ) . forEach ( function ( key ) {
85
+ if ( key . startsWith ( "sideload:" )
86
+ || key . startsWith ( "unload:" )
57
87
|| key === "convert-to-single-host"
58
88
) {
59
89
delete content . scripts [ key ] ;
60
90
}
61
91
} ) ;
62
92
93
+ if ( ! convertTest ) {
94
+ // remove test-related scripts
95
+ Object . keys ( content . scripts ) . forEach ( function ( key ) {
96
+ if ( key . includes ( "test" ) ) {
97
+ delete content . scripts [ key ] ;
98
+ }
99
+ } ) ;
100
+
101
+ // remove test-related packages
102
+ Object . keys ( content . devDependencies ) . forEach ( function ( key ) {
103
+ if ( testPackages . includes ( key ) ) {
104
+ delete content . devDependencies [ key ]
105
+ }
106
+ } ) ;
107
+ }
108
+
63
109
// write updated json to file
64
110
await writeFileAsync ( packageJson , JSON . stringify ( content , null , 2 ) ) ;
65
111
}
66
112
113
+ async function updateLaunchJsonFile ( ) {
114
+ // remove 'Debug Tests' configuration from launch.json
115
+ const launchJson = `.vscode/launch.json` ;
116
+ const launchJsonContent = await readFileAsync ( launchJson , "utf8" ) ;
117
+ const regex = / " c o n f i g u r a t i o n s " : \[ \r ? \n ( .* { ( .* \r ? \n ) * ?.* " n a m e " : " D e b u g T e s t s " , \r ? \n ( .* \r ? \n ) * ?.* } , ) / gm;
118
+ const updatedContent = launchJsonContent . replace ( regex , `"configurations": [` ) ;
119
+ await writeFileAsync ( launchJson , updatedContent ) ;
120
+ }
121
+
122
+ function deleteFolder ( folder ) {
123
+ try {
124
+ if ( fs . existsSync ( folder ) ) {
125
+ fs . readdirSync ( folder ) . forEach ( function ( file , index ) {
126
+ const curPath = `${ folder } /${ file } ` ;
127
+
128
+ if ( fs . lstatSync ( curPath ) . isDirectory ( ) ) {
129
+ deleteFolder ( curPath ) ;
130
+ }
131
+ else {
132
+ fs . unlinkSync ( curPath ) ;
133
+ }
134
+ } ) ;
135
+ fs . rmdirSync ( folder ) ;
136
+ }
137
+ } catch ( err ) {
138
+ throw new Error ( `Unable to delete folder "${ folder } ".\n${ err } ` ) ;
139
+ }
140
+ }
67
141
68
142
/**
69
143
* Modify the project so that it only supports a single host.
70
144
* @param host The host to support.
71
145
*/
72
146
modifyProjectForSingleHost ( host ) . catch ( err => {
73
- console . error ( `Error: ${ err instanceof Error ? err . message : err } ` ) ;
74
- process . exitCode = 1 ;
147
+ console . error ( `Error: ${ err instanceof Error ? err . message : err } ` ) ;
148
+ process . exitCode = 1 ;
75
149
} ) ;
0 commit comments