neonctl 2.21.0 → 2.21.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.
Files changed (3) hide show
  1. package/analytics.js +23 -6
  2. package/index.js +9 -8
  3. package/package.json +2 -2
package/analytics.js CHANGED
@@ -11,11 +11,33 @@ const WRITE_KEY = '3SQXn5ejjXWLEJ8xU2PRYhAotLtTaeeV';
11
11
  let client;
12
12
  let clientInitialized = false;
13
13
  let userId = '';
14
- export const analyticsMiddleware = async (args) => {
14
+ /**
15
+ * Phase 1: Run before validation so the Segment client exists if any
16
+ * middleware (e.g. auth) fails. Enables sendError() in the fail handler.
17
+ * Does not resolve user id or send CLI Started.
18
+ */
19
+ export const initAnalyticsClientMiddleware = (args) => {
15
20
  if (!args.analytics || clientInitialized) {
16
21
  return;
17
22
  }
18
23
  clientInitialized = true;
24
+ client = new Analytics({
25
+ writeKey: WRITE_KEY,
26
+ host: 'https://track.neon.tech',
27
+ });
28
+ log.debug('Initialized CLI analytics client');
29
+ client.identify({
30
+ userId: 'anonymous',
31
+ });
32
+ };
33
+ /**
34
+ * Phase 2: Run after auth. Resolves user id from credentials,
35
+ * identifies the user, and sends CLI Started.
36
+ */
37
+ export const analyticsMiddleware = async (args) => {
38
+ if (!client || !args.analytics) {
39
+ return;
40
+ }
19
41
  try {
20
42
  const credentialsPath = join(args.configDir, CREDENTIALS_FILE);
21
43
  const credentials = readFileSync(credentialsPath, { encoding: 'utf-8' });
@@ -50,11 +72,6 @@ export const analyticsMiddleware = async (args) => {
50
72
  catch (err) {
51
73
  log.debug('Failed to get user id from api', err);
52
74
  }
53
- client = new Analytics({
54
- writeKey: WRITE_KEY,
55
- host: 'https://track.neon.tech',
56
- });
57
- log.debug('Initialized CLI analytics');
58
75
  client.identify({
59
76
  userId: userId?.toString() ?? 'anonymous',
60
77
  });
package/index.js CHANGED
@@ -20,7 +20,7 @@ import { defaultClientID } from './auth.js';
20
20
  import { fillInArgs } from './utils/middlewares.js';
21
21
  import pkg from './pkg.js';
22
22
  import commands from './commands/index.js';
23
- import { analyticsMiddleware, closeAnalytics, getAnalyticsEventProperties, sendError, trackEvent, } from './analytics.js';
23
+ import { analyticsMiddleware, initAnalyticsClientMiddleware, closeAnalytics, getAnalyticsEventProperties, sendError, trackEvent, } from './analytics.js';
24
24
  import { isAxiosError } from 'axios';
25
25
  import { matchErrorCode } from './errors.js';
26
26
  import { showHelp } from './help.js';
@@ -107,10 +107,17 @@ builder = builder
107
107
  type: 'boolean',
108
108
  default: true,
109
109
  },
110
+ analytics: {
111
+ describe: 'Manage analytics. Example: --no-analytics, --analytics false',
112
+ group: 'Global options:',
113
+ type: 'boolean',
114
+ default: true,
115
+ },
110
116
  })
111
117
  .middleware((args) => {
112
118
  fillInArgs(args);
113
119
  }, true)
120
+ .middleware(initAnalyticsClientMiddleware, true)
114
121
  .help(false)
115
122
  .group('help', 'Global options:')
116
123
  .option('help', {
@@ -128,15 +135,9 @@ builder = builder
128
135
  })
129
136
  .middleware(ensureAuth)
130
137
  .middleware(enrichFromContext)
138
+ .middleware(analyticsMiddleware)
131
139
  .command(commands)
132
140
  .strictCommands()
133
- .option('analytics', {
134
- describe: 'Manage analytics. Example: --no-analytics, --analytics false',
135
- group: 'Global options:',
136
- type: 'boolean',
137
- default: true,
138
- })
139
- .middleware(analyticsMiddleware, true)
140
141
  .version(pkg.version)
141
142
  .group('version', 'Global options:')
142
143
  .alias('version', 'v')
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "url": "git+ssh://git@github.com/neondatabase/neonctl.git"
6
6
  },
7
7
  "type": "module",
8
- "version": "2.21.0",
8
+ "version": "2.21.2",
9
9
  "description": "CLI tool for NeonDB Cloud management",
10
10
  "main": "index.js",
11
11
  "author": "NeonDB",
@@ -62,7 +62,7 @@
62
62
  "cli-table": "^0.3.11",
63
63
  "crypto-random-string": "^5.0.0",
64
64
  "diff": "^5.2.0",
65
- "neon-init": "^0.13.0",
65
+ "neon-init": "^0.13.1",
66
66
  "open": "^10.1.0",
67
67
  "openid-client": "^6.8.1",
68
68
  "prompts": "2.4.2",