Skip to content

Commit

Permalink
Allow file extensions in npx convex run (#32731)
Browse files Browse the repository at this point in the history
GitOrigin-RevId: 24da57b70e680d130c10195a6b7cde03a10f3d22
  • Loading branch information
sshader authored and Convex, Inc. committed Jan 6, 2025
1 parent 61cdbaa commit a40efbb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
6 changes: 6 additions & 0 deletions npm-packages/convex/src/cli/lib/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ test("parseFunctionName", async () => {
expect(await parseFunctionName(ctx, "convex/foo/bar", "convex/")).toEqual(
"foo/bar:default",
);
expect(await parseFunctionName(ctx, "convex/foo/bar.ts", "convex/")).toEqual(
"foo/bar:default",
);
expect(
await parseFunctionName(ctx, "convex/foo/bar.ts:baz", "convex/"),
).toEqual("foo/bar:baz");
expect(await parseFunctionName(ctx, "convex/bar/baz", "convex/")).toEqual(
"convex/bar/baz:default",
);
Expand Down
30 changes: 24 additions & 6 deletions npm-packages/convex/src/cli/lib/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,21 +138,39 @@ export async function parseFunctionName(
// if there's a directory with the same name as the functions directory nested directly underneath.
// We'll prefer the `convex/foo/bar:default` version, and check if the file exists, and otherwise treat this as a relative path from the project root.
const filePath = functionName.split(":")[0];
const possibleExtensions = [
".ts",
".js",
".tsx",
".jsx",
".mts",
".mjs",
".cts",
".cjs",
];
let hasExtension = false;
let normalizedFilePath: string = filePath;
for (const extension of possibleExtensions) {
if (filePath.endsWith(extension)) {
normalizedFilePath = filePath.slice(0, -extension.length);
hasExtension = true;
break;
}
}

const exportName = functionName.split(":")[1] ?? "default";
const normalizedName = `${filePath}:${exportName}`;
const normalizedName = `${normalizedFilePath}:${exportName}`;

// This isn't a relative path from the project root
if (!filePath.startsWith(functionDirName)) {
return normalizedName;
}

const filePathWithoutPrefix = filePath.slice(functionDirName.length);
const filePathWithoutPrefix = normalizedFilePath.slice(
functionDirName.length,
);
const functionNameWithoutPrefix = `${filePathWithoutPrefix}:${exportName}`;

const possibleExtensions = [".ts", ".js", ".tsx", ".jsx"];
const hasExtension = possibleExtensions.some((extension) =>
filePath.endsWith(extension),
);
if (hasExtension) {
if (ctx.fs.exists(path.join(functionDirName, filePath))) {
return normalizedName;
Expand Down

0 comments on commit a40efbb

Please sign in to comment.