@gurulu/cli 0.4.0 → 0.4.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/dist/commands/add-server.js +13 -6
- package/dist/commands/alerts.d.ts +5 -0
- package/dist/commands/alerts.js +43 -15
- package/dist/commands/audiences.d.ts +3 -0
- package/dist/commands/audiences.js +34 -7
- package/dist/commands/events.d.ts +6 -0
- package/dist/commands/events.js +182 -1
- package/dist/commands/experiments.d.ts +4 -0
- package/dist/commands/experiments.js +46 -15
- package/dist/commands/funnels.d.ts +17 -0
- package/dist/commands/funnels.js +203 -0
- package/dist/commands/goals.d.ts +18 -0
- package/dist/commands/goals.js +214 -0
- package/dist/commands/install.d.ts +8 -0
- package/dist/commands/install.js +57 -1
- package/dist/commands/sourcemap.d.ts +17 -5
- package/dist/commands/sourcemap.js +73 -6
- package/dist/commands/watch.d.ts +45 -0
- package/dist/commands/watch.js +258 -0
- package/dist/frameworks/detect.js +29 -7
- package/dist/index.js +158 -13
- package/package.json +1 -1
- package/scripts/gurulu-agentic-install.mjs +275 -3
- package/scripts/gurulu-scan.lib.cjs +539 -19
- package/scripts/patches/auto-instrument/ast-helper.cjs +158 -10
- package/scripts/patches/auto-instrument/astro.cjs +12 -6
- package/scripts/patches/auto-instrument/express.cjs +23 -8
- package/scripts/patches/auto-instrument/fastify.cjs +7 -3
- package/scripts/patches/auto-instrument/hono.cjs +392 -0
- package/scripts/patches/auto-instrument/index.cjs +2 -0
- package/scripts/patches/auto-instrument/nestjs.cjs +7 -3
- package/scripts/patches/auto-instrument/nextjs-app-router.cjs +40 -13
- package/scripts/patches/auto-instrument/nextjs-pages.cjs +23 -10
- package/scripts/patches/auto-instrument/remix.cjs +7 -3
- package/scripts/patches/auto-instrument/sdk-helper-map.cjs +241 -0
- package/scripts/patches/auto-instrument/sveltekit.cjs +7 -3
- package/scripts/patches/auto-instrument/vue.cjs +7 -3
- package/scripts/patches/index.cjs +6 -0
|
@@ -16,6 +16,9 @@
|
|
|
16
16
|
// apply transforms, then run `@babel/generator` to serialize the result.
|
|
17
17
|
// Parse errors are NOT swallowed — callers catch and fall back to regex.
|
|
18
18
|
|
|
19
|
+
const fs = require('fs');
|
|
20
|
+
const path = require('path');
|
|
21
|
+
|
|
19
22
|
const parser = require('@babel/parser');
|
|
20
23
|
// `@babel/traverse` exposes its default export under `.default` in CJS.
|
|
21
24
|
const traverseMod = require('@babel/traverse');
|
|
@@ -25,9 +28,93 @@ const generate = generatorMod.default || generatorMod;
|
|
|
25
28
|
const t = require('@babel/types');
|
|
26
29
|
|
|
27
30
|
const IMPORT_LINE = "import { gurulu } from '@/lib/gurulu';";
|
|
31
|
+
const HELPER_REL_PATH = 'src/lib/gurulu';
|
|
28
32
|
const MARKER = '@gurulu-instrumented';
|
|
29
33
|
const MARKER_COMMENT = `// @gurulu-instrumented`;
|
|
30
34
|
|
|
35
|
+
// ---------------------------------------------------------------------------
|
|
36
|
+
// Sprint D / D4 — tsconfig `@/*` path detection + relative-path fallback.
|
|
37
|
+
//
|
|
38
|
+
// Many auto-instrumented files want to write `import { gurulu } from
|
|
39
|
+
// '@/lib/gurulu'`. That works for the typical Next.js scaffold, but breaks
|
|
40
|
+
// in vanilla TS / Vite / Express setups whose `tsconfig.json` does not
|
|
41
|
+
// configure `compilerOptions.paths['@/*']`. Without the alias the inserted
|
|
42
|
+
// import resolves to a missing module and the patched file fails to
|
|
43
|
+
// compile.
|
|
44
|
+
//
|
|
45
|
+
// `resolveGuruluImportSpecifier(repoRoot, relativeRouteFile)` returns the
|
|
46
|
+
// import specifier the patcher should embed. When the alias is configured
|
|
47
|
+
// it returns `'@/lib/gurulu'`; otherwise it computes the POSIX-style
|
|
48
|
+
// relative path from the route file to `src/lib/gurulu` (e.g.
|
|
49
|
+
// `'../../lib/gurulu'`).
|
|
50
|
+
// ---------------------------------------------------------------------------
|
|
51
|
+
|
|
52
|
+
function stripJsonComments(text) {
|
|
53
|
+
// Tolerate `// ...` and `/* ... */` comments as found in tsconfig files.
|
|
54
|
+
return text
|
|
55
|
+
.replace(/\/\*[\s\S]*?\*\//g, '')
|
|
56
|
+
.replace(/(^|[^:\\])\/\/.*$/gm, '$1');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function readTsconfigPaths(repoRoot) {
|
|
60
|
+
const candidates = ['tsconfig.json', 'tsconfig.base.json', 'jsconfig.json'];
|
|
61
|
+
for (const rel of candidates) {
|
|
62
|
+
const abs = path.join(repoRoot, rel);
|
|
63
|
+
if (!fs.existsSync(abs)) continue;
|
|
64
|
+
try {
|
|
65
|
+
const raw = fs.readFileSync(abs, 'utf8');
|
|
66
|
+
const parsed = JSON.parse(stripJsonComments(raw));
|
|
67
|
+
const co = parsed && parsed.compilerOptions;
|
|
68
|
+
if (co && co.paths && typeof co.paths === 'object') {
|
|
69
|
+
return { paths: co.paths, baseUrl: co.baseUrl || '.' };
|
|
70
|
+
}
|
|
71
|
+
} catch {
|
|
72
|
+
// Ignore malformed tsconfig — fall through to the next candidate.
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Returns true when tsconfig.json (or jsconfig.json) declares
|
|
80
|
+
* `compilerOptions.paths['@/*']`. We accept any value — most projects map it
|
|
81
|
+
* to `['./src/*']` but custom roots like `['./*']` also count as wired.
|
|
82
|
+
*/
|
|
83
|
+
function hasAtAlias(repoRoot) {
|
|
84
|
+
const cfg = readTsconfigPaths(repoRoot);
|
|
85
|
+
if (!cfg) return false;
|
|
86
|
+
return Object.prototype.hasOwnProperty.call(cfg.paths, '@/*');
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Compute a POSIX-style relative import path from `fromFileRel` (a repo
|
|
91
|
+
* relative path like `src/app/api/checkout/route.ts`) to the singleton
|
|
92
|
+
* helper at `src/lib/gurulu`. The returned path is suitable for use as an
|
|
93
|
+
* ES module import specifier: it always starts with `./` or `../` and never
|
|
94
|
+
* carries a file extension.
|
|
95
|
+
*/
|
|
96
|
+
function relativeHelperSpecifier(fromFileRel) {
|
|
97
|
+
const fromDir = path.posix.dirname(fromFileRel.split(path.sep).join('/'));
|
|
98
|
+
let rel = path.posix.relative(fromDir, HELPER_REL_PATH);
|
|
99
|
+
if (!rel) rel = '.';
|
|
100
|
+
if (!rel.startsWith('.')) rel = `./${rel}`;
|
|
101
|
+
return rel;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Pick the right import specifier for `import { gurulu } from <here>`
|
|
106
|
+
* given the repo root and the file the import will be written into.
|
|
107
|
+
*
|
|
108
|
+
* - When `@/*` is configured in tsconfig, prefer the alias for clean diffs
|
|
109
|
+
* that match the singleton helper documentation.
|
|
110
|
+
* - Otherwise fall back to a relative import computed from the route file.
|
|
111
|
+
*/
|
|
112
|
+
function resolveGuruluImportSpecifier(repoRoot, fromFileRel) {
|
|
113
|
+
if (repoRoot && hasAtAlias(repoRoot)) return '@/lib/gurulu';
|
|
114
|
+
if (fromFileRel) return relativeHelperSpecifier(fromFileRel);
|
|
115
|
+
return '@/lib/gurulu';
|
|
116
|
+
}
|
|
117
|
+
|
|
31
118
|
const DEFAULT_PARSE_PLUGINS = [
|
|
32
119
|
'typescript',
|
|
33
120
|
'jsx',
|
|
@@ -96,12 +183,62 @@ function tagInstrumented(node) {
|
|
|
96
183
|
t.addComment(node, 'leading', ` @gurulu-instrumented`, true);
|
|
97
184
|
}
|
|
98
185
|
|
|
186
|
+
// Sprint D / D1 — typed-helper selector. Lives in its own CJS module so the
|
|
187
|
+
// auto-instrumenter can swap `gurulu.track('$purchase', {...})` for the
|
|
188
|
+
// canonical `gurulu.purchase({...})` whenever the LLM-extracted properties
|
|
189
|
+
// satisfy the helper's required fields. When `selectHelper` returns null we
|
|
190
|
+
// fall through to the legacy generic-track shape — no behaviour change for
|
|
191
|
+
// custom (non-canonical) events.
|
|
192
|
+
const sdkHelperMap = require('./sdk-helper-map.cjs');
|
|
193
|
+
|
|
194
|
+
function safeParseExpression(text) {
|
|
195
|
+
return parser.parseExpression(text, { plugins: DEFAULT_PARSE_PLUGINS });
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function buildTypedHelperCall(eventName, autoProperties) {
|
|
199
|
+
const helper = sdkHelperMap.selectHelper(eventName, autoProperties);
|
|
200
|
+
if (!helper) return null;
|
|
201
|
+
const argNodes = [];
|
|
202
|
+
for (const expr of helper.argExpressions) {
|
|
203
|
+
try {
|
|
204
|
+
argNodes.push(safeParseExpression(expr));
|
|
205
|
+
} catch (_) {
|
|
206
|
+
// Defensive: if any arg expression is malformed, abort the typed
|
|
207
|
+
// upgrade and let the caller emit `gurulu.track(...)` instead.
|
|
208
|
+
return null;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return t.expressionStatement(
|
|
212
|
+
t.callExpression(
|
|
213
|
+
t.memberExpression(t.identifier('gurulu'), t.identifier(helper.method)),
|
|
214
|
+
argNodes,
|
|
215
|
+
),
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
|
|
99
219
|
/**
|
|
100
220
|
* Build the `gurulu.track('eventName', { ...properties })` statement. Empty
|
|
101
221
|
* object when no auto-properties; otherwise an ObjectExpression with the
|
|
102
222
|
* property AST nodes inlined.
|
|
223
|
+
*
|
|
224
|
+
* Sprint D / D1: when `eventName` is a canonical event ($purchase, $signup,
|
|
225
|
+
* etc.) AND the extracted properties supply every required field, we emit
|
|
226
|
+
* the typed helper instead — `gurulu.purchase({ value, currency })` rather
|
|
227
|
+
* than `gurulu.track('$purchase', { ... })`. The marker comment is identical
|
|
228
|
+
* in either form so idempotency checks still work.
|
|
103
229
|
*/
|
|
104
|
-
function buildTrackStatement(eventName, autoProperties) {
|
|
230
|
+
function buildTrackStatement(eventName, autoProperties, opts = {}) {
|
|
231
|
+
// Try the typed helper first. Tests and patcher modules pass `opts.preferTyped =
|
|
232
|
+
// false` to opt out (e.g. when verifying the legacy shape). Default is true.
|
|
233
|
+
const preferTyped = opts.preferTyped !== false;
|
|
234
|
+
if (preferTyped) {
|
|
235
|
+
const typed = buildTypedHelperCall(eventName, autoProperties);
|
|
236
|
+
if (typed) {
|
|
237
|
+
t.addComment(typed, 'leading', ` @gurulu-instrumented ${eventName}`, true);
|
|
238
|
+
return typed;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
105
242
|
const args = [t.stringLiteral(eventName)];
|
|
106
243
|
if (Array.isArray(autoProperties) && autoProperties.length > 0) {
|
|
107
244
|
const props = [];
|
|
@@ -111,10 +248,7 @@ function buildTrackStatement(eventName, autoProperties) {
|
|
|
111
248
|
let valueNode;
|
|
112
249
|
if (p.source && typeof p.source === 'string') {
|
|
113
250
|
try {
|
|
114
|
-
|
|
115
|
-
plugins: DEFAULT_PARSE_PLUGINS,
|
|
116
|
-
});
|
|
117
|
-
valueNode = parsedExpr;
|
|
251
|
+
valueNode = safeParseExpression(p.source);
|
|
118
252
|
} catch (_) {
|
|
119
253
|
valueNode = t.stringLiteral(String(p.source));
|
|
120
254
|
}
|
|
@@ -193,10 +327,13 @@ function injectTrackBeforeLastReturn(fn, body, trackStatements) {
|
|
|
193
327
|
}
|
|
194
328
|
|
|
195
329
|
/**
|
|
196
|
-
* Ensure `import { gurulu } from
|
|
197
|
-
* import already
|
|
330
|
+
* Ensure `import { gurulu } from <specifier>` is present. No-op when an
|
|
331
|
+
* existing import already binds the `gurulu` named export from a path
|
|
332
|
+
* pointing at our singleton helper (alias OR relative). Sprint D / D4: the
|
|
333
|
+
* specifier is derived from tsconfig — callers may pass an explicit one.
|
|
198
334
|
*/
|
|
199
|
-
function ensureGuruluImport(ast) {
|
|
335
|
+
function ensureGuruluImport(ast, specifierOpt) {
|
|
336
|
+
const specifier = specifierOpt || '@/lib/gurulu';
|
|
200
337
|
let present = false;
|
|
201
338
|
let lastImportIdx = -1;
|
|
202
339
|
const body = ast.program.body;
|
|
@@ -204,7 +341,12 @@ function ensureGuruluImport(ast) {
|
|
|
204
341
|
const node = body[i];
|
|
205
342
|
if (t.isImportDeclaration(node)) {
|
|
206
343
|
lastImportIdx = i;
|
|
207
|
-
|
|
344
|
+
const src = node.source && node.source.value;
|
|
345
|
+
if (
|
|
346
|
+
src === specifier ||
|
|
347
|
+
src === '@/lib/gurulu' ||
|
|
348
|
+
(typeof src === 'string' && /(^|\/)lib\/gurulu$/.test(src))
|
|
349
|
+
) {
|
|
208
350
|
const hasNamed = (node.specifiers || []).some(
|
|
209
351
|
(s) => t.isImportSpecifier(s) && t.isIdentifier(s.imported) && s.imported.name === 'gurulu',
|
|
210
352
|
);
|
|
@@ -215,7 +357,7 @@ function ensureGuruluImport(ast) {
|
|
|
215
357
|
if (present) return;
|
|
216
358
|
const imp = t.importDeclaration(
|
|
217
359
|
[t.importSpecifier(t.identifier('gurulu'), t.identifier('gurulu'))],
|
|
218
|
-
t.stringLiteral(
|
|
360
|
+
t.stringLiteral(specifier),
|
|
219
361
|
);
|
|
220
362
|
body.splice(lastImportIdx + 1, 0, imp);
|
|
221
363
|
}
|
|
@@ -323,10 +465,16 @@ module.exports = {
|
|
|
323
465
|
hasInstrumentedMarker,
|
|
324
466
|
tagInstrumented,
|
|
325
467
|
buildTrackStatement,
|
|
468
|
+
buildTypedHelperCall,
|
|
326
469
|
injectTrackBeforeLastReturn,
|
|
327
470
|
ensureGuruluImport,
|
|
328
471
|
findExportedFunction,
|
|
472
|
+
hasAtAlias,
|
|
473
|
+
relativeHelperSpecifier,
|
|
474
|
+
resolveGuruluImportSpecifier,
|
|
475
|
+
sdkHelperMap,
|
|
329
476
|
IMPORT_LINE,
|
|
477
|
+
HELPER_REL_PATH,
|
|
330
478
|
MARKER,
|
|
331
479
|
MARKER_COMMENT,
|
|
332
480
|
};
|
|
@@ -69,7 +69,7 @@ function hasDataLoadingPattern(frontmatterSource) {
|
|
|
69
69
|
* `---` fences via the shared AST helper, injects gurulu.track() calls, and
|
|
70
70
|
* reconstructs the full .astro file.
|
|
71
71
|
*/
|
|
72
|
-
function astInstrumentFrontmatter(source, events) {
|
|
72
|
+
function astInstrumentFrontmatter(source, events, opts = {}) {
|
|
73
73
|
const fmMatch = source.match(FRONTMATTER_RE);
|
|
74
74
|
if (!fmMatch) return { ok: false, reason: 'no-frontmatter' };
|
|
75
75
|
|
|
@@ -123,7 +123,9 @@ function astInstrumentFrontmatter(source, events) {
|
|
|
123
123
|
body.body.push(...stmts);
|
|
124
124
|
}
|
|
125
125
|
|
|
126
|
-
|
|
126
|
+
// Sprint D / D4 — alias-aware import.
|
|
127
|
+
const fmSpecifier = ast.resolveGuruluImportSpecifier(opts.repoRoot, opts.relPath);
|
|
128
|
+
ast.ensureGuruluImport(tree, fmSpecifier);
|
|
127
129
|
const newFrontmatter = ast.generateSource(tree, frontmatter);
|
|
128
130
|
|
|
129
131
|
// Reconstruct the full .astro file
|
|
@@ -140,7 +142,7 @@ function astInstrumentFrontmatter(source, events) {
|
|
|
140
142
|
};
|
|
141
143
|
}
|
|
142
144
|
|
|
143
|
-
function astInstrumentFile(source, method, events) {
|
|
145
|
+
function astInstrumentFile(source, method, events, opts = {}) {
|
|
144
146
|
const tree = ast.parseSource(source);
|
|
145
147
|
const fns = ast.findExportedFunction(tree, method);
|
|
146
148
|
if (fns.length === 0) return { ok: false, reason: `${method}-not-found` };
|
|
@@ -156,7 +158,9 @@ function astInstrumentFile(source, method, events) {
|
|
|
156
158
|
}
|
|
157
159
|
const stmts = events.map((e) => ast.buildTrackStatement(e.name, e.autoProperties));
|
|
158
160
|
ast.injectTrackBeforeLastReturn(target.fn, target.body, stmts);
|
|
159
|
-
|
|
161
|
+
// Sprint D / D4 — alias-aware import.
|
|
162
|
+
const specifier = ast.resolveGuruluImportSpecifier(opts.repoRoot, opts.relPath);
|
|
163
|
+
ast.ensureGuruluImport(tree, specifier);
|
|
160
164
|
const after = ast.generateSource(tree, source);
|
|
161
165
|
return {
|
|
162
166
|
ok: true,
|
|
@@ -208,12 +212,14 @@ function instrumentEvents(ctx, events) {
|
|
|
208
212
|
for (const group of groups.values()) {
|
|
209
213
|
const abs = path.join(ctx.repoRoot, group.relPath);
|
|
210
214
|
const before = fs.readFileSync(abs, 'utf8');
|
|
215
|
+
// Sprint D / D4 — pass repoRoot + relPath to the import resolver.
|
|
216
|
+
const fileOpts = { repoRoot: (ctx && ctx.repoRoot) || null, relPath: group.relPath };
|
|
211
217
|
let res;
|
|
212
218
|
try {
|
|
213
219
|
const isAstroComponent = group.relPath.endsWith('.astro');
|
|
214
220
|
res = isAstroComponent
|
|
215
|
-
? astInstrumentFrontmatter(before, group.events)
|
|
216
|
-
: astInstrumentFile(before, group.method, group.events);
|
|
221
|
+
? astInstrumentFrontmatter(before, group.events, fileOpts)
|
|
222
|
+
: astInstrumentFile(before, group.method, group.events, fileOpts);
|
|
217
223
|
} catch (err) {
|
|
218
224
|
const msg = (err && err.message) || String(err);
|
|
219
225
|
// eslint-disable-next-line no-console
|
|
@@ -91,7 +91,7 @@ function findRouteHandlers(tree, method, urlPath) {
|
|
|
91
91
|
return results;
|
|
92
92
|
}
|
|
93
93
|
|
|
94
|
-
function astInstrumentFile(source, method, urlPath, events) {
|
|
94
|
+
function astInstrumentFile(source, method, urlPath, events, opts = {}) {
|
|
95
95
|
const tree = ast.parseSource(source);
|
|
96
96
|
const handlers = findRouteHandlers(tree, method, urlPath);
|
|
97
97
|
if (handlers.length === 0) {
|
|
@@ -109,7 +109,9 @@ function astInstrumentFile(source, method, urlPath, events) {
|
|
|
109
109
|
}
|
|
110
110
|
const stmts = events.map((e) => ast.buildTrackStatement(e.name, e.autoProperties));
|
|
111
111
|
ast.injectTrackBeforeLastReturn(target.fn, target.body, stmts);
|
|
112
|
-
|
|
112
|
+
// Sprint D / D4 — alias-aware import (Express usually has no `@/*` alias).
|
|
113
|
+
const specifier = ast.resolveGuruluImportSpecifier(opts.repoRoot, opts.relPath);
|
|
114
|
+
ast.ensureGuruluImport(tree, specifier);
|
|
113
115
|
const after = ast.generateSource(tree, source);
|
|
114
116
|
return {
|
|
115
117
|
ok: true,
|
|
@@ -181,10 +183,21 @@ function regexFindLastResponseCall(source, start, end) {
|
|
|
181
183
|
return { insertAt: lineStart, indent };
|
|
182
184
|
}
|
|
183
185
|
|
|
184
|
-
function regexEnsureImport(source) {
|
|
186
|
+
function regexEnsureImport(source, opts = {}) {
|
|
185
187
|
if (source.includes(IMPORT_LINE_ESM) || source.includes(IMPORT_LINE_CJS)) return source;
|
|
188
|
+
// Sprint D / D4 — alias-aware specifier. ESM-only branch picks `@/lib/gurulu`
|
|
189
|
+
// when tsconfig declares it, otherwise a relative import. The CJS branch
|
|
190
|
+
// continues to use `./lib/gurulu` since CommonJS does not honor TS paths.
|
|
186
191
|
const isCjs = /\brequire\s*\(/.test(source) && !/^import\s/m.test(source);
|
|
187
|
-
|
|
192
|
+
let line;
|
|
193
|
+
if (isCjs) {
|
|
194
|
+
line = IMPORT_LINE_CJS;
|
|
195
|
+
} else if (opts.repoRoot) {
|
|
196
|
+
const specifier = ast.resolveGuruluImportSpecifier(opts.repoRoot, opts.relPath);
|
|
197
|
+
line = `import { gurulu } from '${specifier}';`;
|
|
198
|
+
} else {
|
|
199
|
+
line = IMPORT_LINE_ESM;
|
|
200
|
+
}
|
|
188
201
|
const importRegex = /^(?:import[\s\S]*?;\s*\n)+/m;
|
|
189
202
|
const requireRegex = /^(?:const\s[\s\S]*?require\([\s\S]*?\);\s*\n)+/m;
|
|
190
203
|
const m = source.match(importRegex) || source.match(requireRegex);
|
|
@@ -200,7 +213,7 @@ function regexBuildTrackCall(eventName, indent) {
|
|
|
200
213
|
return `${indent}${MARKER} ${eventName}\n${indent}gurulu.track(${safeName}, {});\n`;
|
|
201
214
|
}
|
|
202
215
|
|
|
203
|
-
function regexInstrumentFile(before, method, urlPath, events) {
|
|
216
|
+
function regexInstrumentFile(before, method, urlPath, events, opts = {}) {
|
|
204
217
|
const body = regexFindHandlerStart(before, method, urlPath);
|
|
205
218
|
if (!body) return { ok: false, reason: 'handler-not-found' };
|
|
206
219
|
const snippet = before.slice(body.start, body.end);
|
|
@@ -218,7 +231,7 @@ function regexInstrumentFile(before, method, urlPath, events) {
|
|
|
218
231
|
if (!ret) return { ok: false, reason: 'no-response-found' };
|
|
219
232
|
const block = needed.map((e) => regexBuildTrackCall(e.name, ret.indent)).join('');
|
|
220
233
|
let after = before.slice(0, ret.insertAt) + block + before.slice(ret.insertAt);
|
|
221
|
-
after = regexEnsureImport(after);
|
|
234
|
+
after = regexEnsureImport(after, opts);
|
|
222
235
|
return {
|
|
223
236
|
ok: true,
|
|
224
237
|
after,
|
|
@@ -305,15 +318,17 @@ function instrumentEvents(ctx, events) {
|
|
|
305
318
|
for (const group of groups.values()) {
|
|
306
319
|
const abs = path.join(ctx.repoRoot, group.relPath);
|
|
307
320
|
const before = fs.readFileSync(abs, 'utf8');
|
|
321
|
+
// Sprint D / D4 — thread repoRoot + relPath into the import resolver.
|
|
322
|
+
const fileOpts = { repoRoot: (ctx && ctx.repoRoot) || null, relPath: group.relPath };
|
|
308
323
|
let res;
|
|
309
324
|
try {
|
|
310
|
-
res = astInstrumentFile(before, group.method, group.urlPath, group.events);
|
|
325
|
+
res = astInstrumentFile(before, group.method, group.urlPath, group.events, fileOpts);
|
|
311
326
|
} catch (err) {
|
|
312
327
|
const msg = (err && err.message) || String(err);
|
|
313
328
|
// eslint-disable-next-line no-console
|
|
314
329
|
console.warn(`[auto-instrument] patch.fallback ${group.relPath}: ${msg}`);
|
|
315
330
|
notes.push(`patch.fallback:${group.relPath}:${msg}`);
|
|
316
|
-
res = regexInstrumentFile(before, group.method, group.urlPath, group.events);
|
|
331
|
+
res = regexInstrumentFile(before, group.method, group.urlPath, group.events, fileOpts);
|
|
317
332
|
}
|
|
318
333
|
if (!res.ok) {
|
|
319
334
|
for (const e of group.events) {
|
|
@@ -104,7 +104,7 @@ function findFastifyHandlers(tree, method, urlPath) {
|
|
|
104
104
|
return results;
|
|
105
105
|
}
|
|
106
106
|
|
|
107
|
-
function astInstrumentFile(source, method, urlPath, events) {
|
|
107
|
+
function astInstrumentFile(source, method, urlPath, events, opts = {}) {
|
|
108
108
|
const tree = ast.parseSource(source);
|
|
109
109
|
const handlers = findFastifyHandlers(tree, method, urlPath);
|
|
110
110
|
if (handlers.length === 0) return { ok: false, reason: 'handler-not-found' };
|
|
@@ -120,7 +120,9 @@ function astInstrumentFile(source, method, urlPath, events) {
|
|
|
120
120
|
}
|
|
121
121
|
const stmts = events.map((e) => ast.buildTrackStatement(e.name, e.autoProperties));
|
|
122
122
|
ast.injectTrackBeforeLastReturn(target.fn, target.body, stmts);
|
|
123
|
-
|
|
123
|
+
// Sprint D / D4 — alias-aware import.
|
|
124
|
+
const specifier = ast.resolveGuruluImportSpecifier(opts.repoRoot, opts.relPath);
|
|
125
|
+
ast.ensureGuruluImport(tree, specifier);
|
|
124
126
|
const after = ast.generateSource(tree, source);
|
|
125
127
|
return {
|
|
126
128
|
ok: true,
|
|
@@ -197,9 +199,11 @@ function instrumentEvents(ctx, events) {
|
|
|
197
199
|
for (const group of groups.values()) {
|
|
198
200
|
const abs = path.join(ctx.repoRoot, group.relPath);
|
|
199
201
|
const before = fs.readFileSync(abs, 'utf8');
|
|
202
|
+
// Sprint D / D4 — pass repoRoot + relPath to the import resolver.
|
|
203
|
+
const fileOpts = { repoRoot: (ctx && ctx.repoRoot) || null, relPath: group.relPath };
|
|
200
204
|
let res;
|
|
201
205
|
try {
|
|
202
|
-
res = astInstrumentFile(before, group.method, group.urlPath, group.events);
|
|
206
|
+
res = astInstrumentFile(before, group.method, group.urlPath, group.events, fileOpts);
|
|
203
207
|
} catch (err) {
|
|
204
208
|
const msg = (err && err.message) || String(err);
|
|
205
209
|
// eslint-disable-next-line no-console
|