@unionschool/easy-agents 0.0.0-stage → 0.2.3
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/README.md +22 -2
- package/bin/easy-agents.js +64 -0
- package/package.json +17 -4
package/README.md
CHANGED
|
@@ -1,3 +1,23 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @unionschool/easy-agents
|
|
2
2
|
|
|
3
|
-
This
|
|
3
|
+
This local CLI preview of the EasyAgents device client saves a gateway address and port, shows your local configuration, and displays the installed CLI version.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
Requires Node.js 22 or later. Install the npm package in the project where you want to use the preview.
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install @unionschool/easy-agents
|
|
11
|
+
npm exec --offline -- easy-agents --version
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Configure a gateway
|
|
15
|
+
|
|
16
|
+
```sh
|
|
17
|
+
npm exec --offline -- easy-agents configure https://gateway.example.com 443
|
|
18
|
+
npm exec --offline -- easy-agents status
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
The CLI saves the gateway URL in `npm-config.json`. The default directory is `~/.easyagents/` on macOS and Linux and `%APPDATA%\EasyAgents\` on Windows. Set `EASYAGENTS_CONFIG_DIR` to use a different directory.
|
|
22
|
+
|
|
23
|
+
This preview saves configuration locally. Device pairing, background operation, and task execution are planned for later releases.
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { mkdirSync, readFileSync, renameSync, unlinkSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { homedir } from 'node:os';
|
|
4
|
+
import { dirname, join } from 'node:path';
|
|
5
|
+
import { randomUUID } from 'node:crypto';
|
|
6
|
+
|
|
7
|
+
const { version } = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8'));
|
|
8
|
+
const configDir = process.env.EASYAGENTS_CONFIG_DIR || (process.platform === 'win32'
|
|
9
|
+
? join(process.env.APPDATA || join(homedir(), 'AppData', 'Roaming'), 'EasyAgents')
|
|
10
|
+
: join(homedir(), '.easyagents'));
|
|
11
|
+
const configPath = join(configDir, 'npm-config.json');
|
|
12
|
+
|
|
13
|
+
function gatewayOrigin(address, port) {
|
|
14
|
+
const url = new URL(address);
|
|
15
|
+
if (!['http:', 'https:'].includes(url.protocol) || !url.hostname || url.username || url.password
|
|
16
|
+
|| url.pathname !== '/' || url.search || url.hash || !/^\d{1,5}$/.test(port)
|
|
17
|
+
|| Number(port) < 1 || Number(port) > 65535) {
|
|
18
|
+
throw new Error('请输入有效的 HTTP 或 HTTPS 网关地址及 1–65535 范围内的端口');
|
|
19
|
+
}
|
|
20
|
+
url.port = port;
|
|
21
|
+
return url.origin;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function saveGateway(gateway) {
|
|
25
|
+
mkdirSync(dirname(configPath), { recursive: true, mode: 0o700 });
|
|
26
|
+
const temporary = join(configDir, '.' + randomUUID() + '.tmp');
|
|
27
|
+
writeFileSync(temporary, JSON.stringify({ gateway }, null, 2) + '\n', { mode: 0o600, flag: 'wx' });
|
|
28
|
+
try {
|
|
29
|
+
renameSync(temporary, configPath);
|
|
30
|
+
} catch (error) {
|
|
31
|
+
unlinkSync(temporary);
|
|
32
|
+
throw error;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const [command = '--help', ...args] = process.argv.slice(2);
|
|
37
|
+
try {
|
|
38
|
+
if (['--help', '-h'].includes(command) && !args.length) {
|
|
39
|
+
console.log('EasyAgents CLI 预览版\n\n'
|
|
40
|
+
+ 'easy-agents --version\n'
|
|
41
|
+
+ 'easy-agents configure <网关地址> <端口>\n'
|
|
42
|
+
+ 'easy-agents status');
|
|
43
|
+
} else if (['--version', '-v'].includes(command) && !args.length) {
|
|
44
|
+
console.log(version);
|
|
45
|
+
} else if (command === 'configure' && args.length === 2) {
|
|
46
|
+
saveGateway(gatewayOrigin(...args));
|
|
47
|
+
console.log('网关已保存');
|
|
48
|
+
} else if (command === 'status' && !args.length) {
|
|
49
|
+
let gateway = '未配置';
|
|
50
|
+
try {
|
|
51
|
+
gateway = JSON.parse(readFileSync(configPath, 'utf8')).gateway;
|
|
52
|
+
if (typeof gateway !== 'string') throw new Error('配置文件中的网关无效');
|
|
53
|
+
} catch (error) {
|
|
54
|
+
if (error.code !== 'ENOENT') throw error;
|
|
55
|
+
}
|
|
56
|
+
console.log('版本:' + version + '\n网关:' + gateway + '\n设备连接:预览版尚未支持配对');
|
|
57
|
+
} else {
|
|
58
|
+
throw new Error('命令或参数无效。请运行 easy-agents --help');
|
|
59
|
+
}
|
|
60
|
+
} catch (error) {
|
|
61
|
+
const message = error.code === 'ERR_INVALID_URL' ? '请输入包含 HTTP 或 HTTPS 的网关地址' : error.message;
|
|
62
|
+
console.error(message);
|
|
63
|
+
process.exitCode = 1;
|
|
64
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unionschool/easy-agents",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.3",
|
|
4
|
+
"description": "EasyAgents Client",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"easy-agents": "bin/easy-agents.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"bin/easy-agents.js"
|
|
11
|
+
],
|
|
12
|
+
"engines": {
|
|
13
|
+
"node": ">=22"
|
|
14
|
+
},
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public",
|
|
17
|
+
"registry": "https://registry.npmjs.org/"
|
|
18
|
+
}
|
|
19
|
+
}
|