-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
227 additions
and
27 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,80 @@ | ||
import { ExecutorContext } from '@nrwl/devkit'; | ||
import { eachValueFrom } from '@nrwl/devkit/src/utils/rxjs-for-await'; | ||
import * as child_process from 'child_process'; | ||
import { build, BuilderConfigType, watch } from 'esbuild-azure-functions'; | ||
import * as path from 'path'; | ||
import { map, Observable } from 'rxjs'; | ||
import { ServeExecutorSchema } from './schema'; | ||
|
||
export default async function* runExecutor( | ||
options: ServeExecutorSchema, | ||
ctx: ExecutorContext | ||
) { | ||
let subProcess: child_process.ChildProcess | null = null; | ||
|
||
const exitHandler = () => { | ||
subProcess?.kill('SIGINT'); | ||
}; | ||
|
||
process.on('SIGUSR1', exitHandler); | ||
process.on('SIGUSR2', exitHandler); | ||
process.on('SIGINT', exitHandler); | ||
process.on('SIGTERM', exitHandler); | ||
|
||
const nxProject = ctx.workspace.projects[ctx.projectName]; | ||
const project = path.join(ctx.root, nxProject.root); | ||
const outDir = path.join(ctx.root, 'dist', nxProject.root); | ||
|
||
const config: BuilderConfigType = { | ||
project, | ||
advancedOptions: { | ||
enableDirnameShim: true, | ||
enableRequireShim: true, | ||
}, | ||
clean: true, | ||
logLevel: options.logLevel || 'verbose', | ||
esbuildOptions: { | ||
outdir: outDir, | ||
minify: true, | ||
sourcemap: true, | ||
}, | ||
}; | ||
|
||
// build once so there are existing artifacts when starting | ||
// the Azure Functions CLI | ||
await build(config); | ||
|
||
subProcess = spawnFuncProcess(project); | ||
|
||
await watch({ | ||
...config, | ||
clean: false, | ||
onRebuild: async (errors) => { | ||
if (!errors) { | ||
// Hack needed because func only hot reloads for changes inside the cwd. | ||
subProcess?.kill('SIGINT'); | ||
|
||
subProcess = spawnFuncProcess(project); | ||
} | ||
}, | ||
}); | ||
|
||
return yield* eachValueFrom( | ||
new Observable((s) => () => s.complete()).pipe( | ||
map(() => ({ success: true })) | ||
) | ||
); | ||
} | ||
|
||
function spawnFuncProcess(cwd: string) { | ||
const subProcess = child_process.spawn('func', ['start'], { | ||
cwd, | ||
stdio: [process.stdin, process.stdout, process.stderr, 'pipe'], | ||
}); | ||
|
||
subProcess.on('error', (error) => { | ||
console.error(error); | ||
}); | ||
|
||
return subProcess; | ||
} |
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,5 @@ | ||
import { BuilderLogLevelType } from 'esbuild-azure-functions/build/src/lib/models'; | ||
|
||
export interface ServeExecutorSchema { | ||
logLevel?: BuilderLogLevelType; | ||
} |
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,16 @@ | ||
{ | ||
"$schema": "http://json-schema.org/schema", | ||
"cli": "nx", | ||
"title": "Serve executor", | ||
"description": "", | ||
"type": "object", | ||
"properties": { | ||
"logLevel": { | ||
"type": "string", | ||
"description": "Builder log level", | ||
"enum": ["verbose", "info", "warn", "error", "off"], | ||
"$default": "verbose" | ||
} | ||
}, | ||
"required": [] | ||
} |