quick-ssh 1.0.2

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/index.js ADDED
@@ -0,0 +1,186 @@
1
+ /**
2
+ * index.js - Quick-SSH npm 生命周期钩子
3
+ *
4
+ * postinstall : 将 Import-Module 语句写入 PowerShell 配置文件,重启终端自动加载
5
+ * preuninstall: 从 PowerShell 配置文件中移除 Import-Module 语句,保留用户数据
6
+ */
7
+
8
+ const fs = require("fs");
9
+ const path = require("path");
10
+
11
+ // ============================================================
12
+ // 常量定义
13
+ // ============================================================
14
+
15
+ const IMPORT_MARKER_START = "# >>> Quick-SSH auto-generated (do not modify) >>>";
16
+ const IMPORT_MARKER_END = "# <<< Quick-SSH auto-generated <<<";
17
+
18
+ /**
19
+ * 获取 PowerShell 配置文件路径(按优先级)
20
+ * 1. PowerShell 7+ → $env:USERPROFILE\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
21
+ * 2. Windows PowerShell → $env:USERPROFILE\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
22
+ */
23
+ function getPowerShellProfilePath() {
24
+ const userProfile = process.env.USERPROFILE;
25
+ if (!userProfile) {
26
+ console.error("[Quick-SSH] 错误:无法获取 %USERPROFILE% 环境变量。");
27
+ return null;
28
+ }
29
+
30
+ const candidates = [
31
+ // PowerShell 7+ (Core)
32
+ path.join(userProfile, "Documents", "PowerShell", "Microsoft.PowerShell_profile.ps1"),
33
+ // Windows PowerShell 5.1-
34
+ path.join(userProfile, "Documents", "WindowsPowerShell", "Microsoft.PowerShell_profile.ps1"),
35
+ ];
36
+
37
+ // 优先返回已存在的配置文件;都不存在则返回 PowerShell 7+ 的路径
38
+ for (const p of candidates) {
39
+ if (fs.existsSync(p)) return p;
40
+ }
41
+ return candidates[0];
42
+ }
43
+
44
+ /**
45
+ * 构建 Import-Module 语句块
46
+ */
47
+ function buildImportBlock(modulePath) {
48
+ const lines = [
49
+ IMPORT_MARKER_START,
50
+ `# Quick-SSH PowerShell SSH 连接管理工具`,
51
+ `# 安装路径: ${modulePath}`,
52
+ `if (Test-Path "${modulePath}") { Import-Module "${modulePath}" -DisableNameChecking }`,
53
+ IMPORT_MARKER_END,
54
+ ];
55
+ return lines.join("\n");
56
+ }
57
+
58
+ /**
59
+ * 获取当前包中 Quick-SSH.psm1 的路径
60
+ */
61
+ function getModulePath() {
62
+ // 在 npm 全局或本地安装时,index.js 位于包根目录
63
+ return path.join(__dirname, "Quick-SSH.psm1");
64
+ }
65
+
66
+ // ============================================================
67
+ // postinstall - 安装后自动注册模块
68
+ // ============================================================
69
+
70
+ function runPostInstall() {
71
+ console.log("[Quick-SSH] 正在配置 PowerShell 自动加载...");
72
+
73
+ const profilePath = getPowerShellProfilePath();
74
+ if (!profilePath) {
75
+ console.error("[Quick-SSH] ❌ 无法定位 PowerShell 配置文件。");
76
+ process.exit(1);
77
+ }
78
+
79
+ const modulePath = getModulePath();
80
+ if (!fs.existsSync(modulePath)) {
81
+ console.error(`[Quick-SSH] ❌ 未找到模块文件: ${modulePath}`);
82
+ process.exit(1);
83
+ }
84
+
85
+ const importBlock = buildImportBlock(modulePath);
86
+ let content = "";
87
+
88
+ // 读取现有配置
89
+ if (fs.existsSync(profilePath)) {
90
+ content = fs.readFileSync(profilePath, "utf-8");
91
+ }
92
+
93
+ // 检查是否已注册,避免重复插入
94
+ if (content.includes(IMPORT_MARKER_START)) {
95
+ // 已存在则替换旧块
96
+ const regex = new RegExp(
97
+ `${escapeRegExp(IMPORT_MARKER_START)}[\\s\\S]*?${escapeRegExp(IMPORT_MARKER_END)}`,
98
+ "g"
99
+ );
100
+ if (regex.test(content)) {
101
+ content = content.replace(regex, importBlock);
102
+ console.log("[Quick-SSH] 🔄 检测到旧的配置,已更新。");
103
+ }
104
+ } else {
105
+ // 追加到文件末尾
106
+ content = content.trimEnd() + "\n\n" + importBlock + "\n";
107
+ console.log("[Quick-SSH] ➕ 已将 Import-Module 写入配置文件。");
108
+ }
109
+
110
+ // 确保目录存在
111
+ const dir = path.dirname(profilePath);
112
+ if (!fs.existsSync(dir)) {
113
+ fs.mkdirSync(dir, { recursive: true });
114
+ }
115
+
116
+ fs.writeFileSync(profilePath, content, "utf-8");
117
+ console.log(`[Quick-SSH] ✔ 配置文件: ${profilePath}`);
118
+ console.log("[Quick-SSH] ✔ 安装完成!请重启 PowerShell 终端或执行:");
119
+ console.log(`[Quick-SSH] & (Get-Content "${profilePath}" -Raw) | Invoke-Expression`);
120
+ }
121
+
122
+ // ============================================================
123
+ // preuninstall - 卸载前清理注册(保留用户数据)
124
+ // ============================================================
125
+
126
+ function runPreUninstall() {
127
+ console.log("[Quick-SSH] 正在清理 PowerShell 配置文件...");
128
+
129
+ const profilePath = getPowerShellProfilePath();
130
+ if (!profilePath) {
131
+ console.log("[Quick-SSH] ✔ 未找到 PowerShell 配置文件,无需清理。");
132
+ return;
133
+ }
134
+
135
+ if (!fs.existsSync(profilePath)) {
136
+ console.log("[Quick-SSH] ✔ 配置文件不存在,无需清理。");
137
+ return;
138
+ }
139
+
140
+ let content = fs.readFileSync(profilePath, "utf-8");
141
+
142
+ // 构建正则移除整个 Import-Module 块
143
+ const regex = new RegExp(
144
+ `\\s*${escapeRegExp(IMPORT_MARKER_START)}[\\s\\S]*?${escapeRegExp(IMPORT_MARKER_END)}\\s*`,
145
+ "g"
146
+ );
147
+
148
+ if (regex.test(content)) {
149
+ content = content.replace(regex, "");
150
+ // 清理多余空行
151
+ content = content.replace(/\n{3,}/g, "\n\n").trimEnd() + "\n";
152
+ fs.writeFileSync(profilePath, content, "utf-8");
153
+ console.log(`[Quick-SSH] ✔ 已从 ${profilePath} 中移除 Import-Module 配置。`);
154
+ } else {
155
+ console.log("[Quick-SSH] ✔ 配置文件中未找到 Quick-SSH 注册信息。");
156
+ }
157
+
158
+ console.log("[Quick-SSH] ✔ 清理完成!用户配置数据已保留 (%USERPROFILE%\\.quickssh\\hosts.json)。");
159
+ }
160
+
161
+ // ============================================================
162
+ // 工具函数
163
+ // ============================================================
164
+
165
+ function escapeRegExp(str) {
166
+ return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
167
+ }
168
+
169
+ // ============================================================
170
+ // 入口
171
+ // ============================================================
172
+
173
+ const command = process.argv[2];
174
+
175
+ switch (command) {
176
+ case "postinstall":
177
+ runPostInstall();
178
+ break;
179
+ case "preuninstall":
180
+ runPreUninstall();
181
+ break;
182
+ default:
183
+ console.log(`[Quick-SSH] index.js - Quick-SSH npm 生命周期脚本`);
184
+ console.log(` 用法: node index.js <postinstall|preuninstall>`);
185
+ break;
186
+ }
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "quick-ssh",
3
+ "version": "1.0.2",
4
+ "description": "🚀 Quick-SSH - 仿 Docker 命令行风格的 PowerShell SSH 连接管理工具",
5
+ "keywords": [
6
+ "ssh",
7
+ "powershell",
8
+ "quick-ssh",
9
+ "qssh",
10
+ "connection-manager",
11
+ "windows"
12
+ ],
13
+ "homepage": "https://github.com/CCE-Li/quick-ssh#readme",
14
+ "bugs": {
15
+ "url": "https://github.com/CCE-Li/quick-ssh/issues"
16
+ },
17
+ "license": "MIT",
18
+ "author": "",
19
+ "main": "index.js",
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "git+https://github.com/CCE-Li/quick-ssh.git"
23
+ },
24
+ "scripts": {
25
+ "postinstall": "node index.js postinstall",
26
+ "preuninstall": "node index.js preuninstall"
27
+ },
28
+ "engines": {
29
+ "node": ">=10.0.0"
30
+ },
31
+ "os": [
32
+ "win32"
33
+ ],
34
+ "private": false,
35
+ "dependencies": {
36
+ "blessed": "^0.1.81"
37
+ }
38
+ }