Skip to content

Commit

Permalink
Make build script to be executed per pkg and introduce a watch script (
Browse files Browse the repository at this point in the history
…#567)

* Make build script to be executed per pkg and introduce a watch script

* add missing build script

* fixed build

* prettierignore turbo files
  • Loading branch information
Andarist authored Jun 26, 2024
1 parent 31a4fbe commit 40beb19
Show file tree
Hide file tree
Showing 22 changed files with 160 additions and 130 deletions.
5 changes: 5 additions & 0 deletions .changeset/sixty-cameras-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@replayio/replay": patch
---

Fixed an issue with missing some files realted to `/metadata` entrypoint
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dist
lerna-debug.log
e2e-repos
tsconfig.tsbuildinfo
.turbo

.pnp.*
.yarn/*
Expand Down
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ node_modules
**/lib/index.js
package-lock.json
e2e-repos
.turbo
4 changes: 2 additions & 2 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/cypress/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/jest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
1 change: 0 additions & 1 deletion packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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\""
},
Expand Down
2 changes: 1 addition & 1 deletion packages/playwright/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/puppeteer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/replay/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
18 changes: 16 additions & 2 deletions packages/replay/scripts/mirror-dist-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
})();
2 changes: 1 addition & 1 deletion packages/replayio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
17 changes: 12 additions & 5 deletions packages/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,24 @@
"./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",
"fs-extra": "^11.2.0",
"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"
]
}
}
2 changes: 1 addition & 1 deletion packages/sourcemap-upload-webpack-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/sourcemap-upload/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/test-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
1 change: 0 additions & 1 deletion scripts/pkg-build/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
84 changes: 26 additions & 58 deletions scripts/pkg-build/src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,22 @@ 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";

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,
Expand Down Expand Up @@ -165,60 +158,35 @@ async function buildPkg(pkg: Package, packagesByName: Map<string, Package>) {
: [`${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();
42 changes: 0 additions & 42 deletions scripts/pkg-build/src/getBuildablePackageGroups.ts

This file was deleted.

11 changes: 11 additions & 0 deletions turbo.json
Original file line number Diff line number Diff line change
@@ -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"]
}
Loading

0 comments on commit 40beb19

Please sign in to comment.