From 304dea1d980598d5ed1e559ee6dd425b184c677b Mon Sep 17 00:00:00 2001 From: Romain Lenzotti Date: Fri, 31 May 2024 10:39:04 +0200 Subject: [PATCH] fix: add monorepo build package command to build a single package --- packages/monorepo/bin/monorepo-build-hybrid.js | 13 +++++++++++++ packages/monorepo/bin/monorepo.js | 1 + packages/monorepo/src/MonoRepo.js | 13 ------------- .../monorepo/src/commands/build/BuildHybridCmd.js | 13 +++++++++++++ packages/monorepo/src/commands/index.js | 1 + packages/monorepo/src/runCommand.js | 6 +++++- packages/monorepo/src/tasks/build.js | 14 +++++++++++++- .../src/utils/packages/transformCjsFileToEsm.js | 2 +- 8 files changed, 47 insertions(+), 16 deletions(-) create mode 100644 packages/monorepo/bin/monorepo-build-hybrid.js create mode 100644 packages/monorepo/src/commands/build/BuildHybridCmd.js diff --git a/packages/monorepo/bin/monorepo-build-hybrid.js b/packages/monorepo/bin/monorepo-build-hybrid.js new file mode 100644 index 0000000..33fc3f2 --- /dev/null +++ b/packages/monorepo/bin/monorepo-build-hybrid.js @@ -0,0 +1,13 @@ +#!/usr/bin/env node +const {program} = require("commander"); + +program + .usage("monorepo build-hybrid [options]") + .option("-v, --verbose", "Enable verbose log", (v, t) => t + 1, 0) + .parse(process.argv); + +(async () => { + const {commands, runCommand} = await import("../src/index.js"); + + runCommand(commands.BuildHybridCmd, program.opts()); +})(); diff --git a/packages/monorepo/bin/monorepo.js b/packages/monorepo/bin/monorepo.js index cfe6fca..b8ca029 100755 --- a/packages/monorepo/bin/monorepo.js +++ b/packages/monorepo/bin/monorepo.js @@ -7,6 +7,7 @@ program .version(cliPkg.version) .command("ci ", "Perform ci actions (configure)") .command("build ", "Build artifacts (workspace, packages)") + .command("build-hybrid ", "Build hybrid esm/cjs package (must be run over a single package)") .command("clean ", "Clean artifacts (workspace, docker)") .command("publish ", "Publish artifacts (packages, examples, ghpages, docker, heroku)") .command("sync ", "Perform synchronisation on given type (repository, packages, examples)") diff --git a/packages/monorepo/src/MonoRepo.js b/packages/monorepo/src/MonoRepo.js index 6b666d5..ef3ff16 100644 --- a/packages/monorepo/src/MonoRepo.js +++ b/packages/monorepo/src/MonoRepo.js @@ -24,7 +24,6 @@ import {cleanTagsDocker} from "./utils/docker/cleanTagsDocker.js"; import {getWorkspaces} from "./utils/workspace/getWorkspaces.js"; import {cleanPackages} from "./utils/packages/cleanPackages.js"; import {yarnBerry} from "./utils/cli/YarnBerry.js"; -import {buildHybridPackage, buildHybridPackages} from "./utils/packages/buildHybridPackages.js"; function getDefaultOptions(rootPkg) { return { @@ -336,8 +335,6 @@ export class MonoRepo { const newCtx = this.fork(options); switch (type) { - case "package": - return buildHybridPackage(process.cwd(), null, newCtx); case "workspaces": case "workspace": return this.buildWorkspace(); @@ -357,16 +354,6 @@ export class MonoRepo { } } - async buildPackage() { - if (this.hasBuild) { - await this.manager.run("build"); - } - - if (this.hasE2E) { - await this.manager.run("test:e2e"); - } - } - async configureWorkspace(options = {}) { return configureWorkspace(this.fork(options)); } diff --git a/packages/monorepo/src/commands/build/BuildHybridCmd.js b/packages/monorepo/src/commands/build/BuildHybridCmd.js new file mode 100644 index 0000000..385ddd5 --- /dev/null +++ b/packages/monorepo/src/commands/build/BuildHybridCmd.js @@ -0,0 +1,13 @@ +import {buildHybridPackage} from "../../utils/packages/buildHybridPackages.js"; + +export class BuildHybridCmd { + getTasks(context) { + return [ + { + task() { + return buildHybridPackage(process.cwd(), null, context); + } + } + ]; + } +} diff --git a/packages/monorepo/src/commands/index.js b/packages/monorepo/src/commands/index.js index a1e453d..71e3678 100644 --- a/packages/monorepo/src/commands/index.js +++ b/packages/monorepo/src/commands/index.js @@ -1,5 +1,6 @@ export * from "./clean/CleanCmd.js"; export * from "./build/BuildCmd.js"; +export * from "./build/BuildHybridCmd.js"; export * from "./version/VersionCmd.js"; export * from "./publish/PublishCmd.js"; export * from "./sync/SyncCmd.js"; diff --git a/packages/monorepo/src/runCommand.js b/packages/monorepo/src/runCommand.js index cff1ba6..67ff944 100644 --- a/packages/monorepo/src/runCommand.js +++ b/packages/monorepo/src/runCommand.js @@ -8,7 +8,11 @@ async function importConfig() { try { return await import(`${process.cwd()}/release.config.mjs`); } catch (er) { - return await import(`${process.cwd()}/release.config.js`); + try { + return await import(`${process.cwd()}/release.config.js`); + } catch (er) { + return {}; + } } } diff --git a/packages/monorepo/src/tasks/build.js b/packages/monorepo/src/tasks/build.js index 13cb0fe..2c02667 100644 --- a/packages/monorepo/src/tasks/build.js +++ b/packages/monorepo/src/tasks/build.js @@ -3,31 +3,43 @@ import {copyPackages} from "../utils/packages/copyPackages.js"; import {writePackages} from "../utils/packages/writePackages.js"; import {syncDependencies} from "../utils/depencencies/syncDependencies.js"; import {clean} from "../utils/common/clean.js"; -import {buildHybridPackages} from "../utils/packages/buildHybridPackages.js"; +import {buildHybridPackage, buildHybridPackages} from "../utils/packages/buildHybridPackages.js"; export function build(context) { return [ { + when: () => context.type === "package", + task() { + return buildHybridPackage(process.cwd(), null, newCtx); + } + }, + { + when: () => context.type !== "package", title: "Clean workspace", task: () => clean([context.outputDir]) }, { + when: () => context.type !== "package", title: "Compile packages", task: () => compilePackages(context) }, { + when: () => context.type !== "package", title: "Sync dependencies", task: () => syncDependencies(context) }, { + when: () => context.type !== "package", title: "Copy packages", task: () => copyPackages(context) }, { + when: () => context.type !== "package", title: "Write package.json", task: () => writePackages(context) }, { + when: () => context.type !== "package", title: "Build hybrid CommonJS/ESM modules", task: () => buildHybridPackages(context) } diff --git a/packages/monorepo/src/utils/packages/transformCjsFileToEsm.js b/packages/monorepo/src/utils/packages/transformCjsFileToEsm.js index 4efcf09..fc507f4 100644 --- a/packages/monorepo/src/utils/packages/transformCjsFileToEsm.js +++ b/packages/monorepo/src/utils/packages/transformCjsFileToEsm.js @@ -97,7 +97,7 @@ export async function transformCjsFileToEsm(dir, context) { .flatMap((t) => transform(t)) .map(async ({code, file}) => { try { - await writeFile(context?.out ? file + ".mjs" : "", code, {encoding: "utf8"}); + await writeFile(context?.out ? file + ".mjs" : file, code, {encoding: "utf8"}); } catch (er) { !context.silent && logger.warn(er); }