2020-11-28 11:50:55 +00:00
import consola from 'consola'
2021-02-01 09:24:49 +00:00
import fse from 'fs-extra'
import globby from 'globby'
2021-09-27 12:49:36 +00:00
import { join , resolve } from 'pathe'
2021-10-15 10:28:34 +00:00
import { hl , prettyPath , writeFile } from '../utils'
2021-01-22 19:55:59 +00:00
import { NitroPreset , NitroContext } from '../context'
2020-11-28 11:50:55 +00:00
2021-01-22 19:55:59 +00:00
export const azure : NitroPreset = {
2020-11-28 11:50:55 +00:00
entry : '{{ _internal.runtimeDir }}/entries/azure' ,
2021-10-18 08:04:38 +00:00
externals : true ,
2021-02-01 09:24:49 +00:00
output : {
serverDir : '{{ output.dir }}/server/functions'
} ,
2020-11-28 11:50:55 +00:00
hooks : {
2021-01-22 19:55:59 +00:00
async 'nitro:compiled' ( ctx : NitroContext ) {
2020-11-28 11:50:55 +00:00
await writeRoutes ( ctx )
}
}
}
2021-10-15 10:28:34 +00:00
async function writeRoutes ( { output } : NitroContext ) {
2021-02-01 09:24:49 +00:00
const host = {
version : '2.0'
}
2020-11-28 11:50:55 +00:00
2021-10-15 10:28:34 +00:00
const config = {
routes : [ ] ,
navigationFallback : {
rewrite : '/api/server'
2021-02-01 09:24:49 +00:00
}
2021-10-15 10:28:34 +00:00
}
2020-11-28 11:50:55 +00:00
2021-10-15 10:28:34 +00:00
const indexPath = resolve ( output . publicDir , 'index.html' )
2021-02-01 09:24:49 +00:00
const indexFileExists = fse . existsSync ( indexPath )
if ( ! indexFileExists ) {
2021-10-15 10:28:34 +00:00
config . routes . unshift (
2021-02-01 09:24:49 +00:00
{
2021-10-15 10:28:34 +00:00
route : '/index.html' ,
redirect : '/'
2021-02-01 09:24:49 +00:00
} ,
{
2021-10-15 10:28:34 +00:00
route : '/' ,
rewrite : '/api/server'
2021-02-01 09:24:49 +00:00
}
)
2020-11-28 11:50:55 +00:00
}
2021-02-01 09:24:49 +00:00
const folderFiles = await globby ( [
2021-10-15 10:28:34 +00:00
join ( output . publicDir , 'index.html' ) ,
join ( output . publicDir , '**/index.html' )
2021-02-01 09:24:49 +00:00
] )
2021-10-15 10:28:34 +00:00
const prefix = output . publicDir . length
2021-02-01 09:24:49 +00:00
const suffix = '/index.html' . length
folderFiles . forEach ( file = >
2021-10-15 10:28:34 +00:00
config . routes . unshift ( {
2021-02-01 09:24:49 +00:00
route : file.slice ( prefix , - suffix ) || '/' ,
2021-10-15 10:28:34 +00:00
rewrite : file.slice ( prefix )
2021-02-01 09:24:49 +00:00
} )
)
2021-10-15 10:28:34 +00:00
const otherFiles = await globby ( [ join ( output . publicDir , '**/*.html' ) , join ( output . publicDir , '*.html' ) ] )
2021-02-01 09:24:49 +00:00
otherFiles . forEach ( ( file ) = > {
if ( file . endsWith ( 'index.html' ) ) {
return
}
2021-10-15 10:28:34 +00:00
const route = file . slice ( prefix , '.html' . length )
const existingRouteIndex = config . routes . findIndex ( _route = > _route . route === route )
2021-02-01 09:24:49 +00:00
if ( existingRouteIndex > - 1 ) {
2021-10-15 10:28:34 +00:00
config . routes . splice ( existingRouteIndex , 1 )
2021-02-01 09:24:49 +00:00
}
2021-10-15 10:28:34 +00:00
config . routes . unshift (
2021-02-01 09:24:49 +00:00
{
route ,
2021-10-15 10:28:34 +00:00
rewrite : file.slice ( prefix )
2021-02-01 09:24:49 +00:00
}
)
} )
2020-11-28 11:50:55 +00:00
const functionDefinition = {
entryPoint : 'handle' ,
bindings : [
{
authLevel : 'anonymous' ,
type : 'httpTrigger' ,
direction : 'in' ,
name : 'req' ,
route : '{*url}' ,
2021-02-01 09:24:49 +00:00
methods : [ 'delete' , 'get' , 'head' , 'options' , 'patch' , 'post' , 'put' ]
2020-11-28 11:50:55 +00:00
} ,
{
type : 'http' ,
direction : 'out' ,
name : 'res'
}
]
}
2021-10-15 10:28:34 +00:00
await writeFile ( resolve ( output . serverDir , 'function.json' ) , JSON . stringify ( functionDefinition ) )
await writeFile ( resolve ( output . serverDir , '../host.json' ) , JSON . stringify ( host ) )
await writeFile ( resolve ( output . publicDir , 'staticwebapp.config.json' ) , JSON . stringify ( config ) )
2021-02-01 09:24:49 +00:00
if ( ! indexFileExists ) {
await writeFile ( indexPath , '' )
}
2020-11-28 11:50:55 +00:00
2021-10-15 10:28:34 +00:00
const apiDir = resolve ( output . serverDir , '..' )
consola . success ( 'Ready to run' , hl ( 'npx @azure/static-web-apps-cli start ' + prettyPath ( output . publicDir ) + ' --api-location ' + prettyPath ( apiDir ) ) , 'for local testing' )
2020-11-28 11:50:55 +00:00
}