-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.publish.ts
1140 lines (1064 loc) · 33.8 KB
/
build.publish.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { re } from "@reliverse/relico";
import { build as bunBuild } from "bun";
import { parseJSONC, parseJSON5 } from "confbox";
import { destr } from "destr";
import { execaCommand } from "execa";
import fs from "fs-extra";
import { globby } from "globby";
import mri from "mri";
import path from "pathe";
import {
readPackageJSON,
defineTSConfig,
definePackageJSON,
type PackageJson,
} from "pkg-types";
import semver from "semver";
import { fileURLToPath } from "url";
import {
pubConfig,
getBunSourcemapOption,
type BuildPublishConfig,
} from "./build.config.js";
// ---------- Constants & Global Setup ----------
const CURRENT_DIR = path.dirname(fileURLToPath(import.meta.url));
const DIST_FOLDERS = ["dist-npm", "dist-jsr"];
// ---------- Parse CLI Flags ----------
// Extended CLI flags for bumping, registry selection, verbose output, etc.
const scriptFlags = mri(process.argv.slice(2), {
string: ["bump", "registry"],
boolean: ["verbose", "dryRun", "allowDirty", "jsrSlowTypes"],
alias: {
v: "verbose",
d: "dryRun",
r: "registry",
},
default: {},
});
// Override pubConfig values with CLI flags if provided.
if (scriptFlags["verbose"] !== undefined) {
pubConfig.verbose = scriptFlags["verbose"];
}
if (scriptFlags["dryRun"] !== undefined) {
pubConfig.dryRun = scriptFlags["dryRun"];
}
if (scriptFlags["registry"]) {
// Validate registry value
if (["npm", "jsr", "npm-jsr"].includes(scriptFlags["registry"])) {
pubConfig.registry = scriptFlags["registry"];
} else {
console.warn(
`Warning: Unrecognized registry "${scriptFlags["registry"]}". Using default: ${pubConfig.registry}`,
);
}
}
if (scriptFlags["allowDirty"] !== undefined) {
pubConfig.allowDirty = scriptFlags["allowDirty"];
}
if (scriptFlags["jsrSlowTypes"] !== undefined) {
pubConfig.jsrSlowTypes = scriptFlags["jsrSlowTypes"];
}
// ---------- Logger Utility ----------
const logger = {
info: (msg: string, newLine = false) =>
console.log(`${newLine ? "\n" : ""}📝 ${re.cyanBright(msg)}`),
success: (msg: string, newLine = false) =>
console.log(`${newLine ? "\n" : ""}✅ ${re.greenBright(msg)}`),
warn: (msg: string, newLine = false) =>
console.log(`${newLine ? "\n" : ""}🔔 ${re.yellowBright(msg)}`),
error: (msg: string, err?: unknown, newLine = false) =>
console.error(
`${newLine ? "\n" : ""}❌ ${msg}`,
err instanceof Error ? err.message : err,
),
verbose: (msg: string, newLine = false) => {
if (pubConfig.verbose) {
console.log(`${newLine ? "\n" : ""}🔍 ${re.magentaBright(msg)}`);
}
},
};
// ---------- Dist Folders Existence Check & Cleanup ----------
async function handleDistFoldersRemoving(): Promise<void> {
try {
await Promise.all(
DIST_FOLDERS.map(async (folder) => {
const folderPath = path.resolve(CURRENT_DIR, folder);
if (await fs.pathExists(folderPath)) {
await fs.remove(folderPath);
logger.verbose(`Removed: ${folderPath}`, true);
}
}),
);
logger.success("Distribution folders cleaned up successfully", true);
} catch (error) {
logger.warn(`Failed to remove some dist folders: ${String(error)}`, true);
}
}
async function removeDistFolders(): Promise<boolean> {
const existingFolders: string[] = [];
for (const folder of DIST_FOLDERS) {
const folderPath = path.resolve(CURRENT_DIR, folder);
if (await fs.pathExists(folderPath)) {
existingFolders.push(folder);
}
}
if (existingFolders.length > 0) {
logger.info(
`Found existing distribution folders: ${existingFolders.join(", ")}`,
true,
);
await handleDistFoldersRemoving();
}
return true;
}
// ---------- Delete Specific Files ----------
async function deleteSpecificFiles(outdirBin: string): Promise<void> {
const patterns = [
"**/*.test.js",
"**/*.test.ts",
"**/*.test.d.ts",
"**/*-temp.js",
"**/*-temp.ts",
"**/*-temp.d.ts",
];
const files = await globby(patterns, {
cwd: outdirBin,
absolute: true,
gitignore: true,
});
if (files.length > 0) {
await Promise.all(files.map((file) => fs.remove(file)));
logger.verbose(`Deleted files:\n${files.join("\n")}`, true);
}
}
// ---------- Bump Versions in Files ----------
async function bumpVersions(
oldVersion: string,
newVersion: string,
): Promise<void> {
try {
const codebase = await globby(["**/*.{ts,json,jsonc,json5,reliverse}"], {
ignore: [
"**/node_modules/**",
"**/.git/**",
"**/dist/**",
"**/build/**",
"**/.next/**",
"**/coverage/**",
"**/.cache/**",
"**/tmp/**",
"**/.temp/**",
"**/package-lock.json",
"**/pnpm-lock.yaml",
"**/yarn.lock",
"**/bun.lock",
],
gitignore: true,
});
/**
* Update the version in a given file if it matches the oldVersion.
*/
const updateFile = async (
filePath: string,
content: string,
): Promise<boolean> => {
try {
// Handle JSON-like files
if (/\.(json|jsonc|json5|reliverse)$/.test(filePath)) {
let parsed: { version?: string } | null = null;
if (filePath.endsWith(".json")) {
parsed = destr(content);
} else if (
filePath.endsWith(".jsonc") ||
filePath.endsWith(".reliverse") // TODO: current implementation doesn't work for `.reliverse` (JSONC)
) {
parsed = parseJSONC(content);
} else if (filePath.endsWith(".json5")) {
parsed = parseJSON5(content);
}
if (!parsed || typeof parsed !== "object") {
return false;
}
if (parsed.version === oldVersion) {
const updated = content.replace(
new RegExp(`"version"\\s*:\\s*"${oldVersion}"`, "g"),
`"version": "${newVersion}"`,
);
await fs.writeFile(filePath, updated, "utf8");
logger.verbose(`Updated version in ${filePath}`, true);
return true;
}
}
// Handle TypeScript files
else if (filePath.endsWith(".ts")) {
// Look for version declarations in TypeScript files
const versionRegexes = [
// export const version = "1.0.0"
new RegExp(
`(export\\s+const\\s+version\\s*=\\s*["'])${oldVersion}(["'])`,
"g",
),
// const version = "1.0.0"
new RegExp(
`(const\\s+version\\s*=\\s*["'])${oldVersion}(["'])`,
"g",
),
// version: "1.0.0"
new RegExp(`(version\\s*:\\s*["'])${oldVersion}(["'])`, "g"),
// VERSION = "1.0.0"
new RegExp(`(VERSION\\s*=\\s*["'])${oldVersion}(["'])`, "g"),
// Exported cliVersion
new RegExp(
`(export\\s+const\\s+cliVersion\\s*=\\s*["'])${oldVersion}(["'])`,
"g",
),
new RegExp(
`(const\\s+cliVersion\\s*=\\s*["'])${oldVersion}(["'])`,
"g",
),
];
let updated = content;
let hasChanges = false;
for (const regex of versionRegexes) {
if (regex.test(content)) {
updated = updated.replace(regex, `$1${newVersion}$2`);
hasChanges = true;
}
}
if (hasChanges) {
await fs.writeFile(filePath, updated, "utf8");
logger.verbose(`Updated version in ${filePath}`);
return true;
}
}
return false;
} catch (error) {
logger.warn(
`Failed to process ${filePath}: ${
error instanceof Error ? error.message : String(error)
}`,
true,
);
return false;
}
};
const results = await Promise.all(
codebase.map(async (file) => {
const content = await fs.readFile(file, "utf8");
return updateFile(file, content);
}),
);
const updatedCount = results.filter(Boolean).length;
if (updatedCount > 0) {
logger.success(
`Updated version from ${oldVersion} to ${newVersion} in ${updatedCount} file(s)`,
true,
);
} else {
logger.warn("No files were updated with the new version", true);
}
} catch (error) {
logger.error("Failed to bump versions:", error, true);
throw error;
}
}
// ---------- Auto-Increment Version Based on Config ----------
function autoIncrementVersion(
oldVersion: string,
mode: "autoPatch" | "autoMinor" | "autoMajor",
): string {
if (!semver.valid(oldVersion)) {
throw new Error(`Can't auto-increment invalid version: ${oldVersion}`);
}
const releaseTypeMap = {
autoPatch: "patch",
autoMinor: "minor",
autoMajor: "major",
} as const;
const newVer = semver.inc(oldVersion, releaseTypeMap[mode]);
if (!newVer) {
throw new Error(`semver.inc failed for ${oldVersion} and mode ${mode}`);
}
return newVer;
}
// ---------- Set Bump Disabled Flag in Config ----------
/**
* Update the disableBump flag in the build configuration file.
*/
async function setBumpDisabled(value: boolean): Promise<void> {
// Do not toggle disableBump if pausePublish is active and we're trying to disable bumping.
if (pubConfig.pausePublish && value) {
logger.verbose("Skipping disableBump toggle due to pausePublish", true);
return;
}
const tsConfigPath = path.join(CURRENT_DIR, "build.config.ts");
const jsConfigPath = path.join(CURRENT_DIR, "build.config.js");
const configPath = (await fs.pathExists(tsConfigPath))
? tsConfigPath
: jsConfigPath;
if (!(await fs.pathExists(configPath))) {
logger.warn(
"No build.config.ts or build.config.js found to update disableBump",
true,
);
return;
}
let content = await fs.readFile(configPath, "utf-8");
// Replace the disableBump value (a boolean) using a regex.
content = content.replace(
/disableBump\s*:\s*(true|false)/,
`disableBump: ${value}`,
);
await fs.writeFile(configPath, content, "utf-8");
logger.verbose(`Updated disableBump to ${value} in ${configPath}`, true);
}
// ---------- Bump Handler ----------
/**
* Handles version bumping. If a CLI version is provided (via --bump), it is used;
* otherwise, the version is auto-incremented using the configured bump mode.
* The bump is skipped if disableBump is true or pausePublish is true.
*/
async function bumpHandler(): Promise<void> {
if (pubConfig.disableBump || pubConfig.pausePublish) {
logger.info(
"Skipping version bump because a previous run already bumped the version or config paused it.",
true,
);
return;
}
const cliVersion = scriptFlags["bump"];
const pkgPath = path.resolve("package.json");
if (!(await fs.pathExists(pkgPath))) {
throw new Error("package.json not found");
}
const pkgJson = await readPackageJSON();
if (!pkgJson.version) {
throw new Error("No version field found in package.json");
}
const oldVersion = pkgJson.version;
if (cliVersion) {
if (!semver.valid(cliVersion)) {
throw new Error(`Invalid version format for --bump: "${cliVersion}"`);
}
if (oldVersion !== cliVersion) {
await bumpVersions(oldVersion, cliVersion);
await setBumpDisabled(true);
} else {
logger.info(`Version is already at ${oldVersion}, no bump needed.`, true);
}
} else {
if (!semver.valid(oldVersion)) {
throw new Error(
`Invalid existing version in package.json: ${oldVersion}`,
);
}
logger.info(
`Auto-incrementing version from ${oldVersion} using "${pubConfig.bump}"`,
true,
);
const incremented = autoIncrementVersion(oldVersion, pubConfig.bump);
if (oldVersion !== incremented) {
await bumpVersions(oldVersion, incremented);
await setBumpDisabled(true);
} else {
logger.info(`Version is already at ${oldVersion}, no bump needed.`, true);
}
}
}
// ---------- Build Config Definition ----------
function defineConfig(isJSR: boolean): BuildPublishConfig {
return {
...pubConfig,
isJSR,
lastBuildFor: isJSR ? "jsr" : "npm",
};
}
// ---------- Create Common Package Fields ----------
async function createCommonPackageFields(): Promise<Partial<PackageJson>> {
const originalPkg = await readPackageJSON();
const { name, author, version, license, description, keywords } = originalPkg;
const pkgHomepage = "https://docs.reliverse.org/cli";
const commonFields: Partial<PackageJson> = {
name,
version,
license: license || "MIT",
description,
homepage: pkgHomepage,
dependencies: originalPkg.dependencies || {},
type: "module",
};
if (author) {
Object.assign(commonFields, {
author,
repository: {
type: "git",
url: `git+https://github.com/${author}/${name}.git`,
},
bugs: {
url: `https://github.com/${author}/${name}/issues`,
email: "blefnk@gmail.com",
},
keywords: [...new Set([...(keywords || []), author])],
});
} else if (keywords && keywords.length > 0) {
commonFields.keywords = keywords;
}
return commonFields;
}
function filterDevDependencies(
devDeps: Record<string, string> | undefined,
): Record<string, string> {
if (!devDeps) return {};
return Object.entries(devDeps).reduce<Record<string, string>>(
(acc, [k, v]) => {
if (
!k.toLowerCase().includes("eslint") &&
!k.toLowerCase().includes("prettier")
) {
acc[k] = v;
}
return acc;
},
{},
);
}
// ---------- Create Dist Package.json ----------
async function createPackageJSON(
outdirRoot: string,
isJSR: boolean,
): Promise<void> {
logger.info("Generating distribution package.json, tsconfig.json...", true);
const commonPkg = await createCommonPackageFields();
const originalPkg = await readPackageJSON();
if (isJSR) {
const jsrPkg = definePackageJSON({
...commonPkg,
exports: {
".": "./bin/main.ts",
},
devDependencies: filterDevDependencies(originalPkg.devDependencies),
});
await fs.writeJSON(path.join(outdirRoot, "package.json"), jsrPkg, {
spaces: 2,
});
} else {
const npmPkg = definePackageJSON({
...commonPkg,
main: "./bin/main.js",
module: "./bin/main.js",
exports: {
".": "./bin/main.js",
},
bin: {
reliverse: "bin/main.js",
},
files: ["bin", "package.json", "README.md", "LICENSE"],
publishConfig: {
access: "public",
},
});
await fs.writeJSON(path.join(outdirRoot, "package.json"), npmPkg, {
spaces: 2,
});
}
}
// ---------- Create TSConfig ----------
async function createTSConfig(
outdirRoot: string,
allowImportingTsExtensions: boolean,
): Promise<void> {
const tsConfig = defineTSConfig({
compilerOptions: {
allowImportingTsExtensions,
target: "ES2023",
module: "NodeNext",
moduleResolution: "nodenext",
lib: ["DOM", "DOM.Iterable", "ES2023"],
resolveJsonModule: true,
verbatimModuleSyntax: true,
isolatedModules: true,
noPropertyAccessFromIndexSignature: true,
forceConsistentCasingInFileNames: true,
noFallthroughCasesInSwitch: true,
esModuleInterop: true,
skipLibCheck: true,
jsx: "preserve",
allowJs: true,
strict: true,
noEmit: true,
noImplicitOverride: true,
noImplicitReturns: true,
noUnusedLocals: true,
noUnusedParameters: true,
noUncheckedIndexedAccess: true,
strictNullChecks: true,
noImplicitAny: true,
exactOptionalPropertyTypes: true,
allowUnreachableCode: false,
allowUnusedLabels: false,
},
include: ["./bin/**/*.ts"],
exclude: ["**/node_modules"],
});
await fs.writeJSON(path.join(outdirRoot, "tsconfig.json"), tsConfig, {
spaces: 2,
});
}
// ---------- Copy README, LICENSE, etc. ----------
/**
* Find a file in the current directory using a case-insensitive match.
*/
async function findFileCaseInsensitive(
targetFile: string,
): Promise<string | null> {
const files = await fs.readdir(".");
const found = files.find(
(file) => file.toLowerCase() === targetFile.toLowerCase(),
);
return found || null;
}
async function copyReadmeLicense(outdirRoot: string): Promise<void> {
logger.info("Copying README.md and LICENSE files...", true);
// --- Copy README file (case-insensitive) ---
const readmeFile = await findFileCaseInsensitive("README.md");
if (readmeFile) {
await fs.copy(readmeFile, path.join(outdirRoot, "README.md"));
logger.verbose(`Copied ${readmeFile} as README.md`, true);
} else {
logger.warn("README.md not found", true);
}
// --- Copy LICENSE file ---
// First try to find a file named "LICENSE" (with no extension)
let licenseFile = await findFileCaseInsensitive("LICENSE");
// If not found, fall back to "LICENSE.md"
if (!licenseFile) {
licenseFile = await findFileCaseInsensitive("LICENSE.md");
}
if (licenseFile) {
// Always copy the chosen file as "LICENSE" in the output folder
await fs.copy(licenseFile, path.join(outdirRoot, "LICENSE"));
logger.verbose(`Copied ${licenseFile} as LICENSE`, true);
} else {
logger.warn("No license file found", true);
}
}
// ---------- Convert JS Imports to TS ----------
/**
* For JSR builds, convert .js imports to .ts.
*/
async function convertJsToTsImports(
outdirBin: string,
isJSR: boolean,
): Promise<void> {
const entries = await fs.readdir(outdirBin);
for (const entry of entries) {
const filePath = path.join(outdirBin, entry);
const stat = await fs.stat(filePath);
if (stat.isDirectory()) {
await convertJsToTsImports(filePath, isJSR);
} else if (
stat.isFile() &&
/\.(ts|tsx|js|jsx|mts|cts|mjs|cjs)$/.test(entry)
) {
if (filePath.includes("template/")) continue;
const content = await fs.readFile(filePath, "utf8");
if (isJSR) {
const finalContent = content.replace(
/(from\s*['"])([^'"]+?)\.js(?=['"])/g,
"$1$2.ts",
);
logger.verbose("Converted .js imports to .ts for JSR build", true);
await fs.writeFile(filePath, finalContent, "utf8");
} else {
// For non–JSR builds, skip rewriting if no change is needed.
await fs.writeFile(filePath, content, "utf8");
}
}
}
}
// ---------- Rename TSX Files ----------
/**
* JSR doesn't support TSX files, so rename them to avoid warnings.
* (They can later be renamed back by @reliverse/cli if needed.)
*/
async function renameTsxFiles(dir: string): Promise<void> {
const files = await globby(["**/*.tsx"], {
cwd: dir,
absolute: true,
gitignore: true,
});
await Promise.all(
files.map(async (filePath) => {
const newPath = filePath.replace(/\.tsx$/, "-tsx.txt");
await fs.rename(filePath, newPath);
logger.verbose(`Renamed: ${filePath} -> ${newPath}`, true);
}),
);
}
async function createJsrConfig(outdirRoot: string): Promise<void> {
const originalPkg = await readPackageJSON();
const { author, name, version, license, description } = originalPkg;
const pkgHomepage = "https://docs.reliverse.org/cli";
const jsrConfig = {
name,
author,
version,
license: license || "MIT",
description,
homepage: pkgHomepage,
exports: "./bin/main.ts",
publish: {
exclude: ["!.", "node_modules/**", ".env"],
},
};
await fs.writeJSON(path.join(outdirRoot, "jsr.jsonc"), jsrConfig, {
spaces: 2,
});
logger.verbose("Generated jsr.jsonc file", true);
}
async function copyJsrFiles(outdirRoot: string): Promise<void> {
await fs.writeFile(
path.join(outdirRoot, ".gitignore"),
"node_modules/\n.env\n",
"utf-8",
);
logger.verbose("Generated .gitignore for JSR", true);
await createJsrConfig(outdirRoot);
const jsrFiles = [
".reliverse",
"bun.lock",
"drizzle.config.ts",
"schema.json",
];
await Promise.all(
jsrFiles.map(async (file) => {
if (await fs.pathExists(file)) {
await fs.copy(file, path.join(outdirRoot, file));
logger.verbose(`Copied JSR file: ${file}`, true);
}
}),
);
}
async function getDirectorySize(outdirRoot: string): Promise<number> {
try {
const files = await fs.readdir(outdirRoot);
const sizes = await Promise.all(
files.map(async (file) => {
const fp = path.join(outdirRoot, file);
const stats = await fs.stat(fp);
return stats.isDirectory() ? getDirectorySize(fp) : stats.size;
}),
);
return sizes.reduce((total, s) => total + s, 0);
} catch (error) {
logger.error(
`Failed to calculate directory size for ${outdirRoot}`,
error,
true,
);
return 0;
}
}
function getRelativeImportPath(
sourceFile: string,
subPath: string,
baseDir: string,
prefix = "",
): string {
const targetPath = path.join(baseDir, subPath);
let relativePath = path
.relative(path.dirname(sourceFile), targetPath)
.replace(/\\/g, "/");
if (!relativePath.startsWith(".") && !relativePath.startsWith("/")) {
relativePath = `./${relativePath}`;
}
return prefix ? `${prefix}${relativePath}` : relativePath;
}
function replaceSymbolPaths(
content: string,
sourceFile: string,
baseDir: string,
symbolPrefix = "~/",
): { changed: boolean; newContent: string } {
const escapedSymbol = symbolPrefix.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
const symbolRegex = new RegExp(`(['"])(${escapedSymbol}[^'"]+)\\1`, "g");
let changed = false;
const newContent = content.replace(
symbolRegex,
(_match, quote, matchedSymbol) => {
const subPath = matchedSymbol.slice(symbolPrefix.length);
const relativeImport = getRelativeImportPath(
sourceFile,
subPath,
baseDir,
);
changed = true;
return `${quote}${relativeImport}${quote}`;
},
);
return { changed, newContent };
}
export async function convertSymbolPaths(
outdirBin: string,
isJSR: boolean,
symbolPrefix = "~/",
): Promise<void> {
if (isJSR) {
logger.info("Converting symbol paths in JSR built files...", true);
} else {
logger.info("Converting symbol paths in NPM built files...", true);
}
if (!(await fs.pathExists(outdirBin))) {
logger.warn(
`[convertSymbolPaths] Directory does not exist: ${outdirBin}`,
true,
);
return;
}
const filePatterns = ["**/*.{js,ts,jsx,tsx,mjs,cjs}"];
const files = await globby(filePatterns, {
cwd: outdirBin,
absolute: true,
gitignore: true,
});
if (files.length === 0) {
logger.info(`No matching files found in: ${outdirBin}`, true);
return;
}
await Promise.all(
files.map(async (file) => {
try {
// Check if file exists before processing
if (!(await fs.pathExists(file))) {
logger.verbose(`File does not exist (skipped): ${file}`, true);
return;
}
const content = await fs.readFile(file, "utf8");
if (!content.includes(symbolPrefix)) return;
const { changed, newContent } = replaceSymbolPaths(
content,
file,
outdirBin,
symbolPrefix,
);
if (changed) {
await fs.writeFile(file, newContent, "utf8");
logger.verbose(`Converted symbol paths in: ${file}`, true);
}
} catch (error: any) {
if (error.code === "ENOENT") {
logger.verbose(
`File not found during processing (skipped): ${file}`,
true,
);
return;
}
logger.error(
`Error processing file ${file}: ${error.message || String(error)}`,
true,
);
}
}),
);
}
// ---------- Build JSR Distribution ----------
async function buildJsrDist(): Promise<void> {
const cfg = { ...defineConfig(true), lastBuildFor: "jsr" as const };
const outdirRoot = cfg.jsrDistDir;
const outdirBin = `${outdirRoot}/bin`;
// Remove any existing JSR dist folder and ensure it exists
await fs.remove(outdirRoot);
await fs.ensureDir(outdirRoot);
logger.info("Creating JSR distribution...", true);
if (cfg.builderJsr === "jsr") {
// Copy the src directory into dist-jsr
await fs.copy(cfg.rootSrcDir, outdirBin);
logger.verbose("Copied source directory to JSR distribution folder", true);
} else if (cfg.builderJsr === "bun") {
const entryFile = path.join(cfg.rootSrcDir, "main.ts");
if (!(await fs.pathExists(entryFile))) {
logger.error(`Could not find entry file at: ${entryFile}`, true);
throw new Error(`Entry file not found: ${entryFile}`);
}
try {
const buildResult = await bunBuild({
entrypoints: [entryFile],
outdir: outdirBin,
target: cfg.target,
format: cfg.format,
splitting: cfg.splitting,
minify: cfg.shouldMinify,
// Convert sourcemap options for Bun:
sourcemap: getBunSourcemapOption(cfg.sourcemap),
throw: true,
naming: {
entry: "[dir]/[name]-[hash].[ext]",
chunk: "[name]-[hash].[ext]",
asset: "[name]-[hash].[ext]",
},
publicPath: pubConfig.publicPath || "/",
define: {
"process.env.NODE_ENV": JSON.stringify(
process.env.NODE_ENV || "production",
),
},
banner: "/* Bundled by @reliverse/relidler */",
footer: "/* End of bundle */",
drop: ["debugger"],
});
logger.verbose(
`Bun build completed with ${buildResult.outputs.length} output file(s).`,
true,
);
if (buildResult.logs && buildResult.logs.length > 0) {
buildResult.logs.forEach((log, index) => {
logger.verbose(`Log ${index + 1}: ${JSON.stringify(log)}`, true);
});
}
} catch (err) {
logger.error("Bun bundler failed:", err, true);
throw err;
}
} else {
// For other bundlers (not "bun" or "jsr"), use unbuild
const entryFile = path.join(cfg.rootSrcDir, "main.ts");
if (!(await fs.pathExists(entryFile))) {
logger.error(`Could not find entry file at: ${entryFile}`, true);
throw new Error(`Entry file not found: ${entryFile}`);
}
await execaCommand("bunx unbuild", { stdio: "inherit" });
const fileCount = await outdirBinFilesCount(outdirBin);
logger.verbose(`unbuild completed with ${fileCount} output file(s).`, true);
}
/**
* For any bundler as a post-build step, do the following:
*/
// `dist-jsr`:
await copyReadmeLicense(outdirRoot);
await createPackageJSON(outdirRoot, true);
await createTSConfig(outdirRoot, true);
await copyJsrFiles(outdirRoot);
// `dist-jsr/bin`:
await renameTsxFiles(outdirBin);
await convertJsToTsImports(outdirBin, true);
await convertSymbolPaths(outdirBin, true);
await deleteSpecificFiles(outdirBin);
const size = await getDirectorySize(outdirRoot);
logger.success(`Successfully created JSR distribution (${size} bytes)`, true);
}
// ---------- Build NPM Distribution ----------
async function buildNpmDist(): Promise<void> {
const cfg = { ...defineConfig(false), lastBuildFor: "npm" as const };
const outdirRoot = cfg.npmDistDir;
const outdirBin = `${outdirRoot}/bin`;
// Remove any existing NPM dist folder and ensure it exists
await fs.remove(outdirRoot);
await fs.ensureDir(outdirRoot);
const entrySrcDir = cfg.rootSrcDir;
logger.info("Creating NPM distribution...", true);
const entryFile = path.join(entrySrcDir, "main.ts");
if (!(await fs.pathExists(entryFile))) {
logger.error(`Could not find entry file at: ${entryFile}`, true);
throw new Error(`Entry file not found: ${entryFile}`);
}
logger.info(
`Starting ${cfg.builderNpm} bundling for entry file: ${entryFile}\n`,
true,
);
// Use Bun only if builderNpm is exactly "bun"
if (cfg.builderNpm !== "bun") {
await execaCommand("bunx unbuild", { stdio: "inherit" });
const fileCount = await outdirBinFilesCount(outdirBin);
logger.verbose(`unbuild completed with ${fileCount} output file(s).`, true);
} else {
try {
const buildResult = await bunBuild({
entrypoints: [entryFile],
outdir: outdirBin,
target: cfg.target,
format: cfg.format,
splitting: cfg.splitting,
minify: cfg.shouldMinify,
// Use the helper for sourcemap conversion:
sourcemap: getBunSourcemapOption(cfg.sourcemap),
throw: true,
naming: {
entry: "[dir]/[name]-[hash].[ext]",
chunk: "[name]-[hash].[ext]",
asset: "[name]-[hash].[ext]",
},
publicPath: pubConfig.publicPath || "/",
define: {
"process.env.NODE_ENV": JSON.stringify(
process.env.NODE_ENV || "production",
),
},
banner: "/* Bundled by @reliverse/relidler */",
footer: "/* End of bundle */",
drop: ["debugger"],
});
logger.verbose(
`Bun build completed with ${buildResult.outputs.length} output file(s).`,
true,
);
if (buildResult.logs && buildResult.logs.length > 0) {
buildResult.logs.forEach((log, index) => {
logger.verbose(`Log ${index + 1}: ${JSON.stringify(log)}`, true);
});
}
} catch (err) {
logger.error("Bun bundler failed:", err, true);
throw err;
}
}
/**
* For any bundler as a post-build step, do the following:
*/
// `dist-npm`:
await copyReadmeLicense(outdirRoot);
await createPackageJSON(outdirRoot, false);
// `dist-npm/bin`:
await convertSymbolPaths(outdirBin, false);
await deleteSpecificFiles(outdirBin);
const size = await getDirectorySize(outdirRoot);
logger.success(`Successfully created NPM distribution (${size} bytes)`, true);
}
// ---------- Publish Functions ----------
async function publishToJsr(dryRun: boolean): Promise<void> {
logger.info("Publishing to JSR...", true);
try {
if (!pubConfig.pausePublish) {
const originalDir = process.cwd();
const jsrDistDir = path.resolve(CURRENT_DIR, "dist-jsr");
try {