|
| 1 | +import { spawnSync } from "node:child_process"; |
| 2 | +import * as path from "node:path"; |
| 3 | + |
| 4 | +import { expect, test } from "@playwright/test"; |
| 5 | +import dedent from "dedent"; |
| 6 | +import semver from "semver"; |
| 7 | +import fse from "fs-extra"; |
| 8 | + |
| 9 | +import { createProject } from "./helpers/vite"; |
| 10 | + |
| 11 | +const nodeBin = process.argv[0]; |
| 12 | +const reactRouterBin = "node_modules/@react-router/dev/dist/cli/index.js"; |
| 13 | + |
| 14 | +const run = (command: string[], options: Parameters<typeof spawnSync>[2]) => |
| 15 | + spawnSync(nodeBin, [reactRouterBin, ...command], options); |
| 16 | + |
| 17 | +const helpText = dedent` |
| 18 | + react-router |
| 19 | +
|
| 20 | + Usage: |
| 21 | + $ react-router build [projectDir] |
| 22 | + $ react-router dev [projectDir] |
| 23 | + $ react-router routes [projectDir] |
| 24 | +
|
| 25 | + Options: |
| 26 | + --help, -h Print this help message and exit |
| 27 | + --version, -v Print the CLI version and exit |
| 28 | + --no-color Disable ANSI colors in console output |
| 29 | + \`build\` Options: |
| 30 | + --assetsInlineLimit Static asset base64 inline threshold in bytes (default: 4096) (number) |
| 31 | + --clearScreen Allow/disable clear screen when logging (boolean) |
| 32 | + --config, -c Use specified config file (string) |
| 33 | + --emptyOutDir Force empty outDir when it's outside of root (boolean) |
| 34 | + --logLevel, -l Info | warn | error | silent (string) |
| 35 | + --minify Enable/disable minification, or specify minifier to use (default: "esbuild") (boolean | "terser" | "esbuild") |
| 36 | + --mode, -m Set env mode (string) |
| 37 | + --profile Start built-in Node.js inspector |
| 38 | + --sourcemapClient Output source maps for client build (default: false) (boolean | "inline" | "hidden") |
| 39 | + --sourcemapServer Output source maps for server build (default: false) (boolean | "inline" | "hidden") |
| 40 | + \`dev\` Options: |
| 41 | + --clearScreen Allow/disable clear screen when logging (boolean) |
| 42 | + --config, -c Use specified config file (string) |
| 43 | + --cors Enable CORS (boolean) |
| 44 | + --force Force the optimizer to ignore the cache and re-bundle (boolean) |
| 45 | + --host Specify hostname (string) |
| 46 | + --logLevel, -l Info | warn | error | silent (string) |
| 47 | + --mode, -m Set env mode (string) |
| 48 | + --open Open browser on startup (boolean | string) |
| 49 | + --port Specify port (number) |
| 50 | + --profile Start built-in Node.js inspector |
| 51 | + --strictPort Exit if specified port is already in use (boolean) |
| 52 | + \`routes\` Options: |
| 53 | + --config, -c Use specified Vite config file (string) |
| 54 | + --json Print the routes as JSON |
| 55 | + \`reveal\` Options: |
| 56 | + --config, -c Use specified Vite config file (string) |
| 57 | + --no-typescript Generate plain JavaScript files |
| 58 | + \`typegen\` Options: |
| 59 | + --watch Automatically regenerate types whenever route config (\`routes.ts\`) or route modules change |
| 60 | +
|
| 61 | + Build your project: |
| 62 | +
|
| 63 | + $ react-router build |
| 64 | +
|
| 65 | + Run your project locally in development: |
| 66 | +
|
| 67 | + $ react-router dev |
| 68 | +
|
| 69 | + Show all routes in your app: |
| 70 | +
|
| 71 | + $ react-router routes |
| 72 | + $ react-router routes my-app |
| 73 | + $ react-router routes --json |
| 74 | + $ react-router routes --config vite.react-router.config.ts |
| 75 | +
|
| 76 | + Reveal the used entry point: |
| 77 | +
|
| 78 | + $ react-router reveal entry.client |
| 79 | + $ react-router reveal entry.server |
| 80 | + $ react-router reveal entry.client --no-typescript |
| 81 | + $ react-router reveal entry.server --no-typescript |
| 82 | + $ react-router reveal entry.server --config vite.react-router.config.ts |
| 83 | +
|
| 84 | + Generate types for route modules: |
| 85 | +
|
| 86 | + $ react-router typegen |
| 87 | + $ react-router typegen --watch |
| 88 | +`; |
| 89 | + |
| 90 | +test.describe("cli", () => { |
| 91 | + test("--help", async () => { |
| 92 | + const cwd = await createProject(); |
| 93 | + const { stdout, stderr, status } = run(["--help"], { |
| 94 | + cwd, |
| 95 | + env: { |
| 96 | + NO_COLOR: "1", |
| 97 | + }, |
| 98 | + }); |
| 99 | + expect(stdout.toString().trim()).toBe(helpText); |
| 100 | + expect(stderr.toString()).toBe(""); |
| 101 | + expect(status).toBe(0); |
| 102 | + }); |
| 103 | + |
| 104 | + test("--version", async () => { |
| 105 | + const cwd = await createProject(); |
| 106 | + let { stdout, stderr, status } = run(["--version"], { cwd }); |
| 107 | + expect(semver.valid(stdout.toString().trim())).not.toBeNull(); |
| 108 | + expect(stderr.toString()).toBe(""); |
| 109 | + expect(status).toBe(0); |
| 110 | + }); |
| 111 | + |
| 112 | + test("routes", async () => { |
| 113 | + const cwd = await createProject(); |
| 114 | + let { stdout, stderr, status } = run(["routes"], { cwd }); |
| 115 | + expect(stdout.toString().trim()).toBe(dedent` |
| 116 | + <Routes> |
| 117 | + <Route file="root.tsx"> |
| 118 | + <Route index file="routes/_index.tsx" /> |
| 119 | + </Route> |
| 120 | + </Routes> |
| 121 | + `); |
| 122 | + expect(stderr.toString()).toBe(""); |
| 123 | + expect(status).toBe(0); |
| 124 | + }); |
| 125 | + |
| 126 | + test.describe("reveal", async () => { |
| 127 | + test("generates entry.{server,client}.tsx in the app directory", async () => { |
| 128 | + const cwd = await createProject(); |
| 129 | + let entryClientFile = path.join(cwd, "app", "entry.client.tsx"); |
| 130 | + let entryServerFile = path.join(cwd, "app", "entry.server.tsx"); |
| 131 | + |
| 132 | + expect(fse.existsSync(entryServerFile)).toBeFalsy(); |
| 133 | + expect(fse.existsSync(entryClientFile)).toBeFalsy(); |
| 134 | + |
| 135 | + run(["reveal"], { cwd }); |
| 136 | + |
| 137 | + expect(fse.existsSync(entryServerFile)).toBeTruthy(); |
| 138 | + expect(fse.existsSync(entryClientFile)).toBeTruthy(); |
| 139 | + }); |
| 140 | + |
| 141 | + test("generates specified entries in the app directory", async () => { |
| 142 | + const cwd = await createProject(); |
| 143 | + |
| 144 | + let entryClientFile = path.join(cwd, "app", "entry.client.tsx"); |
| 145 | + let entryServerFile = path.join(cwd, "app", "entry.server.tsx"); |
| 146 | + |
| 147 | + expect(fse.existsSync(entryServerFile)).toBeFalsy(); |
| 148 | + expect(fse.existsSync(entryClientFile)).toBeFalsy(); |
| 149 | + |
| 150 | + run(["reveal", "entry.server"], { cwd }); |
| 151 | + expect(fse.existsSync(entryServerFile)).toBeTruthy(); |
| 152 | + expect(fse.existsSync(entryClientFile)).toBeFalsy(); |
| 153 | + fse.removeSync(entryServerFile); |
| 154 | + |
| 155 | + run(["reveal", "entry.client"], { cwd }); |
| 156 | + expect(fse.existsSync(entryClientFile)).toBeTruthy(); |
| 157 | + expect(fse.existsSync(entryServerFile)).toBeFalsy(); |
| 158 | + }); |
| 159 | + |
| 160 | + test("generates entry.{server,client}.jsx in the app directory with --no-typescript", async () => { |
| 161 | + const cwd = await createProject(); |
| 162 | + let entryClientFile = path.join(cwd, "app", "entry.client.jsx"); |
| 163 | + let entryServerFile = path.join(cwd, "app", "entry.server.jsx"); |
| 164 | + |
| 165 | + expect(fse.existsSync(entryServerFile)).toBeFalsy(); |
| 166 | + expect(fse.existsSync(entryClientFile)).toBeFalsy(); |
| 167 | + |
| 168 | + run(["reveal", "--no-typescript"], { cwd }); |
| 169 | + |
| 170 | + expect(fse.existsSync(entryServerFile)).toBeTruthy(); |
| 171 | + expect(fse.existsSync(entryClientFile)).toBeTruthy(); |
| 172 | + }); |
| 173 | + }); |
| 174 | +}); |
0 commit comments