@reliverse/rempts 1.7.46 → 1.7.48

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.
Files changed (2) hide show
  1. package/dist-npm/bin/mod.mjs +251 -36
  2. package/package.json +1 -1
@@ -3120,6 +3120,12 @@ const jiti = createJiti(import.meta.url, {
3120
3120
  const COMMAND_EXTENSIONS = [".ts", ".js"];
3121
3121
  const COMMAND_FILENAMES = ["cmd.ts", "cmd.js"];
3122
3122
  const getCallerDirectory = async () => {
3123
+ if (process$1.env._REMPTS_CALLER_DIR) {
3124
+ if (process$1.env.NODE_ENV === "development") {
3125
+ relinka("verbose", `Using explicit caller directory: ${process$1.env._REMPTS_CALLER_DIR}`);
3126
+ }
3127
+ return process$1.env._REMPTS_CALLER_DIR;
3128
+ }
3123
3129
  const stack = new Error().stack?.split("\n") ?? [];
3124
3130
  const cwd = process$1.cwd();
3125
3131
  if (process$1.env.NODE_ENV === "development") {
@@ -3135,22 +3141,82 @@ const getCallerDirectory = async () => {
3135
3141
  if (process$1.env.NODE_ENV === "development") {
3136
3142
  relinka("verbose", `Checking file path: ${filePath}`);
3137
3143
  }
3138
- if (!filePath.includes("node_modules") && !filePath.includes("command-runner") && !filePath.includes("command-typed") && !filePath.includes("launcher-mod") && !filePath.includes("launcher-types") && !filePath.includes("mod.mjs") && // Skip compiled output
3139
- !filePath.includes("mod.js") && // Skip compiled output
3140
- !filePath.includes("mod.ts")) {
3144
+ if (!filePath.includes("node_modules") && !filePath.includes("command-runner") && !filePath.includes("command-typed") && !filePath.includes("launcher-mod") && !filePath.includes("launcher-types") && !filePath.endsWith("mod.mjs") && // Skip compiled output
3145
+ !filePath.endsWith("mod.js") && // Skip compiled output
3146
+ !filePath.endsWith("mod.ts")) {
3141
3147
  try {
3142
3148
  const fileDir = dirname(filePath);
3149
+ let currentDir2 = fileDir;
3150
+ let packageRoot = null;
3151
+ while (currentDir2 !== dirname(currentDir2)) {
3152
+ try {
3153
+ const packageJsonPath = resolve(currentDir2, "package.json");
3154
+ if (await fs.pathExists(packageJsonPath)) {
3155
+ packageRoot = currentDir2;
3156
+ if (process$1.env.NODE_ENV === "development") {
3157
+ relinka("verbose", `Found package.json at: ${packageRoot}`);
3158
+ }
3159
+ const cwdPackageJsonPath = resolve(cwd, "package.json");
3160
+ if (await fs.pathExists(cwdPackageJsonPath)) {
3161
+ try {
3162
+ const callerPackage = JSON.parse(await fs.readFile(packageJsonPath, "utf-8"));
3163
+ const cwdPackage = JSON.parse(await fs.readFile(cwdPackageJsonPath, "utf-8"));
3164
+ if (callerPackage.name !== cwdPackage.name || callerPackage.version !== cwdPackage.version) {
3165
+ if (process$1.env.NODE_ENV === "development") {
3166
+ relinka("verbose", `Using caller package root: ${packageRoot} (${callerPackage.name}@${callerPackage.version})`);
3167
+ }
3168
+ return packageRoot;
3169
+ }
3170
+ } catch {
3171
+ if (resolve(packageRoot) !== resolve(cwd)) {
3172
+ if (process$1.env.NODE_ENV === "development") {
3173
+ relinka("verbose", `Using caller package root (different path): ${packageRoot}`);
3174
+ }
3175
+ return packageRoot;
3176
+ }
3177
+ }
3178
+ } else {
3179
+ if (process$1.env.NODE_ENV === "development") {
3180
+ relinka("verbose", `Using caller package root (no CWD package.json): ${packageRoot}`);
3181
+ }
3182
+ return packageRoot;
3183
+ }
3184
+ break;
3185
+ }
3186
+ } catch {
3187
+ }
3188
+ currentDir2 = dirname(currentDir2);
3189
+ }
3143
3190
  const resolvedFileDir = resolve(fileDir);
3144
3191
  const resolvedCwd = resolve(cwd);
3145
- if (process$1.env.NODE_ENV === "development") {
3146
- relinka(
3147
- "verbose",
3148
- `File dir: ${fileDir}, resolved: ${resolvedFileDir}, cwd: ${resolvedCwd}`
3149
- );
3150
- }
3151
3192
  if (resolvedFileDir !== resolvedCwd && !resolvedFileDir.startsWith(resolvedCwd)) {
3152
- relinka("verbose", `Using caller directory: ${fileDir}`);
3153
- return fileDir;
3193
+ if (process$1.env.NODE_ENV === "development") {
3194
+ relinka("verbose", `Using caller directory (different from CWD): ${fileDir}`);
3195
+ }
3196
+ return packageRoot || fileDir;
3197
+ }
3198
+ } catch {
3199
+ continue;
3200
+ }
3201
+ }
3202
+ }
3203
+ }
3204
+ for (const line of stack) {
3205
+ const match = /\((.*):(\d+):(\d+)\)/.exec(line) || /at (.*):(\d+):(\d+)/.exec(line);
3206
+ if (match?.[1]) {
3207
+ const filePath = match[1];
3208
+ if (filePath.includes("node_modules")) {
3209
+ try {
3210
+ const nodeModulesMatch = filePath.match(/(.+\/node_modules\/(?:@[^\/]+\/[^\/]+|[^\/]+))/);
3211
+ if (nodeModulesMatch?.[1]) {
3212
+ const packageDir = nodeModulesMatch[1];
3213
+ const packageJsonPath = resolve(packageDir, "package.json");
3214
+ if (await fs.pathExists(packageJsonPath)) {
3215
+ if (process$1.env.NODE_ENV === "development") {
3216
+ relinka("verbose", `Found command package in node_modules: ${packageDir}`);
3217
+ }
3218
+ return packageDir;
3219
+ }
3154
3220
  }
3155
3221
  } catch {
3156
3222
  continue;
@@ -3163,33 +3229,18 @@ const getCallerDirectory = async () => {
3163
3229
  try {
3164
3230
  const packageJsonPath = resolve(currentDir, "package.json");
3165
3231
  if (await fs.pathExists(packageJsonPath)) {
3166
- relinka("verbose", `Found package.json at: ${currentDir}`);
3232
+ if (process$1.env.NODE_ENV === "development") {
3233
+ relinka("verbose", `Found package.json at: ${currentDir}`);
3234
+ }
3167
3235
  return currentDir;
3168
3236
  }
3169
3237
  } catch {
3170
3238
  }
3171
3239
  currentDir = dirname(currentDir);
3172
3240
  }
3173
- try {
3174
- const possibleCommandPaths = [
3175
- resolve(cwd, "src", "build", "cmd.ts"),
3176
- resolve(cwd, "src", "build", "cmd.js"),
3177
- resolve(cwd, "src-ts", "build", "cmd.ts"),
3178
- resolve(cwd, "src-ts", "build", "cmd.js"),
3179
- resolve(cwd, "build", "cmd.ts"),
3180
- resolve(cwd, "build", "cmd.js"),
3181
- resolve(cwd, "app", "build", "cmd.ts"),
3182
- resolve(cwd, "app", "build", "cmd.js")
3183
- ];
3184
- for (const path of possibleCommandPaths) {
3185
- if (await fs.pathExists(path)) {
3186
- relinka("verbose", `Found command file at: ${path}, using cwd: ${cwd}`);
3187
- return cwd;
3188
- }
3189
- }
3190
- } catch {
3241
+ if (process$1.env.NODE_ENV === "development") {
3242
+ relinka("verbose", `No suitable caller found, using cwd: ${cwd}`);
3191
3243
  }
3192
- relinka("verbose", `No suitable caller found, using cwd: ${cwd}`);
3193
3244
  return process$1.cwd();
3194
3245
  };
3195
3246
  const tryLoadCommand = async (path) => {
@@ -3211,6 +3262,69 @@ const generateCandidatePaths = async (resolvedPath) => {
3211
3262
  }
3212
3263
  return [resolvedPath];
3213
3264
  };
3265
+ const generateAlternativePaths = async (cmdPath, callerDir) => {
3266
+ const normalizedCmdPath = cmdPath.replace(/^\.\//, "");
3267
+ const paths = [];
3268
+ const commonCommandLocations = [
3269
+ // Direct command file
3270
+ resolve(callerDir, `${normalizedCmdPath}.ts`),
3271
+ resolve(callerDir, `${normalizedCmdPath}.js`),
3272
+ // Command in cmd subdirectory
3273
+ resolve(callerDir, normalizedCmdPath, "cmd.ts"),
3274
+ resolve(callerDir, normalizedCmdPath, "cmd.js"),
3275
+ // Command in app subdirectory
3276
+ resolve(callerDir, "app", normalizedCmdPath, "cmd.ts"),
3277
+ resolve(callerDir, "app", normalizedCmdPath, "cmd.js"),
3278
+ // Command in src subdirectory
3279
+ resolve(callerDir, "src", normalizedCmdPath, "cmd.ts"),
3280
+ resolve(callerDir, "src", normalizedCmdPath, "cmd.js"),
3281
+ // Command in src/app subdirectory
3282
+ resolve(callerDir, "src", "app", normalizedCmdPath, "cmd.ts"),
3283
+ resolve(callerDir, "src", "app", normalizedCmdPath, "cmd.js"),
3284
+ // Command in src-ts subdirectory
3285
+ resolve(callerDir, "src-ts", normalizedCmdPath, "cmd.ts"),
3286
+ resolve(callerDir, "src-ts", normalizedCmdPath, "cmd.js"),
3287
+ // Command in src-ts/app subdirectory (for dler-like structures)
3288
+ resolve(callerDir, "src-ts", "app", normalizedCmdPath, "cmd.ts"),
3289
+ resolve(callerDir, "src-ts", "app", normalizedCmdPath, "cmd.js"),
3290
+ // Command in lib subdirectory
3291
+ resolve(callerDir, "lib", normalizedCmdPath, "cmd.ts"),
3292
+ resolve(callerDir, "lib", normalizedCmdPath, "cmd.js"),
3293
+ // Command in lib/app subdirectory
3294
+ resolve(callerDir, "lib", "app", normalizedCmdPath, "cmd.ts"),
3295
+ resolve(callerDir, "lib", "app", normalizedCmdPath, "cmd.js"),
3296
+ // Command in dist subdirectory (compiled)
3297
+ resolve(callerDir, "dist", normalizedCmdPath, "cmd.js"),
3298
+ resolve(callerDir, "dist", "app", normalizedCmdPath, "cmd.js"),
3299
+ // Command in bin subdirectory
3300
+ resolve(callerDir, "bin", normalizedCmdPath, "cmd.ts"),
3301
+ resolve(callerDir, "bin", normalizedCmdPath, "cmd.js"),
3302
+ // Command in bin/app subdirectory (common for CLI tools)
3303
+ resolve(callerDir, "bin", "app", normalizedCmdPath, "cmd.ts"),
3304
+ resolve(callerDir, "bin", "app", normalizedCmdPath, "cmd.js"),
3305
+ // Command in commands subdirectory
3306
+ resolve(callerDir, "commands", normalizedCmdPath, "cmd.ts"),
3307
+ resolve(callerDir, "commands", normalizedCmdPath, "cmd.js"),
3308
+ // Command in cli subdirectory
3309
+ resolve(callerDir, "cli", normalizedCmdPath, "cmd.ts"),
3310
+ resolve(callerDir, "cli", normalizedCmdPath, "cmd.js"),
3311
+ // Command in cli/commands subdirectory
3312
+ resolve(callerDir, "cli", "commands", normalizedCmdPath, "cmd.ts"),
3313
+ resolve(callerDir, "cli", "commands", normalizedCmdPath, "cmd.js"),
3314
+ // Command in tools subdirectory
3315
+ resolve(callerDir, "tools", normalizedCmdPath, "cmd.ts"),
3316
+ resolve(callerDir, "tools", normalizedCmdPath, "cmd.js"),
3317
+ // Command in scripts subdirectory
3318
+ resolve(callerDir, "scripts", normalizedCmdPath, "cmd.ts"),
3319
+ resolve(callerDir, "scripts", normalizedCmdPath, "cmd.js")
3320
+ ];
3321
+ for (const path of commonCommandLocations) {
3322
+ if (await fs.pathExists(path)) {
3323
+ paths.push(path);
3324
+ }
3325
+ }
3326
+ return paths;
3327
+ };
3214
3328
  const createCommandNotFoundError = (cmdPath, searchedPaths) => new Error(
3215
3329
  `No command file found for "${cmdPath}". Expected to find either:
3216
3330
  - A valid command file at the specified path
@@ -3254,7 +3368,24 @@ async function loadCommand(cmdPath) {
3254
3368
  return command;
3255
3369
  }
3256
3370
  }
3257
- throw createCommandNotFoundError(cmdPath, candidatePaths);
3371
+ let allSearchedPaths = [...candidatePaths];
3372
+ if (callerDir !== process$1.cwd()) {
3373
+ const alternativePaths = await generateAlternativePaths(cmdPath, callerDir);
3374
+ allSearchedPaths.push(...alternativePaths);
3375
+ if (process$1.env.NODE_ENV === "development") {
3376
+ relinka("verbose", `Trying alternative paths: ${alternativePaths.join(", ")}`);
3377
+ }
3378
+ for (const path of alternativePaths) {
3379
+ const command = await tryLoadCommand(path);
3380
+ if (command) {
3381
+ if (process$1.env.NODE_ENV === "development") {
3382
+ relinka("verbose", `Successfully loaded command from alternative path: ${path}`);
3383
+ }
3384
+ return command;
3385
+ }
3386
+ }
3387
+ }
3388
+ throw createCommandNotFoundError(cmdPath, allSearchedPaths);
3258
3389
  } catch (error) {
3259
3390
  if (error instanceof Error && error.message.includes("No command file found")) {
3260
3391
  throw error;
@@ -6322,8 +6453,9 @@ function argsToStringArray(args) {
6322
6453
  }
6323
6454
  async function createCallCmd() {
6324
6455
  return async function callCmd(cmdName, args) {
6456
+ const originalCaller = await getOriginalCallerDirectory();
6325
6457
  try {
6326
- const command = await loadCommand(cmdName);
6458
+ const command = await loadCommandWithCaller(cmdName, originalCaller);
6327
6459
  const stringArgs = args ? argsToStringArray(args) : [];
6328
6460
  await runCmd(command, stringArgs);
6329
6461
  } catch (error) {
@@ -6334,7 +6466,8 @@ async function createCallCmd() {
6334
6466
  }
6335
6467
  async function createGetTypedCmd() {
6336
6468
  return async function getTypedCmd(cmdName) {
6337
- const command = await loadCommand(cmdName);
6469
+ const originalCaller = await getOriginalCallerDirectory();
6470
+ const command = await loadCommandWithCaller(cmdName, originalCaller);
6338
6471
  return {
6339
6472
  command,
6340
6473
  run: async (args) => {
@@ -6345,8 +6478,9 @@ async function createGetTypedCmd() {
6345
6478
  };
6346
6479
  }
6347
6480
  async function callCmdImpl(cmdName, args) {
6481
+ const originalCaller = await getOriginalCallerDirectory();
6348
6482
  try {
6349
- const command = await loadCommand(cmdName);
6483
+ const command = await loadCommandWithCaller(cmdName, originalCaller);
6350
6484
  const stringArgs = args ? argsToStringArray(args) : [];
6351
6485
  await runCmd(command, stringArgs);
6352
6486
  } catch (error) {
@@ -6354,8 +6488,89 @@ async function callCmdImpl(cmdName, args) {
6354
6488
  throw error;
6355
6489
  }
6356
6490
  }
6491
+ async function getOriginalCallerDirectory() {
6492
+ const stack = new Error().stack?.split("\n") ?? [];
6493
+ const cwd = process.cwd();
6494
+ for (const line of stack) {
6495
+ const match = /\((.*):(\d+):(\d+)\)/.exec(line) || /at (.*):(\d+):(\d+)/.exec(line);
6496
+ if (match?.[1]) {
6497
+ const filePath = match[1];
6498
+ if (!filePath.includes("node_modules") && !filePath.includes("command-runner") && !filePath.includes("command-typed") && // Skip this current file
6499
+ !filePath.includes("launcher-mod") && !filePath.includes("launcher-types") && !filePath.endsWith("mod.mjs") && !filePath.endsWith("mod.js") && !filePath.endsWith("mod.ts")) {
6500
+ try {
6501
+ const fileDir = dirname(filePath);
6502
+ let currentDir = fileDir;
6503
+ while (currentDir !== dirname(currentDir)) {
6504
+ try {
6505
+ const packageJsonPath = resolve(currentDir, "package.json");
6506
+ if (await fs.pathExists(packageJsonPath)) {
6507
+ const cwdPackageJsonPath = resolve(cwd, "package.json");
6508
+ if (await fs.pathExists(cwdPackageJsonPath)) {
6509
+ try {
6510
+ const callerPackage = JSON.parse(await fs.readFile(packageJsonPath, "utf-8"));
6511
+ const cwdPackage = JSON.parse(await fs.readFile(cwdPackageJsonPath, "utf-8"));
6512
+ if (callerPackage.name !== cwdPackage.name) {
6513
+ return currentDir;
6514
+ }
6515
+ } catch {
6516
+ if (resolve(currentDir) !== resolve(cwd)) {
6517
+ return currentDir;
6518
+ }
6519
+ }
6520
+ } else {
6521
+ return currentDir;
6522
+ }
6523
+ break;
6524
+ }
6525
+ } catch {
6526
+ }
6527
+ currentDir = dirname(currentDir);
6528
+ }
6529
+ } catch {
6530
+ continue;
6531
+ }
6532
+ }
6533
+ }
6534
+ }
6535
+ for (const line of stack) {
6536
+ const match = /\((.*):(\d+):(\d+)\)/.exec(line) || /at (.*):(\d+):(\d+)/.exec(line);
6537
+ if (match?.[1]) {
6538
+ const filePath = match[1];
6539
+ if (filePath.includes("node_modules")) {
6540
+ try {
6541
+ const nodeModulesMatch = filePath.match(/(.+\/node_modules\/(?:@[^\/]+\/[^\/]+|[^\/]+))/);
6542
+ if (nodeModulesMatch?.[1]) {
6543
+ const packageDir = nodeModulesMatch[1];
6544
+ const packageJsonPath = resolve(packageDir, "package.json");
6545
+ if (await fs.pathExists(packageJsonPath)) {
6546
+ return packageDir;
6547
+ }
6548
+ }
6549
+ } catch {
6550
+ continue;
6551
+ }
6552
+ }
6553
+ }
6554
+ }
6555
+ return cwd;
6556
+ }
6557
+ async function loadCommandWithCaller(cmdPath, callerDir) {
6558
+ const originalEnvVar = process.env._REMPTS_CALLER_DIR;
6559
+ process.env._REMPTS_CALLER_DIR = callerDir;
6560
+ try {
6561
+ const command = await loadCommand(cmdPath);
6562
+ return command;
6563
+ } finally {
6564
+ if (originalEnvVar !== void 0) {
6565
+ process.env._REMPTS_CALLER_DIR = originalEnvVar;
6566
+ } else {
6567
+ delete process.env._REMPTS_CALLER_DIR;
6568
+ }
6569
+ }
6570
+ }
6357
6571
  async function getTypedCmdImpl(cmdName) {
6358
- const command = await loadCommand(cmdName);
6572
+ const originalCaller = await getOriginalCallerDirectory();
6573
+ const command = await loadCommandWithCaller(cmdName, originalCaller);
6359
6574
  return {
6360
6575
  command,
6361
6576
  run: async (args) => {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@reliverse/rempts",
3
3
  "author": "reliverse",
4
- "version": "1.7.46",
4
+ "version": "1.7.48",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "main": "./dist-npm/bin/mod.mjs",