mirror of
https://github.com/hanzoai/frappe.git
synced 2026-11-09 14:28:40 +00:00
Collect errors from in-flight tasks and reject with AggregateError when multiple apps fail concurrently, instead of dropping subsequent failures after the first reject.
948 lines
25 KiB
JavaScript
948 lines
25 KiB
JavaScript
const path = require("path");
|
|
const fs = require("fs");
|
|
const os = require("os");
|
|
const { spawn } = require("child_process");
|
|
const glob = require("fast-glob");
|
|
const esbuild = require("esbuild");
|
|
const vue = require("esbuild-plugin-vue3");
|
|
const yargs = require("yargs");
|
|
const cliui = require("cliui")();
|
|
const chalk = require("chalk");
|
|
const html_plugin = require("./frappe-html");
|
|
const vue_style_plugin = require("./frappe-vue-style");
|
|
const rtlcss = require("rtlcss");
|
|
const postCssPlugin = require("@frappe/esbuild-plugin-postcss2").default;
|
|
const ignore_assets = require("./ignore-assets");
|
|
const sass_options = require("./sass_options");
|
|
const build_cleanup_plugin = require("./build-cleanup");
|
|
|
|
const {
|
|
app_list,
|
|
assets_path,
|
|
apps_path,
|
|
sites_path,
|
|
get_public_path,
|
|
log,
|
|
log_warn,
|
|
log_error,
|
|
bench_path,
|
|
get_redis_subscriber,
|
|
} = require("./utils");
|
|
|
|
const argv = yargs
|
|
.usage("Usage: node esbuild [options]")
|
|
.option("apps", {
|
|
type: "string",
|
|
description: "Run build for specific apps",
|
|
})
|
|
.option("skip_frappe", {
|
|
type: "boolean",
|
|
description: "Skip building frappe assets",
|
|
})
|
|
.option("files", {
|
|
type: "string",
|
|
description: "Run build for specified bundles",
|
|
})
|
|
.option("watch", {
|
|
type: "boolean",
|
|
description: "Run in watch mode and rebuild on file changes",
|
|
})
|
|
.option("live-reload", {
|
|
type: "boolean",
|
|
description: `Automatically reload Desk when assets are rebuilt.
|
|
Can only be used with the --watch flag.`,
|
|
})
|
|
.option("production", {
|
|
type: "boolean",
|
|
description: "Run build in production mode",
|
|
})
|
|
.option("run-build-command", {
|
|
type: "boolean",
|
|
description: "Run build command for apps",
|
|
})
|
|
.option("save-metafiles", {
|
|
type: "boolean",
|
|
description:
|
|
"Saves esbuild metafiles for built assets. Useful for analyzing bundle size. More info: https://esbuild.github.io/api/#metafile",
|
|
})
|
|
.option("using-cached", {
|
|
type: "boolean",
|
|
description:
|
|
"Skips build and uses cached build artifacts to update assets.json (used by Bench)",
|
|
})
|
|
.option("esbuild-target", {
|
|
type: "string",
|
|
description: "Specifies the target of the build output.",
|
|
})
|
|
.option("verbose", {
|
|
type: "boolean",
|
|
description: "Print detailed build output",
|
|
})
|
|
.example("node esbuild --apps frappe,erpnext", "Run build only for frappe and erpnext")
|
|
.example(
|
|
"node esbuild --files frappe/website.bundle.js,frappe/desk.bundle.js",
|
|
"Run build only for specified bundles"
|
|
)
|
|
.version(false).argv;
|
|
|
|
const APPS = (!argv.apps ? app_list : argv.apps.split(",")).filter(
|
|
(app) => !(argv.skip_frappe && app == "frappe")
|
|
);
|
|
const FILES_TO_BUILD = argv.files ? argv.files.split(",") : [];
|
|
const WATCH_MODE = Boolean(argv.watch);
|
|
const PRODUCTION = Boolean(argv.production);
|
|
const RUN_BUILD_COMMAND = !WATCH_MODE && Boolean(argv["run-build-command"]);
|
|
const ESBUILD_TARGET = argv["esbuild-target"] || "es2017";
|
|
// Allow VERBOSE to be inherited by nested esbuild invocations (per-app yarn
|
|
// build) via the environment; CLI flag propagation through yarn run breaks
|
|
// chained scripts like `yarn copy && cp`.
|
|
const VERBOSE = Boolean(argv.verbose) || process.env.FRAPPE_BUILD_VERBOSE === "1";
|
|
|
|
const TOTAL_BUILD_TIME = `${chalk.black.bgGreen(" DONE ")} Total Build Time`;
|
|
const NODE_PATHS = [].concat(
|
|
// node_modules of apps directly importable
|
|
app_list.map((app) => path.resolve(apps_path, app, "node_modules")).filter(fs.existsSync),
|
|
// import js file of any app if you provide the full path
|
|
app_list.map((app) => path.resolve(apps_path, app)).filter(fs.existsSync)
|
|
);
|
|
const USING_CACHED = Boolean(argv["using-cached"]);
|
|
// In non-verbose mode we only keep the tail of child-process output so failed
|
|
// builds still have actionable context without growing unbounded. Verbose mode
|
|
// uses Infinity to preserve the full log for serialized flushing.
|
|
const MAX_OUTPUT_BUFFER = 20000;
|
|
|
|
execute().catch((e) => {
|
|
if (!e?.reported) {
|
|
console.error(e);
|
|
}
|
|
process.exit(1);
|
|
});
|
|
|
|
if (WATCH_MODE) {
|
|
// listen for open files in editor event
|
|
open_in_editor();
|
|
}
|
|
|
|
async function execute() {
|
|
console.time(TOTAL_BUILD_TIME);
|
|
if (USING_CACHED) {
|
|
await update_assets_json_from_built_assets(APPS);
|
|
await update_assets_json_in_cache();
|
|
console.timeEnd(TOTAL_BUILD_TIME);
|
|
process.exit(0);
|
|
}
|
|
|
|
let results;
|
|
try {
|
|
results = await build_assets_for_apps(APPS, FILES_TO_BUILD);
|
|
} catch (e) {
|
|
log_error("There were some problems during build");
|
|
log();
|
|
log(chalk.dim(e.stack));
|
|
if (process.env.CI || PRODUCTION) {
|
|
process.kill(process.pid);
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (!WATCH_MODE) {
|
|
if (VERBOSE) {
|
|
log_built_assets(results);
|
|
console.timeEnd(TOTAL_BUILD_TIME);
|
|
log();
|
|
} else {
|
|
console.timeEnd(TOTAL_BUILD_TIME);
|
|
}
|
|
} else {
|
|
log("Watching for changes...");
|
|
}
|
|
for (const result of results) {
|
|
await write_assets_json(result.metafile);
|
|
}
|
|
if (RUN_BUILD_COMMAND) {
|
|
await run_build_command_for_apps(APPS);
|
|
}
|
|
if (!WATCH_MODE) {
|
|
process.exit(0);
|
|
}
|
|
}
|
|
|
|
async function update_assets_json_from_built_assets(apps) {
|
|
const assets = await get_assets_json_path_and_obj(false);
|
|
const assets_rtl = await get_assets_json_path_and_obj(true);
|
|
|
|
for (const app of apps) {
|
|
await update_assets_obj(app, assets.obj, assets_rtl.obj);
|
|
}
|
|
|
|
for (const { obj, path } of [assets, assets_rtl]) {
|
|
const data = JSON.stringify(obj, null, 4);
|
|
await fs.promises.writeFile(path, data);
|
|
}
|
|
}
|
|
|
|
async function update_assets_obj(app, assets, assets_rtl) {
|
|
const app_path = path.join(apps_path, app, app);
|
|
const dist_path = path.join(app_path, "public", "dist");
|
|
const files = await glob("**/*.bundle.*.{js,css}", { cwd: dist_path });
|
|
const assets_dist = path.join("assets", app, "dist");
|
|
const prefix = path.join("/", assets_dist);
|
|
|
|
// eg: "js/marketplace.bundle.6SCSPSGQ.js"
|
|
for (const file of files) {
|
|
const source_path = path.join(dist_path, file);
|
|
const dest_path = path.join(sites_path, assets_dist, file);
|
|
|
|
// Copy asset file from app/public to sites/assets
|
|
if (!fs.existsSync(dest_path)) {
|
|
const dest_dir = path.dirname(dest_path);
|
|
fs.mkdirSync(dest_dir, { recursive: true });
|
|
fs.copyFileSync(source_path, dest_path);
|
|
}
|
|
|
|
// eg: [ "marketplace", "bundle", "6SCSPSGQ", "js" ]
|
|
const parts = path.basename(file).split(".");
|
|
|
|
// eg: "marketplace.bundle.js"
|
|
const key = [...parts.slice(0, -2), parts.at(-1)].join(".");
|
|
|
|
// eg: "js/marketplace.bundle.6SCSPSGQ.js"
|
|
const value = path.join(prefix, file);
|
|
if (file.includes("-rtl")) {
|
|
assets_rtl[`rtl_${key}`] = value;
|
|
} else {
|
|
assets[key] = value;
|
|
}
|
|
}
|
|
}
|
|
|
|
function build_assets_for_apps(apps, files) {
|
|
let { include_patterns, ignore_patterns } = files.length
|
|
? get_files_to_build(files)
|
|
: get_all_files_to_build(apps);
|
|
|
|
return glob(include_patterns, { ignore: ignore_patterns }).then((files) => {
|
|
let output_path = assets_path;
|
|
|
|
let file_map = {};
|
|
let style_file_map = {};
|
|
let rtl_style_file_map = {};
|
|
for (let file of files) {
|
|
let relative_app_path = path.relative(apps_path, file);
|
|
let app = relative_app_path.split(path.sep)[0];
|
|
|
|
let extension = path.extname(file);
|
|
let output_name = path.basename(file, extension);
|
|
if ([".css", ".scss", ".less", ".sass", ".styl"].includes(extension)) {
|
|
output_name = path.join("css", output_name);
|
|
} else if ([".js", ".ts"].includes(extension)) {
|
|
output_name = path.join("js", output_name);
|
|
}
|
|
output_name = path.join(app, "dist", output_name);
|
|
|
|
if (
|
|
Object.keys(file_map).includes(output_name) ||
|
|
Object.keys(style_file_map).includes(output_name)
|
|
) {
|
|
log_warn(`Duplicate output file ${output_name} generated from ${file}`);
|
|
}
|
|
if ([".css", ".scss", ".less", ".sass", ".styl"].includes(extension)) {
|
|
style_file_map[output_name] = file;
|
|
rtl_style_file_map[output_name.replace("/css/", "/css-rtl/")] = file;
|
|
} else {
|
|
file_map[output_name] = file;
|
|
}
|
|
}
|
|
let build = build_files({
|
|
files: file_map,
|
|
outdir: output_path,
|
|
});
|
|
let style_build = build_style_files({
|
|
files: style_file_map,
|
|
outdir: output_path,
|
|
});
|
|
let rtl_style_build = build_style_files({
|
|
files: rtl_style_file_map,
|
|
outdir: output_path,
|
|
rtl_style: true,
|
|
});
|
|
return Promise.all([build, style_build, rtl_style_build]);
|
|
});
|
|
}
|
|
|
|
function get_all_files_to_build(apps) {
|
|
let include_patterns = [];
|
|
let ignore_patterns = [];
|
|
|
|
for (let app of apps) {
|
|
let public_path = get_public_path(app);
|
|
include_patterns.push(
|
|
path.resolve(public_path, "**", "*.bundle.{js,ts,css,sass,scss,less,styl,jsx}")
|
|
);
|
|
ignore_patterns.push(
|
|
path.resolve(public_path, "node_modules"),
|
|
path.resolve(public_path, "dist")
|
|
);
|
|
}
|
|
|
|
return {
|
|
include_patterns,
|
|
ignore_patterns,
|
|
};
|
|
}
|
|
|
|
function get_files_to_build(files) {
|
|
// files: ['frappe/website.bundle.js', 'erpnext/main.bundle.js']
|
|
let include_patterns = [];
|
|
let ignore_patterns = [];
|
|
|
|
for (let file of files) {
|
|
let [app, bundle] = file.split("/");
|
|
let public_path = get_public_path(app);
|
|
include_patterns.push(path.resolve(public_path, "**", bundle));
|
|
ignore_patterns.push(
|
|
path.resolve(public_path, "node_modules"),
|
|
path.resolve(public_path, "dist")
|
|
);
|
|
}
|
|
|
|
return {
|
|
include_patterns,
|
|
ignore_patterns,
|
|
};
|
|
}
|
|
|
|
function build_files({ files, outdir }) {
|
|
let build_plugins = [vue(), html_plugin, build_cleanup_plugin, vue_style_plugin];
|
|
if (WATCH_MODE) build_plugins.push(watch_plugin);
|
|
return build_or_watch(get_build_options(files, outdir, build_plugins));
|
|
}
|
|
|
|
function build_style_files({ files, outdir, rtl_style = false }) {
|
|
let plugins = [];
|
|
if (rtl_style) {
|
|
plugins.push(rtlcss);
|
|
}
|
|
|
|
let build_plugins = [
|
|
ignore_assets,
|
|
build_cleanup_plugin,
|
|
postCssPlugin({
|
|
plugins: plugins,
|
|
sassOptions: sass_options,
|
|
}),
|
|
];
|
|
|
|
plugins.push(require("autoprefixer"));
|
|
if (WATCH_MODE) build_plugins.push(watch_plugin);
|
|
return build_or_watch(get_build_options(files, outdir, build_plugins));
|
|
}
|
|
|
|
// As of esbuild 0.17 the `watch`/`incremental` build options and the
|
|
// `onRebuild` callback were removed in favour of the context API. In watch
|
|
// mode we create a context, run the initial build via rebuild() (so callers
|
|
// still get a result with a metafile), and then start watching. Rebuilds are
|
|
// handled by `watch_plugin` via the onEnd hook.
|
|
async function build_or_watch(options) {
|
|
if (!WATCH_MODE) {
|
|
return esbuild.build(options);
|
|
}
|
|
let context = await esbuild.context(options);
|
|
let result = await context.rebuild();
|
|
await context.watch();
|
|
return result;
|
|
}
|
|
|
|
function get_build_options(files, outdir, plugins) {
|
|
return {
|
|
entryPoints: files,
|
|
entryNames: "[dir]/[name].[hash]",
|
|
target: [ESBUILD_TARGET],
|
|
outdir,
|
|
sourcemap: true,
|
|
bundle: true,
|
|
metafile: true,
|
|
minify: PRODUCTION,
|
|
nodePaths: NODE_PATHS,
|
|
define: {
|
|
"process.env.NODE_ENV": JSON.stringify(PRODUCTION ? "production" : "development"),
|
|
__VUE_OPTIONS_API__: JSON.stringify(true),
|
|
__VUE_PROD_DEVTOOLS__: JSON.stringify(false),
|
|
},
|
|
plugins: plugins,
|
|
};
|
|
}
|
|
|
|
// Replaces the old `onRebuild` watch callback (removed in esbuild 0.17). The
|
|
// onEnd hook fires after every build, so the first invocation (the initial
|
|
// build, whose assets.json is written by execute()) is skipped; subsequent
|
|
// rebuilds update assets.json and notify the browser.
|
|
const watch_plugin = {
|
|
name: "frappe-watch",
|
|
setup(build) {
|
|
let first_build = true;
|
|
build.onEnd(async (result) => {
|
|
if (first_build) {
|
|
first_build = false;
|
|
return;
|
|
}
|
|
|
|
if (result.errors.length) {
|
|
log_error("There was an error during rebuilding changes.");
|
|
log();
|
|
let error = {
|
|
errors: result.errors,
|
|
stack: result.errors.map((e) => e.text).join("\n"),
|
|
};
|
|
log(chalk.dim(error.stack));
|
|
notify_redis({ error });
|
|
} else {
|
|
let { new_assets_json, prev_assets_json } = await write_assets_json(
|
|
result.metafile
|
|
);
|
|
|
|
let changed_files;
|
|
if (prev_assets_json) {
|
|
changed_files = get_rebuilt_assets(prev_assets_json, new_assets_json);
|
|
|
|
let timestamp = new Date().toLocaleTimeString();
|
|
let message = `${timestamp}: Compiled ${changed_files.length} files...`;
|
|
log(chalk.yellow(message));
|
|
for (let filepath of changed_files) {
|
|
let filename = path.basename(filepath);
|
|
log(" " + filename);
|
|
}
|
|
log();
|
|
}
|
|
notify_redis({ success: true, changed_files });
|
|
}
|
|
});
|
|
},
|
|
};
|
|
|
|
function log_built_assets(results) {
|
|
let outputs = {};
|
|
for (const result of results) {
|
|
outputs = Object.assign(outputs, result.metafile.outputs);
|
|
}
|
|
let column_widths = [60, 20];
|
|
cliui.div(
|
|
{
|
|
text: chalk.cyan.bold("File"),
|
|
width: column_widths[0],
|
|
},
|
|
{
|
|
text: chalk.cyan.bold("Size"),
|
|
width: column_widths[1],
|
|
}
|
|
);
|
|
cliui.div("");
|
|
|
|
let output_by_dist_path = {};
|
|
for (let outfile in outputs) {
|
|
if (outfile.endsWith(".map")) continue;
|
|
let data = outputs[outfile];
|
|
outfile = path.resolve(outfile);
|
|
outfile = path.relative(assets_path, outfile);
|
|
let filename = path.basename(outfile);
|
|
let dist_path = outfile.replace(filename, "");
|
|
output_by_dist_path[dist_path] = output_by_dist_path[dist_path] || [];
|
|
output_by_dist_path[dist_path].push({
|
|
name: filename,
|
|
size: (data.bytes / 1000).toFixed(2) + " Kb",
|
|
});
|
|
}
|
|
|
|
for (let dist_path in output_by_dist_path) {
|
|
let files = output_by_dist_path[dist_path];
|
|
cliui.div({
|
|
text: dist_path,
|
|
width: column_widths[0],
|
|
});
|
|
|
|
for (let i in files) {
|
|
let file = files[i];
|
|
let branch = "";
|
|
if (i < files.length - 1) {
|
|
branch = "├─ ";
|
|
} else {
|
|
branch = "└─ ";
|
|
}
|
|
let color = file.name.endsWith(".js") ? "green" : "blue";
|
|
cliui.div(
|
|
{
|
|
text: branch + chalk[color]("" + file.name),
|
|
width: column_widths[0],
|
|
},
|
|
{
|
|
text: file.size,
|
|
width: column_widths[1],
|
|
}
|
|
);
|
|
}
|
|
cliui.div("");
|
|
}
|
|
log(cliui.toString());
|
|
}
|
|
|
|
// to store previous build's assets.json for comparison
|
|
let prev_assets_json;
|
|
let curr_assets_json;
|
|
|
|
async function write_assets_json(metafile) {
|
|
let rtl = false;
|
|
prev_assets_json = curr_assets_json;
|
|
let out = {};
|
|
for (let output in metafile.outputs) {
|
|
let info = metafile.outputs[output];
|
|
let asset_path = "/" + path.relative(sites_path, output);
|
|
if (info.entryPoint) {
|
|
let key = path.basename(info.entryPoint);
|
|
if (key.endsWith(".css") && asset_path.includes("/css-rtl/")) {
|
|
rtl = true;
|
|
key = `rtl_${key}`;
|
|
}
|
|
out[key] = asset_path;
|
|
}
|
|
}
|
|
|
|
let { obj: assets_json, path: assets_json_path } = await get_assets_json_path_and_obj(rtl);
|
|
// update with new values
|
|
let new_assets_json = Object.assign({}, assets_json, out);
|
|
curr_assets_json = new_assets_json;
|
|
|
|
await fs.promises.writeFile(assets_json_path, JSON.stringify(new_assets_json, null, 4));
|
|
await update_assets_json_in_cache();
|
|
if (argv["save-metafiles"]) {
|
|
// use current timestamp in readable formate as a suffix for filename
|
|
let current_timestamp = new Date().getTime();
|
|
const metafile_name = `meta-${current_timestamp}.json`;
|
|
await fs.promises.writeFile(`${metafile_name}`, JSON.stringify(metafile));
|
|
log(`Saved metafile as ${metafile_name}`);
|
|
}
|
|
return {
|
|
new_assets_json,
|
|
prev_assets_json,
|
|
};
|
|
}
|
|
|
|
async function update_assets_json_in_cache() {
|
|
// Redis won't be present during docker image build
|
|
if (process.env.FRAPPE_DOCKER_BUILD) {
|
|
return;
|
|
}
|
|
|
|
// update assets_json cache in redis, so that it can be read directly by python
|
|
let client = get_redis_subscriber("redis_cache");
|
|
// handle error event to avoid printing stack traces
|
|
try {
|
|
await client.connect();
|
|
} catch (e) {
|
|
log_warn("Cannot connect to redis_cache to update assets_json");
|
|
}
|
|
client.del("assets_json", (err) => {
|
|
client.unref();
|
|
});
|
|
}
|
|
|
|
async function get_assets_json_path_and_obj(is_rtl) {
|
|
const file_name = is_rtl ? "assets-rtl.json" : "assets.json";
|
|
const assets_json_path = path.resolve(assets_path, file_name);
|
|
let assets_json;
|
|
try {
|
|
assets_json = await fs.promises.readFile(assets_json_path, "utf-8");
|
|
} catch (error) {
|
|
assets_json = "{}";
|
|
}
|
|
assets_json = JSON.parse(assets_json);
|
|
return { obj: assets_json, path: assets_json_path };
|
|
}
|
|
|
|
function get_build_concurrency(app_count) {
|
|
const raw = process.env.FRAPPE_BUILD_CONCURRENCY?.trim();
|
|
if (raw) {
|
|
const env_limit = Number(raw);
|
|
if (Number.isInteger(env_limit)) {
|
|
if (env_limit === 0) {
|
|
return Math.min(1, app_count);
|
|
}
|
|
if (env_limit > 0) {
|
|
return Math.min(env_limit, app_count);
|
|
}
|
|
}
|
|
}
|
|
const cpu_limit = Math.max(1, Math.floor(os.cpus().length / 2));
|
|
return Math.min(cpu_limit, app_count);
|
|
}
|
|
|
|
// Track running app-build children so failures or signals can terminate all of
|
|
// them together and avoid orphaned processes.
|
|
const BUILD_CHILDREN = new Set();
|
|
|
|
function register_build_signal_handlers() {
|
|
const exit_after_cleanup = (exit_code) => {
|
|
terminate_build_children().finally(() => process.exit(exit_code));
|
|
};
|
|
|
|
const on_sigint = () => exit_after_cleanup(130);
|
|
const on_sigterm = () => exit_after_cleanup(143);
|
|
|
|
process.once("SIGINT", on_sigint);
|
|
process.once("SIGTERM", on_sigterm);
|
|
|
|
return () => {
|
|
process.removeListener("SIGINT", on_sigint);
|
|
process.removeListener("SIGTERM", on_sigterm);
|
|
};
|
|
}
|
|
|
|
async function run_build_command_for_apps(apps) {
|
|
BUILD_CHILDREN.clear();
|
|
const build_apps = [];
|
|
for (let app of apps) {
|
|
if (app === "frappe") continue;
|
|
|
|
let root_app_path = path.resolve(apps_path, app);
|
|
let package_json = path.resolve(root_app_path, "package.json");
|
|
|
|
if (!fs.existsSync(package_json)) {
|
|
continue;
|
|
}
|
|
|
|
let { scripts } = require(package_json);
|
|
if (!scripts?.build) {
|
|
continue;
|
|
}
|
|
|
|
build_apps.push({ app, root_app_path });
|
|
}
|
|
|
|
if (!build_apps.length) {
|
|
return;
|
|
}
|
|
|
|
const concurrency = get_build_concurrency(build_apps.length);
|
|
const execution_mode =
|
|
concurrency === 1 ? "sequentially" : `in parallel (${concurrency} workers)`;
|
|
log(`Running build commands for ${build_apps.length} app(s) ${execution_mode}...`);
|
|
|
|
const tasks = build_apps.map(
|
|
({ app, root_app_path }) =>
|
|
() =>
|
|
run_app_build(app, root_app_path)
|
|
);
|
|
|
|
const unregister_signal_handlers = register_build_signal_handlers();
|
|
try {
|
|
await run_with_concurrency(tasks, concurrency);
|
|
} catch (error) {
|
|
await terminate_build_children();
|
|
const errors = error instanceof AggregateError ? error.errors : [error];
|
|
for (const err of errors) {
|
|
const step_label = err.step === "install" ? "yarn install" : "yarn build";
|
|
log_error(`${step_label} failed for ${err.app || "an app"}`);
|
|
if (VERBOSE) {
|
|
// Child output (if any) was already streamed by flush_verbose;
|
|
// surface the raw error so spawn/ENOENT failures aren't silent.
|
|
log(chalk.dim(err.stack || err.message || String(err)));
|
|
} else if (err.output) {
|
|
log(err.output.trim());
|
|
}
|
|
}
|
|
if (!VERBOSE && errors.some((e) => !e.output)) {
|
|
log_warn("Run again with --verbose for more details.");
|
|
}
|
|
// Prevent the top-level execute().catch from re-dumping the same error:
|
|
// verbose builds already streamed the child output, and non-verbose builds
|
|
// just printed a trimmed summary above.
|
|
error.reported = true;
|
|
throw error;
|
|
} finally {
|
|
unregister_signal_handlers();
|
|
}
|
|
|
|
log("Build commands finished.");
|
|
}
|
|
|
|
async function run_app_build(app, root_app_path) {
|
|
const started = Date.now();
|
|
let node_modules = path.resolve(root_app_path, "node_modules");
|
|
if (!fs.existsSync(node_modules)) {
|
|
await run_command("yarn install --frozen-lockfile", {
|
|
cwd: root_app_path,
|
|
app,
|
|
step: "install",
|
|
});
|
|
}
|
|
|
|
await run_command("yarn build", { cwd: root_app_path, app, step: "build" });
|
|
|
|
if (!VERBOSE) {
|
|
const elapsed = ((Date.now() - started) / 1000).toFixed(1);
|
|
log(`${chalk.green("✔")} ${chalk.bold(app)} built in ${elapsed}s`);
|
|
}
|
|
}
|
|
|
|
function run_command(command, { cwd, app, step }) {
|
|
return new Promise((resolve, reject) => {
|
|
const child = spawn(command, {
|
|
cwd,
|
|
shell: true,
|
|
stdio: ["ignore", "pipe", "pipe"],
|
|
env: VERBOSE ? { ...process.env, FRAPPE_BUILD_VERBOSE: "1" } : process.env,
|
|
});
|
|
|
|
BUILD_CHILDREN.add(child);
|
|
|
|
const output_buffer = create_output_buffer({
|
|
max_bytes: VERBOSE ? Infinity : MAX_OUTPUT_BUFFER,
|
|
});
|
|
const on_data = (chunk) => output_buffer.append(chunk);
|
|
if (child.stdout) child.stdout.on("data", on_data);
|
|
if (child.stderr) child.stderr.on("data", on_data);
|
|
|
|
const flush_verbose = () => {
|
|
if (!VERBOSE || !output_buffer.length) return;
|
|
const banner = chalk.dim(`──── ${step} · ${chalk.bold(app)} ────`);
|
|
const text = output_buffer.toString();
|
|
const trailing = text.endsWith("\n") ? "" : "\n";
|
|
process.stdout.write(`\n${banner}\n${text}${trailing}`);
|
|
};
|
|
|
|
child.on("error", (error) => {
|
|
BUILD_CHILDREN.delete(child);
|
|
flush_verbose();
|
|
error.app = app;
|
|
error.step = step;
|
|
error.output = output_buffer.toString();
|
|
reject(error);
|
|
});
|
|
|
|
child.on("close", (code, signal) => {
|
|
BUILD_CHILDREN.delete(child);
|
|
flush_verbose();
|
|
if (code === 0) {
|
|
resolve();
|
|
return;
|
|
}
|
|
|
|
let exit_details = `exit code ${code}`;
|
|
if (signal) {
|
|
exit_details = `signal ${signal}`;
|
|
} else if (code === null) {
|
|
exit_details = "unknown exit status";
|
|
}
|
|
|
|
const error = new Error(`${step} command failed for ${app} (${exit_details})`);
|
|
error.app = app;
|
|
error.step = step;
|
|
error.output = output_buffer.toString();
|
|
reject(error);
|
|
});
|
|
});
|
|
}
|
|
|
|
function create_output_buffer({ max_bytes }) {
|
|
const chunks = [];
|
|
let size = 0;
|
|
let truncated = false;
|
|
|
|
const trim = () => {
|
|
if (max_bytes === Infinity) return;
|
|
while (size > max_bytes && chunks.length > 1) {
|
|
size -= chunks.shift().length;
|
|
truncated = true;
|
|
}
|
|
// A single remaining chunk can still exceed the cap; slice its tail.
|
|
if (chunks.length === 1 && size > max_bytes) {
|
|
const last = chunks[0];
|
|
chunks[0] = last.subarray(last.length - max_bytes);
|
|
size = chunks[0].length;
|
|
truncated = true;
|
|
}
|
|
};
|
|
|
|
return {
|
|
append(chunk) {
|
|
const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
chunks.push(buf);
|
|
size += buf.length;
|
|
trim();
|
|
},
|
|
toString() {
|
|
if (!chunks.length) return "";
|
|
const prefix = truncated ? "…(output truncated)…\n" : "";
|
|
return prefix + Buffer.concat(chunks, size).toString();
|
|
},
|
|
get length() {
|
|
return size;
|
|
},
|
|
};
|
|
}
|
|
|
|
function run_with_concurrency(tasks, concurrency) {
|
|
let index = 0;
|
|
let running = 0;
|
|
const errors = [];
|
|
|
|
return new Promise((resolve, reject) => {
|
|
const maybe_done = () => {
|
|
if (running !== 0) return;
|
|
if (errors.length === 1) reject(errors[0]);
|
|
else if (errors.length) reject(new AggregateError(errors, "One or more tasks failed"));
|
|
else resolve();
|
|
};
|
|
|
|
const schedule = () => {
|
|
if (errors.length) return;
|
|
while (running < concurrency && index < tasks.length) {
|
|
const task = tasks[index++];
|
|
running += 1;
|
|
Promise.resolve()
|
|
.then(task)
|
|
.then(() => {
|
|
running -= 1;
|
|
if (errors.length) maybe_done();
|
|
else if (index === tasks.length && running === 0) resolve();
|
|
else schedule();
|
|
})
|
|
.catch((error) => {
|
|
running -= 1;
|
|
errors.push(error);
|
|
maybe_done();
|
|
});
|
|
}
|
|
};
|
|
|
|
if (!tasks.length) {
|
|
resolve();
|
|
return;
|
|
}
|
|
|
|
schedule();
|
|
});
|
|
}
|
|
|
|
async function terminate_build_children({ grace_period_ms = 5000 } = {}) {
|
|
const victims = Array.from(BUILD_CHILDREN);
|
|
BUILD_CHILDREN.clear();
|
|
|
|
if (!victims.length) return;
|
|
|
|
for (const child of victims) {
|
|
try {
|
|
child.kill();
|
|
} catch (error) {
|
|
// no-op
|
|
}
|
|
}
|
|
|
|
await wait_for_children_exit(victims, grace_period_ms);
|
|
|
|
// Anything still alive after the grace period gets SIGKILL so we don't
|
|
// leave orphaned yarn/esbuild processes when the parent exits.
|
|
for (const child of victims) {
|
|
if (child.exitCode !== null || child.signalCode !== null) continue;
|
|
try {
|
|
child.kill("SIGKILL");
|
|
} catch (error) {
|
|
// no-op
|
|
}
|
|
}
|
|
}
|
|
|
|
function wait_for_children_exit(children, timeout_ms) {
|
|
if (!children.length) return Promise.resolve();
|
|
return new Promise((resolve) => {
|
|
let remaining = children.length;
|
|
let settled = false;
|
|
const finish = () => {
|
|
if (settled) return;
|
|
settled = true;
|
|
clearTimeout(timer);
|
|
resolve();
|
|
};
|
|
const done = () => {
|
|
remaining -= 1;
|
|
if (remaining <= 0) finish();
|
|
};
|
|
const timer = setTimeout(finish, timeout_ms);
|
|
timer.unref();
|
|
for (const child of children) {
|
|
if (child.exitCode !== null || child.signalCode !== null) {
|
|
done();
|
|
} else {
|
|
child.once("close", done);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
async function notify_redis({ error, success, changed_files }) {
|
|
// notify redis which in turns tells socketio to publish this to browser
|
|
let subscriber = get_redis_subscriber("redis_queue");
|
|
try {
|
|
await subscriber.connect();
|
|
} catch (e) {
|
|
log_warn("Cannot connect to redis_queue for browser events");
|
|
}
|
|
|
|
let payload = null;
|
|
if (error) {
|
|
let formatted = await esbuild.formatMessages(error.errors, {
|
|
kind: "error",
|
|
terminalWidth: 100,
|
|
});
|
|
let stack = error.stack.replace(new RegExp(bench_path, "g"), "");
|
|
payload = {
|
|
error,
|
|
formatted,
|
|
stack,
|
|
};
|
|
}
|
|
if (success) {
|
|
payload = {
|
|
success: true,
|
|
changed_files,
|
|
live_reload: argv["live-reload"],
|
|
};
|
|
}
|
|
|
|
await subscriber.publish(
|
|
"events",
|
|
JSON.stringify({
|
|
event: "build_event",
|
|
message: payload,
|
|
})
|
|
);
|
|
}
|
|
|
|
async function open_in_editor() {
|
|
let subscriber = get_redis_subscriber("redis_queue");
|
|
try {
|
|
await subscriber.connect();
|
|
} catch (e) {
|
|
log_warn("Cannot connect to redis_queue for open_in_editor events");
|
|
}
|
|
subscriber.subscribe("open_in_editor", (file) => {
|
|
file = JSON.parse(file);
|
|
let file_path = path.resolve(file.file);
|
|
log("Opening file in editor:", file_path);
|
|
let launch = require("launch-editor");
|
|
launch(`${file_path}:${file.line}:${file.column}`);
|
|
});
|
|
}
|
|
|
|
function get_rebuilt_assets(prev_assets, new_assets) {
|
|
let added_files = [];
|
|
let old_files = Object.values(prev_assets);
|
|
let new_files = Object.values(new_assets);
|
|
|
|
for (let filepath of new_files) {
|
|
if (!old_files.includes(filepath)) {
|
|
added_files.push(filepath);
|
|
}
|
|
}
|
|
return added_files;
|
|
}
|