Skip to content

Commit ed3105e

Browse files
committed
fix: swap original server.ts back after build or failure
1 parent 09be6f9 commit ed3105e

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/helpers/serverModuleHelpers.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
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')
34

45
const { satisfies } = require('semver')
56

@@ -32,6 +33,8 @@ export const reqHandler = createRequestHandler(async (request: Request, context:
3233
`
3334

3435
let needSwapping = false
36+
let serverModuleLocation
37+
let serverModuleBackupLocation
3538

3639
/**
3740
* Inspect content of server module and determine which engine is used
@@ -66,7 +69,7 @@ const fixServerTs = async function ({ angularVersion, siteRoot, failPlugin }) {
6669
architect: { build },
6770
} = project
6871

69-
const serverModuleLocation = build?.options?.ssr?.entry
72+
serverModuleLocation = build?.options?.ssr?.entry
7073
if (!serverModuleLocation || !existsSync(serverModuleLocation)) {
7174
console.log('No SSR setup.')
7275
return
@@ -87,6 +90,12 @@ const fixServerTs = async function ({ angularVersion, siteRoot, failPlugin }) {
8790
if (needSwapping) {
8891
console.log(`Swapping server.ts to use ${usedEngine}`)
8992

93+
const parsed = parse(serverModuleLocation)
94+
95+
serverModuleBackupLocation = join(parsed.dir, `${parsed.name}.original${parsed.ext}`)
96+
97+
await rename(serverModuleLocation, serverModuleBackupLocation)
98+
9099
if (usedEngine === 'CommonEngine') {
91100
await writeFile(serverModuleLocation, NetlifyServerTsCommonEngine)
92101
} else if (usedEngine === 'AppEngine') {
@@ -100,9 +109,10 @@ const fixServerTs = async function ({ angularVersion, siteRoot, failPlugin }) {
100109
module.exports.fixServerTs = fixServerTs
101110

102111
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)
106116
// set it to false to not attempt to swap back more times than one
107117
// as we call this in couple hooks to try to ensure it's reverted in case of success and failures, etc
108118
needSwapping = false

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,7 @@ module.exports = {
6969
usedEngine,
7070
})
7171
},
72+
async onEnd() {
73+
await revertServerTsFix()
74+
}
7275
}

0 commit comments

Comments
 (0)