Skip to content

Commit

Permalink
feat(executors): Implement serve
Browse files Browse the repository at this point in the history
Closes #3
  • Loading branch information
beyerleinf committed Oct 9, 2022
1 parent 3dc9766 commit 9617652
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 27 deletions.
147 changes: 120 additions & 27 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"dependencies": {
"@swc/helpers": "~0.3.3",
"esbuild-azure-functions": "^1.2.0",
"rxjs": "^7.5.7",
"tslib": "^2.3.0"
}
}
5 changes: 5 additions & 0 deletions packages/azure-functions/executors.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
"implementation": "./src/executors/build/executor",
"schema": "./src/executors/build/schema.json",
"description": "build executor"
},
"serve": {
"implementation": "./src/executors/serve/executor",
"schema": "./src/executors/serve/schema.json",
"description": "serve executor"
}
}
}
80 changes: 80 additions & 0 deletions packages/azure-functions/src/executors/serve/executor.ts
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;
}
5 changes: 5 additions & 0 deletions packages/azure-functions/src/executors/serve/schema.d.ts
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;
}
16 changes: 16 additions & 0 deletions packages/azure-functions/src/executors/serve/schema.json
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": []
}

0 comments on commit 9617652

Please sign in to comment.