wative 1.0.37 → 1.0.39

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/src/wative.ts ADDED
@@ -0,0 +1,127 @@
1
+ import * as path from 'path';
2
+ import { Command } from 'commander';
3
+ import { execSync } from 'child_process';
4
+ import { getAndGenerateConfig } from './ssh/utils';
5
+
6
+ const os = require('os');
7
+ const program = new Command();
8
+ const packageJson = require('../package.json');
9
+
10
+ const hasSystemd = () => {
11
+ try {
12
+ execSync('systemctl --version', { stdio: 'ignore' });
13
+ return true;
14
+ } catch (error) {
15
+ return false;
16
+ }
17
+ };
18
+
19
+ program
20
+ .version(packageJson.version) // 版本信息
21
+ .description('Wative CLI Tool');
22
+
23
+ // 定义 wative version 命令
24
+ program
25
+ .command('version')
26
+ .description('Show the version of wative')
27
+ .action(() => {
28
+ console.log(`Wative CLI Tool v${packageJson.version}`);
29
+ });
30
+
31
+
32
+ const sshCommand = program.command('ssh').description('Manage SSH service');
33
+
34
+ sshCommand
35
+ .command('start')
36
+ .description('Start SSH service')
37
+ .action(() => {
38
+ if (!hasSystemd()) {
39
+ console.log('Systemd is not available.');
40
+ return;
41
+ }
42
+
43
+ try {
44
+ execSync('wative-ssh', { stdio: 'inherit' });
45
+ } catch (error) { }
46
+
47
+ console.log('Starting SSH service...');
48
+ });
49
+
50
+ // sshCommand
51
+ // .command('stop')
52
+ // .description('Stop SSH service')
53
+ // .action(() => {
54
+ // if (!hasSystemd()) {
55
+ // console.log('Systemd is not available.');
56
+ // return;
57
+ // }
58
+ // console.log('Stopping SSH service...');
59
+ // });
60
+
61
+
62
+ const sshServiceCommand = sshCommand.command('service').description('Manage SSH service');
63
+ sshServiceCommand
64
+ .command('add')
65
+ .description('Add SSH service')
66
+ .action(async () => {
67
+ if (!hasSystemd()) {
68
+ console.log('Systemd is not available.');
69
+ return;
70
+ }
71
+
72
+ const userHomeDirectory = os.homedir();
73
+ const keystorePath = path.join(userHomeDirectory, '.wative');
74
+
75
+ await getAndGenerateConfig(keystorePath);
76
+
77
+ const serviceFilePath = '/etc/systemd/system/wative.service';
78
+ const serviceContent = `[Unit]
79
+ Description=Wative SSH Server
80
+ After=network.target
81
+
82
+ [Service]
83
+ ExecStart=/usr/local/bin/wative-ssh
84
+ Restart=always
85
+ User=root
86
+ Group=root
87
+ StandardOutput=append:/var/log/wative.log
88
+ StandardError=append:/var/log/wative.err
89
+
90
+ [Install]
91
+ WantedBy=multi-user.target
92
+ `;
93
+
94
+ console.log('Adding SSH service...');
95
+ execSync(`echo '${serviceContent}' > ${serviceFilePath}`, { stdio: 'inherit' });
96
+ execSync('systemctl daemon-reload', { stdio: 'inherit' });
97
+ execSync('systemctl enable wative', { stdio: 'inherit' });
98
+ execSync('systemctl start wative', { stdio: 'inherit' });
99
+ });
100
+
101
+ sshServiceCommand
102
+ .command('remove')
103
+ .description('Remove SSH service')
104
+ .action(() => {
105
+ if (!hasSystemd()) {
106
+ console.log('Systemd is not available.');
107
+ return;
108
+ }
109
+ console.log('Removing SSH service...');
110
+ execSync('systemctl stop wative', { stdio: 'inherit' });
111
+ execSync('systemctl disable wative', { stdio: 'inherit' });
112
+ execSync('rm /etc/systemd/system/wative.service', { stdio: 'inherit' });
113
+ execSync('systemctl daemon-reload', { stdio: 'inherit' });
114
+ });
115
+
116
+
117
+ // 定义 wative cmd 命令
118
+ program
119
+ .command('cmd')
120
+ .description('Start wallet-cli shell')
121
+ .action(async () => {
122
+ try {
123
+ execSync('wallet-cli', { stdio: 'inherit' });
124
+ } catch (error) { }
125
+ });
126
+
127
+ program.parse(process.argv);