@wwkit/shared 1.0.11 → 1.0.12
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/package.json +1 -1
- package/src/config-loader.js +16 -20
package/package.json
CHANGED
package/src/config-loader.js
CHANGED
|
@@ -3,13 +3,11 @@ import path from 'node:path'
|
|
|
3
3
|
import os from 'node:os'
|
|
4
4
|
import process from 'node:process'
|
|
5
5
|
import { fileURLToPath } from 'node:url'
|
|
6
|
-
import { createRequire } from 'node:module'
|
|
7
6
|
import JSON5 from 'json5'
|
|
8
7
|
|
|
9
8
|
const ENV_PATTERN = /\{env:(\w+)\}/g
|
|
10
9
|
const PKG_PATTERN = /\{pkg:([^}]+)\}/g
|
|
11
10
|
|
|
12
|
-
const _require = createRequire(import.meta.url)
|
|
13
11
|
const _selfDir = path.dirname(fileURLToPath(import.meta.url))
|
|
14
12
|
|
|
15
13
|
/**
|
|
@@ -35,29 +33,27 @@ function findWorkspaceRoot(startDir) {
|
|
|
35
33
|
|
|
36
34
|
/**
|
|
37
35
|
* 解析 npm 包的安装目录路径
|
|
38
|
-
*
|
|
36
|
+
*
|
|
37
|
+
* 从当前文件位置向外逐级查找 node_modules 目录,在其中定位目标包。
|
|
38
|
+
* {pkg:XX} 的目标包必然与当前包在同一个 node_modules 空间下,
|
|
39
|
+
* 因此只需向外找到最近的 node_modules 即可命中。
|
|
40
|
+
* 开发环境下回退到工作区 packages/ 目录。
|
|
41
|
+
*
|
|
39
42
|
* @param {string} pkgName - 包名,如 @wwkit/harness
|
|
40
43
|
* @returns {string} 目录路径,找不到时返回空串
|
|
41
44
|
*/
|
|
42
45
|
function resolvePkgPath(pkgName) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
const pkgJson = path.join(dir, 'package.json')
|
|
52
|
-
if (fs.existsSync(pkgJson)) {
|
|
53
|
-
try {
|
|
54
|
-
const pkg = JSON.parse(fs.readFileSync(pkgJson, 'utf8'))
|
|
55
|
-
if (pkg.name === pkgName) return dir
|
|
56
|
-
} catch {}
|
|
57
|
-
}
|
|
58
|
-
dir = path.dirname(dir)
|
|
46
|
+
let dir = _selfDir
|
|
47
|
+
while (dir !== path.dirname(dir)) {
|
|
48
|
+
const pkgJsonPath = path.join(dir, 'node_modules', pkgName, 'package.json')
|
|
49
|
+
if (fs.existsSync(pkgJsonPath)) {
|
|
50
|
+
try {
|
|
51
|
+
const pkg = JSON.parse(fs.readFileSync(pkgJsonPath, 'utf8'))
|
|
52
|
+
if (pkg.name === pkgName) return path.dirname(pkgJsonPath)
|
|
53
|
+
} catch {}
|
|
59
54
|
}
|
|
60
|
-
|
|
55
|
+
dir = path.dirname(dir)
|
|
56
|
+
}
|
|
61
57
|
const wsRoot = findWorkspaceRoot(_selfDir)
|
|
62
58
|
if (wsRoot) {
|
|
63
59
|
const seg = pkgName.includes('/') ? pkgName.split('/').pop() : pkgName
|