Skip to content

Commit

Permalink
fix(dev): better error message when remix-serve is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
pcattori committed May 25, 2023
1 parent 33cc4c0 commit fcbb439
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 39 deletions.
5 changes: 5 additions & 0 deletions .changeset/rude-geese-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/dev": patch
---

better error message when `remix-serve` is not found
22 changes: 1 addition & 21 deletions packages/remix-dev/cli/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import { TaskError } from "../codemod/utils/task";
import { transpile as convertFileToJS } from "./useJavascript";
import { warnOnce } from "../warnOnce";
import type { Options } from "../compiler/options";
import { getAppDependencies } from "../dependencies";

export async function create({
appTemplate,
Expand Down Expand Up @@ -502,7 +501,7 @@ let resolveDevOrigin = async (
};

type DevServeFlags = DevOrigin & {
command: string;
command?: string;
restart: boolean;
};
let resolveDevServe = async (
Expand All @@ -518,25 +517,6 @@ let resolveDevServe = async (
let command =
flags.command ??
(dev === true ? undefined : dev.command)
if (!command) {
command = `remix-serve ${path.relative(
process.cwd(),
config.serverBuildPath
)}`;

let usingRemixAppServer =
getAppDependencies(config, true)["@remix-run/serve"] !== undefined;
if (!usingRemixAppServer) {
console.error(
[
`Remix dev server command defaulted to '${command}', but @remix-run/serve is not installed.`,
"If you are using another server, specify how to run it with `-c` or `--command` flag.",
"For example, `remix dev -c 'node ./server.js'`",
].join("\n")
);
process.exit(1);
}
}

let restart =
flags.restart ?? (dev === true ? undefined : dev.restart) ?? true;
Expand Down
63 changes: 45 additions & 18 deletions packages/remix-dev/devServer_unstable/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as stream from "node:stream";
import * as http from "node:http";
import fs from "fs-extra";
import prettyMs from "pretty-ms";
import type { ExecaChildProcess } from "execa";
import execa from "execa";
import express from "express";

Expand All @@ -18,6 +19,7 @@ import { detectPackageManager } from "../cli/detectPackageManager";
import * as HDR from "./hdr";
import type { Result } from "../result";
import { err, ok } from "../result";
import invariant from "../invariant";

type Origin = {
scheme: string;
Expand All @@ -41,7 +43,7 @@ let detectBin = async (): Promise<string> => {
export let serve = async (
initialConfig: RemixConfig,
options: {
command: string;
command?: string;
scheme: string;
host: string;
port: number;
Expand Down Expand Up @@ -83,19 +85,47 @@ export let serve = async (
};

let bin = await detectBin();
let startAppServer = (command: string) => {
console.log(`> ${command}`);
let newAppServer = execa.command(command, {
stdio: "pipe",
env: {
NODE_ENV: "development",
PATH:
bin + (process.platform === "win32" ? ";" : ":") + process.env.PATH,
REMIX_DEV_HTTP_ORIGIN: stringifyOrigin(origin),
},
// https://github.com/sindresorhus/execa/issues/433
windowsHide: false,
});
let startAppServer = (command?: string) => {
let cmd =
command ??
`remix-serve ${path.relative(
process.cwd(),
initialConfig.serverBuildPath
)}`;
console.log(`> ${cmd}`);
let newAppServer = execa
.command(cmd, {
stdio: "pipe",
env: {
NODE_ENV: "development",
PATH:
bin + (process.platform === "win32" ? ";" : ":") + process.env.PATH,
REMIX_DEV_HTTP_ORIGIN: stringifyOrigin(origin),
},
// https://github.com/sindresorhus/execa/issues/433
windowsHide: false,
})
.on("error", (e) => {
// patch execa error types
invariant("errno" in e && typeof e.errno === "number", "errno missing");
invariant("code" in e && typeof e.code === "string", "code missing");
invariant("path" in e && typeof e.path === "string", "path missing");

if (command === undefined) {
console.error(
[
"",
`┏ [error] command not found: ${e.path}`,
`┃ \`remix dev\` did not receive \`--command\` nor \`-c\`, defaulting to \`${cmd}\`.`,
"┃ You probably meant to use `-c` for your app server command.",
"┗ For example: `remix dev -c 'node ./server.js'`",
"",
].join("\n")
);
process.exit(1);
}
throw e;
});

if (newAppServer.stdin)
process.stdin.pipe(newAppServer.stdin, { end: true });
Expand Down Expand Up @@ -164,10 +194,7 @@ export let serve = async (
try {
console.log(`Waiting for app server (${state.manifest?.version})`);
let start = Date.now();
if (
options.command &&
(state.appServer === undefined || options.restart)
) {
if (state.appServer === undefined || options.restart) {
await kill(state.appServer);
state.appServer = startAppServer(options.command);
}
Expand Down

0 comments on commit fcbb439

Please sign in to comment.