aaden-cli 1.6.108 → 1.6.109

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,144 @@
1
+ const fs = require('fs')
2
+ const os = require('os')
3
+ const http = require('http')
4
+ const https = require('https')
5
+
6
+ // do-log 顺带上报的 TSE 适配服务(TseHost)信息,用来在云端看车队的 TSE 版本和服务状态。
7
+ // WSL 门店的 TseHost 跑在 Windows 侧,网站目录里的 jar 不一定是实际在跑的那个,
8
+ // 所以和 PHP 一样按 config.ini 的 [TSE] TseHost 去访问服务,jar 也按服务配置去找。
9
+ // 每一项单独兜底:探测失败就报 null,不能让 do-log 本身挂掉。
10
+
11
+ const CONFIG_INI = '/var/www/html/Resource/config/config.ini'
12
+ const DEFAULT_TSE_HOST = 'http://localhost:8014'
13
+ // Linux:aaden-tse.service 执行 tseServing.sh,里面 java -jar 的那个 jar
14
+ const LINUX_SERVING_SCRIPT = '/var/www/html/TseHost/tseServing.sh'
15
+ const LINUX_DEFAULT_JAR = '/var/www/html/TseHost/tse-0.0.2.jar'
16
+ // Windows(WSL 门店):WinSW 服务目录,jar 名写在 AadenTseServiceRegister.xml 里
17
+ const WINDOWS_USERS_DIR = '/mnt/c/Users'
18
+ const WINDOWS_TSE_DIR = 'Desktop/AadenTseOnWindows'
19
+ const WINDOWS_DEFAULT_JAR = 'tse-0.0.3.jar'
20
+ // 和 PHP 的 Api.php 一样给 3 秒
21
+ const PROBE_TIMEOUT_MS = 3000
22
+
23
+ async function safe(fn, fallback = null) {
24
+ try {
25
+ const value = await fn()
26
+ return value === undefined ? fallback : value
27
+ } catch (e) {
28
+ return fallback
29
+ }
30
+ }
31
+
32
+ function readTseSection() {
33
+ const section = {}
34
+ let inTse = false
35
+ for (const raw of fs.readFileSync(CONFIG_INI, 'utf-8').split(/\r?\n/)) {
36
+ const line = raw.trim()
37
+ if (!line || line.startsWith(';') || line.startsWith('#')) continue
38
+ const header = line.match(/^\[(.+)]$/)
39
+ if (header) {
40
+ inTse = header[1].trim() === 'TSE'
41
+ continue
42
+ }
43
+ if (!inTse) continue
44
+ const eq = line.indexOf('=')
45
+ if (eq < 0) continue
46
+ section[line.slice(0, eq).trim()] = line.slice(eq + 1).trim().replace(/^"(.*)"$/, '$1')
47
+ }
48
+ return section
49
+ }
50
+
51
+ // 与 PHP 的 INI_SCANNER_TYPED 一致:true/on/yes 为真,数字按非 0 为真
52
+ function parseEnabled(value) {
53
+ if (value === undefined) return false
54
+ const v = String(value).trim().toLowerCase()
55
+ if (['true', 'on', 'yes'].includes(v)) return true
56
+ if (/^-?\d+$/.test(v)) return parseInt(v, 10) !== 0
57
+ return false
58
+ }
59
+
60
+ // 连不上(服务没起、Windows 侧没开、超时)统一当作 status 0
61
+ function httpGet(url) {
62
+ return new Promise(resolve => {
63
+ // 兜底:响应中途断掉时 end 不会触发,不能让 do-log 卡在这里
64
+ const timer = setTimeout(() => resolve({status: 0, body: ''}), PROBE_TIMEOUT_MS + 1000)
65
+ const done = result => {
66
+ clearTimeout(timer)
67
+ resolve(result)
68
+ }
69
+ try {
70
+ const client = url.startsWith('https:') ? https : http
71
+ const req = client.get(url, {timeout: PROBE_TIMEOUT_MS}, res => {
72
+ let body = ''
73
+ res.setEncoding('utf-8')
74
+ res.on('data', chunk => {
75
+ if (body.length < 4096) body += chunk
76
+ })
77
+ res.on('end', () => done({status: res.statusCode, body}))
78
+ res.on('error', () => done({status: 0, body: ''}))
79
+ })
80
+ req.on('timeout', () => req.destroy(new Error('timeout')))
81
+ req.on('error', () => done({status: 0, body: ''}))
82
+ } catch (e) {
83
+ done({status: 0, body: ''})
84
+ }
85
+ })
86
+ }
87
+
88
+ function servingJarPath() {
89
+ if (/microsoft|wsl/i.test(os.release()) && fs.existsSync(WINDOWS_USERS_DIR)) {
90
+ for (const user of fs.readdirSync(WINDOWS_USERS_DIR)) {
91
+ const dir = `${WINDOWS_USERS_DIR}/${user}/${WINDOWS_TSE_DIR}`
92
+ const xml = `${dir}/AadenTseServiceRegister.xml`
93
+ if (!fs.existsSync(xml)) continue
94
+ const match = fs.readFileSync(xml, 'utf-8').match(/-jar\s+"?%BASE%\\([^"\s]+\.jar)/i)
95
+ return `${dir}/${match ? match[1] : WINDOWS_DEFAULT_JAR}`
96
+ }
97
+ // 没找到 Windows 侧的服务目录:按 Linux 的装法找
98
+ }
99
+ if (!fs.existsSync(LINUX_SERVING_SCRIPT)) return LINUX_DEFAULT_JAR
100
+ const match = fs.readFileSync(LINUX_SERVING_SCRIPT, 'utf-8').match(/-jar\s+(\S+\.jar)/)
101
+ return match ? match[1] : LINUX_DEFAULT_JAR
102
+ }
103
+
104
+ async function probeTse() {
105
+ const section = await safe(readTseSection, {})
106
+ const tseEnabled = parseEnabled(section.TseEnabled)
107
+ const tseHost = (section.TseHost || DEFAULT_TSE_HOST).replace(/\/+$/, '')
108
+ // 不同版本的 jar 大小不同,没有 /adapterVersion 的老版本只能靠它区分
109
+ const tseJarSize = await safe(() => {
110
+ const jar = servingJarPath()
111
+ return jar && fs.existsSync(jar) ? fs.statSync(jar).size : null
112
+ })
113
+
114
+ if (!tseEnabled) {
115
+ return {tseEnabled, tseHost, tseStatus: 'disabled', tseAdapterVersion: null, tseJarSize}
116
+ }
117
+
118
+ // ok:TSE 正常;error:服务在但 TSE 在自检 / 没初始化 / 硬件报错;down:服务连不上
119
+ const ping = await httpGet(`${tseHost}/ping`)
120
+ const tseStatus = ping.status === 0 ? 'down' : (ping.status === 200 ? 'ok' : 'error')
121
+
122
+ // 连不上时报 null,云端保留上次的版本;服务在但没有这个接口(2024-09 之前的构建)报空串
123
+ let tseAdapterVersion = null
124
+ if (ping.status !== 0) {
125
+ const res = await httpGet(`${tseHost}/adapterVersion`)
126
+ if (res.status === 200) {
127
+ tseAdapterVersion = await safe(() => {
128
+ const major = JSON.parse(res.body).major
129
+ return major === undefined || major === null ? null : String(major)
130
+ })
131
+ } else if (res.status === 404) {
132
+ tseAdapterVersion = ''
133
+ }
134
+ }
135
+
136
+ return {tseEnabled, tseHost, tseStatus, tseAdapterVersion, tseJarSize}
137
+ }
138
+
139
+ // 整体出错就什么都不报,云端保留上次的值
140
+ async function collectTseInfo() {
141
+ return safe(probeTse, {})
142
+ }
143
+
144
+ module.exports = {collectTseInfo}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aaden-cli",
3
- "version": "1.6.108",
3
+ "version": "1.6.109",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -19,7 +19,7 @@
19
19
  "license": "ISC",
20
20
  "release-it": {
21
21
  "hooks": {
22
- "after:bump": "ncc build index.js -o dist -C"
22
+ "after:bump": "rm -rf dist && ncc build index.js -o dist -C"
23
23
  },
24
24
  "npm": {
25
25
  "publish": false
@@ -0,0 +1,39 @@
1
+ #!/bin/bash
2
+ # 一次性修复:摘掉老 git 通道的 doUpdate cron。
3
+ #
4
+ # 病症:设备 crontab 里有
5
+ # 59 4 * * * curl -X POST 'http://localhost/PHP/UpdateSelf.php?op=doUpdate'
6
+ # @reboot ... curl -X POST '.../UpdateSelf.php?op=doUpdate'
7
+ # 它每天 04:59(和 @reboot)从**设备本地陈旧 gitCache** 部署后端;而 git fetch 因 deploy key
8
+ # 未分发到全网而失败(FETCH_HEAD 0 字节),缓存卡在旧版本;git 通道又没有"绝不降级"保护 →
9
+ # 于是把 light-update 刚更好的新版本降级回退(实测 1.9.63 → 1.9.62/1.9.61,每天来回打架)。
10
+ #
11
+ # 结论:light-update(S3/code.zip 通道)已是后端唯一活着的更新通道,且自带"只升不降"版本门。
12
+ # 摘掉 doUpdate cron,终结两条通道打架。crontab 不走 git/S3 部署 → 改了就稳。
13
+ #
14
+ # 幂等:没有该行就不动;grep -vF 精确匹配整串,不误伤 crontab.sh / light-update 那几行。
15
+ set -u
16
+
17
+ MARK='UpdateSelf.php?op=doUpdate'
18
+
19
+ strip_for() { # $1 = 目标用户(空=当前用户)
20
+ local u="$1" cur new
21
+ local args=()
22
+ [ -n "$u" ] && args=(-u "$u")
23
+ cur=$(crontab "${args[@]}" -l 2>/dev/null) || return 0
24
+ printf '%s\n' "$cur" | grep -qF "$MARK" || return 0 # 没有该 cron → 不动
25
+ new=$(printf '%s\n' "$cur" | grep -vF "$MARK")
26
+ printf '%s\n' "$new" | crontab "${args[@]}" - \
27
+ && echo " 已从 ${u:-当前用户} 的 crontab 摘除 doUpdate cron"
28
+ }
29
+
30
+ # doUpdate cron 在 aaden 用户的 crontab。patch 正常由 root 的 auto-upgrade 触发 → 用 -u aaden;
31
+ # 兜底再扫一遍当前用户(万一某些设备以 aaden 直接跑 patch),谁有就摘谁。
32
+ if [ "$(id -u)" = "0" ]; then
33
+ strip_for aaden
34
+ strip_for ""
35
+ else
36
+ strip_for ""
37
+ fi
38
+
39
+ echo " removeGitDoUpdateCron 完成(light-update 成为后端唯一更新通道)"
@@ -1,19 +0,0 @@
1
- echo "Innerken322." | sudo -S su
2
- sudo touch /etc/aaden/AbstractDatevCsvFile.php
3
- sudo chmod 777 /etc/aaden/AbstractDatevCsvFile.php
4
- php -r $'print preg_replace(\'/ {4}public function toCsvArray\(\$items\)\r?\n(?:.*\r?\n)+? {4}}/\', <<<\'PHP\'
5
- public function toCsvArray($items)
6
- {
7
- yield $this->getHeader()->toArray($items[\'header\']);
8
-
9
- $fields = $this->getFields();
10
-
11
- yield array_map(Utils::itAccessMethod(\'getName\'), $fields);
12
-
13
- foreach ($items[\'body\'] as $item) {
14
- yield array_map(Utils::itAccessMethod(\'transform\', $item), $fields);
15
- }
16
- }
17
- PHP
18
- , file_get_contents("/var/www/html/PHP/Datev/FileStructures/AbstractDatevCsvFile.php"));' > /etc/aaden/AbstractDatevCsvFile.php
19
- sudo cp /etc/aaden/AbstractDatevCsvFile.php /var/www/html/PHP/Datev/FileStructures/AbstractDatevCsvFile.php
@@ -1,19 +0,0 @@
1
- echo "Innerken322." | sudo -S su
2
- sudo touch /etc/aaden/AbstractDatevCsvFile.php
3
- sudo chmod 777 /etc/aaden/AbstractDatevCsvFile.php
4
- php -r $'print preg_replace(\'/ {4}public function toCsvArray\(\$items\)\r?\n(?:.*\r?\n)+? {4}}/\', <<<\'PHP\'
5
- public function toCsvArray($items)
6
- {
7
- yield $this->getHeader()->toArray($items[\'header\']);
8
-
9
- $fields = $this->getFields();
10
-
11
- yield array_map(Utils::itAccessMethod(\'getName\'), $fields);
12
-
13
- foreach ($items[\'body\'] as $item) {
14
- yield array_map(Utils::itAccessMethod(\'transform\', $item), $fields);
15
- }
16
- }
17
- PHP
18
- , file_get_contents("/var/www/html/PHP/Datev/FileStructures/AbstractDatevCsvFile.php"));' > /etc/aaden/AbstractDatevCsvFile.php
19
- sudo cp /etc/aaden/AbstractDatevCsvFile.php /var/www/html/PHP/Datev/FileStructures/AbstractDatevCsvFile.php