@stryke/capnp 0.6.3 → 0.7.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/bin/capnpc.cjs CHANGED
@@ -1,5 +1,4 @@
1
1
  #!/usr/bin/env node
2
- "use strict";
3
2
  var __create = Object.create;
4
3
  var __defProp = Object.defineProperty;
5
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
@@ -38,6 +37,30 @@ module.exports = __toCommonJS(capnpc_exports);
38
37
  var import_console = require("@storm-software/config-tools/logger/console");
39
38
  var import_utilities = require("@storm-software/config-tools/utilities");
40
39
 
40
+ // ../path/src/exists.ts
41
+ var import_node_fs = require("fs");
42
+ var import_promises = require("fs/promises");
43
+ var existsSync = /* @__PURE__ */ __name((filePath) => {
44
+ return (0, import_node_fs.existsSync)(filePath);
45
+ }, "existsSync");
46
+ var exists = /* @__PURE__ */ __name(async (filePath) => {
47
+ return (0, import_promises.access)(filePath, import_promises.constants.F_OK).then(() => true).catch(() => false);
48
+ }, "exists");
49
+
50
+ // ../fs/src/helpers.ts
51
+ var import_nanotar = require("nanotar");
52
+ var import_node_fs2 = require("fs");
53
+ var import_promises2 = require("fs/promises");
54
+ async function createDirectory(path) {
55
+ if (await exists(path)) {
56
+ return;
57
+ }
58
+ return (0, import_promises2.mkdir)(path, {
59
+ recursive: true
60
+ });
61
+ }
62
+ __name(createDirectory, "createDirectory");
63
+
41
64
  // ../fs/src/list-files.ts
42
65
  var import_defu = __toESM(require("defu"), 1);
43
66
  var import_glob = require("glob");
@@ -626,14 +649,14 @@ var isError = /* @__PURE__ */ __name((obj) => {
626
649
  }, "isError");
627
650
 
628
651
  // ../fs/src/read-file.ts
629
- var import_node_fs = require("fs");
630
- var import_promises = require("fs/promises");
631
- var readFile = /* @__PURE__ */ __name(async (filePath) => {
652
+ var import_node_fs3 = require("fs");
653
+ var import_promises3 = require("fs/promises");
654
+ var readFile2 = /* @__PURE__ */ __name(async (filePath) => {
632
655
  try {
633
656
  if (!filePath) {
634
657
  throw new Error("No file path provided to read data");
635
658
  }
636
- return await (0, import_promises.readFile)(filePath, {
659
+ return await (0, import_promises3.readFile)(filePath, {
637
660
  encoding: "utf8"
638
661
  });
639
662
  } catch {
@@ -641,7 +664,7 @@ var readFile = /* @__PURE__ */ __name(async (filePath) => {
641
664
  }
642
665
  }, "readFile");
643
666
  async function readJsonFile(path, options) {
644
- const content = await readFile(path);
667
+ const content = await readFile2(path);
645
668
  if (options) {
646
669
  options.endsWithNewline = content.codePointAt(content.length - 1) === 10;
647
670
  }
@@ -657,21 +680,11 @@ async function readJsonFile(path, options) {
657
680
  }
658
681
  __name(readJsonFile, "readJsonFile");
659
682
 
660
- // ../path/src/exists.ts
661
- var import_node_fs2 = require("fs");
662
- var import_promises2 = require("fs/promises");
663
- var existsSync2 = /* @__PURE__ */ __name((filePath) => {
664
- return (0, import_node_fs2.existsSync)(filePath);
665
- }, "existsSync");
666
- var exists = /* @__PURE__ */ __name(async (filePath) => {
667
- return (0, import_promises2.access)(filePath, import_promises2.constants.F_OK).then(() => true).catch(() => false);
668
- }, "exists");
669
-
670
683
  // ../path/src/file-path-fns.ts
671
684
  var import_node_path = require("path");
672
685
 
673
686
  // ../path/src/is-file.ts
674
- var import_node_fs3 = require("fs");
687
+ var import_node_fs4 = require("fs");
675
688
 
676
689
  // ../path/src/join-paths.ts
677
690
  var _DRIVE_LETTER_START_RE = /^[A-Z]:\//i;
@@ -802,9 +815,6 @@ __name(normalizeString, "normalizeString");
802
815
 
803
816
  // ../path/src/regex.ts
804
817
  var DRIVE_LETTER_START_REGEX = /^[A-Z]:\//i;
805
- var DRIVE_LETTER_REGEX = /^[A-Z]:$/i;
806
- var UNC_REGEX = /^[/\\]{2}/;
807
- var ABSOLUTE_PATH_REGEX = /^[/\\](?![/\\])|^[/\\]{2}(?!\.)|^~[/\\]|^[A-Z]:[/\\]/i;
808
818
 
809
819
  // ../path/src/slash.ts
810
820
  function slash(path) {
@@ -815,12 +825,6 @@ function slash(path) {
815
825
  }
816
826
  __name(slash, "slash");
817
827
 
818
- // ../path/src/is-file.ts
819
- function isAbsolutePath(path) {
820
- return ABSOLUTE_PATH_REGEX.test(slash(path));
821
- }
822
- __name(isAbsolutePath, "isAbsolutePath");
823
-
824
828
  // ../path/src/correct-path.ts
825
829
  function normalizeWindowsPath2(input = "") {
826
830
  if (!input) {
@@ -829,97 +833,6 @@ function normalizeWindowsPath2(input = "") {
829
833
  return slash(input).replace(DRIVE_LETTER_START_REGEX, (r) => r.toUpperCase());
830
834
  }
831
835
  __name(normalizeWindowsPath2, "normalizeWindowsPath");
832
- function correctPath(path) {
833
- if (!path || path.length === 0) {
834
- return ".";
835
- }
836
- path = normalizeWindowsPath2(path);
837
- const isUNCPath = path.match(UNC_REGEX);
838
- const isPathAbsolute = isAbsolutePath(path);
839
- const trailingSeparator = path.endsWith("/");
840
- path = normalizeString2(path, !isPathAbsolute);
841
- if (path.length === 0) {
842
- if (isPathAbsolute) {
843
- return "/";
844
- }
845
- return trailingSeparator ? "./" : ".";
846
- }
847
- if (trailingSeparator) {
848
- path += "/";
849
- }
850
- if (DRIVE_LETTER_REGEX.test(path)) {
851
- path += "/";
852
- }
853
- if (isUNCPath) {
854
- if (!isPathAbsolute) {
855
- return `//./${path}`;
856
- }
857
- return `//${path}`;
858
- }
859
- return !path.startsWith("/") && isPathAbsolute && !DRIVE_LETTER_REGEX.test(path) ? `/${path}` : path;
860
- }
861
- __name(correctPath, "correctPath");
862
- function normalizeString2(path, allowAboveRoot) {
863
- let res = "";
864
- let lastSegmentLength = 0;
865
- let lastSlash = -1;
866
- let dots = 0;
867
- let char = null;
868
- for (let index = 0; index <= path.length; ++index) {
869
- if (index < path.length) {
870
- char = path[index];
871
- } else if (char === "/") {
872
- break;
873
- } else {
874
- char = "/";
875
- }
876
- if (char === "/") {
877
- if (lastSlash === index - 1 || dots === 1) {
878
- } else if (dots === 2) {
879
- if (res.length < 2 || lastSegmentLength !== 2 || res[res.length - 1] !== "." || res[res.length - 2] !== ".") {
880
- if (res.length > 2) {
881
- const lastSlashIndex = res.lastIndexOf("/");
882
- if (lastSlashIndex === -1) {
883
- res = "";
884
- lastSegmentLength = 0;
885
- } else {
886
- res = res.slice(0, lastSlashIndex);
887
- lastSegmentLength = res.length - 1 - res.lastIndexOf("/");
888
- }
889
- lastSlash = index;
890
- dots = 0;
891
- continue;
892
- } else if (res.length > 0) {
893
- res = "";
894
- lastSegmentLength = 0;
895
- lastSlash = index;
896
- dots = 0;
897
- continue;
898
- }
899
- }
900
- if (allowAboveRoot) {
901
- res += res.length > 0 ? "/.." : "..";
902
- lastSegmentLength = 2;
903
- }
904
- } else {
905
- if (res.length > 0) {
906
- res += `/${path.slice(lastSlash + 1, index)}`;
907
- } else {
908
- res = path.slice(lastSlash + 1, index);
909
- }
910
- lastSegmentLength = index - lastSlash - 1;
911
- }
912
- lastSlash = index;
913
- dots = 0;
914
- } else if (char === "." && dots !== -1) {
915
- ++dots;
916
- } else {
917
- dots = -1;
918
- }
919
- }
920
- return res;
921
- }
922
- __name(normalizeString2, "normalizeString");
923
836
 
924
837
  // ../path/src/get-workspace-root.ts
925
838
  var import_config_tools = require("@storm-software/config-tools");
@@ -952,46 +865,13 @@ __name(relativePath, "relativePath");
952
865
 
953
866
  // bin/capnpc.ts
954
867
  var import_commander = require("commander");
955
- var import_typescript3 = __toESM(require("typescript"), 1);
956
-
957
- // ../fs/src/helpers.ts
958
- var import_nanotar = require("nanotar");
959
- var import_node_fs4 = require("fs");
960
- var import_promises3 = require("fs/promises");
961
- async function createDirectory(path) {
962
- if (await exists(path)) {
963
- return;
964
- }
965
- return (0, import_promises3.mkdir)(path, {
966
- recursive: true
967
- });
968
- }
969
- __name(createDirectory, "createDirectory");
970
-
971
- // ../fs/src/write-file.ts
972
- var import_j_toml2 = require("@ltd/j-toml");
973
- var import_defu2 = __toESM(require("defu"), 1);
974
- var import_node_fs5 = require("fs");
975
868
  var import_promises4 = require("fs/promises");
976
- var writeFile = /* @__PURE__ */ __name(async (filePath, content = "", options = {}) => {
977
- if (!filePath) {
978
- throw new Error("No file path provided to read data");
979
- }
980
- const directory = findFilePath(correctPath(filePath));
981
- if (!existsSync2(directory)) {
982
- if (options.createDirectory !== false) {
983
- await createDirectory(directory);
984
- } else {
985
- throw new Error(`Directory ${directory} does not exist`);
986
- }
987
- }
988
- return (0, import_promises4.writeFile)(filePath, content || "", options);
989
- }, "writeFile");
869
+ var import_typescript3 = __toESM(require("typescript"), 1);
990
870
 
991
- // ../../node_modules/.pnpm/capnp-es@0.0.11_patch_hash=8f737a83e1b5be10396ff9b257b56b8b8e7a3dbd2754765e9b1e2019839e9c31_typescript@5.8.3/node_modules/capnp-es/dist/shared/capnp-es.CbTQkT9D.mjs
871
+ // ../../node_modules/.pnpm/capnp-es@0.0.11_patch_hash=824b6bae5f47936633f815d4ffa9019910937dad06e41df5249655046cadee7a_typescript@5.8.3/node_modules/capnp-es/dist/shared/capnp-es.CbTQkT9D.mjs
992
872
  var import_typescript = __toESM(require("typescript"), 1);
993
873
 
994
- // ../../node_modules/.pnpm/capnp-es@0.0.11_patch_hash=8f737a83e1b5be10396ff9b257b56b8b8e7a3dbd2754765e9b1e2019839e9c31_typescript@5.8.3/node_modules/capnp-es/dist/shared/capnp-es.0-_cOx6D.mjs
874
+ // ../../node_modules/.pnpm/capnp-es@0.0.11_patch_hash=824b6bae5f47936633f815d4ffa9019910937dad06e41df5249655046cadee7a_typescript@5.8.3/node_modules/capnp-es/dist/shared/capnp-es.0-_cOx6D.mjs
995
875
  var ListElementSize = /* @__PURE__ */ ((ListElementSize2) => {
996
876
  ListElementSize2[ListElementSize2["VOID"] = 0] = "VOID";
997
877
  ListElementSize2[ListElementSize2["BIT"] = 1] = "BIT";
@@ -2984,7 +2864,7 @@ function checkDataBounds(byteOffset, byteLength, s) {
2984
2864
  }
2985
2865
  __name(checkDataBounds, "checkDataBounds");
2986
2866
 
2987
- // ../../node_modules/.pnpm/capnp-es@0.0.11_patch_hash=8f737a83e1b5be10396ff9b257b56b8b8e7a3dbd2754765e9b1e2019839e9c31_typescript@5.8.3/node_modules/capnp-es/dist/shared/capnp-es.BvfUH5E6.mjs
2867
+ // ../../node_modules/.pnpm/capnp-es@0.0.11_patch_hash=824b6bae5f47936633f815d4ffa9019910937dad06e41df5249655046cadee7a_typescript@5.8.3/node_modules/capnp-es/dist/shared/capnp-es.BvfUH5E6.mjs
2988
2868
  function CompositeList(CompositeClass) {
2989
2869
  return class extends List {
2990
2870
  static _capnp = {
@@ -3055,7 +2935,7 @@ var getUint8Mask = _makePrimitiveMaskFn(
3055
2935
  DataView.prototype.setUint8
3056
2936
  );
3057
2937
 
3058
- // ../../node_modules/.pnpm/capnp-es@0.0.11_patch_hash=8f737a83e1b5be10396ff9b257b56b8b8e7a3dbd2754765e9b1e2019839e9c31_typescript@5.8.3/node_modules/capnp-es/dist/capnp/schema.mjs
2938
+ // ../../node_modules/.pnpm/capnp-es@0.0.11_patch_hash=824b6bae5f47936633f815d4ffa9019910937dad06e41df5249655046cadee7a_typescript@5.8.3/node_modules/capnp-es/dist/capnp/schema.mjs
3059
2939
  var _capnpFileId = BigInt("0xa93fc509624c72d9");
3060
2940
  var Node_Parameter = class extends Struct {
3061
2941
  static {
@@ -5715,7 +5595,7 @@ CodeGeneratorRequest._Nodes = CompositeList(Node);
5715
5595
  CodeGeneratorRequest._SourceInfo = CompositeList(Node_SourceInfo);
5716
5596
  CodeGeneratorRequest._RequestedFiles = CompositeList(CodeGeneratorRequest_RequestedFile);
5717
5597
 
5718
- // ../../node_modules/.pnpm/capnp-es@0.0.11_patch_hash=8f737a83e1b5be10396ff9b257b56b8b8e7a3dbd2754765e9b1e2019839e9c31_typescript@5.8.3/node_modules/capnp-es/dist/shared/capnp-es.GpvEvMIK.mjs
5598
+ // ../../node_modules/.pnpm/capnp-es@0.0.11_patch_hash=824b6bae5f47936633f815d4ffa9019910937dad06e41df5249655046cadee7a_typescript@5.8.3/node_modules/capnp-es/dist/shared/capnp-es.GpvEvMIK.mjs
5719
5599
  var ArenaKind = /* @__PURE__ */ ((ArenaKind2) => {
5720
5600
  ArenaKind2[ArenaKind2["SINGLE_SEGMENT"] = 0] = "SINGLE_SEGMENT";
5721
5601
  ArenaKind2[ArenaKind2["MULTI_SEGMENT"] = 1] = "MULTI_SEGMENT";
@@ -6712,7 +6592,7 @@ function copy(m) {
6712
6592
  }
6713
6593
  __name(copy, "copy");
6714
6594
 
6715
- // ../../node_modules/.pnpm/capnp-es@0.0.11_patch_hash=8f737a83e1b5be10396ff9b257b56b8b8e7a3dbd2754765e9b1e2019839e9c31_typescript@5.8.3/node_modules/capnp-es/dist/shared/capnp-es.CbTQkT9D.mjs
6595
+ // ../../node_modules/.pnpm/capnp-es@0.0.11_patch_hash=824b6bae5f47936633f815d4ffa9019910937dad06e41df5249655046cadee7a_typescript@5.8.3/node_modules/capnp-es/dist/shared/capnp-es.CbTQkT9D.mjs
6716
6596
  var GEN_EXPLICIT_DEFAULT_NON_PRIMITIVE = "CAPNP-ES000 Don't know how to generate a %s field with an explicit default value.";
6717
6597
  var GEN_FIELD_NON_INLINE_STRUCT_LIST = "CAPNP-ES001 Don't know how to generate non-inline struct lists.";
6718
6598
  var GEN_NODE_LOOKUP_FAIL = "CAPNP-ES002 Failed to look up node id %s.";
@@ -6824,10 +6704,11 @@ var Primitives = {
6824
6704
  setter: "setVoid"
6825
6705
  }
6826
6706
  };
6827
- var SOURCE_COMMENT = `/* eslint-disable */
6707
+ var SOURCE_COMMENT = `
6708
+ /* eslint-disable */
6828
6709
  // biome-ignore lint: disable
6829
6710
 
6830
- // Generated by Storm Stack
6711
+ // Generated by storm-capnpc
6831
6712
  // Note: Do not edit this file manually - it will be overwritten automatically
6832
6713
 
6833
6714
  `;
@@ -7861,7 +7742,7 @@ function generateCapnpImport(ctx) {
7861
7742
  (n) => n.name === "importPath"
7862
7743
  );
7863
7744
  const importAnnotation = tsImportPathAnnotation && fileNode.annotations.find((a) => a.id === tsImportPathAnnotation.id);
7864
- const importPath = importAnnotation === void 0 ? "storm-capnp" : importAnnotation.value.text;
7745
+ const importPath = importAnnotation === void 0 ? "@stryke/capnp" : importAnnotation.value.text;
7865
7746
  ctx.codeParts.push(`import * as $ from '${importPath}';`);
7866
7747
  }
7867
7748
  __name(generateCapnpImport, "generateCapnpImport");
@@ -7870,7 +7751,7 @@ function generateNestedImports(ctx) {
7870
7751
  const { name } = imp;
7871
7752
  let importPath;
7872
7753
  if (name.startsWith("/capnp/")) {
7873
- importPath = `storm-capnp/capnp/${name.slice(7).replace(/\.capnp$/, "")}`;
7754
+ importPath = `@stryke/capnp/schemas/${name.slice(7).replace(/\.capnp$/, "")}`;
7874
7755
  } else {
7875
7756
  importPath = name.replace(/\.capnp$/, ".js");
7876
7757
  if (importPath[0] !== ".") {
@@ -7906,12 +7787,35 @@ async function compileAll(codeGenRequest, opts) {
7906
7787
  const req = new Message(codeGenRequest, false).getRoot(
7907
7788
  CodeGeneratorRequest
7908
7789
  );
7790
+ console.log(
7791
+ `
7792
+ Running capnpc for ${req.requestedFiles.length} capnp schema files
7793
+ `
7794
+ );
7909
7795
  const ctx = new CodeGeneratorContext();
7910
7796
  ctx.files = req.requestedFiles.map((file) => loadRequestedFile(req, file));
7797
+ if (ctx.files.length === 0) {
7798
+ throw new Error(GEN_NO_FILES);
7799
+ }
7800
+ console.log(
7801
+ `
7802
+ Compiling ${ctx.files.length} files: ${ctx.files.map((f) => f.tsPath).join(", ")}
7803
+ `
7804
+ );
7911
7805
  const files = new Map(
7912
7806
  ctx.files.map((file) => [file.tsPath, compileFile(file)])
7913
7807
  );
7914
- tsCompile(files, opts?.dts === true, opts?.js === true, opts?.tsconfig);
7808
+ if (files.size === 0) {
7809
+ throw new Error(GEN_NO_FILES);
7810
+ }
7811
+ if (opts?.dts === true || opts?.js === true) {
7812
+ console.log(
7813
+ `
7814
+ TypeScript Compiling ${files.size} files: ${[...files.keys()].join(", ")}
7815
+ `
7816
+ );
7817
+ tsCompile(files, opts?.dts === true, opts?.js === true, opts?.tsconfig);
7818
+ }
7915
7819
  if (!opts?.ts) {
7916
7820
  for (const [fileName] of files) {
7917
7821
  if (fileName.endsWith(".ts") && !fileName.endsWith(".d.ts")) {
@@ -8010,92 +7914,58 @@ function tsCompile(files, dts, js, tsconfig) {
8010
7914
  }
8011
7915
  __name(tsCompile, "tsCompile");
8012
7916
 
8013
- // ../../node_modules/.pnpm/capnp-es@0.0.11_patch_hash=8f737a83e1b5be10396ff9b257b56b8b8e7a3dbd2754765e9b1e2019839e9c31_typescript@5.8.3/node_modules/capnp-es/dist/compiler/index.mjs
7917
+ // ../../node_modules/.pnpm/capnp-es@0.0.11_patch_hash=824b6bae5f47936633f815d4ffa9019910937dad06e41df5249655046cadee7a_typescript@5.8.3/node_modules/capnp-es/dist/compiler/index.mjs
8014
7918
  var import_typescript2 = require("typescript");
8015
7919
 
8016
7920
  // src/compile.ts
8017
7921
  var import_node_buffer2 = require("buffer");
8018
7922
  var import_node_child_process = require("child_process");
8019
- var import_node_fs6 = require("fs");
8020
7923
  async function capnpc(options) {
8021
- try {
8022
- const { output, tsconfig, schema = [] } = options;
8023
- let dataBuf = import_node_buffer2.Buffer.alloc(0);
8024
- if (!process.stdin.isTTY) {
8025
- const chunks = [];
8026
- process.stdin.on("data", (chunk) => {
8027
- chunks.push(chunk);
8028
- });
8029
- await new Promise((resolve) => {
8030
- process.stdin.on("end", resolve);
8031
- });
8032
- const reqBuffer = import_node_buffer2.Buffer.alloc(chunks.reduce((l, chunk) => l + chunk.byteLength, 0));
8033
- let i = 0;
8034
- for (const chunk of chunks) {
8035
- chunk.copy(reqBuffer, i);
8036
- i += chunk.byteLength;
8037
- }
8038
- dataBuf = reqBuffer;
8039
- }
8040
- if (dataBuf.byteLength === 0) {
8041
- const opts = [];
8042
- if (output) {
8043
- opts.push(`-o-:${output}`);
8044
- } else {
8045
- opts.push("-o-");
8046
- }
8047
- dataBuf = await new Promise((resolve) => {
8048
- (0, import_node_child_process.exec)(`capnpc ${opts.join(" ")} ${schema.join(" ")}`, {
8049
- encoding: "buffer"
8050
- }, (error, stdout, stderr) => {
8051
- if (stderr.length > 0) {
8052
- process.stderr.write(stderr);
8053
- }
8054
- if (error) {
8055
- throw error;
8056
- }
8057
- resolve(stdout);
8058
- });
8059
- });
8060
- }
8061
- const result = await compileAll(dataBuf, {
8062
- ts: options.ts ?? true,
8063
- js: options.js ?? false,
8064
- dts: options.dts ?? true,
8065
- tsconfig: tsconfig?.options
7924
+ const { output, tsconfig, schemas = [], tty } = options;
7925
+ let dataBuf = import_node_buffer2.Buffer.alloc(0);
7926
+ if (tty) {
7927
+ const chunks = [];
7928
+ process.stdin.on("data", (chunk) => {
7929
+ chunks.push(chunk);
8066
7930
  });
8067
- for (const [fileName, content] of result.files) {
8068
- let filePath = fileName;
8069
- if (!(0, import_node_fs6.existsSync)(findFilePath(filePath))) {
8070
- const fullPath = `/${filePath}`;
8071
- if ((0, import_node_fs6.existsSync)(findFilePath(fullPath))) {
8072
- filePath = fullPath;
8073
- }
8074
- }
8075
- if (output) {
8076
- filePath = joinPaths(output, fileName);
8077
- }
8078
- if (!(0, import_node_fs6.existsSync)(findFilePath(filePath))) {
8079
- await createDirectory(findFilePath(filePath));
8080
- }
8081
- await writeFile(
8082
- filePath,
8083
- // https://github.com/microsoft/TypeScript/issues/54632
8084
- content.replace(/^\s+/gm, (match) => " ".repeat(match.length / 2))
8085
- );
7931
+ await new Promise((resolve) => {
7932
+ process.stdin.on("end", resolve);
7933
+ });
7934
+ const reqBuffer = import_node_buffer2.Buffer.alloc(chunks.reduce((l, chunk) => l + chunk.byteLength, 0));
7935
+ let i = 0;
7936
+ for (const chunk of chunks) {
7937
+ chunk.copy(reqBuffer, i);
7938
+ i += chunk.byteLength;
8086
7939
  }
8087
- return result;
8088
- } catch (error) {
8089
- if (error instanceof Error) {
8090
- console.error(`Error: ${error.message}`);
8091
- if (error.stack) {
8092
- console.error(error.stack);
8093
- }
7940
+ dataBuf = reqBuffer;
7941
+ }
7942
+ if (dataBuf.byteLength === 0) {
7943
+ const opts = [];
7944
+ if (output) {
7945
+ opts.push(`-o-:${output}`);
8094
7946
  } else {
8095
- console.error("An unknown error occurred:", error);
8096
- }
8097
- throw error;
7947
+ opts.push("-o-");
7948
+ }
7949
+ dataBuf = await new Promise((resolve) => {
7950
+ (0, import_node_child_process.exec)(`capnpc ${opts.join(" ")} ${schemas.join(" ")}`, {
7951
+ encoding: "buffer"
7952
+ }, (error, stdout, stderr) => {
7953
+ if (stderr.length > 0) {
7954
+ process.stderr.write(stderr);
7955
+ }
7956
+ if (error) {
7957
+ throw error;
7958
+ }
7959
+ resolve(stdout);
7960
+ });
7961
+ });
8098
7962
  }
7963
+ return compileAll(dataBuf, {
7964
+ ts: options.ts ?? true,
7965
+ js: false,
7966
+ dts: false,
7967
+ tsconfig: tsconfig?.options
7968
+ });
8099
7969
  }
8100
7970
  __name(capnpc, "capnpc");
8101
7971
 
@@ -8112,9 +7982,13 @@ function createProgram() {
8112
7982
  }
8113
7983
  const program = new import_commander.Command("storm-capnpc");
8114
7984
  program.version("1.0.0", "-v --version", "display CLI version");
7985
+ const projectRootOption = new import_commander.Option("-p --project-root <path>", "The path to the project root directory");
8115
7986
  const tsOption = new import_commander.Option("--ts", "An indicator to generate TypeScript files").default(true);
7987
+ const noTsOption = new import_commander.Option("--no-ts", "An indicator to disable generation of TypeScript files");
8116
7988
  const jsOption = new import_commander.Option("--js", "An indicator to generate JavaScript files").default(false);
8117
- const dtsOption = new import_commander.Option("--dts", "An indicator to generate TypeScript declaration files").default(true);
7989
+ const dtsOption = new import_commander.Option("--dts", "An indicator to generate TypeScript declaration files");
7990
+ const noDtsOption = new import_commander.Option("--no-dts", "An indicator to disable generation of TypeScript declaration files");
7991
+ const ttyOption = new import_commander.Option("--tty", "An indicator to enable TTY mode for the compiler");
8118
7992
  const importPathOption = new import_commander.Option("-I --import-path <dir...>", "Add <dir> to the list of directories searched for non-relative imports").default([]).argParser((val) => {
8119
7993
  if (val.startsWith("-I") || val.startsWith("--import-path")) {
8120
7994
  return val.split(",").map((dir) => dir.trim());
@@ -8125,54 +7999,47 @@ function createProgram() {
8125
7999
  });
8126
8000
  const generateId = new import_commander.Option("-i --generate-id", "Generate a new 64-bit unique ID for use in a Cap'n Proto schema").default(true);
8127
8001
  const standardImportOption = new import_commander.Option("--standard-import", "Add default import paths; use only those specified by -I").default(true);
8128
- const schemaOption = new import_commander.Option("-s --schema <path...>", "The directories containing the Cap'n Proto schema files to compile (default: current working directory)").default([
8129
- joinPaths(process.cwd(), "**/*.capnp")
8130
- ]).argParser((val) => {
8131
- let result = [];
8132
- if (val.startsWith("--schema") || val.startsWith("-s")) {
8133
- result = val.split(",").map((dir) => dir.trim());
8134
- }
8135
- result = [
8136
- val.trim()
8137
- ];
8138
- return result.map((dir) => dir.endsWith(".capnp") ? dir : joinPaths(dir, "**/*.capnp"));
8139
- });
8002
+ const schemaOption = new import_commander.Option("-s --schema <path>", "The directory (or a glob to the directory) containing the Cap'n Proto schema files to compile (default: current working directory)").default(joinPaths("{projectRoot}", "**/*.capnp"));
8140
8003
  const outputOption = new import_commander.Option("-o --output <path>", "The directory to output the generated files to");
8141
- const tsconfigOption = new import_commander.Option("-p --tsconfig <path>", "The path to the TypeScript configuration file to use for compilation").default(joinPaths(process.cwd(), "tsconfig.json"));
8004
+ const tsconfigOption = new import_commander.Option("--tsconfig <path>", "The path to the TypeScript configuration file to use for compilation").default(joinPaths("{projectRoot}", "tsconfig.json"));
8142
8005
  const workspaceRootOption = new import_commander.Option("-w --workspace-root <path>", "The path to the workspace root directory").default(root);
8143
8006
  program.command("compile", {
8144
8007
  isDefault: true
8145
- }).description("Run the Storm Cap'n Proto compiler").addOption(schemaOption).addOption(outputOption).addOption(importPathOption).addOption(tsconfigOption).addOption(generateId).addOption(standardImportOption).addOption(tsOption).addOption(jsOption).addOption(dtsOption).addOption(workspaceRootOption).action(compileAction).showSuggestionAfterError(true).showHelpAfterError(true);
8008
+ }).description("Run the Storm Cap'n Proto compiler").addOption(projectRootOption).addOption(schemaOption).addOption(outputOption).addOption(importPathOption).addOption(tsconfigOption).addOption(generateId).addOption(standardImportOption).addOption(tsOption).addOption(noTsOption).addOption(jsOption).addOption(dtsOption).addOption(noDtsOption).addOption(workspaceRootOption).addOption(ttyOption).action(compileAction).showSuggestionAfterError(true).showHelpAfterError(true);
8146
8009
  return program;
8147
8010
  }
8148
8011
  __name(createProgram, "createProgram");
8149
8012
  async function compileAction(options) {
8150
- (0, import_console.writeInfo)(`\u{1F4E6} Storm Cap'n Proto Compiler will output ${options.ts ? "TypeScript code" : ""}${options.js ? options.ts ? ", JavaScript code" : "JavaScript code" : ""}${options.dts ? options.ts || options.js ? ", TypeScript declarations" : "TypeScript declarations" : ""} files ${options.output ? `to ${options.output}...` : ""}`, {
8151
- logLevel: "all"
8152
- });
8153
- if (!existsSync2(options.tsconfig)) {
8154
- (0, import_console.writeFatal)(options.tsconfig ? `\u2716 The specified TypeScript configuration file "${options.tsconfig}" does not exist. Please provide a valid path.` : "\u2716 The specified TypeScript configuration file does not exist. Please provide a valid path.", {
8013
+ const tsconfigPath = options.tsconfig.replace("{projectRoot}", options.projectRoot);
8014
+ const schema = options.schema ? options.schema.replace("{projectRoot}", options.projectRoot) : options.projectRoot;
8015
+ if (!existsSync(tsconfigPath)) {
8016
+ const errorMessage = options.tsconfig ? `\u2716 The specified TypeScript configuration file "${tsconfigPath}" does not exist. Please provide a valid path.` : "\u2716 The specified TypeScript configuration file does not exist. Please provide a valid path.";
8017
+ (0, import_console.writeFatal)(errorMessage, {
8155
8018
  logLevel: "all"
8156
8019
  });
8157
- return;
8020
+ throw new Error(errorMessage);
8158
8021
  }
8159
- const resolvedTsconfig = await readJsonFile(options.tsconfig);
8160
- const tsconfig = import_typescript3.default.parseJsonConfigFileContent(resolvedTsconfig, import_typescript3.default.sys, findFilePath(options.tsconfig));
8161
- tsconfig.options.configFilePath = options.tsconfig;
8022
+ const resolvedTsconfig = await readJsonFile(tsconfigPath);
8023
+ const tsconfig = import_typescript3.default.parseJsonConfigFileContent(resolvedTsconfig, import_typescript3.default.sys, findFilePath(tsconfigPath));
8024
+ tsconfig.options.configFilePath = tsconfigPath;
8162
8025
  tsconfig.options.noImplicitOverride = false;
8163
- tsconfig.options.outDir = relativePath(findFilePath(joinPaths(options.workspaceRoot, options.tsconfig)), joinPaths(options.workspaceRoot, options.schema.length > 0 && options.schema[0] ? options.schema[0].endsWith(".capnp") ? findFilePath(options.schema[0]) : options.schema[0] : ""));
8164
- const schema = [];
8165
- for (const schemaPath of options.schema) {
8166
- if (!schemaPath || !schemaPath.includes("*") && !existsSync2(schemaPath)) {
8167
- (0, import_console.writeFatal)(`\u2716 The schema path "${schemaPath}" is invalid. Please provide a valid path.`, {
8168
- logLevel: "all"
8169
- });
8170
- return;
8171
- }
8172
- schema.push(...await listFiles(schemaPath));
8026
+ tsconfig.options.noUnusedLocals = false;
8027
+ tsconfig.options.noUnusedParameters = false;
8028
+ tsconfig.options.outDir = joinPaths(options.projectRoot, relativePath(findFilePath(tsconfigPath), joinPaths(options.workspaceRoot, schema.endsWith(".capnp") ? findFilePath(schema) : schema)));
8029
+ (0, import_console.writeInfo)(`\u{1F4E6} Storm Cap'n Proto Compiler will output ${options.ts ? "TypeScript code" : ""}${options.js ? options.ts ? ", JavaScript code" : "JavaScript code" : ""}${options.dts ? options.ts || options.js ? ", TypeScript declarations" : "TypeScript declarations" : ""} files from schemas at ${schema} to ${tsconfig.options.outDir}...`, {
8030
+ logLevel: "all"
8031
+ });
8032
+ const schemas = [];
8033
+ if (!schema || !schema.includes("*") && !existsSync(schema)) {
8034
+ const errorMessage = `\u2716 The schema path "${schema}" is invalid. Please provide a valid path.`;
8035
+ (0, import_console.writeFatal)(errorMessage, {
8036
+ logLevel: "all"
8037
+ });
8038
+ throw new Error(errorMessage);
8173
8039
  }
8174
- if (schema.length === 0) {
8175
- (0, import_console.writeFatal)(`\u2716 No Cap'n Proto schema files found in the specified source paths: ${options.schema.join(", ")}. Please ensure that the paths are correct and contain .capnp files.`, {
8040
+ schemas.push(...await listFiles(schema.includes("*") ? schema.endsWith(".capnp") ? schema : `${schema}.capnp` : joinPaths(schema, "**/*.capnp")));
8041
+ if (schemas.length === 0) {
8042
+ (0, import_console.writeFatal)(`\u2716 No Cap'n Proto schema files found in the specified source paths: ${schemas.join(", ")}. Please ensure that the paths are correct and contain .capnp files.`, {
8176
8043
  logLevel: "all"
8177
8044
  });
8178
8045
  return;
@@ -8180,14 +8047,39 @@ async function compileAction(options) {
8180
8047
  const result = await capnpc({
8181
8048
  ...options,
8182
8049
  tsconfig,
8183
- schema
8050
+ schemas,
8051
+ ts: options.ts ?? (options.noTs !== void 0 ? !options.noTs : true),
8052
+ dts: options.dts ?? (options.noDts !== void 0 ? !options.noDts : true)
8184
8053
  });
8185
8054
  if (result.files.size === 0) {
8186
- (0, import_console.writeInfo)("\u26A0\uFE0F No files were generated. Please check your schema files.", {
8055
+ (0, import_console.writeWarning)("\u26A0\uFE0F No files were generated. Please check your schema files.", {
8187
8056
  logLevel: "all"
8188
8057
  });
8189
8058
  return;
8190
8059
  }
8060
+ (0, import_console.writeInfo)(`Writing ${result.files.size} generated files to disk...`, {
8061
+ logLevel: "all"
8062
+ });
8063
+ for (const [fileName, content] of result.files) {
8064
+ let filePath = fileName;
8065
+ if (!existsSync(findFilePath(filePath))) {
8066
+ const fullPath = `/${filePath}`;
8067
+ if (existsSync(findFilePath(fullPath))) {
8068
+ filePath = fullPath;
8069
+ }
8070
+ }
8071
+ if (options.output) {
8072
+ filePath = joinPaths(options.output, fileName);
8073
+ }
8074
+ if (!existsSync(findFilePath(filePath))) {
8075
+ await createDirectory(findFilePath(filePath));
8076
+ }
8077
+ await (0, import_promises4.writeFile)(
8078
+ filePath,
8079
+ // https://github.com/microsoft/TypeScript/issues/54632
8080
+ content.replace(/^\s+/gm, (match) => " ".repeat(match.length / 2))
8081
+ );
8082
+ }
8191
8083
  (0, import_console.writeSuccess)("\u26A1 Storm Cap'n Proto Compiler completed successfully.", {
8192
8084
  logLevel: "all"
8193
8085
  });
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+ import { Command } from 'commander';
3
+
4
+ declare function createProgram(): Command;
5
+
6
+ export { createProgram };
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+ import { Command } from 'commander';
3
+
4
+ declare function createProgram(): Command;
5
+
6
+ export { createProgram };