-
Notifications
You must be signed in to change notification settings - Fork 553
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add azure functions preset (#45)
- Loading branch information
Showing
2 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import replace from '@rollup/plugin-replace' | ||
import archiver from 'archiver' | ||
import consola from 'consola' | ||
import createEtag from 'etag' | ||
import { createWriteStream, readdirSync, readFileSync, statSync } from 'fs-extra' | ||
import mime from 'mime' | ||
import { join, relative, resolve } from 'upath' | ||
|
||
import { prettyPath, writeFile } from '../utils' | ||
import { SigmaPreset, SigmaContext } from '../context' | ||
|
||
export const azure: SigmaPreset = { | ||
inlineChunks: false, | ||
entry: '{{ _internal.runtimeDir }}/entries/azure', | ||
hooks: { | ||
'sigma:rollup:before' (ctx: SigmaContext) { | ||
const manifest = JSON.stringify(getStaticManifest(ctx)).replace(/\\"/g, '\\\\"') | ||
ctx.rollupConfig.plugins.push(replace({ | ||
values: { | ||
'process.env.STATIC_MANIFEST': `\`${manifest}\`` | ||
} | ||
})) | ||
}, | ||
async 'sigma:compiled' (ctx: SigmaContext) { | ||
await writeRoutes(ctx) | ||
} | ||
} | ||
} | ||
|
||
function getStaticManifest ({ output: { dir } }: SigmaContext) { | ||
const files = [] | ||
const staticRoot = resolve(dir, 'public') | ||
|
||
const addFiles = (directory: string) => { | ||
const listing = readdirSync(directory) | ||
listing.forEach((filename) => { | ||
const fullPath = resolve(directory, filename) | ||
if (statSync(fullPath).isDirectory()) { | ||
return addFiles(fullPath) | ||
} | ||
files.push('/' + relative(staticRoot, fullPath)) | ||
}) | ||
} | ||
|
||
addFiles(staticRoot) | ||
const metadata = files.reduce((metadata, filename) => { | ||
let mimeType = mime.getType(filename) || 'text/plain' | ||
if (mimeType.startsWith('text')) { | ||
mimeType += '; charset=utf-8' | ||
} | ||
const etag = createEtag(readFileSync(join(staticRoot, filename))) | ||
metadata[filename] = [mimeType, etag] | ||
return metadata | ||
}, {} as Record<string, string>) | ||
|
||
return { | ||
files, | ||
metadata | ||
} | ||
} | ||
|
||
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() | ||
}) | ||
} | ||
|
||
async function writeRoutes ({ output: { dir, serverDir } }: SigmaContext) { | ||
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}\``) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './azure' | ||
export * from './browser' | ||
export * from './cloudflare' | ||
export * from './lambda' | ||
|