1
1
const { existsSync } = require ( 'node:fs' )
2
- const { readFile, writeFile } = require ( 'node:fs/promises' )
2
+ const { readFile, writeFile, rename, rm } = require ( 'node:fs/promises' )
3
+ const { parse, join } = require ( 'node:path' )
3
4
4
5
const { satisfies } = require ( 'semver' )
5
6
@@ -32,6 +33,8 @@ export const reqHandler = createRequestHandler(async (request: Request, context:
32
33
`
33
34
34
35
let needSwapping = false
36
+ let serverModuleLocation
37
+ let serverModuleBackupLocation
35
38
36
39
/**
37
40
* Inspect content of server module and determine which engine is used
@@ -66,7 +69,7 @@ const fixServerTs = async function ({ angularVersion, siteRoot, failPlugin }) {
66
69
architect : { build } ,
67
70
} = project
68
71
69
- const serverModuleLocation = build ?. options ?. ssr ?. entry
72
+ serverModuleLocation = build ?. options ?. ssr ?. entry
70
73
if ( ! serverModuleLocation || ! existsSync ( serverModuleLocation ) ) {
71
74
console . log ( 'No SSR setup.' )
72
75
return
@@ -87,6 +90,12 @@ const fixServerTs = async function ({ angularVersion, siteRoot, failPlugin }) {
87
90
if ( needSwapping ) {
88
91
console . log ( `Swapping server.ts to use ${ usedEngine } ` )
89
92
93
+ const parsed = parse ( serverModuleLocation )
94
+
95
+ serverModuleBackupLocation = join ( parsed . dir , `${ parsed . name } .original${ parsed . ext } ` )
96
+
97
+ await rename ( serverModuleLocation , serverModuleBackupLocation )
98
+
90
99
if ( usedEngine === 'CommonEngine' ) {
91
100
await writeFile ( serverModuleLocation , NetlifyServerTsCommonEngine )
92
101
} else if ( usedEngine === 'AppEngine' ) {
@@ -100,9 +109,10 @@ const fixServerTs = async function ({ angularVersion, siteRoot, failPlugin }) {
100
109
module . exports . fixServerTs = fixServerTs
101
110
102
111
const revertServerTsFix = async function ( ) {
103
- if ( needSwapping ) {
104
- // TODO: revert swap
105
-
112
+ if ( needSwapping && serverModuleLocation && serverModuleBackupLocation ) {
113
+
114
+ await rm ( serverModuleLocation )
115
+ await rename ( serverModuleBackupLocation , serverModuleLocation )
106
116
// set it to false to not attempt to swap back more times than one
107
117
// as we call this in couple hooks to try to ensure it's reverted in case of success and failures, etc
108
118
needSwapping = false
0 commit comments