@wonderwhy-er/desktop-commander 0.2.8 → 0.2.10

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.
@@ -55,8 +55,8 @@ export declare const ReadFileArgsSchema: z.ZodObject<{
55
55
  offset: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
56
56
  length: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
57
57
  }, "strip", z.ZodTypeAny, {
58
- path: string;
59
58
  length: number;
59
+ path: string;
60
60
  isUrl: boolean;
61
61
  offset: number;
62
62
  }, {
@@ -0,0 +1,368 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * Installation Source Tracking Script
5
+ * Runs during npm install to detect how Desktop Commander was installed
6
+ *
7
+ * Debug logging can be enabled with:
8
+ * - DEBUG=desktop-commander npm install
9
+ * - DEBUG=* npm install
10
+ * - NODE_ENV=development npm install
11
+ * - DC_DEBUG=true npm install
12
+ */
13
+
14
+ import { randomUUID } from 'crypto';
15
+ import * as https from 'https';
16
+ import { platform } from 'os';
17
+ import path from 'path';
18
+ import { fileURLToPath } from 'url';
19
+
20
+ // Get current file directory
21
+ const __filename = fileURLToPath(import.meta.url);
22
+ const __dirname = path.dirname(__filename);
23
+
24
+ // Debug logging utility - configurable via environment variables
25
+ const DEBUG_ENABLED = process.env.DEBUG === 'desktop-commander' ||
26
+ process.env.DEBUG === '*' ||
27
+ process.env.NODE_ENV === 'development' ||
28
+ process.env.DC_DEBUG === 'true';
29
+
30
+ const debug = (...args) => {
31
+ if (DEBUG_ENABLED) {
32
+ console.log('[Desktop Commander Debug]', ...args);
33
+ }
34
+ };
35
+
36
+ const log = (...args) => {
37
+ // Always show important messages, but prefix differently for debug vs production
38
+ if (DEBUG_ENABLED) {
39
+ console.log('[Desktop Commander]', ...args);
40
+ }
41
+ };
42
+
43
+ /**
44
+ * Get the client ID from the Desktop Commander config file, or generate a new one
45
+ */
46
+ async function getClientId() {
47
+ try {
48
+ const { homedir } = await import('os');
49
+ const { join } = await import('path');
50
+ const fs = await import('fs');
51
+
52
+ const USER_HOME = homedir();
53
+ const CONFIG_DIR = join(USER_HOME, '.claude-server-commander');
54
+ const CONFIG_FILE = join(CONFIG_DIR, 'config.json');
55
+
56
+ // Try to read existing config
57
+ if (fs.existsSync(CONFIG_FILE)) {
58
+ const configData = fs.readFileSync(CONFIG_FILE, 'utf8');
59
+ const config = JSON.parse(configData);
60
+ if (config.clientId) {
61
+ debug(`Using existing clientId from config: ${config.clientId.substring(0, 8)}...`);
62
+ return config.clientId;
63
+ }
64
+ }
65
+
66
+ debug('No existing clientId found, generating new one');
67
+ // Fallback to random UUID if config doesn't exist or lacks clientId
68
+ return randomUUID();
69
+ } catch (error) {
70
+ debug(`Error reading config file: ${error.message}, using random UUID`);
71
+ // If anything goes wrong, fall back to random UUID
72
+ return randomUUID();
73
+ }
74
+ }
75
+
76
+ // Google Analytics configuration (same as setup script)
77
+ const GA_MEASUREMENT_ID = 'G-NGGDNL0K4L';
78
+ const GA_API_SECRET = '5M0mC--2S_6t94m8WrI60A';
79
+ const GA_BASE_URL = `https://www.google-analytics.com/mp/collect?measurement_id=${GA_MEASUREMENT_ID}&api_secret=${GA_API_SECRET}`;
80
+
81
+ /**
82
+ * Detect installation source from environment and process context
83
+ */
84
+ async function detectInstallationSource() {
85
+ // Check npm environment variables for clues
86
+ const npmConfigUserAgent = process.env.npm_config_user_agent || '';
87
+ const npmExecpath = process.env.npm_execpath || '';
88
+ const npmCommand = process.env.npm_command || '';
89
+ const npmLifecycleEvent = process.env.npm_lifecycle_event || '';
90
+
91
+ // Check process arguments and parent commands
92
+ const processArgs = process.argv.join(' ');
93
+ const processTitle = process.title || '';
94
+
95
+ debug('Installation source detection...');
96
+ debug(`npm_config_user_agent: ${npmConfigUserAgent}`);
97
+ debug(`npm_execpath: ${npmExecpath}`);
98
+ debug(`npm_command: ${npmCommand}`);
99
+ debug(`npm_lifecycle_event: ${npmLifecycleEvent}`);
100
+ debug(`process.argv: ${processArgs}`);
101
+ debug(`process.title: ${processTitle}`);
102
+
103
+ // Try to get parent process information
104
+ let parentProcessInfo = null;
105
+ try {
106
+ const { execSync } = await import('child_process');
107
+ const ppid = process.ppid;
108
+ if (ppid && process.platform !== 'win32') {
109
+ // Get parent process command line on Unix systems
110
+ const parentCmd = execSync(`ps -p ${ppid} -o command=`, { encoding: 'utf8' }).trim();
111
+ parentProcessInfo = parentCmd;
112
+ debug(`parent process: ${parentCmd}`);
113
+ }
114
+ } catch (error) {
115
+ debug(`Could not get parent process info: ${error.message}`);
116
+ }
117
+
118
+ // Smithery detection - look for smithery in the process chain
119
+ const smitheryIndicators = [
120
+ npmConfigUserAgent.includes('smithery'),
121
+ npmExecpath.includes('smithery'),
122
+ processArgs.includes('smithery'),
123
+ processArgs.includes('@smithery/cli'),
124
+ processTitle.includes('smithery'),
125
+ parentProcessInfo && parentProcessInfo.includes('smithery'),
126
+ parentProcessInfo && parentProcessInfo.includes('@smithery/cli')
127
+ ];
128
+
129
+ if (smitheryIndicators.some(indicator => indicator)) {
130
+ return {
131
+ source: 'smithery',
132
+ details: {
133
+ detection_method: 'process_chain',
134
+ user_agent: npmConfigUserAgent,
135
+ exec_path: npmExecpath,
136
+ command: npmCommand,
137
+ parent_process: parentProcessInfo || 'unknown',
138
+ process_args: processArgs
139
+ }
140
+ };
141
+ }
142
+
143
+ // Direct NPX usage
144
+ if (npmCommand === 'exec' || processArgs.includes('npx')) {
145
+ return {
146
+ source: 'npx-direct',
147
+ details: {
148
+ user_agent: npmConfigUserAgent,
149
+ command: npmCommand,
150
+ lifecycle_event: npmLifecycleEvent
151
+ }
152
+ };
153
+ }
154
+
155
+ // Regular npm install
156
+ if (npmCommand === 'install' || npmLifecycleEvent === 'postinstall') {
157
+ return {
158
+ source: 'npm-install',
159
+ details: {
160
+ user_agent: npmConfigUserAgent,
161
+ command: npmCommand,
162
+ lifecycle_event: npmLifecycleEvent
163
+ }
164
+ };
165
+ }
166
+
167
+ // GitHub Codespaces
168
+ if (process.env.CODESPACES) {
169
+ return {
170
+ source: 'github-codespaces',
171
+ details: {
172
+ codespace: process.env.CODESPACE_NAME || 'unknown'
173
+ }
174
+ };
175
+ }
176
+
177
+ // VS Code
178
+ if (process.env.VSCODE_PID || process.env.TERM_PROGRAM === 'vscode') {
179
+ return {
180
+ source: 'vscode',
181
+ details: {
182
+ term_program: process.env.TERM_PROGRAM,
183
+ vscode_pid: process.env.VSCODE_PID
184
+ }
185
+ };
186
+ }
187
+
188
+ // GitPod
189
+ if (process.env.GITPOD_WORKSPACE_ID) {
190
+ return {
191
+ source: 'gitpod',
192
+ details: {
193
+ workspace_id: process.env.GITPOD_WORKSPACE_ID.substring(0, 8) + '...' // Truncate for privacy
194
+ }
195
+ };
196
+ }
197
+
198
+ // CI/CD environments
199
+ if (process.env.CI) {
200
+ if (process.env.GITHUB_ACTIONS) {
201
+ return {
202
+ source: 'github-actions',
203
+ details: {
204
+ repository: process.env.GITHUB_REPOSITORY,
205
+ workflow: process.env.GITHUB_WORKFLOW
206
+ }
207
+ };
208
+ }
209
+ if (process.env.GITLAB_CI) {
210
+ return {
211
+ source: 'gitlab-ci',
212
+ details: {
213
+ project: process.env.CI_PROJECT_NAME
214
+ }
215
+ };
216
+ }
217
+ if (process.env.JENKINS_URL) {
218
+ return {
219
+ source: 'jenkins',
220
+ details: {
221
+ job: process.env.JOB_NAME
222
+ }
223
+ };
224
+ }
225
+ return {
226
+ source: 'ci-cd-other',
227
+ details: {
228
+ ci_env: 'unknown'
229
+ }
230
+ };
231
+ }
232
+
233
+ // Docker detection
234
+ if (process.env.DOCKER_CONTAINER) {
235
+ return {
236
+ source: 'docker',
237
+ details: {
238
+ container_id: process.env.HOSTNAME?.substring(0, 8) + '...' || 'unknown'
239
+ }
240
+ };
241
+ }
242
+
243
+ // Check for .dockerenv file (need to use fs import)
244
+ try {
245
+ const fs = await import('fs');
246
+ if (fs.existsSync('/.dockerenv')) {
247
+ return {
248
+ source: 'docker',
249
+ details: {
250
+ container_id: process.env.HOSTNAME?.substring(0, 8) + '...' || 'unknown'
251
+ }
252
+ };
253
+ }
254
+ } catch (error) {
255
+ // Ignore fs errors
256
+ }
257
+
258
+ // Default fallback
259
+ return {
260
+ source: 'unknown',
261
+ details: {
262
+ user_agent: npmConfigUserAgent || 'none',
263
+ command: npmCommand || 'none',
264
+ lifecycle: npmLifecycleEvent || 'none'
265
+ }
266
+ };
267
+ }
268
+
269
+ /**
270
+ * Send installation tracking to analytics
271
+ */
272
+ async function trackInstallation(installationData) {
273
+ if (!GA_MEASUREMENT_ID || !GA_API_SECRET) {
274
+ debug('Analytics not configured, skipping tracking');
275
+ return;
276
+ }
277
+
278
+ try {
279
+ const uniqueUserId = await getClientId();
280
+ log("user id", uniqueUserId)
281
+ // Prepare GA4 payload
282
+ const payload = {
283
+ client_id: uniqueUserId,
284
+ non_personalized_ads: false,
285
+ timestamp_micros: Date.now() * 1000,
286
+ events: [{
287
+ name: 'package_installed',
288
+ params: {
289
+ timestamp: new Date().toISOString(),
290
+ platform: platform(),
291
+ installation_source: installationData.source,
292
+ installation_details: JSON.stringify(installationData.details),
293
+ package_name: '@wonderwhy-er/desktop-commander',
294
+ install_method: 'npm-lifecycle',
295
+ node_version: process.version,
296
+ npm_version: process.env.npm_version || 'unknown'
297
+ }
298
+ }]
299
+ };
300
+
301
+ const postData = JSON.stringify(payload);
302
+
303
+ const options = {
304
+ method: 'POST',
305
+ headers: {
306
+ 'Content-Type': 'application/json',
307
+ 'Content-Length': Buffer.byteLength(postData)
308
+ }
309
+ };
310
+
311
+ await new Promise((resolve, reject) => {
312
+ const req = https.request(GA_BASE_URL, options);
313
+
314
+ const timeoutId = setTimeout(() => {
315
+ req.destroy();
316
+ reject(new Error('Request timeout'));
317
+ }, 5000);
318
+
319
+ req.on('error', (error) => {
320
+ clearTimeout(timeoutId);
321
+ debug(`Analytics error: ${error.message}`);
322
+ resolve(); // Don't fail installation on analytics error
323
+ });
324
+
325
+ req.on('response', (res) => {
326
+ clearTimeout(timeoutId);
327
+ // Consume the response data to complete the request
328
+ res.on('data', () => {}); // Ignore response data
329
+ res.on('end', () => {
330
+ log(`Installation tracked: ${installationData.source}`);
331
+ resolve();
332
+ });
333
+ });
334
+
335
+ req.write(postData);
336
+ req.end();
337
+ });
338
+
339
+ } catch (error) {
340
+ debug(`Failed to track installation: ${error.message}`);
341
+ // Don't fail the installation process
342
+ }
343
+ }
344
+
345
+ /**
346
+ * Main execution
347
+ */
348
+ async function main() {
349
+ try {
350
+ log('Package installation detected');
351
+
352
+ const installationData = await detectInstallationSource();
353
+ log(`Installation source: ${installationData.source}`);
354
+
355
+ await trackInstallation(installationData);
356
+
357
+ } catch (error) {
358
+ debug(`Installation tracking error: ${error.message}`);
359
+ // Don't fail the installation
360
+ }
361
+ }
362
+
363
+ // Only run if this script is executed directly (not imported)
364
+ if (import.meta.url === `file://${process.argv[1]}`) {
365
+ main();
366
+ }
367
+
368
+ export { detectInstallationSource, trackInstallation };