@comity-dev/build 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Filippo Bovo and contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * comity-build — Comity package build tool.
4
+ *
5
+ * Usage:
6
+ * comity-build [options]
7
+ *
8
+ * Options:
9
+ * --help, -h Show this help
10
+ * --watch, -w Watch mode
11
+ *
12
+ * The tool reads the current working directory's tsconfig.json and builds
13
+ * ESM, CJS, and type declaration outputs.
14
+ */
15
+ export {};
@@ -0,0 +1,257 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * comity-build — Comity package build tool.
4
+ *
5
+ * Usage:
6
+ * comity-build [options]
7
+ *
8
+ * Options:
9
+ * --help, -h Show this help
10
+ * --watch, -w Watch mode
11
+ *
12
+ * The tool reads the current working directory's tsconfig.json and builds
13
+ * ESM, CJS, and type declaration outputs.
14
+ */
15
+ import { spawn } from "node:child_process";
16
+ import { access, rm } from "node:fs/promises";
17
+ import { dirname, join, relative, resolve } from "node:path";
18
+ import { fileURLToPath } from "node:url";
19
+ const __dirname = dirname(fileURLToPath(import.meta.url));
20
+ const ROOT = resolve(__dirname, "..", "..", ".."); // monorepo root (from dist/bin/)
21
+ const CWD = process.cwd(); // package directory
22
+ /**
23
+ * Execute a command as a child process.
24
+ * @param {string} command - The command to execute.
25
+ * @param {string[]} args - The command arguments.
26
+ * @param {object} options - Additional options for spawn.
27
+ * @returns {Promise<void>} - A promise that resolves when the command completes.
28
+ */
29
+ function exec(command, args, options = {}) {
30
+ return new Promise((resolve, reject) => {
31
+ const child = spawn(command, args.filter((a) => a !== ""), {
32
+ stdio: "inherit",
33
+ cwd: options.cwd || CWD,
34
+ ...options,
35
+ });
36
+ child.on("close", (code) => {
37
+ if (code === 0) {
38
+ resolve();
39
+ }
40
+ else {
41
+ reject(new Error(`${command} exited with code ${code}`));
42
+ }
43
+ });
44
+ child.on("error", reject);
45
+ });
46
+ }
47
+ /**
48
+ * Check if a file exists.
49
+ * @param {string} path - The file path.
50
+ * @returns {Promise<boolean>} - A promise that resolves to true if the file exists, false otherwise.
51
+ */
52
+ async function fileExists(path) {
53
+ try {
54
+ await access(path);
55
+ return true;
56
+ }
57
+ catch {
58
+ return false;
59
+ }
60
+ }
61
+ /**
62
+ * Clean the dist directory.
63
+ */
64
+ async function clean() {
65
+ const distDir = join(CWD, "dist");
66
+ if (await fileExists(distDir)) {
67
+ console.log("Cleaning...");
68
+ await rm(distDir, { recursive: true, force: true });
69
+ }
70
+ }
71
+ /**
72
+ * Build the package in the current working directory.
73
+ * @param {object} options - Build options.
74
+ */
75
+ async function buildPackage(options = {}) {
76
+ const tsconfig = join(CWD, "tsconfig.json");
77
+ if (!(await fileExists(tsconfig))) {
78
+ console.warn("tsconfig.json not found, skipping build.");
79
+ return;
80
+ }
81
+ // Watch mode
82
+ if (options.watch) {
83
+ console.log(`👀 Starting watch mode on ${relative(ROOT, CWD)}...`);
84
+ await typeCheck(tsconfig);
85
+ return watchBuildWithTSC(tsconfig);
86
+ }
87
+ console.log(`🏗️ Building ${relative(ROOT, CWD)}...`);
88
+ // 1. Cleanup
89
+ await clean();
90
+ // 2. Type check
91
+ await typeCheck(tsconfig);
92
+ // 3. ESM + CJS + DTS build
93
+ await buildWithTSC(tsconfig);
94
+ console.log("Build completed\n");
95
+ }
96
+ /**
97
+ * Type check the project using tsc.
98
+ * @param {string} tsconfig - Path to the tsconfig.json file.
99
+ */
100
+ async function typeCheck(tsconfig) {
101
+ console.log("Type checking...");
102
+ await exec("tsc", ["--noEmit", "--project", tsconfig]);
103
+ }
104
+ /**
105
+ * Build the project using tsc with specific options.
106
+ * @param {string} tsconfig - Path to the tsconfig.json file.
107
+ */
108
+ async function buildWithTSC(tsconfig) {
109
+ console.log("Building ESM...");
110
+ await exec("tsc", [
111
+ "--project",
112
+ tsconfig,
113
+ "--outDir",
114
+ join(CWD, "dist", "esm"),
115
+ "--declaration",
116
+ "false",
117
+ "--sourceMap",
118
+ process.env["NODE_ENV"] !== "production" ? "true" : "false",
119
+ ]);
120
+ console.log("Building CJS...");
121
+ await exec("tsc", [
122
+ "--project",
123
+ tsconfig,
124
+ "--outDir",
125
+ join(CWD, "dist", "cjs"),
126
+ "--declaration",
127
+ "false",
128
+ "--sourceMap",
129
+ process.env["NODE_ENV"] !== "production" ? "true" : "false",
130
+ "--module",
131
+ "CommonJS",
132
+ "--moduleResolution",
133
+ "node",
134
+ "--allowSyntheticDefaultImports",
135
+ "true",
136
+ ]);
137
+ console.log("Building types...");
138
+ await exec("tsc", [
139
+ "--project",
140
+ tsconfig,
141
+ "--outDir",
142
+ join(CWD, "dist", "types"),
143
+ "--declaration",
144
+ "true",
145
+ "--emitDeclarationOnly",
146
+ "true",
147
+ "--sourceMap",
148
+ process.env["NODE_ENV"] !== "production" ? "true" : "false",
149
+ ]);
150
+ }
151
+ /**
152
+ * Watch mode build using tsc.
153
+ * @param {string} tsconfig - Path to the tsconfig.json file.
154
+ */
155
+ async function watchBuildWithTSC(tsconfig) {
156
+ const processes = [];
157
+ // Function to start a watch process
158
+ function startWatchProcess(label, args) {
159
+ console.log(`Starting ${label} watch...`);
160
+ const child = spawn("tsc", args.filter((a) => a), {
161
+ stdio: "inherit",
162
+ cwd: CWD,
163
+ });
164
+ processes.push(child);
165
+ return child;
166
+ }
167
+ try {
168
+ startWatchProcess("ESM", [
169
+ "--watch",
170
+ "--preserveWatchOutput",
171
+ "--project",
172
+ tsconfig,
173
+ "--outDir",
174
+ join(CWD, "dist", "esm"),
175
+ "--declaration",
176
+ "false",
177
+ "--sourceMap",
178
+ "true",
179
+ ]);
180
+ startWatchProcess("CJS", [
181
+ "--watch",
182
+ "--preserveWatchOutput",
183
+ "--project",
184
+ tsconfig,
185
+ "--outDir",
186
+ join(CWD, "dist", "cjs"),
187
+ "--declaration",
188
+ "false",
189
+ "--sourceMap",
190
+ "true",
191
+ "--module",
192
+ "CommonJS",
193
+ "--moduleResolution",
194
+ "node",
195
+ "--allowSyntheticDefaultImports",
196
+ "true",
197
+ ]);
198
+ startWatchProcess("Types", [
199
+ "--watch",
200
+ "--preserveWatchOutput",
201
+ "--project",
202
+ tsconfig,
203
+ "--outDir",
204
+ join(CWD, "dist", "types"),
205
+ "--declaration",
206
+ "true",
207
+ "--emitDeclarationOnly",
208
+ "true",
209
+ "--sourceMap",
210
+ "true",
211
+ ]);
212
+ // Handle graceful shutdown on SIGINT
213
+ await new Promise((resolve) => {
214
+ process.on("SIGINT", () => {
215
+ console.log("\nStopping watch processes...");
216
+ processes.forEach((p) => p.kill("SIGINT"));
217
+ resolve();
218
+ });
219
+ });
220
+ }
221
+ catch (error) {
222
+ console.error(error);
223
+ processes.forEach((p) => p.kill());
224
+ throw error;
225
+ }
226
+ }
227
+ // CLI entry point
228
+ async function main() {
229
+ const args = process.argv.slice(2);
230
+ // Flags parsing
231
+ const flags = {
232
+ watch: args.includes("--watch") || args.includes("-w"),
233
+ help: args.includes("--help") || args.includes("-h"),
234
+ };
235
+ if (flags.help) {
236
+ console.log(`comity-build — Comity package build tool
237
+
238
+ Usage:
239
+ comity-build [options]
240
+
241
+ Options:
242
+ --help, -h Show this help
243
+ --watch, -w Watch mode
244
+
245
+ The tool reads the current working directory's tsconfig.json and builds
246
+ ESM, CJS, and type declaration outputs.`);
247
+ process.exit(0);
248
+ }
249
+ try {
250
+ await buildPackage(flags);
251
+ }
252
+ catch (error) {
253
+ console.error(error);
254
+ process.exit(1);
255
+ }
256
+ }
257
+ main();
@@ -0,0 +1,6 @@
1
+ /**
2
+ * @comity-dev/build — Comity build tooling.
3
+ *
4
+ * This package provides the `comity-build` binary for compiling Comity packages.
5
+ * The CLI entry point is at `src/bin/comity-build.ts`.
6
+ */
package/dist/index.js ADDED
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ /**
3
+ * @comity-dev/build — Comity build tooling.
4
+ *
5
+ * This package provides the `comity-build` binary for compiling Comity packages.
6
+ * The CLI entry point is at `src/bin/comity-build.ts`.
7
+ */
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@comity-dev/build",
3
+ "version": "0.1.0",
4
+ "description": "Comity build tooling for package compilation. Development tooling.",
5
+ "type": "module",
6
+ "private": false,
7
+ "license": "MIT",
8
+ "comity": {
9
+ "layer": "dev-tooling"
10
+ },
11
+ "engines": {
12
+ "node": ">=24.0.0"
13
+ },
14
+ "bin": {
15
+ "comity-build": "./dist/bin/comity-build.js"
16
+ },
17
+ "main": "./dist/index.js",
18
+ "types": "./dist/index.d.ts",
19
+ "exports": {
20
+ ".": {
21
+ "types": "./dist/index.d.ts",
22
+ "default": "./dist/index.js"
23
+ }
24
+ },
25
+ "files": [
26
+ "./dist"
27
+ ],
28
+ "dependencies": {
29
+ "typescript": "^5.9.3"
30
+ },
31
+ "devDependencies": {
32
+ "@types/node": "^24.13.3",
33
+ "typescript": "^5.9.3"
34
+ },
35
+ "scripts": {
36
+ "build": "tsc -p tsconfig.json",
37
+ "test": "vitest run"
38
+ }
39
+ }