-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(swc/plugins): Insert swc runtime data (#44)
- Loading branch information
Showing
9 changed files
with
187 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
"max": "0.59.40" | ||
}, | ||
{ | ||
"min": "v0.61.0", | ||
"min": "0.61.0", | ||
"max": "0.64.10" | ||
}, | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}); |