feat(server-dev): Skip calling next and lowdefy build using npm/yarn start.

This commit is contained in:
Sam Tolmay 2022-01-26 21:50:19 +02:00
parent cf66a6f839
commit 1a8699a012
No known key found for this signature in database
GPG Key ID: D004126FCD1A6DF0
5 changed files with 22 additions and 18 deletions

View File

@ -16,6 +16,7 @@
/* eslint-disable no-console */
import path from 'path';
import { createRequire } from 'module';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
@ -30,10 +31,16 @@ import shutdownServer from './processes/shutdownServer.mjs';
import startWatchers from './processes/startWatchers.mjs';
const argv = yargs(hideBin(process.argv)).argv;
const require = createRequire(import.meta.url);
async function getContext() {
const { verbose = false } = argv;
const context = {
bin: {
// TODO: The string replace is a little hacky and will fail if the location of the bin changes,
lowdefyBuild: require.resolve('@lowdefy/build').replace('index.js', 'scripts/run.js'),
next: require.resolve('next').replace('server/next.js', 'bin/next'),
},
directories: {
build: path.resolve(process.cwd(), './build'),
config: path.resolve(

View File

@ -16,12 +16,12 @@
import { spawnProcess } from '@lowdefy/node-utils';
function lowdefyBuild({ packageManager, directories }) {
function lowdefyBuild({ bin, directories }) {
return async () => {
await spawnProcess({
command: 'node',
args: [bin.lowdefyBuild],
logger: console,
args: ['run', 'build:lowdefy'],
command: packageManager,
processOptions: {
env: {
...process.env,

View File

@ -16,14 +16,14 @@
import { spawnProcess } from '@lowdefy/node-utils';
function nextBuild({ packageManager, verbose }) {
function nextBuild(context) {
return async () => {
console.log('Building app...');
await spawnProcess({
logger: console,
args: ['run', 'build:next'],
command: packageManager,
silent: !verbose,
command: 'node',
args: [context.bin.next, 'build'],
silent: !context.verbose,
});
};
}

View File

@ -16,18 +16,18 @@
function shutdownServer(context) {
return () => {
if (context.serverProcess) {
if (context.nextServer) {
// console.log(
// `Existing server ${context.serverProcess.pid}, killed: ${context.serverProcess.killed}`
// `Existing server ${context.nextServer.pid}, killed: ${context.nextServer.killed}`
// );
if (!context.serverProcess.killed) {
if (!context.nextServer.killed) {
console.log('Shutting down server...');
context.serverProcess.kill();
context.nextServer.kill();
// console.log(
// `Killed server ${context.serverProcess.pid}, killed: ${context.serverProcess.killed}`
// `Killed server ${context.nextServer.pid}, killed: ${context.nextServer.killed}`
// );
}
context.serverProcess = null;
context.nextServer = null;
}
};
}

View File

@ -14,18 +14,15 @@
limitations under the License.
*/
import { createRequire } from 'module';
import spawnProcess from '../utils/spawnProcess.mjs';
const require = createRequire(import.meta.url);
function startServerProcess(context) {
context.shutdownServer();
const nextCliUrl = require.resolve('next').replace('server/next.js', 'bin/next');
const nextServer = spawnProcess({
logger: console,
command: 'node',
args: [nextCliUrl, 'start'],
args: [context.bin.next, 'start'],
silent: false,
processOptions: {
env: {