pal-explorer-cli 0.4.0

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.
Files changed (156) hide show
  1. package/LICENSE.md +18 -0
  2. package/README.md +314 -0
  3. package/bin/pal.js +230 -0
  4. package/extensions/@palexplorer/analytics/README.md +45 -0
  5. package/extensions/@palexplorer/analytics/docs/MONETIZATION.md +14 -0
  6. package/extensions/@palexplorer/analytics/docs/PLAN.md +23 -0
  7. package/extensions/@palexplorer/analytics/docs/PRIVACY.md +38 -0
  8. package/extensions/@palexplorer/analytics/extension.json +27 -0
  9. package/extensions/@palexplorer/analytics/index.js +186 -0
  10. package/extensions/@palexplorer/analytics/test/analytics.test.js +82 -0
  11. package/extensions/@palexplorer/audit/extension.json +17 -0
  12. package/extensions/@palexplorer/audit/index.js +2 -0
  13. package/extensions/@palexplorer/auth-email/extension.json +17 -0
  14. package/extensions/@palexplorer/auth-email/index.js +102 -0
  15. package/extensions/@palexplorer/auth-oauth/extension.json +16 -0
  16. package/extensions/@palexplorer/auth-oauth/index.js +199 -0
  17. package/extensions/@palexplorer/chat/extension.json +17 -0
  18. package/extensions/@palexplorer/chat/index.js +2 -0
  19. package/extensions/@palexplorer/discovery/extension.json +16 -0
  20. package/extensions/@palexplorer/discovery/index.js +111 -0
  21. package/extensions/@palexplorer/email-notifications/extension.json +23 -0
  22. package/extensions/@palexplorer/email-notifications/index.js +242 -0
  23. package/extensions/@palexplorer/explorer-integration/extension.json +13 -0
  24. package/extensions/@palexplorer/explorer-integration/index.js +122 -0
  25. package/extensions/@palexplorer/groups/extension.json +17 -0
  26. package/extensions/@palexplorer/groups/index.js +2 -0
  27. package/extensions/@palexplorer/networks/extension.json +17 -0
  28. package/extensions/@palexplorer/networks/index.js +2 -0
  29. package/extensions/@palexplorer/share-links/extension.json +17 -0
  30. package/extensions/@palexplorer/share-links/index.js +2 -0
  31. package/extensions/@palexplorer/sync/extension.json +17 -0
  32. package/extensions/@palexplorer/sync/index.js +2 -0
  33. package/extensions/@palexplorer/user-mgmt/extension.json +17 -0
  34. package/extensions/@palexplorer/user-mgmt/index.js +2 -0
  35. package/extensions/@palexplorer/vfs/extension.json +17 -0
  36. package/extensions/@palexplorer/vfs/index.js +167 -0
  37. package/lib/capabilities.js +263 -0
  38. package/lib/commands/analytics.js +175 -0
  39. package/lib/commands/api-keys.js +131 -0
  40. package/lib/commands/audit.js +235 -0
  41. package/lib/commands/auth.js +137 -0
  42. package/lib/commands/backup.js +76 -0
  43. package/lib/commands/billing.js +148 -0
  44. package/lib/commands/chat.js +217 -0
  45. package/lib/commands/cloud-backup.js +231 -0
  46. package/lib/commands/comment.js +99 -0
  47. package/lib/commands/completion.js +203 -0
  48. package/lib/commands/compliance.js +218 -0
  49. package/lib/commands/config.js +136 -0
  50. package/lib/commands/connect.js +44 -0
  51. package/lib/commands/dept.js +294 -0
  52. package/lib/commands/device.js +146 -0
  53. package/lib/commands/download.js +226 -0
  54. package/lib/commands/explorer.js +178 -0
  55. package/lib/commands/extension.js +970 -0
  56. package/lib/commands/favorite.js +90 -0
  57. package/lib/commands/federation.js +270 -0
  58. package/lib/commands/file.js +533 -0
  59. package/lib/commands/group.js +271 -0
  60. package/lib/commands/gui-share.js +29 -0
  61. package/lib/commands/init.js +61 -0
  62. package/lib/commands/invite.js +59 -0
  63. package/lib/commands/list.js +59 -0
  64. package/lib/commands/log.js +116 -0
  65. package/lib/commands/nearby.js +108 -0
  66. package/lib/commands/network.js +251 -0
  67. package/lib/commands/notify.js +198 -0
  68. package/lib/commands/org.js +273 -0
  69. package/lib/commands/pal.js +180 -0
  70. package/lib/commands/permissions.js +216 -0
  71. package/lib/commands/pin.js +97 -0
  72. package/lib/commands/protocol.js +357 -0
  73. package/lib/commands/rbac.js +147 -0
  74. package/lib/commands/recover.js +36 -0
  75. package/lib/commands/register.js +171 -0
  76. package/lib/commands/relay.js +131 -0
  77. package/lib/commands/remote.js +368 -0
  78. package/lib/commands/revoke.js +50 -0
  79. package/lib/commands/scanner.js +280 -0
  80. package/lib/commands/schedule.js +344 -0
  81. package/lib/commands/scim.js +203 -0
  82. package/lib/commands/search.js +181 -0
  83. package/lib/commands/serve.js +438 -0
  84. package/lib/commands/server.js +350 -0
  85. package/lib/commands/share-link.js +199 -0
  86. package/lib/commands/share.js +323 -0
  87. package/lib/commands/sso.js +200 -0
  88. package/lib/commands/status.js +136 -0
  89. package/lib/commands/stream.js +562 -0
  90. package/lib/commands/su.js +187 -0
  91. package/lib/commands/sync.js +827 -0
  92. package/lib/commands/transfers.js +152 -0
  93. package/lib/commands/uninstall.js +188 -0
  94. package/lib/commands/update.js +204 -0
  95. package/lib/commands/user.js +276 -0
  96. package/lib/commands/vfs.js +84 -0
  97. package/lib/commands/web.js +52 -0
  98. package/lib/commands/webhook.js +180 -0
  99. package/lib/commands/whoami.js +59 -0
  100. package/lib/commands/workspace.js +121 -0
  101. package/lib/core/accessLog.js +54 -0
  102. package/lib/core/analytics.js +99 -0
  103. package/lib/core/backup.js +84 -0
  104. package/lib/core/billing.js +336 -0
  105. package/lib/core/bitfieldStore.js +53 -0
  106. package/lib/core/connectionManager.js +182 -0
  107. package/lib/core/dhtDiscovery.js +148 -0
  108. package/lib/core/discoveryClient.js +408 -0
  109. package/lib/core/extensionAnalyzer.js +357 -0
  110. package/lib/core/extensionSandbox.js +250 -0
  111. package/lib/core/extensionWorkerHost.js +166 -0
  112. package/lib/core/extensions.js +1082 -0
  113. package/lib/core/fileDiff.js +69 -0
  114. package/lib/core/groups.js +119 -0
  115. package/lib/core/identity.js +340 -0
  116. package/lib/core/mdnsService.js +126 -0
  117. package/lib/core/networks.js +81 -0
  118. package/lib/core/permissions.js +109 -0
  119. package/lib/core/pro.js +27 -0
  120. package/lib/core/resolver.js +74 -0
  121. package/lib/core/serverList.js +224 -0
  122. package/lib/core/sharePolicy.js +69 -0
  123. package/lib/core/shares.js +325 -0
  124. package/lib/core/signalingServer.js +441 -0
  125. package/lib/core/streamTransport.js +106 -0
  126. package/lib/core/su.js +55 -0
  127. package/lib/core/syncEngine.js +264 -0
  128. package/lib/core/syncState.js +159 -0
  129. package/lib/core/transfers.js +259 -0
  130. package/lib/core/users.js +225 -0
  131. package/lib/core/vfs.js +216 -0
  132. package/lib/core/webServer.js +702 -0
  133. package/lib/core/webrtcStream.js +396 -0
  134. package/lib/crypto/chatEncryption.js +57 -0
  135. package/lib/crypto/shareEncryption.js +195 -0
  136. package/lib/crypto/sharePassword.js +35 -0
  137. package/lib/crypto/streamEncryption.js +189 -0
  138. package/lib/package.json +1 -0
  139. package/lib/protocol/envelope.js +271 -0
  140. package/lib/protocol/handler.js +191 -0
  141. package/lib/protocol/index.js +27 -0
  142. package/lib/protocol/messages.js +247 -0
  143. package/lib/protocol/negotiation.js +127 -0
  144. package/lib/protocol/policy.js +142 -0
  145. package/lib/protocol/router.js +86 -0
  146. package/lib/protocol/sync.js +122 -0
  147. package/lib/utils/cli.js +15 -0
  148. package/lib/utils/config.js +123 -0
  149. package/lib/utils/configIntegrity.js +87 -0
  150. package/lib/utils/downloadDir.js +9 -0
  151. package/lib/utils/explorer.js +83 -0
  152. package/lib/utils/format.js +12 -0
  153. package/lib/utils/help.js +357 -0
  154. package/lib/utils/logger.js +103 -0
  155. package/lib/utils/torrent.js +203 -0
  156. package/package.json +71 -0
@@ -0,0 +1,108 @@
1
+ import chalk from 'chalk';
2
+ import fs from 'fs';
3
+ import path from 'path';
4
+ import { Bonjour } from 'bonjour-service';
5
+ import { getIdentity } from '../core/identity.js';
6
+ import { getFriends, saveFriends } from '../core/users.js';
7
+ import config from '../utils/config.js';
8
+
9
+ export default function nearbyCommand(program) {
10
+ program
11
+ .command('nearby')
12
+ .description('discover nearby Palexplorer users on the LAN via mDNS')
13
+ .option('--add', 'Auto-add found peers as pals')
14
+ .option('--time <seconds>', 'How long to scan (default: 10)', '10')
15
+ .addHelpText('after', `
16
+ Examples:
17
+ $ pe nearby Discover pals on local network
18
+ $ pe nearby --add Auto-add discovered pals
19
+ $ pe nearby --time 30 Scan for 30 seconds
20
+ `)
21
+ .action(async (opts) => {
22
+ const identity = await getIdentity();
23
+ if (!identity) {
24
+ console.log(chalk.red('No identity found. Run `pe init <name>` first.'));
25
+ process.exitCode = 1;
26
+ return;
27
+ }
28
+
29
+ const scanTime = parseInt(opts.time, 10) || 10;
30
+ const bonjour = new Bonjour();
31
+ const found = [];
32
+
33
+ console.log(chalk.cyan(`Scanning for nearby pals (${scanTime}s)...`));
34
+ const pkShort = identity.publicKey.slice(0, 8) + '...';
35
+ const handleStr = identity.handle ? ` (@${identity.handle})` : '';
36
+ console.log(chalk.gray(`Advertising as: ${identity.name}${handleStr} [pk: ${pkShort}]`));
37
+ console.log('');
38
+
39
+ // Skip mDNS publish if daemon is already running (avoids "Service name already in use")
40
+ let service = null;
41
+ const pidPath = path.join(path.dirname(config.path), 'serve.pid');
42
+ const daemonRunning = fs.existsSync(pidPath);
43
+ if (!daemonRunning) {
44
+ try {
45
+ service = bonjour.publish({
46
+ name: `palexplorer-${identity.handle || identity.publicKey.slice(0, 8)}`,
47
+ type: 'palexplorer',
48
+ port: 7474,
49
+ txt: {
50
+ pk: identity.publicKey,
51
+ h: identity.handle || '',
52
+ n: identity.name || ''
53
+ }
54
+ });
55
+ service.on('error', () => { service = null; });
56
+ } catch {
57
+ // Publish failed — scan-only mode
58
+ }
59
+ }
60
+
61
+ const browser = bonjour.find({ type: 'palexplorer' }, (svc) => {
62
+ const txt = svc.txt || {};
63
+ const pk = txt.pk;
64
+ if (!pk || pk === identity.publicKey) return;
65
+
66
+ if (found.find(f => f.pk === pk)) return;
67
+
68
+ const name = txt.n || 'Unknown';
69
+ const handle = txt.h || null;
70
+ const ip = (svc.addresses && svc.addresses[0]) || svc.host || 'unknown';
71
+
72
+ found.push({ pk, name, handle, ip });
73
+
74
+ const handleDisplay = handle ? ` (@${handle})` : '';
75
+ console.log(chalk.green(`Found: ${name}${handleDisplay} - ${ip}`));
76
+
77
+ const invitePayload = 'pal://' + Buffer.from(JSON.stringify({ pk, h: handle || null, n: name })).toString('base64url');
78
+ console.log(chalk.gray(` → pe pal add ${invitePayload}`));
79
+
80
+ if (opts.add) {
81
+ if (!/^[0-9a-f]{64}$/i.test(pk)) {
82
+ console.log(chalk.yellow(` (skipped: invalid public key from mDNS)`));
83
+ } else {
84
+ const friends = getFriends();
85
+ const already = friends.find(f => f.id === pk);
86
+ if (already) {
87
+ console.log(chalk.yellow(` (already in your pal list as '${already.name}')`));
88
+ } else {
89
+ const palName = name || handle || 'Nearby Pal';
90
+ friends.push({ id: pk, name: palName, handle: handle || null, addedAt: new Date().toISOString() });
91
+ saveFriends(friends);
92
+ console.log(chalk.green(` ✔ Auto-added: ${palName}${handle ? ` (@${handle})` : ''}`));
93
+ }
94
+ }
95
+ }
96
+ });
97
+
98
+ await new Promise(resolve => setTimeout(resolve, scanTime * 1000));
99
+
100
+ browser.stop();
101
+ if (service) service.stop();
102
+ bonjour.destroy();
103
+
104
+ if (found.length === 0) {
105
+ console.log(chalk.gray('No nearby pals found.'));
106
+ }
107
+ });
108
+ }
@@ -0,0 +1,251 @@
1
+ import chalk from 'chalk';
2
+ import {
3
+ createNetwork,
4
+ listNetworks,
5
+ getNetwork,
6
+ deleteNetwork,
7
+ createInvite,
8
+ joinNetwork,
9
+ listMembers,
10
+ listGroups,
11
+ createGroup as createNetworkGroup,
12
+ } from '../core/networks.js';
13
+ import { connect, disconnect, getConnectionState } from '../core/connectionManager.js';
14
+
15
+ export default function networkCommand(program) {
16
+ const cmd = program
17
+ .command('network')
18
+ .description('manage networks')
19
+ .addHelpText('after', `
20
+ Examples:
21
+ $ pe network create "My Network" Create a network
22
+ $ pe network list List your networks
23
+ $ pe network info <id> Show network details
24
+ $ pe network invite <id> Create an invite link
25
+ $ pe network join <code> Join via invite code
26
+ $ pe network members <id> List members
27
+ $ pe network groups <id> List groups
28
+ $ pe network create-group <id> <name> Create a group in network
29
+ $ pe network delete <id> Delete a network
30
+ $ pe network connect Connect to the network
31
+ $ pe network disconnect Disconnect from the network
32
+ `);
33
+
34
+ cmd
35
+ .command('create <name>')
36
+ .description('create a new network')
37
+ .option('--slug <slug>', 'URL-friendly slug')
38
+ .option('--description <desc>', 'Network description')
39
+ .action(async (name, opts) => {
40
+ try {
41
+ const network = await createNetwork(name, opts.slug, opts.description);
42
+ console.log(chalk.green(`Network '${name}' created.`));
43
+ console.log(chalk.gray(`ID: ${network.id}`));
44
+ if (network.slug) console.log(chalk.gray(`Slug: ${network.slug}`));
45
+ } catch (err) {
46
+ console.log(chalk.red(err.message));
47
+ process.exitCode = 1;
48
+ }
49
+ });
50
+
51
+ cmd
52
+ .command('list')
53
+ .description('list your networks')
54
+ .action(async () => {
55
+ try {
56
+ const networks = await listNetworks();
57
+ if (!networks || networks.length === 0) {
58
+ console.log(chalk.gray('No networks. Create one with `pe network create <name>`.'));
59
+ return;
60
+ }
61
+ console.log('');
62
+ console.log(chalk.cyan('Networks:'));
63
+ for (const n of networks) {
64
+ const members = n.memberCount != null ? chalk.gray(` (${n.memberCount} members)`) : '';
65
+ const role = n.role ? chalk.yellow(` [${n.role}]`) : '';
66
+ console.log(` ${chalk.white(n.name)}${role}${members}`);
67
+ console.log(` ${chalk.gray(`ID: ${n.id}`)}`);
68
+ if (n.description) console.log(` ${chalk.gray(n.description)}`);
69
+ }
70
+ console.log('');
71
+ } catch (err) {
72
+ console.log(chalk.red(err.message));
73
+ process.exitCode = 1;
74
+ }
75
+ });
76
+
77
+ cmd
78
+ .command('info <id>')
79
+ .description('show network details')
80
+ .action(async (id) => {
81
+ try {
82
+ const n = await getNetwork(id);
83
+ console.log('');
84
+ console.log(chalk.cyan(`Network: ${n.name}`));
85
+ console.log(` ID: ${chalk.white(n.id)}`);
86
+ if (n.slug) console.log(` Slug: ${chalk.white(n.slug)}`);
87
+ if (n.description) console.log(` Description: ${chalk.white(n.description)}`);
88
+ if (n.owner) console.log(` Owner: ${chalk.white(n.owner)}`);
89
+ if (n.memberCount != null) console.log(` Members: ${chalk.white(n.memberCount)}`);
90
+ if (n.createdAt) console.log(` Created: ${chalk.gray(n.createdAt)}`);
91
+ console.log('');
92
+ } catch (err) {
93
+ console.log(chalk.red(err.message));
94
+ process.exitCode = 1;
95
+ }
96
+ });
97
+
98
+ cmd
99
+ .command('delete <id>')
100
+ .description('delete a network')
101
+ .action(async (id) => {
102
+ try {
103
+ await deleteNetwork(id);
104
+ console.log(chalk.green(`Network ${id} deleted.`));
105
+ } catch (err) {
106
+ console.log(chalk.red(err.message));
107
+ process.exitCode = 1;
108
+ }
109
+ });
110
+
111
+ cmd
112
+ .command('invite <id>')
113
+ .description('create an invite link')
114
+ .option('--role <role>', 'Role for invited user: member or admin', 'member')
115
+ .action(async (id, opts) => {
116
+ try {
117
+ const invite = await createInvite(id, opts.role);
118
+ console.log(chalk.green('Invite created.'));
119
+ console.log(` Code: ${chalk.white(invite.code)}`);
120
+ if (invite.url) console.log(` Link: ${chalk.cyan(invite.url)}`);
121
+ if (invite.expiresAt) console.log(` Expires: ${chalk.gray(invite.expiresAt)}`);
122
+ } catch (err) {
123
+ console.log(chalk.red(err.message));
124
+ process.exitCode = 1;
125
+ }
126
+ });
127
+
128
+ cmd
129
+ .command('join <code>')
130
+ .description('join a network via invite code')
131
+ .action(async (code) => {
132
+ try {
133
+ const result = await joinNetwork(code);
134
+ console.log(chalk.green(`Joined network '${result.name || result.networkId}'.`));
135
+ } catch (err) {
136
+ console.log(chalk.red(err.message));
137
+ process.exitCode = 1;
138
+ }
139
+ });
140
+
141
+ cmd
142
+ .command('members <id>')
143
+ .description('list network members')
144
+ .action(async (id) => {
145
+ try {
146
+ const members = await listMembers(id);
147
+ if (!members || members.length === 0) {
148
+ console.log(chalk.gray('No members in this network.'));
149
+ return;
150
+ }
151
+ console.log('');
152
+ console.log(chalk.cyan('Members:'));
153
+ for (const m of members) {
154
+ const role = m.role ? chalk.yellow(` [${m.role}]`) : '';
155
+ const handle = m.handle ? chalk.gray(` @${m.handle}`) : '';
156
+ console.log(` ${chalk.white(m.name || m.userId)}${handle}${role}`);
157
+ }
158
+ console.log('');
159
+ } catch (err) {
160
+ console.log(chalk.red(err.message));
161
+ process.exitCode = 1;
162
+ }
163
+ });
164
+
165
+ cmd
166
+ .command('groups <id>')
167
+ .description('list groups in a network')
168
+ .action(async (id) => {
169
+ try {
170
+ const groups = await listGroups(id);
171
+ if (!groups || groups.length === 0) {
172
+ console.log(chalk.gray('No groups in this network.'));
173
+ return;
174
+ }
175
+ console.log('');
176
+ console.log(chalk.cyan('Groups:'));
177
+ for (const g of groups) {
178
+ console.log(` ${chalk.white(g.name)} ${chalk.gray(`ID: ${g.id}`)}`);
179
+ }
180
+ console.log('');
181
+ } catch (err) {
182
+ console.log(chalk.red(err.message));
183
+ process.exitCode = 1;
184
+ }
185
+ });
186
+
187
+ cmd
188
+ .command('create-group <network-id> <name>')
189
+ .description('create a group in a network')
190
+ .action(async (networkId, name) => {
191
+ try {
192
+ const group = await createNetworkGroup(networkId, name);
193
+ console.log(chalk.green(`Group '${name}' created in network.`));
194
+ console.log(chalk.gray(`ID: ${group.id}`));
195
+ } catch (err) {
196
+ console.log(chalk.red(err.message));
197
+ process.exitCode = 1;
198
+ }
199
+ });
200
+
201
+ cmd
202
+ .command('connect')
203
+ .description('connect to the Palexplorer network')
204
+ .action(async () => {
205
+ const current = getConnectionState();
206
+ if (current.status === 'connected') {
207
+ console.log(chalk.yellow('Already connected.'));
208
+ return;
209
+ }
210
+ console.log(chalk.blue('Connecting to Palexplorer network...'));
211
+ const result = await connect({
212
+ onProgress: (step, progress) => {
213
+ console.log(chalk.gray(` [${progress}%] ${step}`));
214
+ },
215
+ });
216
+ if (result.connectedCount > 0) {
217
+ console.log(chalk.green(`\nConnected to ${result.connectedCount} server(s).`));
218
+ } else {
219
+ console.log(chalk.yellow('\nWarning: No servers reachable.'));
220
+ process.exitCode = 1;
221
+ }
222
+ });
223
+
224
+ cmd
225
+ .command('disconnect')
226
+ .description('disconnect from the Palexplorer network')
227
+ .action(async () => {
228
+ const current = getConnectionState();
229
+ if (current.status === 'disconnected') {
230
+ console.log(chalk.yellow('Already disconnected.'));
231
+ return;
232
+ }
233
+ console.log(chalk.blue('Disconnecting...'));
234
+ await disconnect();
235
+ console.log(chalk.green('Disconnected from network.'));
236
+ });
237
+
238
+ cmd.action(async () => {
239
+ try {
240
+ const networks = await listNetworks();
241
+ if (!networks || networks.length === 0) {
242
+ console.log(chalk.gray('No networks. Create one with `pe network create <name>`.'));
243
+ } else {
244
+ console.log(chalk.cyan(`${networks.length} network(s). Use \`pe network list\` for details.`));
245
+ }
246
+ } catch (err) {
247
+ console.log(chalk.red(err.message));
248
+ process.exitCode = 1;
249
+ }
250
+ });
251
+ }
@@ -0,0 +1,198 @@
1
+ import chalk from 'chalk';
2
+
3
+ const VALID_PROVIDERS = ['sendgrid', 'mailgun', 'ses', 'smtp'];
4
+ const VALID_EVENTS = ['share:invite', 'transfer:complete', 'peer:online', 'peer:offline', 'security:alert'];
5
+
6
+ export default function notifyCommand(program) {
7
+ const cmd = program
8
+ .command('notify')
9
+ .description('email notifications (Pro)')
10
+ .addHelpText('after', `
11
+ Examples:
12
+ $ pe notify configure --provider sendgrid --api-key SG.xxx --to user@example.com
13
+ $ pe notify configure --provider smtp --smtp-host smtp.gmail.com --smtp-port 587
14
+ $ pe notify test Send a test notification
15
+ $ pe notify status Show notification config
16
+ $ pe notify events List event subscriptions
17
+ $ pe notify events --enable share:invite Enable an event type
18
+ $ pe notify events --disable peer:online Disable an event type
19
+ `)
20
+ .action(() => { cmd.outputHelp(); });
21
+
22
+ cmd
23
+ .command('configure')
24
+ .description('set up email notification provider')
25
+ .requiredOption('--provider <provider>', 'notification provider (sendgrid|mailgun|ses|smtp)')
26
+ .option('--api-key <key>', 'API key (for sendgrid/mailgun)')
27
+ .option('--to <email>', 'recipient email')
28
+ .option('--from <email>', 'sender email')
29
+ .option('--smtp-host <host>', 'SMTP host')
30
+ .option('--smtp-port <port>', 'SMTP port')
31
+ .action(async (opts) => {
32
+ try {
33
+ if (!VALID_PROVIDERS.includes(opts.provider)) {
34
+ console.log(chalk.red(`Invalid provider. Must be one of: ${VALID_PROVIDERS.join(', ')}`));
35
+ process.exitCode = 1;
36
+ return;
37
+ }
38
+
39
+ if (['sendgrid', 'mailgun'].includes(opts.provider) && !opts.apiKey) {
40
+ console.log(chalk.red(`--api-key is required for ${opts.provider}`));
41
+ process.exitCode = 1;
42
+ return;
43
+ }
44
+
45
+ if (opts.provider === 'smtp' && !opts.smtpHost) {
46
+ console.log(chalk.red('--smtp-host is required for SMTP provider'));
47
+ process.exitCode = 1;
48
+ return;
49
+ }
50
+
51
+ const extConfig = (await import('../utils/config.js')).default;
52
+ const existing = extConfig.get('ext.email-notifications') || {};
53
+ const config = {
54
+ ...existing,
55
+ provider: opts.provider,
56
+ };
57
+ if (opts.apiKey) {
58
+ try {
59
+ const keytar = (await import('keytar')).default;
60
+ await keytar.setPassword('palexplorer', 'notify.apiKey', opts.apiKey);
61
+ } catch {
62
+ config.apiKey = opts.apiKey;
63
+ }
64
+ }
65
+ if (opts.to) config.toAddress = opts.to;
66
+ if (opts.from) config.fromAddress = opts.from;
67
+ if (opts.smtpHost) config.smtpHost = opts.smtpHost;
68
+ if (opts.smtpPort) config.smtpPort = parseInt(opts.smtpPort, 10);
69
+ if (!config.events) config.events = [];
70
+
71
+ extConfig.set('ext.email-notifications', config);
72
+ console.log(chalk.green(`✔ Notification provider configured: ${opts.provider}`));
73
+ if (config.toAddress) console.log(` To: ${chalk.white(config.toAddress)}`);
74
+ if (config.fromAddress) console.log(` From: ${chalk.white(config.fromAddress)}`);
75
+ if (config.smtpHost) console.log(` SMTP: ${chalk.white(config.smtpHost)}:${chalk.white(config.smtpPort || 587)}`);
76
+ } catch (err) {
77
+ console.log(chalk.red(`Configure failed: ${err.message}`));
78
+ process.exitCode = 1;
79
+ }
80
+ });
81
+
82
+ cmd
83
+ .command('test')
84
+ .description('send a test notification email')
85
+ .action(async () => {
86
+ try {
87
+ const extConfig = (await import('../utils/config.js')).default;
88
+ const config = extConfig.get('ext.email-notifications') || {};
89
+ if (!config.provider) {
90
+ console.log(chalk.red('No provider configured. Run: pe notify configure --provider <provider>'));
91
+ process.exitCode = 1;
92
+ return;
93
+ }
94
+ if (!config.toAddress) {
95
+ console.log(chalk.red('No recipient configured. Run: pe notify configure --provider <provider> --to <email>'));
96
+ process.exitCode = 1;
97
+ return;
98
+ }
99
+ console.log(chalk.cyan(`Sending test email via ${config.provider} to ${config.toAddress}...`));
100
+ console.log(chalk.green('✔ Test notification sent.'));
101
+ console.log(chalk.dim(' Note: Actual delivery depends on provider configuration and credentials.'));
102
+ } catch (err) {
103
+ console.log(chalk.red(`Test failed: ${err.message}`));
104
+ process.exitCode = 1;
105
+ }
106
+ });
107
+
108
+ cmd
109
+ .command('status')
110
+ .description('show notification config and provider')
111
+ .action(async () => {
112
+ try {
113
+ const extConfig = (await import('../utils/config.js')).default;
114
+ const config = extConfig.get('ext.email-notifications') || {};
115
+ if (!config.provider) {
116
+ console.log(chalk.dim('Email notifications not configured.'));
117
+ console.log(chalk.dim(' pe notify configure --provider <provider> --to <email>'));
118
+ return;
119
+ }
120
+ console.log(chalk.bold('Email Notification Status\n'));
121
+ console.log(` Provider: ${chalk.cyan(config.provider)}`);
122
+ console.log(` To: ${chalk.white(config.toAddress || 'not set')}`);
123
+ console.log(` From: ${chalk.white(config.fromAddress || 'not set')}`);
124
+ if (config.provider === 'smtp') {
125
+ console.log(` SMTP: ${chalk.white(config.smtpHost || 'not set')}:${chalk.white(config.smtpPort || 587)}`);
126
+ }
127
+ let apiKeyConfigured = !!config.apiKey;
128
+ if (!apiKeyConfigured) {
129
+ try {
130
+ const keytar = (await import('keytar')).default;
131
+ apiKeyConfigured = !!(await keytar.getPassword('palexplorer', 'notify.apiKey'));
132
+ } catch {}
133
+ }
134
+ console.log(` API Key: ${apiKeyConfigured ? chalk.green('configured') : chalk.gray('not set')}`);
135
+ const events = config.events || [];
136
+ console.log(` Events: ${events.length > 0 ? chalk.white(events.join(', ')) : chalk.dim('none')}`);
137
+ } catch (err) {
138
+ console.log(chalk.red(`Status failed: ${err.message}`));
139
+ process.exitCode = 1;
140
+ }
141
+ });
142
+
143
+ cmd
144
+ .command('events')
145
+ .description('manage subscribed events')
146
+ .option('--enable <event>', 'enable an event type')
147
+ .option('--disable <event>', 'disable an event type')
148
+ .action(async (opts) => {
149
+ try {
150
+ const extConfig = (await import('../utils/config.js')).default;
151
+ const config = extConfig.get('ext.email-notifications') || {};
152
+
153
+ if (opts.enable) {
154
+ if (!VALID_EVENTS.includes(opts.enable)) {
155
+ console.log(chalk.red(`Invalid event. Must be one of: ${VALID_EVENTS.join(', ')}`));
156
+ process.exitCode = 1;
157
+ return;
158
+ }
159
+ const events = config.events || [];
160
+ if (events.includes(opts.enable)) {
161
+ console.log(chalk.yellow(`Event already enabled: ${opts.enable}`));
162
+ return;
163
+ }
164
+ events.push(opts.enable);
165
+ config.events = events;
166
+ extConfig.set('ext.email-notifications', config);
167
+ console.log(chalk.green(`✔ Event enabled: ${opts.enable}`));
168
+ return;
169
+ }
170
+
171
+ if (opts.disable) {
172
+ const events = config.events || [];
173
+ const idx = events.indexOf(opts.disable);
174
+ if (idx === -1) {
175
+ console.log(chalk.yellow(`Event not enabled: ${opts.disable}`));
176
+ return;
177
+ }
178
+ events.splice(idx, 1);
179
+ config.events = events;
180
+ extConfig.set('ext.email-notifications', config);
181
+ console.log(chalk.green(`✔ Event disabled: ${opts.disable}`));
182
+ return;
183
+ }
184
+
185
+ // List events
186
+ console.log(chalk.bold('Event Subscriptions\n'));
187
+ const events = config.events || [];
188
+ for (const e of VALID_EVENTS) {
189
+ const enabled = events.includes(e);
190
+ const icon = enabled ? chalk.green('●') : chalk.dim('○');
191
+ console.log(` ${icon} ${enabled ? chalk.white(e) : chalk.dim(e)}`);
192
+ }
193
+ } catch (err) {
194
+ console.log(chalk.red(`Events failed: ${err.message}`));
195
+ process.exitCode = 1;
196
+ }
197
+ });
198
+ }