Files
58c2c80401 Migrate from Yarn to pnpm (#246)
* Migrate from Yarn to pnpm

- Replace Yarn 4.0.2 with pnpm 10.29.3 as the package manager
- Add pnpm-workspace.yaml and .npmrc configuration
- Update all scripts to use pnpm commands
- Rename getYarnWorkspaces() to getWorkspaces() in scripts/helpers.ts
- Update CI workflow to use pnpm/action-setup@v4 with Node 22
- Add @jitl/tsconfig as root devDependency for proper resolution
- Add variant packages as devDependencies for test resolution
- Fix TypeScript 5.9 compatibility in memory.ts (Int32Array<ArrayBuffer>)
- Upgrade typedoc to 0.28.17 for TypeScript 5.9 support
- Upgrade typedoc-plugin-markdown to 4.10.0
- Regenerate documentation with new typedoc version

* Clarify git commit --amend rule in CLAUDE.md

* CI: Add --ignore-scripts to pnpm install

Equivalent to yarn's YARN_ENABLE_SCRIPTS=0 - prevents prepare scripts
from running during install, which would fail on a fresh checkout.

* Fix pnpm tarball naming in smoketest scripts

pnpm pack generates tarball names like jitl-quickjs-ffi-types-0.32.0.tgz
(without @ prefix, with version) instead of @jitl-quickjs-ffi-types.tgz

* Update examples to TypeScript 5.9 for compatibility

The library now requires TypeScript 5.9+ due to stricter typed array
generics (Int32Array<ArrayBuffer>). Update all TypeScript-using examples.

* Use --legacy-peer-deps for create-react-app smoketest

react-scripts@5 only supports TypeScript 3.x/4.x, but our library now
requires TypeScript 5.9+ for typed array generics. Use --legacy-peer-deps
to work around the peer dependency conflict.

* Fix formatting issues

* Fix remaining formatting issues

* Add reminder to run prettier and lint before pushing

* Document CI workflow in CLAUDE.md

* Add typedoc.json and remark plugins for in-page table of contents

- Add typedoc-plugin-remark, remark-toc, and remark-insert-headings
- Configure remark plugins to generate "Contents" section for Class,
  Interface, Enum, TypeAlias, and Function pages
- This restores the in-page TOC that lists methods/properties that was
  removed in typedoc-plugin-markdown 4.3.0

* Use custom remark plugin for headless TOC in docs

Replace remark-insert-headings + remark-toc with a custom plugin that
inserts the table of contents directly after the class description,
without a separate "Contents" heading.

* Revert to standard remark plugins for TOC with Contents heading

Use remark-insert-headings + remark-toc instead of custom plugin.
Remove mdast-util-toc dependency and custom remark-toc-headless.mjs.

* Fix lockfile - remove mdast-util-toc

* Ignore pnpm-lock.yaml in prettier, update .yarn to .pnpm

---------

Co-authored-by: Hanzo Dev <[email protected]>
2026-02-16 20:35:52 +00:00

211 lines
5.4 KiB
TypeScript

import path from "node:path"
import fs from "node:fs"
import p from "node:child_process"
import prettier from "prettier"
export const repoRoot = path.resolve(__dirname, "..")
const packagesDir = path.resolve(repoRoot, "./packages")
const tarDir = path.resolve(repoRoot, "./build/tar")
let packageSubdirs: string[] | undefined
const INSTALL_FROM_REGISTRY = Boolean(process.env.INSTALL_FROM_REGISTRY)
function getPackageJson(packageName: string) {
packageSubdirs ??= fs.readdirSync(packagesDir).map((dir) => path.join(packagesDir, dir))
const subname = packageName.split("/").at(-1) ?? "none"
let searchDirs = packageSubdirs.filter((dir) => dir.endsWith(subname))
if (searchDirs.length === 0) {
searchDirs = packageSubdirs
}
for (const dir of searchDirs) {
try {
const packageJson = JSON.parse(fs.readFileSync(path.join(dir, "package.json"), "utf8"))
if (packageJson.name === packageName) {
return packageJson
}
} catch (e) {
console.warn(e)
}
}
throw new Error(`Could not find package.json for ${packageName} in ${searchDirs}`)
}
function getTarFile(packageName: string) {
// pnpm pack generates: scope-name-version.tgz (without @ prefix)
// e.g., @jitl/[email protected] -> jitl-quickjs-ffi-types-0.32.0.tgz
const packageJson = getPackageJson(packageName)
const tarName =
packageName.replace("@", "").replace("/", "-") + "-" + packageJson.version + ".tgz"
return path.join(tarDir, tarName)
}
export function exec(command: string) {
console.log(`+ ${command}`)
p.execSync(command, { encoding: "utf-8", stdio: "inherit" })
}
class InstallFlow {
removed = new Set<string>()
added = new Set<string>()
constructor(
public into: string,
public commands: { install: string; remove: string; fromRegistry: boolean },
) {}
add(packageName: string) {
this.visit(packageName, {
after: (pkg) => {
this.removed.add(pkg.name)
},
})
this.visit(packageName, {
after: (pkg) => {
if (this.commands.fromRegistry) {
if (pkg.name === packageName) {
this.added.add(`${pkg.name}@${pkg.version}`)
}
return
}
const tarFile = getTarFile(pkg.name)
this.added.add(`${pkg.name}@${tarFile}`)
},
})
return this
}
private visit(
packageName: string,
handle: { before?(pkg: PackageJson): void; after?(pkg: PackageJson): void },
) {
const packageJson = getPackageJson(packageName)
handle.before?.(packageJson)
for (const dependency of Object.keys(packageJson.dependencies ?? {})) {
this.visit(dependency, handle)
}
handle.after?.(packageJson)
}
run() {
if (this.removed.size > 0) {
exec(`cd ${this.into} && ${this.commands.remove} ${[...this.removed].join(" ")}`)
}
if (this.added.size > 0) {
exec(`cd ${this.into} && ${this.commands.install} ${[...this.added].join(" ")}`)
}
}
}
export function installDependencyGraphFromTar(
into: string,
packageNameOrNames: string | string[],
cmds: {
install: string
remove: string
fromRegistry: boolean
} = {
install: "npm install",
remove: "npm remove",
fromRegistry: INSTALL_FROM_REGISTRY,
},
) {
const packageNames = Array.isArray(packageNameOrNames) ? packageNameOrNames : [packageNameOrNames]
const flow = new InstallFlow(into, cmds)
for (const packageName of packageNames) {
flow.add(packageName)
}
flow.run()
}
export function resolve(...parts: string[]) {
return path.resolve(...parts)
}
export async function writePretty(filePath: string, text: string) {
let output = text
const isPrettierUnsupportedType =
filePath.endsWith(".mk") || filePath.endsWith("Makefile") || path.extname(filePath) === ""
if (!isPrettierUnsupportedType) {
const prettierConfig = (await prettier.resolveConfig(filePath)) ?? {}
output = await prettier.format(text, {
...prettierConfig,
filepath: filePath,
})
}
console.warn(`write`, path.relative(process.cwd(), filePath))
fs.writeFileSync(filePath, output)
}
export interface WorkspaceJson {
location: string
name: string
}
export function getWorkspaces(): WorkspaceJson[] {
const result = JSON.parse(p.execSync("pnpm ls -r --json --depth=-1", { encoding: "utf-8" }))
// pnpm returns single JSON array with { name, path, ... } objects
return result.map((pkg: { name: string; path: string }) => ({
name: pkg.name,
location: pkg.path,
}))
}
export function readJson<T>(filepath: string): T {
return JSON.parse(fs.readFileSync(filepath, "utf8"))
}
export function tryReadJson<T>(filepath: string): T | undefined {
try {
return readJson(filepath)
} catch (e) {
return undefined
}
}
export interface PackageJson {
name: string
license: "MIT" | undefined
type?: "module"
private?: boolean
version: string
description: string
sideEffects: false
repository: {
type: string
url: string
}
scripts: Record<string, string>
files: string[]
dependencies: Record<string, string>
devDependencies?: Record<string, string>
exports: Record<
string,
| {
types: string
browser?: string
module?: string
iife?: string
workerd?: string
import: string | undefined
require: string | undefined
default: string | undefined
}
| string
| undefined
>
types?: string
main?: string
module?: string
browser?: string
author: {
name: string
email?: string
url: string
}
}