Nuxt/packages/nitro/src/presets/azure.ts

75 lines
1.9 KiB
TypeScript
Raw Normal View History

2020-11-28 11:50:55 +00:00
import archiver from 'archiver'
import consola from 'consola'
2020-11-28 22:49:39 +00:00
import { createWriteStream } from 'fs-extra'
import { join, resolve } from 'upath'
2020-11-28 11:50:55 +00:00
import { 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
inlineChunks: false,
2020-11-28 22:49:39 +00:00
serveStatic: true,
2020-11-28 11:50:55 +00:00
entry: '{{ _internal.runtimeDir }}/entries/azure',
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)
}
}
}
function zipDirectory (dir: string, outfile: string): Promise<undefined> {
const archive = archiver('zip', { zlib: { level: 9 } })
const stream = createWriteStream(outfile)
return new Promise((resolve, reject) => {
archive
.directory(dir, false)
.on('error', (err: Error) => reject(err))
.pipe(stream)
stream.on('close', () => resolve(undefined))
archive.finalize()
})
}
2021-01-22 19:55:59 +00:00
async function writeRoutes ({ output: { dir, serverDir } }: NitroContext) {
2020-11-28 11:50:55 +00:00
const host = {
version: '2.0',
extensions: { http: { routePrefix: '' } }
}
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(serverDir, 'function.json'), JSON.stringify(functionDefinition))
await writeFile(resolve(dir, 'host.json'), JSON.stringify(host))
await zipDirectory(dir, join(dir, 'deploy.zip'))
const zipPath = prettyPath(resolve(dir, 'deploy.zip'))
consola.success(`Ready to run \`az functionapp deployment source config-zip -g <resource-group> -n <app-name> --src ${zipPath}\``)
}