Files
webstudio/packages/scripts/build-package.ts
T
Oleg Isonen cd0174807d chore: use console.info instead of console.log (#3049)
## Description

We allow console.info (btw we should also allow warn/error/info), we
only disallow console.log in the linter because that's what we use for
debugging.

## Steps for reproduction

1. click button
2. expect xyz

## Code Review

- [ ] hi @TrySound , I need you to do
  - conceptual review (architecture, feature-correctness)
  - detailed review (read every line)
  - test it on preview

## Before requesting a review

- [ ] made a self-review
- [ ] added inline comments where things may be not obvious (the "why",
not "what")

## Before merging

- [ ] tested locally and on preview environment (preview dev login:
5de6)
- [ ] updated [test
cases](https://github.com/webstudio-is/webstudio/blob/main/apps/builder/docs/test-cases.md)
document
- [ ] added tests
- [ ] if any new env variables are added, added them to `.env.example`
and the `builder/env-check.js` if mandatory
2024-03-24 12:52:46 +01:00

89 lines
2.1 KiB
TypeScript
Executable File

#!/usr/bin/env tsx
import { rm, cp, access, writeFile, mkdir } from "node:fs/promises";
import { join } from "node:path";
import { totalist } from "totalist";
import { build, context, type BuildOptions } from "esbuild";
const args = process.argv.slice(2);
const watch = args.includes("--watch");
const noGeneratedAsEntries = args.includes("--no-generated-as-entries");
const entryPoints: string[] = [];
const assets: string[] = [];
await totalist("./src", (rel) => {
if (rel.endsWith(".ts") || rel.endsWith(".tsx")) {
if (
rel.includes(".test.") ||
rel.includes(".stories.") ||
(noGeneratedAsEntries && rel.includes("__generated__/"))
) {
return;
}
entryPoints.push(join("src", rel));
}
if (rel.endsWith(".json")) {
assets.push(rel);
}
});
await rm("lib", { recursive: true, force: true });
// regenerate them so we can safely write files into those directories without relying on esbuild creating them
// in the watch mode esbuild might not create them soon enough for them to be available for other parts of the script
await mkdir("lib");
await mkdir("lib/cjs");
const esmConfig: BuildOptions = {
entryPoints,
outdir: "lib",
format: "esm",
};
const cjsConfig: BuildOptions = {
entryPoints,
outdir: "lib/cjs",
format: "cjs",
};
if (watch) {
const esmContext = await context(esmConfig);
await esmContext.watch();
const cjsContext = await context(cjsConfig);
await cjsContext.watch();
} else {
await build(esmConfig);
await build(cjsConfig);
}
await writeFile(
"lib/cjs/package.json",
JSON.stringify({ type: "commonjs" }) + "\n",
"utf8"
);
for (const rel of assets) {
await cp(join("src", rel), join("lib", rel));
await cp(join("src", rel), join("lib/cjs", rel));
}
if (noGeneratedAsEntries) {
try {
await access("./src/__generated__");
await Promise.all([
cp("./src/__generated__", "./lib/__generated__", { recursive: true }),
cp("./src/__generated__", "./lib/cjs/__generated__", {
recursive: true,
}),
]);
} catch {
// noop
}
}
if (watch) {
console.info("watching...");
} else {
console.info("build succeeded");
}