metame-cli 1.5.22 → 1.5.24

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.
@@ -0,0 +1,77 @@
1
+ 'use strict';
2
+
3
+ const fs = require('fs');
4
+ const path = require('path');
5
+ const os = require('os');
6
+ const Module = require('module');
7
+
8
+ function readRuntimeEnvFile(baseDir) {
9
+ const runtimeFile = path.join(baseDir, 'runtime-env.json');
10
+ try {
11
+ if (!fs.existsSync(runtimeFile)) return null;
12
+ return JSON.parse(fs.readFileSync(runtimeFile, 'utf8'));
13
+ } catch {
14
+ return null;
15
+ }
16
+ }
17
+
18
+ function collectNodeModuleCandidates(baseDir) {
19
+ const candidates = [];
20
+ const runtimeEnv = readRuntimeEnvFile(baseDir);
21
+ const metameRoot = String(process.env.METAME_ROOT || runtimeEnv?.metameRoot || '').trim();
22
+ const runtimeNodeModules = String(runtimeEnv?.nodeModules || '').trim();
23
+
24
+ if (runtimeNodeModules) candidates.push(runtimeNodeModules);
25
+ if (metameRoot) candidates.push(path.join(metameRoot, 'node_modules'));
26
+
27
+ candidates.push(path.join(baseDir, 'node_modules'));
28
+ candidates.push(path.resolve(baseDir, '..', 'node_modules'));
29
+
30
+ const home = process.env.HOME || process.env.USERPROFILE || os.homedir();
31
+ if (home) candidates.push(path.join(home, '.metame', 'node_modules'));
32
+
33
+ return [...new Set(candidates.filter(Boolean))];
34
+ }
35
+
36
+ function bootstrapRuntimeModulePaths(baseDir = __dirname) {
37
+ const runtimeEnv = readRuntimeEnvFile(baseDir);
38
+ if (!process.env.METAME_ROOT && runtimeEnv?.metameRoot) {
39
+ process.env.METAME_ROOT = runtimeEnv.metameRoot;
40
+ }
41
+
42
+ const existingNodePath = String(process.env.NODE_PATH || '')
43
+ .split(path.delimiter)
44
+ .map(p => p.trim())
45
+ .filter(Boolean);
46
+
47
+ let updated = false;
48
+ for (const candidate of collectNodeModuleCandidates(baseDir)) {
49
+ try {
50
+ if (!fs.existsSync(candidate) || !fs.statSync(candidate).isDirectory()) continue;
51
+ } catch {
52
+ continue;
53
+ }
54
+ if (!existingNodePath.includes(candidate)) {
55
+ existingNodePath.unshift(candidate);
56
+ updated = true;
57
+ }
58
+ if (!Module.globalPaths.includes(candidate)) {
59
+ Module.globalPaths.unshift(candidate);
60
+ updated = true;
61
+ }
62
+ }
63
+
64
+ if (updated) {
65
+ process.env.NODE_PATH = existingNodePath.join(path.delimiter);
66
+ if (typeof Module._initPaths === 'function') Module._initPaths();
67
+ }
68
+
69
+ return {
70
+ metameRoot: process.env.METAME_ROOT || '',
71
+ nodePath: process.env.NODE_PATH || '',
72
+ };
73
+ }
74
+
75
+ module.exports = {
76
+ bootstrapRuntimeModulePaths,
77
+ };