convex
Version:
Client for the Convex Cloud
85 lines (84 loc) • 2.51 kB
JavaScript
;
import child_process from "child_process";
import chalk from "chalk";
import path from "path";
import * as Sentry from "@sentry/node";
export async function processTypeCheckResult(ctx, typeCheckMode, runTypeCheck) {
if (typeCheckMode === "disable") {
return;
}
const result = await runTypeCheck();
if (result === "cantTypeCheck" && typeCheckMode === "enable" || result === "typecheckFailed") {
console.error(
chalk.gray("To ignore failing typecheck, use `--typecheck=disable`.")
);
return await ctx.fatalError(1, "fs");
}
}
export async function typeCheckFunctions(ctx, functionsDir) {
const tsconfig = path.join(functionsDir, "tsconfig.json");
if (!ctx.fs.exists(tsconfig)) {
console.error(
"Can't find convex/tsconfig.json to use to typecheck Convex functions."
);
console.error("Run `npx convex codegen --tsconfig` to create one.");
return "cantTypeCheck";
}
return runTsc(ctx, ["--project", functionsDir]);
}
async function runTsc(ctx, tscArgs) {
const tscPath = path.join(
"node_modules",
".bin",
process.platform == "win32" ? "tsc.CMD" : "tsc"
);
if (!ctx.fs.exists(tscPath)) {
return "cantTypeCheck";
}
const result = child_process.spawnSync(
tscPath,
tscArgs.concat("--listFiles")
);
if (result.status === null) {
console.error(chalk.red(`TypeScript typecheck timed out.`));
if (result.error) {
console.error(chalk.red(`${result.error}`));
}
return "typecheckFailed";
}
const filesTouched = result.stdout.toString("utf-8").split("\n").map((s) => s.trim()).filter((s) => s.length > 0);
let anyPathsFound = false;
for (const fileTouched of filesTouched) {
const absPath = path.resolve(fileTouched);
let st;
try {
st = ctx.fs.stat(absPath);
anyPathsFound = true;
} catch (err) {
continue;
}
ctx.fs.registerPath(absPath, st);
}
if (filesTouched.length > 0 && !anyPathsFound) {
const err = new Error(
`Failed to stat any files emitted by tsc (received ${filesTouched.length})`
);
Sentry.captureException(err);
}
if (!result.error && result.status === 0) {
return "success";
}
try {
child_process.execFileSync(
tscPath,
tscArgs,
{ stdio: "inherit" }
);
ctx.fs.invalidate();
return "success";
} catch (e) {
console.error(chalk.red("TypeScript typecheck via `tsc` failed."));
return "typecheckFailed";
}
}
//# sourceMappingURL=typecheck.js.map