@wwkit/cft 1.0.11 → 1.0.13
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/bin/index.js +7 -1
- package/package.json +2 -2
- package/scripts/postinstall.js +32 -1
- package/src/config.js +6 -0
- package/src/env.js +84 -0
- package/src/index.js +2 -1
package/bin/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
import { install, status, clear } from '../src/index.js'
|
|
3
|
+
import { install, status, clear, showConfig, showEnv } from '../src/index.js'
|
|
4
4
|
|
|
5
5
|
const command = process.argv[2]
|
|
6
6
|
|
|
@@ -21,12 +21,18 @@ if (command === 'install') {
|
|
|
21
21
|
} else if (command === 'clear') {
|
|
22
22
|
clear()
|
|
23
23
|
process.exit(0)
|
|
24
|
+
} else if (command === 'config') {
|
|
25
|
+
showConfig()
|
|
26
|
+
} else if (command === 'env') {
|
|
27
|
+
showEnv()
|
|
24
28
|
} else if (command === 'help' || command === undefined) {
|
|
25
29
|
console.log(`cft - Chrome for Testing 安装器
|
|
26
30
|
|
|
27
31
|
用法:
|
|
28
32
|
cft install 下载并安装 Chrome for Testing(已装同版本则跳过)
|
|
29
33
|
cft status 显示安装状态
|
|
34
|
+
cft config 显示用户配置(~/.config/cft/config.json5)
|
|
35
|
+
cft env 显示 cft 创建的全局环境变量
|
|
30
36
|
cft clear 删除安装产物(~/.config/cft/)
|
|
31
37
|
cft help 显示本帮助
|
|
32
38
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wwkit/cft",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.13",
|
|
4
4
|
"author": "bluesliu <langcai163@163.com>",
|
|
5
5
|
"description": "Chrome for Testing (CFT) installer — download, extract, chmod, dep-check",
|
|
6
6
|
"type": "module",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"extract-zip": "^2.0.1",
|
|
39
|
-
"@wwkit/shared": "1.0.
|
|
39
|
+
"@wwkit/shared": "1.0.13"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"jest": "^29.7.0"
|
package/scripts/postinstall.js
CHANGED
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
import { spawnSync } from 'node:child_process'
|
|
4
4
|
import path from 'node:path'
|
|
5
5
|
import { fileURLToPath } from 'node:url'
|
|
6
|
+
import { Shell } from '@wwkit/shared'
|
|
7
|
+
import { getLocation, getOs } from '../src/index.js'
|
|
6
8
|
|
|
7
9
|
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
|
8
10
|
const bin = path.join(__dirname, '..', 'bin', 'index.js')
|
|
@@ -11,4 +13,33 @@ const result = spawnSync('node', [bin, 'install'], { stdio: 'inherit' })
|
|
|
11
13
|
if (result.status !== 0) {
|
|
12
14
|
console.error('cft: postinstall (cft install) failed')
|
|
13
15
|
process.exit(1)
|
|
14
|
-
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// 参考 harness postinstall:将 cft 安装相关目录写入环境变量。
|
|
19
|
+
// 安装目录由 getXdgConfigDir('cft') 决定(~/.config/cft 或 $XDG_CONFIG_HOME/cft),
|
|
20
|
+
// 不走 config.json5,保持 location 不可配置的设计一致。
|
|
21
|
+
// chrome/driver 目录与 installer.js 的计算逻辑一致:
|
|
22
|
+
// chrome-${osType} / chromedriver-${osType}(osType 来自 getOs())。
|
|
23
|
+
// setEnvVar 自带 marker 块幂等:值相同则跳过,值变化则原地替换。
|
|
24
|
+
const installDir = getLocation()
|
|
25
|
+
const osType = getOs()
|
|
26
|
+
const chromeDir = path.join(installDir, `chrome-${osType}`)
|
|
27
|
+
const driverDir = path.join(installDir, `chromedriver-${osType}`)
|
|
28
|
+
const shell = new Shell()
|
|
29
|
+
|
|
30
|
+
function setEnv(name, value) {
|
|
31
|
+
try {
|
|
32
|
+
const wrote = shell.setEnvVar(name, value)
|
|
33
|
+
console.log(
|
|
34
|
+
wrote
|
|
35
|
+
? `cft: set ${name}=${value}`
|
|
36
|
+
: `cft: ${name} already set to ${value}`
|
|
37
|
+
)
|
|
38
|
+
} catch (e) {
|
|
39
|
+
console.warn(`cft: failed to set ${name} — ${e.message}`)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
setEnv('SELENIUM_CFT_DIR', installDir)
|
|
44
|
+
setEnv('SELENIUM_CFT_CHROME_DIR', chromeDir)
|
|
45
|
+
setEnv('SELENIUM_CFT_DRIVER_DIR', driverDir)
|
package/src/config.js
CHANGED
package/src/env.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import fs from 'node:fs'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
import process from 'node:process'
|
|
4
|
+
|
|
5
|
+
import { Shell } from '@wwkit/shared'
|
|
6
|
+
|
|
7
|
+
import { getLocation } from './config.js'
|
|
8
|
+
import { getOs } from './platform.js'
|
|
9
|
+
|
|
10
|
+
const CFT_ENV_VARS = [
|
|
11
|
+
'SELENIUM_CFT_DIR',
|
|
12
|
+
'SELENIUM_CFT_CHROME_DIR',
|
|
13
|
+
'SELENIUM_CFT_DRIVER_DIR',
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 计算 cft 期望写入的环境变量值(与 postinstall 逻辑一致)。
|
|
18
|
+
*/
|
|
19
|
+
function expectedValues() {
|
|
20
|
+
const installDir = getLocation()
|
|
21
|
+
const osType = getOs()
|
|
22
|
+
return {
|
|
23
|
+
SELENIUM_CFT_DIR: installDir,
|
|
24
|
+
SELENIUM_CFT_CHROME_DIR: path.join(installDir, `chrome-${osType}`),
|
|
25
|
+
SELENIUM_CFT_DRIVER_DIR: path.join(installDir, `chromedriver-${osType}`),
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 从 shell profile 文件中提取 cft 实际写入的环境变量值。
|
|
31
|
+
* 遍历 Shell.getProfiles() 返回的所有 profile,解析 wwkit marker 块。
|
|
32
|
+
*/
|
|
33
|
+
function profileValues() {
|
|
34
|
+
const shell = new Shell()
|
|
35
|
+
const result = {}
|
|
36
|
+
for (const { path: profilePath } of shell.getProfiles()) {
|
|
37
|
+
let content = ''
|
|
38
|
+
try {
|
|
39
|
+
content = fs.readFileSync(profilePath, 'utf8')
|
|
40
|
+
} catch {
|
|
41
|
+
continue
|
|
42
|
+
}
|
|
43
|
+
for (const name of CFT_ENV_VARS) {
|
|
44
|
+
if (result[name]) continue
|
|
45
|
+
const beginMarker = `# >>> wwkit ${name} >>>`
|
|
46
|
+
const endMarker = `# <<< wwkit ${name} <<<`
|
|
47
|
+
const beginIdx = content.indexOf(beginMarker)
|
|
48
|
+
if (beginIdx === -1) continue
|
|
49
|
+
const endIdx = content.indexOf(endMarker, beginIdx)
|
|
50
|
+
if (endIdx === -1) continue
|
|
51
|
+
const block = content.slice(beginIdx, endIdx)
|
|
52
|
+
// 提取引号内的值(兼容 export NAME="val" 与 fish set -gx NAME "val")
|
|
53
|
+
const m = block.match(/"([^"]*)"/)
|
|
54
|
+
if (m) {
|
|
55
|
+
result[name] = { value: m[1], profile: profilePath }
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return result
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 显示 cft 创建的全局环境变量:期望值、profile 实际写入值、当前 shell 值。
|
|
64
|
+
*/
|
|
65
|
+
export function showEnv() {
|
|
66
|
+
const expected = expectedValues()
|
|
67
|
+
const profVals = profileValues()
|
|
68
|
+
console.log('cft 管理的环境变量(由 postinstall 写入 shell profile):\n')
|
|
69
|
+
for (const name of CFT_ENV_VARS) {
|
|
70
|
+
const exp = expected[name]
|
|
71
|
+
const prof = profVals[name]
|
|
72
|
+
const env = process.env[name] || ''
|
|
73
|
+
console.log(` ${name}`)
|
|
74
|
+
console.log(` 期望值: ${exp}`)
|
|
75
|
+
if (prof) {
|
|
76
|
+
console.log(` profile 写入: ${prof.value}`)
|
|
77
|
+
console.log(` ← ${prof.profile}`)
|
|
78
|
+
} else {
|
|
79
|
+
console.log(` profile 写入: (未写入,请运行 cft install 或重新安装包)`)
|
|
80
|
+
}
|
|
81
|
+
console.log(` 当前 shell: ${env || '(未设置 — 请重新打开终端或 source 你的 profile)'}`)
|
|
82
|
+
console.log()
|
|
83
|
+
}
|
|
84
|
+
}
|
package/src/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export { install, status, clear } from './installer.js'
|
|
2
2
|
export { getOs, isWindows, isMacos, isLinux, VALID_OS_TYPES } from './platform.js'
|
|
3
|
-
export { getConfig, getVersion, getRegistry, getProxy, getLocation, copyBuiltinConfig, INSTALL_LOCATION, USER_CONFIG_FILE } from './config.js'
|
|
3
|
+
export { getConfig, getVersion, getRegistry, getProxy, getLocation, copyBuiltinConfig, INSTALL_LOCATION, USER_CONFIG_FILE, showConfig } from './config.js'
|
|
4
|
+
export { showEnv } from './env.js'
|