-
Notifications
You must be signed in to change notification settings - Fork 525
/
azure.ts
117 lines (105 loc) · 3.15 KB
/
azure.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import fse from 'fs-extra'
import { join, resolve } from 'pathe'
import { writeFile } from '../utils'
import { defineNitroPreset } from '../preset'
import type { Nitro } from '../types'
export const azure = defineNitroPreset({
entry: '#internal/nitro/entries/azure',
output: {
serverDir: '{{ output.dir }}/server/functions'
},
commands: {
preview: 'npx @azure/static-web-apps-cli start ./public --api-location ./server'
},
hooks: {
async 'compiled' (ctx: Nitro) {
await writeRoutes(ctx)
}
}
})
async function writeRoutes (nitro: Nitro) {
const host = {
version: '2.0'
}
let nodeVersion = '16'
try {
const currentNodeVersion = fse.readJSONSync(join(nitro.options.rootDir, 'package.json')).engines.node
if (['16', '14'].includes(currentNodeVersion)) {
nodeVersion = currentNodeVersion
}
} catch {
const currentNodeVersion = process.versions.node.slice(0, 2)
if (['16', '14'].includes(currentNodeVersion)) {
nodeVersion = currentNodeVersion
}
}
const config = {
platform: {
apiRuntime: `node:${nodeVersion}`
},
routes: [],
navigationFallback: {
rewrite: '/api/server'
}
}
const routeFiles = nitro._prerenderedRoutes || []
const indexFileExists = routeFiles.some(route => route.fileName === '/index.html')
if (!indexFileExists) {
config.routes.unshift(
{
route: '/index.html',
redirect: '/'
},
{
route: '/',
rewrite: '/api/server'
}
)
}
const suffix = '/index.html'.length
for (const { fileName } of routeFiles) {
if (!fileName.endsWith('/index.html')) { continue }
config.routes.unshift({
route: fileName.slice(0, -suffix) || '/',
rewrite: fileName
})
}
for (const { fileName } of routeFiles) {
if (!fileName.endsWith('.html') || fileName.endsWith('index.html')) { continue }
const route = fileName.slice(0, -'.html'.length)
const existingRouteIndex = config.routes.findIndex(_route => _route.route === route)
if (existingRouteIndex > -1) {
config.routes.splice(existingRouteIndex, 1)
}
config.routes.unshift({
route,
rewrite: fileName
})
}
const functionDefinition = {
entryPoint: 'handle',
bindings: [
{
authLevel: 'anonymous',
type: 'httpTrigger',
direction: 'in',
name: 'req',
route: '{*url}',
methods: ['delete', 'get', 'head', 'options', 'patch', 'post', 'put']
},
{
type: 'http',
direction: 'out',
name: 'res'
}
]
}
await writeFile(resolve(nitro.options.output.serverDir, 'function.json'), JSON.stringify(functionDefinition, null, 2))
await writeFile(resolve(nitro.options.output.serverDir, '../host.json'), JSON.stringify(host, null, 2))
const stubPackageJson = resolve(nitro.options.output.serverDir, '../package.json')
await writeFile(stubPackageJson, JSON.stringify({ private: true }))
await writeFile(resolve(nitro.options.rootDir, 'staticwebapp.config.json'), JSON.stringify(config, null, 2))
if (!indexFileExists) {
await writeFile(resolve(nitro.options.output.publicDir, 'index.html'), '')
}
}