๐Ÿ“ฆ colinhacks / zshy

๐Ÿ“„ main.ts ยท 1165 lines
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165import * as fs from "node:fs";
import * as path from "node:path";
import parseArgs from "arg";
import glob from "fast-glob";
import { table } from "table";
import * as ts from "typescript";
import { type BuildContext, compileProject } from "./compile.js";
import {
  detectConfigIndentation,
  findConfigPath,
  formatForLog,
  isSourceFile,
  isTestFile,
  log,
  readTsconfig,
  relativePosix,
  removeExtension,
  setSilent,
  toPosix,
} from "./utils.js";

interface RawConfig {
  exports?: Record<string, string>;
  bin?: Record<string, string> | string | null;
  cjs?: boolean | null;
  conditions?: Record<string, "esm" | "cjs" | "src">;
  tsconfig?: string; // optional path to tsconfig.json file
  noEdit?: boolean;
}

interface NormalizedConfig {
  exports: Record<string, string>;
  bin: Record<string, string> | string | null;
  conditions: Record<string, "esm" | "cjs" | "src">;
  cjs: boolean;
  tsconfig: string;
  noEdit: boolean;
}

export async function main(): Promise<void> {
  log.prefix = "ยป  ";

  ///////////////////////////////////
  ///    parse command line args  ///
  ///////////////////////////////////

  // Parse command line arguments using arg library
  let args;
  try {
    args = parseArgs({
      "--help": Boolean,
      "--verbose": Boolean,
      "--silent": Boolean,
      "--project": String,
      "--dry-run": Boolean,
      "--fail-threshold": String,
      // "--attw": Boolean,

      // Aliases
      "-h": "--help",
      "-p": "--project",
    });
  } catch (error) {
    if (error instanceof Error) {
      log.error(`โŒ ${error.message}`);
    }
    console.error(`Use --help for usage information`);
    process.exit(1);
  }

  // Handle help flag
  if (args["--help"]) {
    console.log(`
Usage: zshy [options]

Options:
  -h, --help                        Show this help message
  -p, --project <path>              Path to tsconfig.json file
      --verbose                     Enable verbose output
      --silent                      Suppress all output
      --dry-run                     Don't write any files, just log what would be done
      --fail-threshold <threshold>  When to exit with non-zero error code
                                      "error" (default)
                                      "warn"
                                      "never"

Examples:
  zshy                                    # Run build
  zshy --project ./tsconfig.build.json    # Use specific tsconfig file (defaults to tsconfig.json)
  zshy --verbose                          # Enable verbose logging
  zshy --silent                           # Suppress all output
  zshy --dry-run                          # Preview changes without writing files
		`);
    process.exit(0);
  }

  const userAgent = process.env.npm_config_user_agent;
  let pmExec: string;

  if (userAgent?.startsWith("pnpm")) {
    pmExec = "pnpm exec";
  } else if (userAgent?.startsWith("yarn")) {
    pmExec = "yarn exec";
  } else {
    pmExec = "npx";
  }

  // Validate that --verbose and --silent are not both passed
  if (args["--verbose"] && args["--silent"]) {
    log.error("โŒ Cannot use both --verbose and --silent flags together");
    process.exit(1);
  }

  // Set silent mode if --silent flag is passed
  const isSilent = !!args["--silent"];
  const isVerbose = !!args["--verbose"];
  setSilent(isSilent);

  if (!isSilent) {
    const originalPrefix = log.prefix;
    log.prefix = undefined;
    log.info(`   โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—`);
    log.info(`   โ•‘ zshy ยป the bundler-free TypeScript build tool โ•‘`);
    log.info(`   โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•`);
    log.prefix = originalPrefix;
    log.info("Starting build...");
  }

  const isDryRun = !!args["--dry-run"];
  const failThreshold = args["--fail-threshold"] || "error"; // Default to 'error'

  const isCjsInterop = true; // Enable CJS interop for testing

  // Validate that the threshold value is one of the allowed values
  if (failThreshold !== "never" && failThreshold !== "warn" && failThreshold !== "error") {
    log.error(
      `โŒ Invalid value for --fail-threshold: "${failThreshold}". Valid values are "never", "warn", or "error"`
    );
    process.exit(1);
  }

  const isAttw = false; // args["--attw"];

  if (isVerbose) {
    log.info("Verbose mode enabled");
    log.info(`Detected package manager: ${pmExec}`);
  }

  if (isDryRun) {
    log.info("Dry run mode enabled. No files will be written or modified.");
  }

  // Display message about fail threshold setting
  if (isVerbose) {
    if (failThreshold === "never") {
      log.info("Build will always succeed regardless of errors or warnings");
    } else if (failThreshold === "warn") {
      log.warn("Build will fail on warnings or errors");
    } else {
      log.info("Build will fail only on errors (default)");
    }
  }

  ///////////////////////////////////
  ///    find and read pkg json   ///
  ///////////////////////////////////

  // Find package.json by scanning up the file system
  const packageJsonPath = findConfigPath("package.json");

  if (!packageJsonPath) {
    log.error("โŒ package.json not found in current directory or any parent directories");
    process.exit(1);
  }

  // read package.json and extract the "zshy" exports config
  const pkgJsonRaw = fs.readFileSync(packageJsonPath, "utf-8");
  // console.log("๐Ÿ“ฆ Extracting entry points from package.json exports...");
  const pkgJson = JSON.parse(pkgJsonRaw);

  // Detect indentation from package.json to preserve it.
  const pkgJsonIndent = detectConfigIndentation(pkgJsonRaw);

  const pkgJsonDir = path.dirname(packageJsonPath);
  const pkgJsonRelPath = relativePosix(pkgJsonDir, packageJsonPath);

  // print project root
  if (!isSilent) {
    log.info(`Detected project root: ${pkgJsonDir}`);
    log.info(`Reading package.json from ./${pkgJsonRelPath}`);
  }

  /////////////////////////////////
  ///    parse zshy config      ///
  /////////////////////////////////

  // const pkgJson = JSON.parse(fs.readFileSync("./package.json", "utf-8"));
  const CONFIG_KEY = "zshy";

  let rawConfig!: RawConfig;

  if (!pkgJson[CONFIG_KEY]) {
    log.error(`โŒ No "${CONFIG_KEY}" key found in package.json`);
    process.exit(1);
  }

  if (typeof pkgJson[CONFIG_KEY] === "string") {
    rawConfig = {
      exports: { ".": pkgJson[CONFIG_KEY] },
    };
  } else if (typeof pkgJson[CONFIG_KEY] === "object") {
    rawConfig = { ...pkgJson[CONFIG_KEY] };

    if (typeof rawConfig.exports === "string") {
      rawConfig.exports = { ".": rawConfig.exports };
    } else if (typeof rawConfig.exports === "undefined") {
      // exports is optional if bin is specified
      if (!rawConfig.bin) {
        log.error(`โŒ Missing "exports" key in package.json#/${CONFIG_KEY}`);
        process.exit(1);
      }
    } else if (typeof rawConfig.exports !== "object") {
      log.error(`โŒ Invalid "exports" key in package.json#/${CONFIG_KEY}`);
      process.exit(1);
    }

    // Validate bin field if present
    if (rawConfig.bin !== undefined) {
      if (typeof rawConfig.bin === "string") {
        // Keep string format - we'll handle this in entry point extraction
      } else if (typeof rawConfig.bin === "object" && rawConfig.bin !== null) {
        // Object format is valid
      } else {
        log.error(`โŒ Invalid "bin" key in package.json#/${CONFIG_KEY}, expected string or object`);
        process.exit(1);
      }
    }

    // Validate conditions field if present
    if (rawConfig.conditions !== undefined) {
      if (typeof rawConfig.conditions === "object" && rawConfig.conditions !== null) {
        // const { import: importCondition, require: requireCondition, ...rest } = config.conditions;
        for (const [condition, value] of Object.entries(rawConfig.conditions)) {
          if (value !== "esm" && value !== "cjs" && value !== "src") {
            log.error(
              `โŒ Invalid condition value "${value}" for "${condition}" in package.json#/${CONFIG_KEY}/conditions. Valid values are "esm", "cjs", "src", or null`
            );
            process.exit(1);
          }
        }
      } else {
        log.error(`โŒ Invalid "conditions" key in package.json#/${CONFIG_KEY}, expected object`);
        process.exit(1);
      }
    }
  } else if (typeof pkgJson[CONFIG_KEY] === "undefined") {
    log.error(`โŒ Missing "${CONFIG_KEY}" key in package.json`);
    process.exit(1);
  } else {
    log.error(`โŒ Invalid "${CONFIG_KEY}" key in package.json, expected string or object`);
    process.exit(1);
  }

  if (isVerbose) {
    log.info(`Parsed zshy config: ${formatForLog(rawConfig)}`);
  }

  // Check for deprecated sourceDialects
  if ("sourceDialects" in rawConfig) {
    log.error(
      'โŒ The "sourceDialects" option is no longer supported. Use "conditions" instead to configure custom export conditions.'
    );
    process.exit(1);
  }

  const config = { ...rawConfig } as NormalizedConfig;

  // Normalize boolean options
  config.noEdit ??= false;

  // Normalize cjs property
  if (config.cjs === undefined) {
    config.cjs = true; // Default to true if not specified
  }

  // Validate that if cjs is disabled, no conditions are set to "cjs"
  if (config.cjs === false && config.conditions) {
    const cjsConditions = Object.entries(config.conditions).filter(([_, value]) => value === "cjs");
    if (cjsConditions.length > 0) {
      const conditionNames = cjsConditions.map(([name]) => name).join(", ");
      log.error(
        `โŒ CJS is disabled (cjs: false) but the following conditions are set to "cjs": ${conditionNames}. Either enable CJS or change these conditions.`
      );
      process.exit(1);
    }
  }

  // Validate that if cjs is disabled, package.json type must be "module"
  if (config.cjs === false && pkgJson.type !== "module") {
    log.error(
      `โŒ CJS is disabled (cjs: false) but package.json#/type is not set to "module". When disabling CommonJS builds, you must set "type": "module" in your package.json.`
    );
    process.exit(1);
  }

  ///////////////////////////
  ///    read tsconfig    ///
  ///////////////////////////

  // Determine tsconfig.json path
  let tsconfigPath: string;
  if (args["--project"]) {
    // CLI flag takes priority
    const resolvedProjectPath = path.resolve(process.cwd(), args["--project"]);

    if (fs.existsSync(resolvedProjectPath)) {
      if (fs.statSync(resolvedProjectPath).isDirectory()) {
        log.error(`--project must point to a tsconfig.json file, not a directory: ${resolvedProjectPath}`);
        process.exit(1);
      } else {
        // Use the file directly
        tsconfigPath = resolvedProjectPath;
      }
    } else {
      log.error(`tsconfig.json file not found: ${resolvedProjectPath}`);
      process.exit(1);
    }
  } else if (config.tsconfig) {
    // Fallback to package.json config
    const resolvedProjectPath = path.resolve(pkgJsonDir, config.tsconfig);

    if (fs.existsSync(resolvedProjectPath)) {
      if (fs.statSync(resolvedProjectPath).isDirectory()) {
        log.error(`zshy.tsconfig must point to a tsconfig.json file, not a directory: ${resolvedProjectPath}`);
        process.exit(1);
      } else {
        // Use the file directly
        tsconfigPath = resolvedProjectPath;
      }
    } else {
      log.error(`Tsconfig file not found: ${resolvedProjectPath}`);
      process.exit(1);
    }
  } else {
    // Default to tsconfig.json in the package.json directory
    tsconfigPath = path.join(pkgJsonDir, "tsconfig.json");
  }

  const _parsedConfig = readTsconfig(tsconfigPath);
  if (!fs.existsSync(tsconfigPath)) {
    // Check if tsconfig.json exists
    log.error(`โŒ tsconfig.json not found at ${toPosix(path.resolve(tsconfigPath))}`);
    process.exit(1);
  }
  if (!isSilent) {
    log.info(`Reading tsconfig from ./${relativePosix(pkgJsonDir, tsconfigPath)}`);
  }

  // if (_parsedConfig.rootDir) {
  // 	console.error(
  // 		`โŒ rootDir is determined from your set of entrypoints; you can remove it from your tsconfig.json.`,
  // 	);
  // 	process.exit(1);
  // }

  // if (_parsedConfig.declarationDir) {
  // 	console.error(
  // 		`โŒ declarationDir is not supported in zshy; you should remove it from your tsconfig.json.`,
  // 	);
  // 	process.exit(1);
  // }

  // set/override compiler options
  delete _parsedConfig.customConditions; //  can't be set for CommonJS builds

  const outDir = path.resolve(pkgJsonDir, _parsedConfig?.outDir || "./dist");
  const relOutDir = relativePosix(pkgJsonDir, outDir);
  const declarationDir = path.resolve(pkgJsonDir, _parsedConfig?.declarationDir || relOutDir);
  const relDeclarationDir = relativePosix(pkgJsonDir, declarationDir);

  const tsconfigJson: ts.CompilerOptions = {
    ..._parsedConfig,
    outDir,
    target: _parsedConfig.target ?? ts.ScriptTarget.ES2020, // ensure compatible target for CommonJS
    skipLibCheck: true, // skip library checks to reduce errors
    declaration: true,
    esModuleInterop: true,
    noEmit: false,
    emitDeclarationOnly: false,
    rewriteRelativeImportExtensions: true,
    verbatimModuleSyntax: false,
    composite: false,
  };

  if (relOutDir === "") {
    if (!pkgJson.files) {
      log.info(
        'You\'re building your code to the project root. This means your compiled files will be generated alongside your source files.\n   โžœ Setting "files" in package.json to exclude TypeScript source from the published package.'
      );
      pkgJson.files = ["**/*.js", "**/*.mjs", "**/*.cjs", "**/*.d.ts", "**/*.d.mts", "**/*.d.cts"];
    } else {
      log.info(
        `You\'re building your code to the project root. This means your compiled files will be generated alongside your source files.
   Ensure that your "files" in package.json does not include TypeScript source files, or your users may experience .d.ts resolution issues in some environments:
     "files": ["**/*.js", "**/*.mjs", "**/*.cjs", "**/*.d.ts", "**/*.d.mts", "**/*.d.cts"]`
      );
    }
  } else if (!pkgJson.files) {
    log.warn(`The "files" key is missing in package.json. Setting to "${relOutDir}".`);
    pkgJson.files = [relOutDir];
    if (relOutDir !== relDeclarationDir) {
      pkgJson.files.push(relDeclarationDir);
    }
  }

  /////////////////////////////////
  ///   extract entry points    ///
  /////////////////////////////////

  // Extract entry points from zshy exports config
  if (!isSilent) {
    log.info("Determining entrypoints...");
  }
  const entryPoints: string[] = [];
  const assetEntrypoints: Array<{ exportPath: string; sourcePath: string }> = [];

  const rows: string[][] = [["Subpath", "Entrypoint"]];

  for (const [exportPath, sourcePath] of Object.entries(config.exports ?? {})) {
    if (exportPath.includes("package.json")) continue;
    let cleanExportPath!: string;
    if (exportPath === ".") {
      cleanExportPath = pkgJson.name;
    } else if (exportPath.startsWith("./")) {
      cleanExportPath = pkgJson.name + "/" + exportPath.slice(2);
    } else {
      log.warn(`Invalid subpath export "${exportPath}" โ€” should start with "./"`);
      process.exit(1);
    }
    if (typeof sourcePath === "string") {
      if (sourcePath.includes("*")) {
        if (!sourcePath.endsWith("/*") && !sourcePath.endsWith("/**/*")) {
          log.error(`โŒ Wildcard paths should end with /* or /**/* (for deep globs): ${sourcePath}`);
          process.exit(1);
        }

        let pattern: string;

        if (sourcePath.endsWith("/**/*")) {
          // Handle deep glob patterns like "./src/**/*"
          pattern = sourcePath.slice(0, -5) + "/**/*.{ts,tsx,mts,cts}";
        } else {
          // Handle shallow glob patterns like "./src/plugins/*"
          pattern = sourcePath.slice(0, -2) + "/*.{ts,tsx,mts,cts}";
        }

        if (isVerbose) {
          log.info(`Matching glob: ${pattern}`);
        }
        const wildcardFiles = await glob(pattern, {
          ignore: ["**/*.d.ts", "**/*.d.mts", "**/*.d.cts"],
          cwd: pkgJsonDir,
        });
        // Filter out test files (__tests__ directories, .test.*, .spec.*)
        const filteredFiles = wildcardFiles.filter((file) => !isTestFile(file));
        entryPoints.push(...filteredFiles);

        rows.push([`"${cleanExportPath}"`, `${sourcePath} (${filteredFiles.length} matches)`]);
      } else if (isSourceFile(sourcePath)) {
        // Skip test files even if explicitly specified
        if (isTestFile(sourcePath)) {
          log.warn(`Skipping test file: ${sourcePath}`);
          continue;
        }
        entryPoints.push(sourcePath);

        rows.push([`"${cleanExportPath}"`, sourcePath]);
      } else {
        // Any non-compilable file should be treated as an asset
        assetEntrypoints.push({ exportPath, sourcePath });
        rows.push([`"${cleanExportPath}"`, `${sourcePath}`]);
      }
    }
  }

  // Extract bin entry points from zshy bin config
  if (config.bin) {
    if (typeof config.bin === "string") {
      // Single bin entry
      if (isSourceFile(config.bin)) {
        if (isTestFile(config.bin)) {
          log.warn(`Skipping test file in bin: ${config.bin}`);
        } else {
          entryPoints.push(config.bin);
          rows.push([`bin:${pkgJson.name}`, config.bin]);
        }
      }
    } else {
      // Multiple bin entries
      for (const [binName, sourcePath] of Object.entries(config.bin)) {
        if (typeof sourcePath === "string" && isSourceFile(sourcePath)) {
          if (isTestFile(sourcePath)) {
            log.warn(`Skipping test file in bin: ${sourcePath}`);
          } else {
            entryPoints.push(sourcePath);
            rows.push([`bin:${binName}`, sourcePath]);
          }
        }
      }
    }
  }

  if (!isSilent) {
    const originalPrefix = log.prefix;
    log.prefix = undefined;
    log.info(
      "   " +
        table(rows, {
          drawHorizontalLine: (lineIndex, rowCount) => {
            return (
              lineIndex === 0 ||
              lineIndex === 1 ||
              // lineIndex === rowCount - 1 ||
              lineIndex === rowCount
            );
          },
        })
          .split("\n")
          .join("\n   ")
          .trim()
    );
    log.prefix = originalPrefix;
  }

  // disallow .mts and .cts files
  // if (entryPoints.some((ep) => ep.endsWith(".mts") || ep.endsWith(".cts"))) {
  //   emojiLog(
  //     "โŒ",
  //     "Source files with .mts or .cts extensions are not supported. Please use regular .ts files.",
  //     "error"
  //   );
  //   process.exit(1);
  // }
  if (entryPoints.length === 0) {
    log.error("โŒ No entry points found matching the specified patterns in package.json#/zshy exports or bin");
    process.exit(1);
  }

  ///////////////////////////////
  ///   compute root dir      ///
  ///////////////////////////////

  // Compute common ancestor directory for all entry points
  let rootDir: string;
  if (tsconfigJson.rootDir) {
    rootDir = path.resolve(tsconfigPath, tsconfigJson.rootDir);
  } else {
    // compute rootDir from entrypoints
    rootDir =
      entryPoints.length > 0
        ? entryPoints.reduce(
            (common, entryPoint) => {
              const entryDir = path.dirname(path.resolve(entryPoint));
              const commonDir = path.resolve(common);

              // Find the longest common path
              const entryParts = entryDir.split(path.sep);
              const commonParts = commonDir.split(path.sep);

              let i = 0;
              while (i < entryParts.length && i < commonParts.length && entryParts[i] === commonParts[i]) {
                i++;
              }

              return commonParts.slice(0, i).join(path.sep) || path.sep;
            },
            path.dirname(path.resolve(entryPoints[0]!))
          )
        : process.cwd();
  }

  const relRootDir = relativePosix(pkgJsonDir, rootDir);

  //////////////////////////////////
  ///   display resolved paths   ///
  //////////////////////////////////
  if (!isSilent) {
    log.info("Resolved build paths:");
  }
  const pathRows: string[][] = [["Location", "Resolved path"]];

  pathRows.push(["rootDir", relRootDir ? `./${relRootDir}` : "."]);
  pathRows.push(["outDir", relOutDir ? `./${relOutDir}` : "."]);

  if (relDeclarationDir !== relOutDir) {
    pathRows.push(["declarationDir", relDeclarationDir ? `./${relDeclarationDir}` : "."]);
  }

  if (!isSilent) {
    const originalPrefix = log.prefix;
    log.prefix = undefined;
    log.info(
      "   " +
        table(pathRows, {
          drawHorizontalLine: (lineIndex, rowCount) => {
            return (
              lineIndex === 0 ||
              lineIndex === 1 ||
              // lineIndex === rowCount - 1 ||
              lineIndex === rowCount
            );
          },
        })
          .split("\n")
          .join("\n   ")
          .trim()
    );
    log.prefix = originalPrefix;
  }

  const isTypeModule = pkgJson.type === "module";
  if (!isSilent) {
    if (isTypeModule) {
      log.info(`Package is an ES module (package.json#/type is "module")`);
    } else {
      log.info(
        `Package is a CommonJS module (${pkgJson.type === "commonjs" ? 'package.json#/type is "commonjs"' : 'package.json#/type not set to "module"'})`
      );
    }
  }

  //////////////////////////////////////////////
  ///   clean up outDir and declarationDir   ///
  //////////////////////////////////////////////
  const prefix = isDryRun ? "[dryrun] " : "";
  if (relRootDir.startsWith(relOutDir)) {
    if (!isSilent) {
      log.info(`${prefix}Skipping cleanup of outDir as it contains source files`);
    }
  } else {
    // source files are in the outDir, so skip cleanup
    // clean up outDir and declarationDir
    if (!isSilent) {
      log.info(`${prefix}Cleaning up outDir...`);
    }
    if (!isDryRun) {
      fs.rmSync(outDir, { recursive: true, force: true });

      // // print success message in verbose mode
      if (isVerbose) {
        if (fs.existsSync(outDir)) {
          log.error(`โŒ Failed to clean up outDir: ${relOutDir}. Directory still exists.`);
        }
      }
    }
  }
  if (relDeclarationDir !== relOutDir) {
    // already done
  } else if (relRootDir.startsWith(relDeclarationDir)) {
    if (!isSilent) {
      log.info(`${prefix}Skipping cleanup of declarationDir as it contains source files`);
    }
  } else {
    if (!isSilent) {
      log.info(`${prefix}Cleaning up declarationDir...`);
    }
    if (!isDryRun) {
      fs.rmSync(declarationDir, { recursive: true, force: true });
      // // print success message in verbose mode
      if (isVerbose) {
        if (fs.existsSync(declarationDir)) {
          log.error(`โŒ Failed to clean up declarationDir: ${relDeclarationDir}. Directory still exists.`);
        }
      }
    }
  }

  ///////////////////////////////
  ///       compile tsc        ///
  ///////////////////////////////

  const uniqueEntryPoints = [...new Set(entryPoints)];
  // try {
  if (isVerbose) {
    log.info(`Resolved entrypoints: ${formatForLog(uniqueEntryPoints)}`);
    log.info(
      `Resolved compilerOptions: ${formatForLog({
        ...tsconfigJson,
        module: ts.ModuleKind[tsconfigJson.module!],
        moduleResolution: ts.ModuleResolutionKind[tsconfigJson.moduleResolution!],
        target: ts.ScriptTarget[tsconfigJson.target!],
      })}`
    );
  }

  // Create a build context to track written files, copied assets, and compilation errors/warnings
  const buildContext: BuildContext = {
    writtenFiles: new Set<string>(),
    copiedAssets: new Set<string>(),
    errorCount: 0,
    warningCount: 0,
  };

  // Check if CJS should be skipped
  const skipCjs = config.cjs === false;

  // CJS
  if (!skipCjs) {
    if (!isSilent) {
      log.info(`Building CJS...${isTypeModule ? ` (rewriting .ts -> .cjs/.d.cts)` : ``}`);
    }
    await compileProject(
      {
        configPath: tsconfigPath,
        ext: isTypeModule ? "cjs" : "js",
        format: "cjs",
        verbose: isVerbose,
        dryRun: isDryRun,
        pkgJsonDir,
        rootDir,
        cjsInterop: isCjsInterop,
        compilerOptions: {
          ...tsconfigJson,
          module: ts.ModuleKind.CommonJS,
          moduleResolution: ts.ModuleResolutionKind.Node10,
          outDir,
        },
      },
      uniqueEntryPoints,
      buildContext
    );
  } else {
    if (!isSilent) {
      log.info("Skipping CJS build (cjs: false)");
    }
  }

  // ESM
  if (!isSilent) {
    log.info(`Building ESM...${isTypeModule ? `` : ` (rewriting .ts -> .mjs/.d.mts)`}`);
  }
  await compileProject(
    {
      configPath: tsconfigPath,
      ext: isTypeModule ? "js" : "mjs",
      format: "esm",
      verbose: isVerbose,
      dryRun: isDryRun,
      pkgJsonDir,
      rootDir,
      cjsInterop: isCjsInterop,
      compilerOptions: {
        ...tsconfigJson,
        module: ts.ModuleKind.ESNext,
        moduleResolution: ts.ModuleResolutionKind.Bundler,
        outDir,
      },
    },
    uniqueEntryPoints,
    buildContext
  );

  ///////////////////////////////////
  ///      copy asset entrypoints  ///
  ///////////////////////////////////

  // Copy asset entrypoints to output directory
  if (assetEntrypoints.length > 0) {
    if (!isSilent) {
      log.info(`${prefix}Copying ${assetEntrypoints.length} asset${assetEntrypoints.length === 1 ? "" : "s"}...`);
    }

    for (const { sourcePath } of assetEntrypoints) {
      const sourceFile = path.resolve(pkgJsonDir, sourcePath);
      const relativePath = path.relative(rootDir, path.resolve(pkgJsonDir, sourcePath));
      const destFile = path.resolve(outDir, relativePath);
      const destDir = path.dirname(destFile);

      if (!fs.existsSync(sourceFile)) {
        log.warn(`Asset not found: ${sourcePath}`);
        continue;
      }

      if (!isDryRun) {
        fs.mkdirSync(destDir, { recursive: true });
        fs.copyFileSync(sourceFile, destFile);
      }

      // Track the copied file
      buildContext.copiedAssets.add(toPosix(path.relative(pkgJsonDir, destFile)));

      if (isVerbose) {
        const relativeSource = toPosix(path.relative(pkgJsonDir, sourceFile));
        const relativeDest = toPosix(path.relative(pkgJsonDir, destFile));
        log.info(`${isDryRun ? "[dryrun] " : ""}Copied asset: ./${relativeSource} โ†’ ./${relativeDest}`);
      }
    }
  }

  ///////////////////////////////////
  ///      display written files  ///
  ///////////////////////////////////

  // Display files that were written or would be written (only in verbose mode)
  if (isVerbose && buildContext.writtenFiles.size > 0) {
    log.info(`${prefix}Writing files (${buildContext.writtenFiles.size} total)...`);

    // Sort files by relative path for consistent display
    const sortedFiles = [...buildContext.writtenFiles]
      .map((file) => relativePosix(pkgJsonDir, file))
      .sort()
      .map((relPath) => (relPath.startsWith(".") ? relPath : `./${relPath}`));

    // Temporarily disable prefix for individual file listings
    const originalPrefix = log.prefix;
    log.prefix = undefined;
    sortedFiles.forEach((file) => {
      log.info(`     ${file}`);
    });
    log.prefix = originalPrefix;
  }

  ///////////////////////////////
  ///   generate exports      ///
  ///////////////////////////////

  // generate package.json exports
  if (config.noEdit) {
    if (!isSilent) {
      log.info("[noedit] Skipping modification of package.json");
    }
  } else {
    // Generate exports based on zshy config
    if (!isSilent) {
      log.info(`${prefix}Updating package.json...`);
    }
    const newExports: Record<string, any> = {};

    if (config.exports) {
      for (const [exportPath, sourcePath] of Object.entries(config.exports)) {
        if (exportPath.includes("package.json")) {
          newExports[exportPath] = sourcePath;
          continue;
        }
        const absSourcePath = path.resolve(pkgJsonDir, sourcePath);
        const relSourcePath = path.relative(rootDir, absSourcePath);
        const absJsPath = path.resolve(outDir, relSourcePath);
        const absDtsPath = path.resolve(declarationDir, relSourcePath);
        let relJsPath = "./" + relativePosix(pkgJsonDir, absJsPath);
        let relDtsPath = "./" + relativePosix(pkgJsonDir, absDtsPath);

        if (typeof sourcePath === "string") {
          if (sourcePath.endsWith("/*") || sourcePath.endsWith("/**/*")) {
            // Handle wildcard exports
            const finalExportPath = exportPath;

            if (finalExportPath.includes("**")) {
              log.error(`โŒ Export keys cannot contain "**": ${finalExportPath}`);
              process.exit(1);
            }

            // Convert deep glob patterns to simple wildcard patterns in the final export
            if (sourcePath.endsWith("/**/*")) {
              // Also convert the output paths from /**/* to /*
              if (relJsPath.endsWith("/**/*")) {
                relJsPath = relJsPath.slice(0, -5) + "/*";
              }
              if (relDtsPath.endsWith("/**/*")) {
                relDtsPath = relDtsPath.slice(0, -5) + "/*";
              }
            }

            // Build exports object with proper condition ordering
            const exportObj: Record<string, string> = {};

            // Add custom conditions first in their original order
            if (config.conditions) {
              for (const [condition, value] of Object.entries(config.conditions)) {
                if (value === "src") {
                  exportObj[condition] = sourcePath;
                } else if (value === "esm") {
                  exportObj[condition] = relJsPath;
                } else if (value === "cjs") {
                  exportObj[condition] = relJsPath;
                }
              }
            }

            // Add standard conditions
            exportObj.types = relDtsPath;
            if (skipCjs) {
              // ESM-only: use default condition instead of import
              exportObj.default = relJsPath;
            } else {
              // Dual CJS/ESM: use import and require
              exportObj.import = relJsPath;
              exportObj.require = relJsPath;
            }

            newExports[finalExportPath] = exportObj;
          } else if (isSourceFile(sourcePath)) {
            const esmPath = removeExtension(relJsPath) + (isTypeModule ? `.js` : `.mjs`);
            const cjsPath = removeExtension(relJsPath) + (isTypeModule ? `.cjs` : `.js`);
            // Use ESM type declarations when CJS is skipped, otherwise use CJS declarations
            const dtsExt = skipCjs ? (isTypeModule ? ".d.ts" : ".d.mts") : isTypeModule ? ".d.cts" : ".d.ts";
            const dtsPath = removeExtension(relDtsPath) + dtsExt;

            // Build exports object with proper condition ordering
            const exportObj: Record<string, string> = {};

            // Add custom conditions first in their original order
            if (config.conditions) {
              for (const [condition, value] of Object.entries(config.conditions)) {
                if (value === "src") {
                  exportObj[condition] = sourcePath;
                } else if (value === "esm") {
                  exportObj[condition] = esmPath;
                } else if (value === "cjs") {
                  exportObj[condition] = cjsPath;
                }
              }
            }

            // Add standard conditions
            exportObj.types = dtsPath;
            if (skipCjs) {
              // ESM-only: use default condition instead of import
              exportObj.default = esmPath;
            } else {
              // Dual CJS/ESM: use import and require
              exportObj.import = esmPath;
              exportObj.require = cjsPath;
            }

            newExports[exportPath] = exportObj;

            if (exportPath === ".") {
              if (!skipCjs) {
                pkgJson.main = cjsPath;
                pkgJson.module = esmPath;
                pkgJson.types = dtsPath;
              } else {
                // Only set module and types, not main
                pkgJson.module = esmPath;
                pkgJson.types = dtsPath;
              }
              if (isVerbose) {
                log.info(`Setting "main": ${formatForLog(cjsPath)}`);
                log.info(`Setting "module": ${formatForLog(esmPath)}`);
                log.info(`Setting "types": ${formatForLog(dtsPath)}`);
              }
            }
          }
        }
      }

      // Handle asset entrypoints (only those that don't already have exports from TypeScript compilation)
      for (const { exportPath, sourcePath } of assetEntrypoints) {
        // Skip if this export path was already handled by TypeScript compilation
        if (newExports[exportPath]) {
          continue;
        }

        const absSourcePath = path.resolve(pkgJsonDir, sourcePath);
        const relSourcePath = path.relative(rootDir, absSourcePath);
        const absAssetPath = path.resolve(outDir, relSourcePath);
        const relAssetPath = "./" + relativePosix(pkgJsonDir, absAssetPath);

        // Assets are not source code - they just get copied and referenced with a simple path
        newExports[exportPath] = relAssetPath;

        // Handle root export special fields (only if no TypeScript root export exists)
        if (exportPath === ".") {
          if (!skipCjs) {
            pkgJson.main = relAssetPath;
            pkgJson.module = relAssetPath;
            pkgJson.types = relAssetPath;
          } else {
            pkgJson.module = relAssetPath;
            pkgJson.types = relAssetPath;
          }
          if (isVerbose) {
            log.info(`Setting "main": ${formatForLog(relAssetPath)}`);
            log.info(`Setting "module": ${formatForLog(relAssetPath)}`);
            log.info(`Setting "types": ${formatForLog(relAssetPath)}`);
          }
        }
      }

      pkgJson.exports = newExports;
      if (isVerbose) {
        log.info(`Setting "exports": ${formatForLog(newExports)}`);
      }
    }

    ///////////////////////////////
    ///      generate bin        ///
    ///////////////////////////////

    // Generate bin field based on zshy bin config
    if (config.bin) {
      const newBin: Record<string, string> = {};

      // Convert config.bin to object format for processing
      const binEntries = typeof config.bin === "string" ? [[pkgJson.name, config.bin]] : Object.entries(config.bin);

      for (const [binName, sourcePath] of binEntries) {
        if (typeof sourcePath === "string" && isSourceFile(sourcePath)) {
          const absSourcePath = path.resolve(pkgJsonDir, sourcePath);
          const relSourcePath = path.relative(rootDir, absSourcePath);
          const absJsPath = path.resolve(outDir, relSourcePath);
          const relJsPath = "./" + relativePosix(pkgJsonDir, absJsPath);

          // Use ESM files for bin when CJS is skipped, otherwise use CJS
          const binExt = skipCjs ? (isTypeModule ? ".js" : ".mjs") : isTypeModule ? ".cjs" : ".js";
          const binPath = removeExtension(relJsPath) + binExt;
          newBin[binName] = binPath;
        }
      }

      // If original config.bin was a string, output as string
      if (typeof config.bin === "string") {
        pkgJson.bin = Object.values(newBin)[0];
      } else {
        // Output as object
        pkgJson.bin = newBin;
      }

      if (isVerbose) {
        log.info(`Setting "bin": ${formatForLog(pkgJson.bin)}`);
      }
    }

    if (isDryRun) {
      ///////////////////////////////
      ///     write pkg json      ///
      ///////////////////////////////
      log.info("[dryrun] Skipping package.json modification");
    } else {
      fs.writeFileSync(packageJsonPath, JSON.stringify(pkgJson, null, pkgJsonIndent) + "\n");
    }
  }

  //////////////////////////////////
  ///     write jsr exports      ///
  //////////////////////////////////

  // Check if jsr.json exists in the project
  const jsrJsonPath = findConfigPath("jsr.json");

  if (jsrJsonPath) {
    if (config.noEdit) {
      if (!isSilent) {
        log.info("[noedit] Skipping modification of jsr.json");
      }
    } else {
      if (!isSilent) {
        log.info(`${prefix}Updating jsr.json...`);
      }

      // read jsr.json
      const jsrJsonRaw = fs.readFileSync(jsrJsonPath, "utf-8");
      const jsrJson = JSON.parse(jsrJsonRaw);

      // Detect indentation from jsr.json to preserve it.
      const jsrJsonIndent = detectConfigIndentation(jsrJsonRaw);

      const jsrJsonDir = path.dirname(jsrJsonPath);
      const jsrJsonRelPath = relativePosix(jsrJsonDir, jsrJsonPath);

      if (!isSilent) {
        log.info(`Reading jsr.json from ./${jsrJsonRelPath}`);
      }

      // Copy exports from zshy config to jsr.json exports
      const jsrExports = config.exports;
      jsrJson.exports = jsrExports;
      if (isVerbose) {
        log.info(`Setting "exports": ${formatForLog(jsrExports)}`);
      }

      // Write jsr json
      if (isDryRun) {
        log.info("[dryrun] Skipping jsr.json modification");
      } else {
        fs.writeFileSync(jsrJsonPath, JSON.stringify(jsrJson, null, jsrJsonIndent) + "\n");
      }
    }
  }

  if (isAttw) {
    // run `@arethetypeswrong/cli --pack .` to check types

    if (!isSilent) {
      log.info("Checking types with @arethetypeswrong/cli...");
    }
    const { execFile } = await import("node:child_process");
    const { promisify } = await import("node:util");

    const execFileAsync = promisify(execFile);
    const [cmd, ...args] = `${pmExec} @arethetypeswrong/cli --pack ${pkgJsonDir} --format table-flipped`.split(" ");
    console.dir([cmd, ...args], { depth: null });

    let stdout = "";
    let stderr = "";
    let exitCode = 0;

    try {
      const result = await execFileAsync(cmd!, args, {
        cwd: pkgJsonDir,
        encoding: "utf-8",
      });
      stdout = result.stdout;
      stderr = result.stderr;
    } catch (error: any) {
      stdout = error.stdout || "";
      stderr = error.stderr || "";
      exitCode = error.code || 1;
    }

    const output = stdout || stderr;
    if (output) {
      const indentedOutput = output
        .split("\n")
        .map((line: string) => `   ${line}`)
        .join("\n");

      if (exitCode === 0) {
        log.info(indentedOutput);
      } else {
        log.error(indentedOutput);
        log.warn("ATTW found issues, but the build was not affected.");
      }
    }
  }

  // Report total compilation results
  if (buildContext.errorCount > 0 || buildContext.warningCount > 0) {
    log.info(
      `Compilation finished with ${buildContext.errorCount} error${buildContext.errorCount === 1 ? "" : "s"} and ${buildContext.warningCount} warning${buildContext.warningCount === 1 ? "" : "s"}`
    );

    // Apply threshold rules for exit code
    if (failThreshold !== "never" && buildContext.errorCount > 0) {
      // Both 'warn' and 'error' thresholds cause failure on errors
      log.error("โŒ Build completed with errors");
      process.exit(1);
    } else if (failThreshold === "warn" && buildContext.warningCount > 0) {
      // Only 'warn' threshold causes failure on warnings
      log.warn(`Build completed with warnings (exiting with error due to --fail-threshold=warn)`);
      process.exit(1);
    } else if (buildContext.errorCount > 0) {
      // If we got here with errors, we're in 'never' mode
      log.warn(`Build completed with errors (continuing due to --fail-threshold=never)`);
    } else {
      // Just warnings and not failing on them
      log.info(`Build complete with warnings`);
    }
  } else {
    if (!isSilent) {
      log.info("Build complete! โœ…");
    }
  }
}