@reliverse/rempts 1.7.23 → 1.7.25

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.
@@ -628,11 +628,7 @@ async function runCommandWithArgs(command, argv, parserOptions, returnCtx) {
628
628
  if (def.type === "boolean") {
629
629
  defaultMap[argKey] = def.default !== void 0 ? def.default : false;
630
630
  } else if (def.default !== void 0) {
631
- if (def.type === "array" && typeof def.default === "string") {
632
- defaultMap[argKey] = [def.default];
633
- } else {
634
- defaultMap[argKey] = def.default;
635
- }
631
+ defaultMap[argKey] = def.type === "array" && typeof def.default === "string" ? [def.default] : def.default;
636
632
  }
637
633
  }
638
634
  const mergedParserOptions = {
@@ -668,38 +664,27 @@ async function runCommandWithArgs(command, argv, parserOptions, returnCtx) {
668
664
  if (def.type === "array" && rawVal !== void 0 && !Array.isArray(rawVal)) {
669
665
  rawVal = [rawVal];
670
666
  }
671
- const valueOrDefault = rawVal ?? defaultMap[key];
672
- if (valueOrDefault == null && def.required) {
667
+ const casted = rawVal !== void 0 ? castArgValue(def, rawVal, key) : def.default;
668
+ const argUsed = rawVal !== void 0 && (def.type === "boolean" ? casted === true : true);
669
+ if (casted == null && def.required) {
673
670
  await showUsage(command, parserOptions);
674
671
  relinka("error", `Missing required argument: --${key}`);
675
672
  if (autoExit) process.exit(1);
676
673
  else throw new Error(`Missing required argument: --${key}`);
677
674
  }
678
- if (def.dependencies && Array.isArray(def.dependencies)) {
679
- const finalValue = parsed[key] ?? defaultMap[key];
680
- if (finalValue !== void 0) {
681
- const missingDeps = def.dependencies.filter(
682
- (dependency) => !(parsed[dependency] ?? defaultMap[dependency])
675
+ if (argUsed && def.dependencies?.length) {
676
+ const missingDeps = def.dependencies.filter((d) => {
677
+ const depVal = parsed[d] ?? defaultMap[d];
678
+ return !depVal;
679
+ });
680
+ if (missingDeps.length > 0) {
681
+ const depsList = missingDeps.map((d) => `--${d}`).join(", ");
682
+ throw new Error(
683
+ `Argument --${key} can only be used when ${depsList} ${missingDeps.length === 1 ? "is" : "are"} set`
683
684
  );
684
- if (missingDeps.length > 0) {
685
- const depsList = missingDeps.map((d) => `--${d}`).join(", ");
686
- throw new Error(
687
- `Argument --${key} can only be used when ${depsList} ${missingDeps.length === 1 ? "is" : "are"} set`
688
- );
689
- }
690
685
  }
691
686
  }
692
- try {
693
- if (def.type === "boolean") {
694
- finalArgs[key] = rawVal !== void 0 ? castArgValue(def, rawVal, key) : false;
695
- } else {
696
- finalArgs[key] = castArgValue(def, rawVal, key);
697
- }
698
- } catch (err) {
699
- relinka("error", String(err));
700
- if (autoExit) process.exit(1);
701
- else throw err;
702
- }
687
+ finalArgs[key] = def.type === "boolean" ? Boolean(casted) : casted;
703
688
  }
704
689
  const ctx = {
705
690
  args: finalArgs,
@@ -852,11 +837,7 @@ export async function runCmd(command, argv = [], parserOptions = {}) {
852
837
  if (def.type === "boolean") {
853
838
  defaultMap[argKey] = def.default !== void 0 ? def.default : false;
854
839
  } else if (def.default !== void 0) {
855
- if (def.type === "array" && typeof def.default === "string") {
856
- defaultMap[argKey] = [def.default];
857
- } else {
858
- defaultMap[argKey] = def.default;
859
- }
840
+ defaultMap[argKey] = def.type === "array" && typeof def.default === "string" ? [def.default] : def.default;
860
841
  }
861
842
  }
862
843
  const mergedParserOptions = {
@@ -889,34 +870,24 @@ export async function runCmd(command, argv = [], parserOptions = {}) {
889
870
  if (def.type === "array" && rawVal !== void 0 && !Array.isArray(rawVal)) {
890
871
  rawVal = [rawVal];
891
872
  }
892
- const valueOrDefault = rawVal ?? defaultMap[key];
893
- if (valueOrDefault == null && def.required) {
873
+ const casted = rawVal !== void 0 ? castArgValue(def, rawVal, key) : def.default;
874
+ const argUsed = rawVal !== void 0 && (def.type === "boolean" ? casted === true : true);
875
+ if (casted == null && def.required) {
894
876
  throw new Error(`Missing required argument: --${key}`);
895
877
  }
896
- if (def.dependencies && Array.isArray(def.dependencies)) {
897
- const finalValue = parsed[key] ?? defaultMap[key];
898
- if (finalValue !== void 0) {
899
- const missingDeps = def.dependencies.filter(
900
- (dependency) => !(parsed[dependency] ?? defaultMap[dependency])
878
+ if (argUsed && def.dependencies?.length) {
879
+ const missingDeps = def.dependencies.filter((d) => {
880
+ const depVal = parsed[d] ?? defaultMap[d];
881
+ return !depVal;
882
+ });
883
+ if (missingDeps.length) {
884
+ const depsList = missingDeps.map((d) => `--${d}`).join(", ");
885
+ throw new Error(
886
+ `Argument --${key} can only be used when ${depsList} ${missingDeps.length === 1 ? "is" : "are"} set`
901
887
  );
902
- if (missingDeps.length > 0) {
903
- const depsList = missingDeps.map((d) => `--${d}`).join(", ");
904
- throw new Error(
905
- `Argument --${key} can only be used when ${depsList} ${missingDeps.length === 1 ? "is" : "are"} set`
906
- );
907
- }
908
888
  }
909
889
  }
910
- try {
911
- if (def.type === "boolean") {
912
- finalArgs[key] = rawVal !== void 0 ? castArgValue(def, rawVal, key) : false;
913
- } else {
914
- finalArgs[key] = castArgValue(def, rawVal, key);
915
- }
916
- } catch (err) {
917
- relinka("error", String(err));
918
- throw err;
919
- }
890
+ finalArgs[key] = def.type === "boolean" ? Boolean(casted) : casted;
920
891
  }
921
892
  const ctx = {
922
893
  args: finalArgs,
@@ -937,11 +908,7 @@ function getParsedContext(command, argv, parserOptions) {
937
908
  if (def.type === "boolean") {
938
909
  defaultMap[argKey] = def.default !== void 0 ? def.default : false;
939
910
  } else if (def.default !== void 0) {
940
- if (def.type === "array" && typeof def.default === "string") {
941
- defaultMap[argKey] = [def.default];
942
- } else {
943
- defaultMap[argKey] = def.default;
944
- }
911
+ defaultMap[argKey] = def.type === "array" && typeof def.default === "string" ? [def.default] : def.default;
945
912
  }
946
913
  }
947
914
  const mergedParserOptions = {
@@ -8,58 +8,71 @@ const jiti = createJiti(import.meta.url, {
8
8
  fsCache: true,
9
9
  sourceMaps: true
10
10
  });
11
+ const COMMAND_EXTENSIONS = [".ts", ".js"];
12
+ const COMMAND_FILENAMES = ["cmd.ts", "cmd.js"];
13
+ const getCallerDirectory = () => {
14
+ const stack = new Error().stack?.split("\n") ?? [];
15
+ for (const line of stack) {
16
+ const match = /\((.*):(\d+):(\d+)\)/.exec(line) || /at (.*):(\d+):(\d+)/.exec(line);
17
+ if (match?.[1] && !match[1].includes("run-command")) {
18
+ return dirname(match[1]);
19
+ }
20
+ }
21
+ return process.cwd();
22
+ };
23
+ const tryLoadCommand = async (path) => {
24
+ if (!await fs.pathExists(path)) return null;
25
+ try {
26
+ relinka("verbose", `Attempting to load command from: ${path}`);
27
+ const cmd = await jiti.import(path, { default: true });
28
+ relinka("verbose", `Successfully loaded command from: ${path}`);
29
+ return cmd;
30
+ } catch {
31
+ relinka("verbose", `Failed to load ${path} as a command file`);
32
+ return null;
33
+ }
34
+ };
35
+ const generateCandidatePaths = async (resolvedPath) => {
36
+ if (!await fs.pathExists(resolvedPath)) {
37
+ return COMMAND_EXTENSIONS.map((ext) => `${resolvedPath}${ext}`);
38
+ }
39
+ if (await fs.isDirectory(resolvedPath)) {
40
+ return COMMAND_FILENAMES.map((filename) => resolve(resolvedPath, filename));
41
+ }
42
+ return [resolvedPath];
43
+ };
44
+ const createCommandNotFoundError = (cmdPath, searchedPaths) => new Error(
45
+ `No command file found for "${cmdPath}". Expected to find either:
46
+ - A valid command file at the specified path
47
+ - A directory containing cmd.ts or cmd.js
48
+ - A file path that can be resolved with .ts or .js extension
49
+ Searched paths: ${searchedPaths.join(", ")}
50
+ Please ensure one of these exists and exports a default command.`
51
+ );
52
+ const createLoadError = (cmdPath, originalError) => new Error(
53
+ `Failed to load command from "${cmdPath}"
54
+ For developers: Ensure the command file:
55
+ - Exists and is accessible
56
+ - Exports a default command (e.g., export default defineCommand({...}))
57
+ - Is a valid TypeScript/JavaScript module
58
+ Original error: ${originalError instanceof Error ? originalError.message : String(originalError)}`
59
+ );
11
60
  export async function loadCommand(cmdPath) {
12
61
  try {
13
- const err = new Error();
14
- const stack = err.stack?.split("\n");
15
- let callerFile;
16
- if (stack) {
17
- for (const line of stack) {
18
- const match = /\((.*):(\d+):(\d+)\)/.exec(line) || /at (.*):(\d+):(\d+)/.exec(line);
19
- if (match?.[1] && !match[1].includes("run-command")) {
20
- callerFile = match[1];
21
- break;
22
- }
23
- }
24
- }
25
- const callerDir = callerFile ? dirname(callerFile) : process.cwd();
62
+ const callerDir = getCallerDirectory();
26
63
  const normalizedPath = cmdPath.replace(/^\.\//, "");
27
64
  const resolvedPath = resolve(callerDir, normalizedPath);
28
- if (!resolvedPath.endsWith("cmd.ts") && !resolvedPath.endsWith("cmd.js")) {
29
- const possiblePaths = [
30
- resolve(resolvedPath, "cmd.ts"),
31
- resolve(resolvedPath, "cmd.js")
32
- ];
33
- for (const path of possiblePaths) {
34
- if (await fs.pathExists(path)) {
35
- relinka("verbose", `Loading command from: ${path}`);
36
- const cmd2 = await jiti.import(path, { default: true });
37
- relinka("verbose", `Successfully loaded command from: ${path}`);
38
- return cmd2;
39
- }
40
- }
41
- throw new Error(
42
- `No command file found in ${resolvedPath}. Expected to find either:
43
- - ${possiblePaths[0]}
44
- - ${possiblePaths[1]}
45
- Please ensure one of these files exists and exports a default command.`
46
- );
65
+ const candidatePaths = await generateCandidatePaths(resolvedPath);
66
+ for (const path of candidatePaths) {
67
+ const command = await tryLoadCommand(path);
68
+ if (command) return command;
47
69
  }
48
- relinka("verbose", `Loading command from: ${resolvedPath}`);
49
- const cmd = await jiti.import(resolvedPath, { default: true });
50
- relinka("verbose", `Successfully loaded command from: ${resolvedPath}`);
51
- return cmd;
70
+ throw createCommandNotFoundError(cmdPath, candidatePaths);
52
71
  } catch (error) {
53
72
  if (error instanceof Error && error.message.includes("No command file found")) {
54
73
  throw error;
55
74
  }
56
75
  relinka("error", `Failed to load command from ${cmdPath}:`, error);
57
- throw new Error(
58
- `Failed to load command from ${cmdPath}:
59
- - Make sure the file exists and is accessible
60
- - Ensure the file exports a default command
61
- - Check that the file is a valid TypeScript/JavaScript module
62
- Original error: ${error instanceof Error ? error.message : String(error)}`
63
- );
76
+ throw createLoadError(cmdPath, error);
64
77
  }
65
78
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "dependencies": {
3
3
  "@figliolia/chalk-animation": "^1.0.4",
4
- "@reliverse/pathkit": "^1.2.1",
4
+ "@reliverse/pathkit": "^1.3.3",
5
5
  "@reliverse/reliarg": "^1.0.3",
6
6
  "@reliverse/relico": "^1.1.2",
7
7
  "@reliverse/relifso": "^1.4.5",
@@ -29,7 +29,7 @@
29
29
  "license": "MIT",
30
30
  "name": "@reliverse/rempts",
31
31
  "type": "module",
32
- "version": "1.7.23",
32
+ "version": "1.7.25",
33
33
  "author": "reliverse",
34
34
  "bugs": {
35
35
  "email": "blefnk@gmail.com",