Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

cli-test-files.ts

29 lines1.4 KB

1// @effect-diagnostics nodeBuiltinImport:off - Vite bundles this config helper before workspace dependencies can resolve.
2import * as NodeFSP from "node:fs/promises";
3import * as NodePath from "node:path";
4​
5const TEST_FILE = /\.(test|spec)\.[cm]?[jt]sx?$/;
6​
7/**
8 * Repo-relative paths of the test files a `vitest run` command line names, or undefined
9 * unless every filter is an existing test file. Configs use it to set up only the
10 * projects that own those files; undefined keeps every project.
11 */
12export async function cliTestFiles(argv = process.argv, cwd = process.cwd()) {
13 const vitest = argv.findIndex((arg) => /[\\/]vitest(\.mjs)?$/.test(arg));
14 if (vitest === -1 || argv[vitest + 1] !== "run") return undefined;
15 // Loaded only for test runs so dev servers and builds skip it.
16 const { parseCLI } = await import("vite-plus/test/node");
17 const { filter, options } = parseCLI(["vitest", ...argv.slice(vitest + 1)]);
18 if (options.root !== undefined || filter.length === 0) return undefined;
19 const repoRoot = NodePath.resolve(import.meta.dirname, "../..");
20 const files: string[] = [];
21 for (const arg of filter) {
22 const file = NodePath.resolve(cwd, arg.replace(/:\d+$/, ""));
23 if (!TEST_FILE.test(file)) return undefined;
24 const info = await NodeFSP.stat(file).catch(() => undefined);
25 if (!info?.isFile()) return undefined;
26 files.push(NodePath.relative(repoRoot, file).replaceAll("\\", "/"));
27 }
28 return files;