Skip to content

Commit

Permalink
feat(swc/plugins): Insert swc runtime data (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 authored Aug 26, 2024
1 parent e34cc7b commit 63b8bd8
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 43 deletions.
5 changes: 0 additions & 5 deletions swc-plugins/.eslintignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
*.d.ts
/lib/generated
/components/ui


# TODO: Fix eslint config
app/
lib/
4 changes: 3 additions & 1 deletion swc-plugins/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"extends": "next/core-web-vitals",
"rules": {
"@typescript-eslint/no-unused-vars": "off"
"@typescript-eslint/no-unused-vars": "off",
"@next/next/no-server-import-in-page": "off",
"@typescript-eslint/ban-types": "off"
}
}
13 changes: 0 additions & 13 deletions swc-plugins/app/import/page.tsx

This file was deleted.

70 changes: 70 additions & 0 deletions swc-plugins/app/import/runtime/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { db } from "@/lib/prisma";
import { createCaller } from "@/lib/server";
import { NextRequest, NextResponse } from "next/server";
import { z } from "zod";

const VersionSchema = z.object({
version: z.string(),
swcCoreVersion: z.string(),
});

const BodySchema = z.object({
runtime: z.enum(["@swc/core", "next", "rspack"]),
versions: z.array(VersionSchema),
});

export async function POST(req: NextRequest) {
if (process.env.NODE_ENV === "production") {
return NextResponse.json(
{
error: "Not allowed",
},
{
status: 403,
}
);
}

const { runtime, versions } = BodySchema.parse(await req.json());

const rt = await db.swcRuntime.findUniqueOrThrow({
where: {
name: runtime,
},
});
const api = await createCaller();

const items: {
runtimeId: bigint;
version: string;
compatRangeId: bigint;
swcCoreVersion: string;
}[] = [];

for (const version of versions) {
const compatRange = await api.compatRange.byVersion({
version: version.swcCoreVersion,
});
if (!compatRange) {
console.log(`No compat range found for ${version.swcCoreVersion}`);
continue;
}

items.push({
runtimeId: rt.id,
// Just to ensure it's a valid semver
version: version.version.replace("v", ""),
compatRangeId: compatRange.id,
// Just to ensure it's a valid semver
swcCoreVersion: version.swcCoreVersion.replace("v", ""),
});
}

await db.swcRuntimeVersion.createMany({
data: items,
});

return NextResponse.json({
ok: true,
});
}
2 changes: 1 addition & 1 deletion swc-plugins/data/ranges.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"max": "0.59.40"
},
{
"min": "v0.61.0",
"min": "0.61.0",
"max": "0.64.10"
},
{
Expand Down
9 changes: 3 additions & 6 deletions swc-plugins/lib/api/compatRange/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,7 @@ export const compatRangeRouter = router({
});

for (const range of versions) {
if (semver.lt(version, range.from)) {
continue;
}

if (semver.gte(version, range.to)) {
if (semver.gt(version, range.from) && semver.lt(version, range.to)) {
return range;
}
}
Expand Down Expand Up @@ -160,7 +156,8 @@ function merge(ranges: { name: string; version: string }[]): VersionRange[] {
* @param newValue semver
*/
function mergeVersion(min: string, max: string, newValue: string) {
const minVersion = semver.lt(min, newValue) ? min : newValue;
const minVersion =
min !== "0.0.0" && semver.lt(min, newValue) ? min : newValue;
const maxVersion = semver.gt(max, newValue) ? max : newValue;

return { min: minVersion, max: maxVersion };
Expand Down
4 changes: 3 additions & 1 deletion swc-plugins/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,10 @@
"postcss": "8.4.33",
"prisma": "^5.17.0",
"tailwindcss": "3.4.1",
"toml": "^3.0.0",
"tsconfig-replace-paths": "^0.0.14",
"typescript": "^5.5.4",
"zod-prisma-types": "^3.1.6"
"zod-prisma-types": "^3.1.6",
"zx": "^8.1.4"
}
}
37 changes: 21 additions & 16 deletions swc-plugins/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,16 @@ model SwcRuntime {
}

model SwcRuntimeVersion {
id BigInt @id @default(autoincrement())
runtime SwcRuntime @relation(fields: [runtimeId], references: [id], onDelete: Cascade)
runtimeId BigInt
version String
compatRange CompatRange @relation(fields: [compatRangeId], references: [id])
compatRangeId BigInt
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
id BigInt @id @default(autoincrement())
runtime SwcRuntime @relation(fields: [runtimeId], references: [id], onDelete: Cascade)
runtimeId BigInt
version String
/// The version of `swc_core` used by the plugin.
swcCoreVersion String
compatRange CompatRange @relation(fields: [compatRangeId], references: [id])
compatRangeId BigInt
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@unique([runtimeId, version])
}
Expand All @@ -176,14 +178,17 @@ model SwcPlugin {
}

model SwcPluginVersion {
id BigInt @id @default(autoincrement())
plugin SwcPlugin @relation(fields: [pluginId], references: [id], onDelete: Cascade)
pluginId BigInt
version String
compatRange CompatRange @relation(fields: [compatRangeId], references: [id])
compatRangeId BigInt
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
id BigInt @id @default(autoincrement())
plugin SwcPlugin @relation(fields: [pluginId], references: [id], onDelete: Cascade)
pluginId BigInt
/// The version of the plugin.
version String
/// The version of `swc_core` used by the plugin.
swcCoreVersion String?
compatRange CompatRange @relation(fields: [compatRangeId], references: [id])
compatRangeId BigInt
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@unique([pluginId, version])
}
86 changes: 86 additions & 0 deletions swc-plugins/scripts/import-runtime.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/usr/bin/env zx
//
// Run this script using an absolute path to the runtime directory.
import path from "path";
import semver from "semver";
import toml from "toml";
import { $ } from "zx";

const runtimeName = process.argv[2];
const runtimeDir = process.argv[3];

if (!runtimeName || !runtimeDir) {
console.error("Runtime name and directory are required");
process.exit(1);
}

const $$ = $({ cwd: runtimeDir });

const repositoryRoot = (await $$`git rev-parse --show-toplevel`.text()).trim();
const cargoLockPath = path.resolve(`${runtimeDir}/Cargo.lock`);
const relativePathToCargoLock = path.relative(repositoryRoot, cargoLockPath);

console.log("Runtime name:", runtimeName);
console.log("Runtime dir:", runtimeDir);
console.log("Repository root:", repositoryRoot);
console.log("Cargo.lock path:", cargoLockPath);
console.log("Relative path to Cargo.lock:", relativePathToCargoLock);

// Get all git tags
const gitTags = (await $$`git tag`.text()).split("\n");

const data = {
runtime: runtimeName,
versions: [],
};

// For each tag, get the content of `${runtimeDir}/Cargo.lock`.
for (const tag of gitTags) {
let tagVersion = tag.replace("v", "");
if (!semver.valid(tagVersion)) {
console.log(`Skipping tag ${tag} because it is not a valid semver`);
continue;
}

try {
const cargoLock =
await $$`git show ${tag}:${relativePathToCargoLock}`.text();

const parsed = toml.parse(cargoLock);
const packages = parsed.package;

for (const pkg of packages) {
if (pkg.name === "swc_core") {
const swcCoreVersion = pkg.version;

data.versions.push({
version: tagVersion,
swcCoreVersion,
});
console.log(`Found swc_core version ${swcCoreVersion} for tag ${tag}`);
}
}

// Send the data to the server
if (data.versions.length >= 20) {
await fetch("http://localhost:50000/import/runtime", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
});
data.versions = [];
}
} catch (e) {
console.error(`Failed to parse Cargo.lock for tag ${tag}: ${e}`);
}
}

await fetch("http://localhost:50000/import/runtime", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
});

0 comments on commit 63b8bd8

Please sign in to comment.