@reliverse/rempts 1.7.47 → 1.7.49

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 +160 -72
  2. package/package.json +1 -1
@@ -3123,9 +3123,9 @@ const getCallerDirectory = async () => {
3123
3123
  const stack = new Error().stack?.split("\n") ?? [];
3124
3124
  const cwd = process$1.cwd();
3125
3125
  if (process$1.env.NODE_ENV === "development") {
3126
- relinka("verbose", "Stack trace for getCallerDirectory:");
3126
+ relinka("log", "Stack trace for getCallerDirectory:");
3127
3127
  stack.forEach((line, index) => {
3128
- relinka("verbose", ` [${index}]: ${line.trim()}`);
3128
+ relinka("log", ` [${index}]: ${line.trim()}`);
3129
3129
  });
3130
3130
  }
3131
3131
  for (const line of stack) {
@@ -3133,42 +3133,83 @@ const getCallerDirectory = async () => {
3133
3133
  if (match?.[1]) {
3134
3134
  const filePath = match[1];
3135
3135
  if (process$1.env.NODE_ENV === "development") {
3136
- relinka("verbose", `Checking file path: ${filePath}`);
3136
+ relinka("log", `Checking file path: ${filePath}`);
3137
3137
  }
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")) {
3138
+ 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
3139
+ !filePath.endsWith("mod.js") && // Skip compiled output
3140
+ !filePath.endsWith("mod.ts")) {
3141
3141
  try {
3142
3142
  const fileDir = dirname(filePath);
3143
+ let currentDir2 = fileDir;
3144
+ let packageRoot = null;
3145
+ while (currentDir2 !== dirname(currentDir2)) {
3146
+ try {
3147
+ const packageJsonPath = resolve(currentDir2, "package.json");
3148
+ if (await fs.pathExists(packageJsonPath)) {
3149
+ packageRoot = currentDir2;
3150
+ if (process$1.env.NODE_ENV === "development") {
3151
+ relinka("log", `Found package.json at: ${packageRoot}`);
3152
+ }
3153
+ const cwdPackageJsonPath = resolve(cwd, "package.json");
3154
+ if (await fs.pathExists(cwdPackageJsonPath)) {
3155
+ try {
3156
+ const callerPackage = JSON.parse(await fs.readFile(packageJsonPath, "utf-8"));
3157
+ const cwdPackage = JSON.parse(await fs.readFile(cwdPackageJsonPath, "utf-8"));
3158
+ if (callerPackage.name !== cwdPackage.name || callerPackage.version !== cwdPackage.version) {
3159
+ if (process$1.env.NODE_ENV === "development") {
3160
+ relinka("log", `Using caller package root: ${packageRoot} (${callerPackage.name}@${callerPackage.version})`);
3161
+ }
3162
+ return packageRoot;
3163
+ }
3164
+ } catch {
3165
+ if (resolve(packageRoot) !== resolve(cwd)) {
3166
+ if (process$1.env.NODE_ENV === "development") {
3167
+ relinka("log", `Using caller package root (different path): ${packageRoot}`);
3168
+ }
3169
+ return packageRoot;
3170
+ }
3171
+ }
3172
+ } else {
3173
+ if (process$1.env.NODE_ENV === "development") {
3174
+ relinka("log", `Using caller package root (no CWD package.json): ${packageRoot}`);
3175
+ }
3176
+ return packageRoot;
3177
+ }
3178
+ break;
3179
+ }
3180
+ } catch {
3181
+ }
3182
+ currentDir2 = dirname(currentDir2);
3183
+ }
3143
3184
  const resolvedFileDir = resolve(fileDir);
3144
3185
  const resolvedCwd = resolve(cwd);
3145
- if (process$1.env.NODE_ENV === "development") {
3146
- relinka("verbose", `File dir: ${fileDir}, resolved: ${resolvedFileDir}, cwd: ${resolvedCwd}`);
3147
- }
3148
3186
  if (resolvedFileDir !== resolvedCwd && !resolvedFileDir.startsWith(resolvedCwd)) {
3149
- relinka("verbose", `Using caller directory: ${fileDir}`);
3150
- return fileDir;
3187
+ if (process$1.env.NODE_ENV === "development") {
3188
+ relinka("log", `Using caller directory (different from CWD): ${fileDir}`);
3189
+ }
3190
+ return packageRoot || fileDir;
3151
3191
  }
3152
- if (resolvedFileDir === resolvedCwd) {
3153
- let currentDir2 = fileDir;
3154
- while (currentDir2 !== dirname(currentDir2)) {
3155
- try {
3156
- const packageJsonPath = resolve(currentDir2, "package.json");
3157
- if (await fs.pathExists(packageJsonPath)) {
3158
- const cwdPackageJsonPath = resolve(cwd, "package.json");
3159
- if (await fs.pathExists(cwdPackageJsonPath)) {
3160
- const callerPackageJson = await fs.readFile(packageJsonPath, "utf-8");
3161
- const cwdPackageJson = await fs.readFile(cwdPackageJsonPath, "utf-8");
3162
- if (callerPackageJson !== cwdPackageJson) {
3163
- relinka("verbose", `Found different package.json at: ${currentDir2}, using caller directory: ${fileDir}`);
3164
- return fileDir;
3165
- }
3166
- }
3167
- break;
3168
- }
3169
- } catch {
3192
+ } catch {
3193
+ continue;
3194
+ }
3195
+ }
3196
+ }
3197
+ }
3198
+ for (const line of stack) {
3199
+ const match = /\((.*):(\d+):(\d+)\)/.exec(line) || /at (.*):(\d+):(\d+)/.exec(line);
3200
+ if (match?.[1]) {
3201
+ const filePath = match[1];
3202
+ if (filePath.includes("node_modules")) {
3203
+ try {
3204
+ const nodeModulesMatch = filePath.match(/(.+\/node_modules\/[^\/]+)/);
3205
+ if (nodeModulesMatch?.[1]) {
3206
+ const packageDir = nodeModulesMatch[1];
3207
+ const packageJsonPath = resolve(packageDir, "package.json");
3208
+ if (await fs.pathExists(packageJsonPath)) {
3209
+ if (process$1.env.NODE_ENV === "development") {
3210
+ relinka("log", `Found command package in node_modules: ${packageDir}`);
3170
3211
  }
3171
- currentDir2 = dirname(currentDir2);
3212
+ return packageDir;
3172
3213
  }
3173
3214
  }
3174
3215
  } catch {
@@ -3182,34 +3223,40 @@ const getCallerDirectory = async () => {
3182
3223
  try {
3183
3224
  const packageJsonPath = resolve(currentDir, "package.json");
3184
3225
  if (await fs.pathExists(packageJsonPath)) {
3185
- relinka("verbose", `Found package.json at: ${currentDir}`);
3226
+ if (process$1.env.NODE_ENV === "development") {
3227
+ relinka("log", `Found package.json at: ${currentDir}`);
3228
+ }
3186
3229
  return currentDir;
3187
3230
  }
3188
3231
  } catch {
3189
3232
  }
3190
3233
  currentDir = dirname(currentDir);
3191
3234
  }
3192
- try {
3193
- const possibleCommandPaths = [
3194
- resolve(cwd, "src", "build", "cmd.ts"),
3195
- resolve(cwd, "src", "build", "cmd.js"),
3196
- resolve(cwd, "src-ts", "build", "cmd.ts"),
3197
- resolve(cwd, "src-ts", "build", "cmd.js"),
3198
- resolve(cwd, "build", "cmd.ts"),
3199
- resolve(cwd, "build", "cmd.js"),
3200
- resolve(cwd, "app", "build", "cmd.ts"),
3201
- resolve(cwd, "app", "build", "cmd.js")
3202
- ];
3203
- for (const path of possibleCommandPaths) {
3204
- if (await fs.pathExists(path)) {
3205
- relinka("verbose", `Found command file at: ${path}, using cwd: ${cwd}`);
3206
- return cwd;
3235
+ if (process$1.env.NODE_ENV === "development") {
3236
+ relinka("log", `No suitable caller found, using cwd: ${cwd}`);
3237
+ }
3238
+ return process$1.cwd();
3239
+ };
3240
+ const getNodeModulesPackageDirsFromStack = async () => {
3241
+ const stack = new Error().stack?.split("\n") ?? [];
3242
+ const packageDirs = /* @__PURE__ */ new Set();
3243
+ for (const line of stack) {
3244
+ const match = /\((.*):(\d+):(\d+)\)/.exec(line) || /at (.*):(\d+):(\d+)/.exec(line);
3245
+ if (!match?.[1]) continue;
3246
+ const filePath = match[1];
3247
+ if (!filePath.includes("node_modules")) continue;
3248
+ try {
3249
+ const nodeModulesMatch = filePath.match(/(.+\\node_modules\\(?:@[^\\/]+\\)?[^\\/]+)|(.+\/node_modules\/(?:@[^\/]+\/)?[^\/]+)/);
3250
+ const packageDir = (nodeModulesMatch?.[1] || nodeModulesMatch?.[2])?.trim();
3251
+ if (!packageDir) continue;
3252
+ const packageJsonPath = resolve(packageDir, "package.json");
3253
+ if (await fs.pathExists(packageJsonPath)) {
3254
+ packageDirs.add(packageDir);
3207
3255
  }
3256
+ } catch {
3208
3257
  }
3209
- } catch {
3210
3258
  }
3211
- relinka("verbose", `No suitable caller found, using cwd: ${cwd}`);
3212
- return process$1.cwd();
3259
+ return Array.from(packageDirs);
3213
3260
  };
3214
3261
  const tryLoadCommand = async (path) => {
3215
3262
  if (!await fs.pathExists(path)) return null;
@@ -3217,7 +3264,7 @@ const tryLoadCommand = async (path) => {
3217
3264
  const cmd = await jiti.import(path, { default: true });
3218
3265
  return cmd;
3219
3266
  } catch {
3220
- relinka("verbose", `Failed to load ${path} as a command file`);
3267
+ relinka("log", `Failed to load ${path} as a command file`);
3221
3268
  return null;
3222
3269
  }
3223
3270
  };
@@ -3246,15 +3293,45 @@ const generateAlternativePaths = async (cmdPath, callerDir) => {
3246
3293
  // Command in src subdirectory
3247
3294
  resolve(callerDir, "src", normalizedCmdPath, "cmd.ts"),
3248
3295
  resolve(callerDir, "src", normalizedCmdPath, "cmd.js"),
3296
+ // Command in src/app subdirectory
3297
+ resolve(callerDir, "src", "app", normalizedCmdPath, "cmd.ts"),
3298
+ resolve(callerDir, "src", "app", normalizedCmdPath, "cmd.js"),
3249
3299
  // Command in src-ts subdirectory
3250
3300
  resolve(callerDir, "src-ts", normalizedCmdPath, "cmd.ts"),
3251
3301
  resolve(callerDir, "src-ts", normalizedCmdPath, "cmd.js"),
3302
+ // Command in src-ts/app subdirectory (for dler-like structures)
3303
+ resolve(callerDir, "src-ts", "app", normalizedCmdPath, "cmd.ts"),
3304
+ resolve(callerDir, "src-ts", "app", normalizedCmdPath, "cmd.js"),
3305
+ // Command in lib subdirectory
3306
+ resolve(callerDir, "lib", normalizedCmdPath, "cmd.ts"),
3307
+ resolve(callerDir, "lib", normalizedCmdPath, "cmd.js"),
3308
+ // Command in lib/app subdirectory
3309
+ resolve(callerDir, "lib", "app", normalizedCmdPath, "cmd.ts"),
3310
+ resolve(callerDir, "lib", "app", normalizedCmdPath, "cmd.js"),
3311
+ // Command in dist subdirectory (compiled)
3312
+ resolve(callerDir, "dist", normalizedCmdPath, "cmd.js"),
3313
+ resolve(callerDir, "dist", "app", normalizedCmdPath, "cmd.js"),
3252
3314
  // Command in bin subdirectory
3253
3315
  resolve(callerDir, "bin", normalizedCmdPath, "cmd.ts"),
3254
3316
  resolve(callerDir, "bin", normalizedCmdPath, "cmd.js"),
3255
3317
  // Command in bin/app subdirectory (common for CLI tools)
3256
3318
  resolve(callerDir, "bin", "app", normalizedCmdPath, "cmd.ts"),
3257
- resolve(callerDir, "bin", "app", normalizedCmdPath, "cmd.js")
3319
+ resolve(callerDir, "bin", "app", normalizedCmdPath, "cmd.js"),
3320
+ // Command in commands subdirectory
3321
+ resolve(callerDir, "commands", normalizedCmdPath, "cmd.ts"),
3322
+ resolve(callerDir, "commands", normalizedCmdPath, "cmd.js"),
3323
+ // Command in cli subdirectory
3324
+ resolve(callerDir, "cli", normalizedCmdPath, "cmd.ts"),
3325
+ resolve(callerDir, "cli", normalizedCmdPath, "cmd.js"),
3326
+ // Command in cli/commands subdirectory
3327
+ resolve(callerDir, "cli", "commands", normalizedCmdPath, "cmd.ts"),
3328
+ resolve(callerDir, "cli", "commands", normalizedCmdPath, "cmd.js"),
3329
+ // Command in tools subdirectory
3330
+ resolve(callerDir, "tools", normalizedCmdPath, "cmd.ts"),
3331
+ resolve(callerDir, "tools", normalizedCmdPath, "cmd.js"),
3332
+ // Command in scripts subdirectory
3333
+ resolve(callerDir, "scripts", normalizedCmdPath, "cmd.ts"),
3334
+ resolve(callerDir, "scripts", normalizedCmdPath, "cmd.js")
3258
3335
  ];
3259
3336
  for (const path of commonCommandLocations) {
3260
3337
  if (await fs.pathExists(path)) {
@@ -3288,40 +3365,51 @@ async function loadCommand(cmdPath) {
3288
3365
  const normalizedPath = cmdPath.replace(/^\.\//, "");
3289
3366
  const resolvedPath = resolve(callerDir, normalizedPath);
3290
3367
  if (process$1.env.NODE_ENV === "development") {
3291
- relinka("verbose", `Loading command: ${cmdPath}`);
3292
- relinka("verbose", `Caller directory: ${callerDir}`);
3293
- relinka("verbose", `Normalized path: ${normalizedPath}`);
3294
- relinka("verbose", `Resolved path: ${resolvedPath}`);
3295
- }
3296
- const candidatePaths = await generateCandidatePaths(resolvedPath);
3297
- if (process$1.env.NODE_ENV === "development") {
3298
- relinka("verbose", `Candidate paths: ${candidatePaths.join(", ")}`);
3299
- }
3300
- for (const path of candidatePaths) {
3301
- const command = await tryLoadCommand(path);
3302
- if (command) {
3303
- if (process$1.env.NODE_ENV === "development") {
3304
- relinka("verbose", `Successfully loaded command from: ${path}`);
3368
+ relinka("log", `Loading command: ${cmdPath}`);
3369
+ relinka("log", `Caller directory: ${callerDir}`);
3370
+ relinka("log", `Normalized path: ${normalizedPath}`);
3371
+ relinka("log", `Resolved path: ${resolvedPath}`);
3372
+ }
3373
+ const searchedPaths = [];
3374
+ const baseDirs = [callerDir];
3375
+ const stackPackageDirs = await getNodeModulesPackageDirsFromStack();
3376
+ for (const dir of stackPackageDirs) {
3377
+ if (!baseDirs.includes(dir)) baseDirs.push(dir);
3378
+ }
3379
+ for (const baseDir of baseDirs) {
3380
+ const alternativePaths = await generateAlternativePaths(cmdPath, baseDir);
3381
+ if (process$1.env.NODE_ENV === "development") {
3382
+ relinka("log", `Trying alternative paths in base ${baseDir}: ${alternativePaths.join(", ")}`);
3383
+ }
3384
+ searchedPaths.push(...alternativePaths);
3385
+ for (const path of alternativePaths) {
3386
+ const command = await tryLoadCommand(path);
3387
+ if (command) {
3388
+ if (process$1.env.NODE_ENV === "development") {
3389
+ relinka("log", `Successfully loaded command from alternative path: ${path}`);
3390
+ }
3391
+ return command;
3305
3392
  }
3306
- return command;
3307
3393
  }
3308
3394
  }
3309
- if (callerDir !== process$1.cwd()) {
3310
- const alternativePaths = await generateAlternativePaths(cmdPath, callerDir);
3395
+ const looksLikeExplicitPath = /[\\\/]|\.ts$|\.js$/.test(normalizedPath);
3396
+ if (looksLikeExplicitPath) {
3397
+ const candidatePaths = await generateCandidatePaths(resolvedPath);
3311
3398
  if (process$1.env.NODE_ENV === "development") {
3312
- relinka("verbose", `Trying alternative paths: ${alternativePaths.join(", ")}`);
3399
+ relinka("log", `Candidate paths: ${candidatePaths.join(", ")}`);
3313
3400
  }
3314
- for (const path of alternativePaths) {
3401
+ searchedPaths.push(...candidatePaths);
3402
+ for (const path of candidatePaths) {
3315
3403
  const command = await tryLoadCommand(path);
3316
3404
  if (command) {
3317
3405
  if (process$1.env.NODE_ENV === "development") {
3318
- relinka("verbose", `Successfully loaded command from alternative path: ${path}`);
3406
+ relinka("log", `Successfully loaded command from: ${path}`);
3319
3407
  }
3320
3408
  return command;
3321
3409
  }
3322
3410
  }
3323
3411
  }
3324
- throw createCommandNotFoundError(cmdPath, candidatePaths);
3412
+ throw createCommandNotFoundError(cmdPath, searchedPaths);
3325
3413
  } catch (error) {
3326
3414
  if (error instanceof Error && error.message.includes("No command file found")) {
3327
3415
  throw error;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@reliverse/rempts",
3
3
  "author": "reliverse",
4
- "version": "1.7.47",
4
+ "version": "1.7.49",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "main": "./dist-npm/bin/mod.mjs",