-
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Decouple CLI arg parsing/execution for NODE_ENV initialization #12505
Changes from 3 commits
f62c224
c01b823
f920e54
44c4e31
4095308
c9d314d
12cf86a
9b1e38b
5446875
3ebe742
2fe399b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,25 @@ | ||
#!/usr/bin/env node | ||
import { run } from "./run"; | ||
import { parseArgs } from "./parse"; | ||
|
||
run().then( | ||
() => { | ||
process.exit(0); | ||
}, | ||
(error: unknown) => { | ||
if (error) console.error(error); | ||
process.exit(1); | ||
} | ||
); | ||
let { input, flags, command } = parseArgs(); | ||
|
||
// If not already set, default `NODE_ENV` so React loads the proper | ||
// version in it's CJS entry script. We have to do this before importing `run.ts` | ||
// since that is what imports `react` (indirectly via `react-router`) | ||
if (command === "dev") { | ||
process.env.NODE_ENV = process.env.NODE_ENV ?? "development"; | ||
} else { | ||
process.env.NODE_ENV = process.env.NODE_ENV ?? "production"; | ||
} | ||
|
||
import("./run").then(({ run }) => { | ||
run(input, flags, command).then( | ||
() => { | ||
process.exit(0); | ||
}, | ||
(error: unknown) => { | ||
if (error) console.error(error); | ||
process.exit(1); | ||
} | ||
); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then run the CLI |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import arg from "arg"; | ||
import semver from "semver"; | ||
|
||
/** | ||
* Parse command line arguments for `react-router` dev CLI | ||
*/ | ||
export function parseArgs(argv: string[] = process.argv.slice(2)) { | ||
// Check the node version | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No changes to this logic, just extracted out from |
||
let versions = process.versions; | ||
let MINIMUM_NODE_VERSION = 20; | ||
if ( | ||
versions && | ||
versions.node && | ||
semver.major(versions.node) < MINIMUM_NODE_VERSION | ||
) { | ||
console.warn( | ||
`️⚠️ Oops, Node v${versions.node} detected. react-router requires ` + | ||
`a Node version greater than ${MINIMUM_NODE_VERSION}.` | ||
); | ||
} | ||
|
||
let isBooleanFlag = (arg: string) => { | ||
let index = argv.indexOf(arg); | ||
let nextArg = argv[index + 1]; | ||
return !nextArg || nextArg.startsWith("-"); | ||
}; | ||
|
||
let args = arg( | ||
{ | ||
"--force": Boolean, | ||
"--help": Boolean, | ||
"-h": "--help", | ||
"--json": Boolean, | ||
"--token": String, | ||
"--typescript": Boolean, | ||
"--no-typescript": Boolean, | ||
"--version": Boolean, | ||
"-v": "--version", | ||
"--port": Number, | ||
"-p": "--port", | ||
"--config": String, | ||
"-c": "--config", | ||
"--assetsInlineLimit": Number, | ||
"--clearScreen": Boolean, | ||
"--cors": Boolean, | ||
"--emptyOutDir": Boolean, | ||
"--host": isBooleanFlag("--host") ? Boolean : String, | ||
"--logLevel": String, | ||
"-l": "--logLevel", | ||
"--minify": String, | ||
"--mode": String, | ||
"-m": "--mode", | ||
"--open": isBooleanFlag("--open") ? Boolean : String, | ||
"--strictPort": Boolean, | ||
"--profile": Boolean, | ||
"--sourcemapClient": isBooleanFlag("--sourcemapClient") | ||
? Boolean | ||
: String, | ||
"--sourcemapServer": isBooleanFlag("--sourcemapServer") | ||
? Boolean | ||
: String, | ||
"--watch": Boolean, | ||
}, | ||
{ | ||
argv, | ||
} | ||
); | ||
|
||
let flags: any = Object.entries(args).reduce((acc, [key, value]) => { | ||
key = key.replace(/^--/, ""); | ||
acc[key] = value; | ||
return acc; | ||
}, {} as any); | ||
|
||
flags.interactive = flags.interactive ?? require.main === module; | ||
if (args["--no-typescript"]) { | ||
flags.typescript = false; | ||
} | ||
|
||
return { | ||
input: args._, | ||
command: args._[0], | ||
flags, | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,7 @@ | ||
#!/usr/bin/env node | ||
|
||
// If not already set, default `NODE_ENV=production` so React loads the proper | ||
// version in it's CJS entry script | ||
process.env.NODE_ENV = process.env.NODE_ENV ?? "production"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Set this correctly before calling the CLI so the proper version of React gets loaded (#12078) |
||
|
||
require("./dist/cli"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parse args and set
NODE_ENV
first