Skip to content

Instantly share code, notes, and snippets.

@ZackDeRose
Created January 26, 2023 05:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZackDeRose/68ad59073e40cf576424c10149764df0 to your computer and use it in GitHub Desktop.
Save ZackDeRose/68ad59073e40cf576424c10149764df0 to your computer and use it in GitHub Desktop.
import { ServeFullstackExecutorSchema } from './schema';
import { ExecutorContext } from '@nrwl/devkit';
import { ChildProcess, exec } from 'child_process';
import * as chalk from 'chalk';
const LARGE_BUFFER = 1024 * 1000000;
export default async function runExecutor(
options: ServeFullstackExecutorSchema,
_context: ExecutorContext
) {
const prefixSize =
Math.max(options.backendProject.length, options.frontendProject.length) + 3;
await startBackendServer(options, prefixSize);
}
async function startBackendServer(
options: ServeFullstackExecutorSchema,
targetPadSize: number
) {
let frontendServerStarted = false;
return new Promise(() => {
const childProcess = exec(`npx nx serve ${options.backendProject}`, {
maxBuffer: LARGE_BUFFER,
});
process.on('exit', () => childProcess.kill());
process.on('SIGTERM', () => childProcess.kill());
prefixTerminalOutput(
childProcess,
chalk.bgGreen(padTargetName(options.backendProject, targetPadSize))
);
childProcess.stdout.on('data', (data) => {
if (!frontendServerStarted && data.includes('No errors found.')) {
startFrontendServer(options.frontendProject, targetPadSize);
frontendServerStarted = true;
}
});
});
}
async function startFrontendServer(
frontendProject: string,
targetPadSize: number
) {
return new Promise(() => {
const childProcess = exec(`npx nx serve ${frontendProject}`, {
maxBuffer: LARGE_BUFFER,
});
process.on('exit', () => childProcess.kill());
process.on('SIGTERM', () => childProcess.kill());
prefixTerminalOutput(
childProcess,
chalk.bgBlue(padTargetName(frontendProject, targetPadSize))
);
});
}
function prefixTerminalOutput(cp: ChildProcess, prefix: string) {
function logWithPrefix(data: string) {
if (!data) {
return;
}
console.log(
data
.split('\n')
.map((line) => `${prefix} ${line}`)
.join('\n')
);
}
cp.stdout.on('data', logWithPrefix);
cp.stdout.on('error', logWithPrefix);
cp.stderr.on('data', logWithPrefix);
cp.stderr.on('error', logWithPrefix);
}
function padTargetName(name: string, targetSize: number) {
const builder = [`${name}`];
builder.push(' '.repeat(Math.floor((targetSize - name.length) / 2)));
builder.unshift(' '.repeat(Math.ceil((targetSize - name.length) / 2)));
builder.push(' ');
return builder.join('');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment