From 40beb199c1d1dec640611fec0e04e911e24b5fe3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Wed, 26 Jun 2024 14:12:11 +0200 Subject: [PATCH] Make build script to be executed per pkg and introduce a watch script (#567) * Make build script to be executed per pkg and introduce a watch script * add missing build script * fixed build * prettierignore turbo files --- .changeset/sixty-cameras-kick.md | 5 ++ .gitignore | 1 + .prettierignore | 1 + .vscode/tasks.json | 4 +- package.json | 5 +- packages/cypress/package.json | 2 +- packages/jest/package.json | 2 +- packages/node/package.json | 1 - packages/playwright/package.json | 2 +- packages/puppeteer/package.json | 2 +- packages/replay/package.json | 2 +- .../replay/scripts/mirror-dist-metadata.js | 18 +++- packages/replayio/package.json | 2 +- packages/shared/package.json | 17 ++-- .../package.json | 2 +- packages/sourcemap-upload/package.json | 2 +- packages/test-utils/package.json | 2 +- scripts/pkg-build/package.json | 1 - scripts/pkg-build/src/bin.ts | 84 ++++++------------- .../src/getBuildablePackageGroups.ts | 42 ---------- turbo.json | 11 +++ yarn.lock | 82 ++++++++++++++++-- 22 files changed, 160 insertions(+), 130 deletions(-) create mode 100644 .changeset/sixty-cameras-kick.md delete mode 100644 scripts/pkg-build/src/getBuildablePackageGroups.ts create mode 100644 turbo.json diff --git a/.changeset/sixty-cameras-kick.md b/.changeset/sixty-cameras-kick.md new file mode 100644 index 000000000..d13a701de --- /dev/null +++ b/.changeset/sixty-cameras-kick.md @@ -0,0 +1,5 @@ +--- +"@replayio/replay": patch +--- + +Fixed an issue with missing some files realted to `/metadata` entrypoint diff --git a/.gitignore b/.gitignore index 7b4c67402..12099300a 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ dist lerna-debug.log e2e-repos tsconfig.tsbuildinfo +.turbo .pnp.* .yarn/* diff --git a/.prettierignore b/.prettierignore index 98e3095d2..52fe9e750 100644 --- a/.prettierignore +++ b/.prettierignore @@ -4,3 +4,4 @@ node_modules **/lib/index.js package-lock.json e2e-repos +.turbo diff --git a/.vscode/tasks.json b/.vscode/tasks.json index a1d039c0e..2ea29e4b9 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -2,9 +2,9 @@ "version": "2.0.0", "tasks": [ { - "label": "tsc -w", + "label": "turbo watch", "type": "shell", - "command": "yarn build -w", + "command": "yarn run watch", "presentation": { "echo": true, "reveal": "silent", diff --git a/package.json b/package.json index 7a6f0da26..640beee8f 100644 --- a/package.json +++ b/package.json @@ -12,8 +12,8 @@ "example": "examples" }, "scripts": { - "build": "pkg-build", - "bootstrap": "pkg-build", + "build": "turbo run build --filter=\"./packages/*\"", + "watch": "turbo watch build --filter=\"./packages/*\"", "test": "yarn run test:unit", "test:unit": "yarn workspaces foreach --all --exclude=. --exclude=examples/create-react-app-typescript --exclude=packages/cypress/example --topological run test", "typecheck": "yarn workspaces foreach --all run typecheck", @@ -33,6 +33,7 @@ "@types/ws": "^8", "prettier": "^2.7.1", "replayio": "workspace:^", + "turbo": "^2.0.5", "typescript": "^5.5.2", "ws": "^8.17.0" }, diff --git a/packages/cypress/package.json b/packages/cypress/package.json index 79b31619f..dbee51c18 100644 --- a/packages/cypress/package.json +++ b/packages/cypress/package.json @@ -18,7 +18,7 @@ ], "scripts": { "prepare": "npm run build", - "build": "rm -rf dist/ tsconfig.tsbuildinfo && tsc", + "build": "pkg-build", "test": "echo \"Error: no test specified\"", "typecheck": "tsc --noEmit" }, diff --git a/packages/jest/package.json b/packages/jest/package.json index 01499a201..22ec3c4c6 100644 --- a/packages/jest/package.json +++ b/packages/jest/package.json @@ -18,7 +18,7 @@ ], "scripts": { "prepare": "yarn run build", - "build": "rm -rf dist/ tsconfig.tsbuildinfo && tsc", + "build": "pkg-build", "test": "echo \"Error: no test specified\"", "typecheck": "tsc --noEmit" }, diff --git a/packages/node/package.json b/packages/node/package.json index 19f0494f0..5b4a0edb0 100644 --- a/packages/node/package.json +++ b/packages/node/package.json @@ -6,7 +6,6 @@ "replay-node": "bin/replay-node" }, "scripts": { - "build": "echo \"Error: no build specified\"", "test": "echo \"Error: no test specified\"", "typecheck": "echo \"Error: no typecheck specified\"" }, diff --git a/packages/playwright/package.json b/packages/playwright/package.json index 75efc984d..9f79460fc 100644 --- a/packages/playwright/package.json +++ b/packages/playwright/package.json @@ -16,7 +16,7 @@ ], "scripts": { "prepare": "yarn run build", - "build": "rm -rf dist/ tsconfig.tsbuildinfo && tsc", + "build": "pkg-build", "test": "echo \"Error: no test specified\"", "typecheck": "tsc --noEmit" }, diff --git a/packages/puppeteer/package.json b/packages/puppeteer/package.json index 1c1766925..7da8c8e32 100644 --- a/packages/puppeteer/package.json +++ b/packages/puppeteer/package.json @@ -14,7 +14,7 @@ "scripts": { "postinstall": "node ./first-run.js", "prepare": "yarn run build", - "build": "rm -rf dist/ tsconfig.tsbuildinfo && tsc", + "build": "pkg-build", "test": "echo \"Error: no test specified\"", "typecheck": "tsc --noEmit" }, diff --git a/packages/replay/package.json b/packages/replay/package.json index 368f02625..5658e081e 100644 --- a/packages/replay/package.json +++ b/packages/replay/package.json @@ -20,7 +20,7 @@ ], "scripts": { "prepare": "yarn run build", - "build": "rm -rf dist/ metadata/ tsconfig.tsbuildinfo && tsc && node ./scripts/mirror-dist-metadata.js", + "build": "pkg-build && node ./scripts/mirror-dist-metadata.js", "test": "jest --ci", "typecheck": "tsc --noEmit" }, diff --git a/packages/replay/scripts/mirror-dist-metadata.js b/packages/replay/scripts/mirror-dist-metadata.js index e53684469..8b70a98d7 100644 --- a/packages/replay/scripts/mirror-dist-metadata.js +++ b/packages/replay/scripts/mirror-dist-metadata.js @@ -10,7 +10,13 @@ async function mirrorDistFiles(sourceDir, targetDir) { const targetPath = path.join(targetDir, filename); if ((await fs.stat(sourcePath)).isDirectory()) { - await fs.mkdir(targetPath); + try { + await fs.mkdir(targetPath); + } catch (err) { + if (err.code !== "EEXIST") { + throw err; + } + } await mirrorDistFiles(sourcePath, targetPath); return; } @@ -40,6 +46,14 @@ async function mirrorDistFiles(sourceDir, targetDir) { (async () => { const source = path.join(__dirname, "..", "dist", "metadata"); const target = path.join(__dirname, "..", "metadata"); - await fs.mkdir(target); + + try { + await fs.mkdir(target); + } catch (err) { + if (err.code !== "EEXIST") { + throw err; + } + } + await mirrorDistFiles(source, target); })(); diff --git a/packages/replayio/package.json b/packages/replayio/package.json index c15608ffc..03aa1ad4d 100644 --- a/packages/replayio/package.json +++ b/packages/replayio/package.json @@ -11,7 +11,7 @@ "node": ">=18" }, "scripts": { - "build": "rm -rf dist/ metadata/ tsconfig.tsbuildinfo && tsc", + "build": "pkg-build", "lint": "prettier --write .", "test": "jest --ci", "typecheck": "tsc --noEmit", diff --git a/packages/shared/package.json b/packages/shared/package.json index 8bb163004..2945c5f78 100644 --- a/packages/shared/package.json +++ b/packages/shared/package.json @@ -6,11 +6,8 @@ "./logger": "./dist/logger/index.js", "./*": "./dist/*.js" }, - "@replay-cli/pkg-build": { - "entrypoints": [ - "./src/*.ts", - "./src/logger/index.ts" - ] + "scripts": { + "build": "pkg-build" }, "dependencies": { "debug": "^4.3.4", @@ -18,5 +15,15 @@ "undici": "^5.28.4", "winston": "^3.13.0", "winston-loki": "^6.1.2" + }, + "devDependencies": { + "@replay-cli/pkg-build": "workspace:^", + "typescript": "^5.5.2" + }, + "@replay-cli/pkg-build": { + "entrypoints": [ + "./src/*.ts", + "./src/logger/index.ts" + ] } } diff --git a/packages/sourcemap-upload-webpack-plugin/package.json b/packages/sourcemap-upload-webpack-plugin/package.json index b73804cd6..fe1be05ce 100644 --- a/packages/sourcemap-upload-webpack-plugin/package.json +++ b/packages/sourcemap-upload-webpack-plugin/package.json @@ -12,7 +12,7 @@ "node": ">=10.13" }, "scripts": { - "build": "rm -rf dist/ tsconfig.tsbuildinfo && tsc", + "build": "pkg-build", "lint": "eslint", "lint:fix": "eslint --fix", "prepare": "yarn run build", diff --git a/packages/sourcemap-upload/package.json b/packages/sourcemap-upload/package.json index 871d0dafb..a7b783964 100644 --- a/packages/sourcemap-upload/package.json +++ b/packages/sourcemap-upload/package.json @@ -6,7 +6,7 @@ "node": ">=10.13" }, "scripts": { - "build": "rm -rf dist/ tsconfig.tsbuildinfo && tsc", + "build": "pkg-build", "lint": "eslint", "lint:fix": "eslint --fix", "prepare": "yarn run build", diff --git a/packages/test-utils/package.json b/packages/test-utils/package.json index f4021f9ec..10783806b 100644 --- a/packages/test-utils/package.json +++ b/packages/test-utils/package.json @@ -13,7 +13,7 @@ }, "scripts": { "prepare": "yarn run build", - "build": "rm -rf dist/ tsconfig.tsbuildinfo && tsc", + "build": "pkg-build", "test": "echo \"Error: no test specified\"", "typecheck": "tsc --noEmit" }, diff --git a/scripts/pkg-build/package.json b/scripts/pkg-build/package.json index 5a9152243..89638da16 100644 --- a/scripts/pkg-build/package.json +++ b/scripts/pkg-build/package.json @@ -5,7 +5,6 @@ "bin": "./bin.js", "dependencies": { "@manypkg/get-packages": "^2.2.1", - "@pnpm/deps.graph-sequencer": "^2.0.1", "@rollup/plugin-commonjs": "^26.0.1", "@rollup/plugin-json": "^6.1.0", "@rollup/plugin-node-resolve": "^15.2.3", diff --git a/scripts/pkg-build/src/bin.ts b/scripts/pkg-build/src/bin.ts index 7ee5d5263..46708e32f 100755 --- a/scripts/pkg-build/src/bin.ts +++ b/scripts/pkg-build/src/bin.ts @@ -5,11 +5,10 @@ import { nodeResolve } from "@rollup/plugin-node-resolve"; import builtInModules from "builtin-modules"; import chalk from "chalk"; import fastGlob from "fast-glob"; +import assert from "node:assert/strict"; import { spawnSync } from "node:child_process"; -import fs from "node:fs/promises"; import { rollup } from "rollup"; import { dts } from "rollup-plugin-dts"; -import { getBuildablePackageGroups } from "./getBuildablePackageGroups"; import { makePackagePredicate, PackagePredicate } from "./makePackagePredicate"; import { esbuild } from "./plugins/esbuild"; import { resolveErrors } from "./plugins/resolveErrors"; @@ -17,17 +16,11 @@ import { resolveErrors } from "./plugins/resolveErrors"; const statusFailed = chalk.redBright; const statusSuccess = chalk.greenBright; -const tscPath = spawnSync("yarn", ["bin", "tsc"]).stdout.toString().trim(); - -async function rm(path: string) { - try { - await fs.rm(path, { recursive: true }); - } catch (err: any) { - if (err.code !== "ENOENT") { - throw err; - } - } +const tscPathResult = spawnSync("yarn", ["bin", "tsc"]); +if (tscPathResult.status !== 0) { + throw new Error("Failed to find tsc"); } +const tscPath = tscPathResult.stdout.toString().trim(); async function buildJs( pkg: Package, @@ -165,60 +158,35 @@ async function buildPkg(pkg: Package, packagesByName: Map) { : [`${pkg.dir}/src/index.ts`] ).filter(input => !/\.(test|spec)\./i.test(input)); - try { - const options = { bundledDependenciesDirs, input, isBundledDependency, isExternal }; - // TODO: parallelize this better - const tscResult = spawnSync(tscPath, ["-b"], { stdio: "inherit" }); - if (tscResult.status !== 0) { - throw new Error("tsc failed"); - } - await buildJs(pkg, options); - await buildDts(pkg, { - ...options, - input: input.map(f => f.replace("/src/", "/dist/").replace(/\.ts$/, ".d.ts")), - }); - return { - status: "fulfilled" as const, - pkg: packageJson.name, - }; - } catch (error) { - return { - status: "rejected" as const, - reason: error, - pkg: packageJson.name, - }; + const options = { bundledDependenciesDirs, input, isBundledDependency, isExternal }; + // TODO: parallelize this better + const tscResult = spawnSync(tscPath, [], { stdio: "inherit" }); + if (tscResult.status !== 0) { + throw new Error("tsc failed"); } + await buildJs(pkg, options); + await buildDts(pkg, { + ...options, + input: input.map(f => f.replace("/src/", "/dist/").replace(/\.ts$/, ".d.ts")), + }); } -async function buildAll() { +async function build() { const cwd = process.cwd(); const { packages } = await getPackages(cwd); - const packagesByName = new Map(packages.map(pkg => [pkg.packageJson.name, pkg])); - await Promise.all( - packages.flatMap(pkg => [rm(`${pkg.dir}/dist`), rm(`${pkg.dir}/tsconfig.tsbuildinfo`)]) - ); - - const packageGroups = getBuildablePackageGroups(packages); - - for (const group of packageGroups) { - const packages = group.map(name => packagesByName.get(name)!); - const results = await Promise.all(packages.map(pkg => buildPkg(pkg, packagesByName))); - const successes = results.filter(r => r.status === "fulfilled"); + const pkg = packages.find(pkg => pkg.dir === cwd); + assert(pkg, `Could not find monorepo package at current directory: ${cwd}`); - successes.forEach(({ pkg }) => { - console.log(`${statusSuccess("✔")} Built ${pkg}`); - }); - - if (successes.length !== results.length) { - const failures = results.filter(r => r.status === "rejected"); + const packagesByName = new Map(packages.map(pkg => [pkg.packageJson.name, pkg])); - failures.forEach(({ pkg, reason }) => { - console.log(`${statusFailed("✘")} Failed to build ${pkg}:\n`, reason); - }); - process.exit(1); - } + try { + await buildPkg(pkg, packagesByName); + console.log(`${statusSuccess("✔")} Built successfully`); + } catch (e) { + console.error(`${statusFailed("✘")} Failed to build:\n`, e); + process.exit(1); } } -buildAll(); +build(); diff --git a/scripts/pkg-build/src/getBuildablePackageGroups.ts b/scripts/pkg-build/src/getBuildablePackageGroups.ts deleted file mode 100644 index fae50cd34..000000000 --- a/scripts/pkg-build/src/getBuildablePackageGroups.ts +++ /dev/null @@ -1,42 +0,0 @@ -import type { Package } from "@manypkg/get-packages"; -import { graphSequencer } from "@pnpm/deps.graph-sequencer"; -import fs from "node:fs"; - -// based on https://github.com/pnpm/pnpm/blob/6e031e7428b3e46fc093f47a5702ac8510703a91/workspace/sort-packages/src/index.ts -function sortPackages(packages: Package[]) { - const keys = packages.map(pkg => pkg.packageJson.name); - const setOfKeys = new Set(keys); - const graph = new Map( - packages.map( - pkg => - [ - pkg.packageJson.name, - [ - ...Object.keys(pkg.packageJson.dependencies || {}), - ...Object.keys(pkg.packageJson.devDependencies || {}), - ...Object.keys(pkg.packageJson.peerDependencies || {}), - ].filter(d => d !== pkg.packageJson.name && setOfKeys.has(d)), - ] as const - ) - ); - const sequenced = graphSequencer(graph, keys); - if (sequenced.cycles.length) { - throw new Error( - `Cycles detected in the package graph: ${sequenced.cycles - .map(cycle => cycle.join(" -> ")) - .join(", ")}` - ); - } - return sequenced.chunks; -} - -export function getBuildablePackageGroups(packages: Package[]) { - return sortPackages( - packages.filter( - pkg => - pkg.relativeDir.startsWith("packages/") && - !/\/examples?($|\/)/.test(pkg.relativeDir) && - fs.existsSync(`${pkg.dir}/tsconfig.json`) - ) - ); -} diff --git a/turbo.json b/turbo.json new file mode 100644 index 000000000..c22271dbf --- /dev/null +++ b/turbo.json @@ -0,0 +1,11 @@ +{ + "$schema": "https://turbo.build/schema.json", + "tasks": { + "build": { + "dependsOn": ["^build"], + "inputs": ["$TURBO_DEFAULT$", "src/**", "tsconfig.json"], + "outputs": ["dist/**", ".tsbuildinfo"] + } + }, + "globalDependencies": ["tsconfig.json"] +} diff --git a/yarn.lock b/yarn.lock index eaa622407..c2016bcc5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3339,13 +3339,6 @@ __metadata: languageName: node linkType: hard -"@pnpm/deps.graph-sequencer@npm:^2.0.1": - version: 2.0.1 - resolution: "@pnpm/deps.graph-sequencer@npm:2.0.1" - checksum: 10c0/59569709bf243722fb0d97af78c95bb6ddec7d616cddd69c5698687897f780acacd3558a4cd06d8f69a4720f276250f7cf0d8d899e8ca96d9f2c7a3933426481 - languageName: node - linkType: hard - "@protobufjs/aspromise@npm:^1.1.1, @protobufjs/aspromise@npm:^1.1.2": version: 1.1.2 resolution: "@protobufjs/aspromise@npm:1.1.2" @@ -3424,7 +3417,6 @@ __metadata: resolution: "@replay-cli/pkg-build@workspace:scripts/pkg-build" dependencies: "@manypkg/get-packages": "npm:^2.2.1" - "@pnpm/deps.graph-sequencer": "npm:^2.0.1" "@replay-cli/tsconfig": "workspace:^" "@rollup/plugin-commonjs": "npm:^26.0.1" "@rollup/plugin-json": "npm:^6.1.0" @@ -3447,8 +3439,10 @@ __metadata: version: 0.0.0-use.local resolution: "@replay-cli/shared@workspace:packages/shared" dependencies: + "@replay-cli/pkg-build": "workspace:^" debug: "npm:^4.3.4" fs-extra: "npm:^11.2.0" + typescript: "npm:^5.5.2" undici: "npm:^5.28.4" winston: "npm:^3.13.0" winston-loki: "npm:^6.1.2" @@ -15653,6 +15647,7 @@ __metadata: "@types/ws": "npm:^8" prettier: "npm:^2.7.1" replayio: "workspace:^" + turbo: "npm:^2.0.5" typescript: "npm:^5.5.2" ws: "npm:^8.17.0" languageName: unknown @@ -17735,6 +17730,77 @@ __metadata: languageName: node linkType: hard +"turbo-darwin-64@npm:2.0.5": + version: 2.0.5 + resolution: "turbo-darwin-64@npm:2.0.5" + conditions: os=darwin & cpu=x64 + languageName: node + linkType: hard + +"turbo-darwin-arm64@npm:2.0.5": + version: 2.0.5 + resolution: "turbo-darwin-arm64@npm:2.0.5" + conditions: os=darwin & cpu=arm64 + languageName: node + linkType: hard + +"turbo-linux-64@npm:2.0.5": + version: 2.0.5 + resolution: "turbo-linux-64@npm:2.0.5" + conditions: os=linux & cpu=x64 + languageName: node + linkType: hard + +"turbo-linux-arm64@npm:2.0.5": + version: 2.0.5 + resolution: "turbo-linux-arm64@npm:2.0.5" + conditions: os=linux & cpu=arm64 + languageName: node + linkType: hard + +"turbo-windows-64@npm:2.0.5": + version: 2.0.5 + resolution: "turbo-windows-64@npm:2.0.5" + conditions: os=win32 & cpu=x64 + languageName: node + linkType: hard + +"turbo-windows-arm64@npm:2.0.5": + version: 2.0.5 + resolution: "turbo-windows-arm64@npm:2.0.5" + conditions: os=win32 & cpu=arm64 + languageName: node + linkType: hard + +"turbo@npm:^2.0.5": + version: 2.0.5 + resolution: "turbo@npm:2.0.5" + dependencies: + turbo-darwin-64: "npm:2.0.5" + turbo-darwin-arm64: "npm:2.0.5" + turbo-linux-64: "npm:2.0.5" + turbo-linux-arm64: "npm:2.0.5" + turbo-windows-64: "npm:2.0.5" + turbo-windows-arm64: "npm:2.0.5" + dependenciesMeta: + turbo-darwin-64: + optional: true + turbo-darwin-arm64: + optional: true + turbo-linux-64: + optional: true + turbo-linux-arm64: + optional: true + turbo-windows-64: + optional: true + turbo-windows-arm64: + optional: true + bin: + turbo: bin/turbo + checksum: 10c0/f6590b90d9de060c2c522415e0a63a4ddec750fff892e334dde7f7c726f6e07a7605fdab424ce3c31ed09420cefa9c0f34e2fbd6774d3890c1302b4eb94b3650 + languageName: node + linkType: hard + "tweetnacl@npm:^0.14.3, tweetnacl@npm:~0.14.0": version: 0.14.5 resolution: "tweetnacl@npm:0.14.5"