@grafema/cli 0.2.5-beta → 0.2.6-beta
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/README.md +12 -0
- package/dist/cli.js +6 -2
- package/dist/cli.js.map +1 -1
- package/dist/commands/analyze.d.ts +3 -10
- package/dist/commands/analyze.d.ts.map +1 -1
- package/dist/commands/analyze.js +5 -347
- package/dist/commands/analyze.js.map +1 -1
- package/dist/commands/analyzeAction.d.ts +28 -0
- package/dist/commands/analyzeAction.d.ts.map +1 -0
- package/dist/commands/analyzeAction.js +243 -0
- package/dist/commands/analyzeAction.js.map +1 -0
- package/dist/commands/check.js +2 -2
- package/dist/commands/check.js.map +1 -1
- package/dist/commands/context.d.ts +16 -0
- package/dist/commands/context.d.ts.map +1 -0
- package/dist/commands/context.js +238 -0
- package/dist/commands/context.js.map +1 -0
- package/dist/commands/doctor/checks.js +1 -1
- package/dist/commands/doctor/checks.js.map +1 -1
- package/dist/commands/explain.d.ts.map +1 -1
- package/dist/commands/explain.js +4 -3
- package/dist/commands/explain.js.map +1 -1
- package/dist/commands/file.d.ts +15 -0
- package/dist/commands/file.d.ts.map +1 -0
- package/dist/commands/file.js +144 -0
- package/dist/commands/file.js.map +1 -0
- package/dist/commands/impact.d.ts.map +1 -1
- package/dist/commands/impact.js +2 -3
- package/dist/commands/impact.js.map +1 -1
- package/dist/commands/init.d.ts.map +1 -1
- package/dist/commands/init.js +13 -1
- package/dist/commands/init.js.map +1 -1
- package/dist/commands/ls.d.ts.map +1 -1
- package/dist/commands/ls.js +3 -2
- package/dist/commands/ls.js.map +1 -1
- package/dist/commands/query.d.ts +8 -0
- package/dist/commands/query.d.ts.map +1 -1
- package/dist/commands/query.js +158 -51
- package/dist/commands/query.js.map +1 -1
- package/dist/commands/schema.d.ts.map +1 -1
- package/dist/commands/schema.js +3 -2
- package/dist/commands/schema.js.map +1 -1
- package/dist/commands/server.d.ts.map +1 -1
- package/dist/commands/server.js +8 -59
- package/dist/commands/server.js.map +1 -1
- package/dist/commands/setup-skill.d.ts +17 -0
- package/dist/commands/setup-skill.d.ts.map +1 -0
- package/dist/commands/setup-skill.js +131 -0
- package/dist/commands/setup-skill.js.map +1 -0
- package/dist/commands/trace.d.ts.map +1 -1
- package/dist/commands/trace.js +20 -10
- package/dist/commands/trace.js.map +1 -1
- package/dist/plugins/builtinPlugins.d.ts +10 -0
- package/dist/plugins/builtinPlugins.d.ts.map +1 -0
- package/dist/plugins/builtinPlugins.js +68 -0
- package/dist/plugins/builtinPlugins.js.map +1 -0
- package/dist/plugins/pluginLoader.d.ts +16 -0
- package/dist/plugins/pluginLoader.d.ts.map +1 -0
- package/dist/plugins/pluginLoader.js +101 -0
- package/dist/plugins/pluginLoader.js.map +1 -0
- package/dist/plugins/pluginResolver.js +38 -0
- package/dist/utils/codePreview.d.ts +1 -0
- package/dist/utils/codePreview.d.ts.map +1 -1
- package/dist/utils/codePreview.js +5 -3
- package/dist/utils/codePreview.js.map +1 -1
- package/dist/utils/formatNode.d.ts +1 -1
- package/dist/utils/formatNode.d.ts.map +1 -1
- package/dist/utils/formatNode.js +2 -2
- package/dist/utils/formatNode.js.map +1 -1
- package/dist/utils/pathUtils.d.ts +2 -0
- package/dist/utils/pathUtils.d.ts.map +1 -0
- package/dist/utils/pathUtils.js +9 -0
- package/dist/utils/pathUtils.js.map +1 -0
- package/package.json +7 -9
- package/skills/grafema-codebase-analysis/SKILL.md +295 -0
- package/skills/grafema-codebase-analysis/references/node-edge-types.md +123 -0
- package/skills/grafema-codebase-analysis/references/query-patterns.md +205 -0
- package/src/cli.ts +8 -2
- package/src/commands/analyze.ts +5 -435
- package/src/commands/analyzeAction.ts +284 -0
- package/src/commands/check.ts +2 -2
- package/src/commands/context.ts +309 -0
- package/src/commands/doctor/checks.ts +1 -1
- package/src/commands/explain.ts +4 -3
- package/src/commands/explore.tsx +7 -5
- package/src/commands/file.ts +179 -0
- package/src/commands/impact.ts +2 -3
- package/src/commands/init.ts +13 -1
- package/src/commands/ls.ts +3 -2
- package/src/commands/query.ts +167 -52
- package/src/commands/schema.ts +3 -2
- package/src/commands/server.ts +8 -64
- package/src/commands/setup-skill.ts +162 -0
- package/src/commands/trace.ts +18 -9
- package/src/plugins/builtinPlugins.ts +108 -0
- package/src/plugins/pluginLoader.ts +123 -0
- package/src/plugins/pluginResolver.js +38 -0
- package/src/utils/codePreview.ts +7 -3
- package/src/utils/formatNode.ts +3 -3
- package/src/utils/pathUtils.ts +9 -0
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Plugin loading — resolves built-in and custom plugins from config.
|
|
3
|
+
*
|
|
4
|
+
* Handles:
|
|
5
|
+
* - ESM resolve hook for custom plugin @grafema/* imports
|
|
6
|
+
* - Loading custom plugins from .grafema/plugins/
|
|
7
|
+
* - Creating plugin instances from config phases
|
|
8
|
+
*/
|
|
9
|
+
import { join } from 'path';
|
|
10
|
+
import { existsSync, readdirSync } from 'fs';
|
|
11
|
+
import { pathToFileURL } from 'url';
|
|
12
|
+
import { register } from 'node:module';
|
|
13
|
+
import { BUILTIN_PLUGINS } from './builtinPlugins.js';
|
|
14
|
+
/**
|
|
15
|
+
* Register ESM resolve hook so custom plugins can import @grafema/* packages.
|
|
16
|
+
*
|
|
17
|
+
* Plugins in .grafema/plugins/ do `import { Plugin } from '@grafema/core'`,
|
|
18
|
+
* but @grafema/core isn't in the target project's node_modules/.
|
|
19
|
+
* This hook redirects those imports to the CLI's bundled packages.
|
|
20
|
+
*
|
|
21
|
+
* Uses module.register() (stable Node.js 20.6+ API).
|
|
22
|
+
* Safe to call multiple times — subsequent calls add redundant hooks
|
|
23
|
+
* that short-circuit on the same specifiers.
|
|
24
|
+
*/
|
|
25
|
+
let pluginResolverRegistered = false;
|
|
26
|
+
export function registerPluginResolver() {
|
|
27
|
+
if (pluginResolverRegistered)
|
|
28
|
+
return;
|
|
29
|
+
pluginResolverRegistered = true;
|
|
30
|
+
const grafemaPackages = {};
|
|
31
|
+
for (const pkg of ['@grafema/core', '@grafema/types']) {
|
|
32
|
+
try {
|
|
33
|
+
grafemaPackages[pkg] = import.meta.resolve(pkg);
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
// Package not available from CLI context — skip
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
register(new URL('./pluginResolver.js', import.meta.url), { data: { grafemaPackages } });
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Load custom plugins from .grafema/plugins/ directory
|
|
43
|
+
*/
|
|
44
|
+
export async function loadCustomPlugins(projectPath, log) {
|
|
45
|
+
const pluginsDir = join(projectPath, '.grafema', 'plugins');
|
|
46
|
+
if (!existsSync(pluginsDir)) {
|
|
47
|
+
return {};
|
|
48
|
+
}
|
|
49
|
+
// Ensure @grafema/* imports resolve for custom plugins (REG-380)
|
|
50
|
+
registerPluginResolver();
|
|
51
|
+
const customPlugins = {};
|
|
52
|
+
try {
|
|
53
|
+
const files = readdirSync(pluginsDir).filter((f) => f.endsWith('.js') || f.endsWith('.mjs') || f.endsWith('.cjs'));
|
|
54
|
+
for (const file of files) {
|
|
55
|
+
try {
|
|
56
|
+
const pluginPath = join(pluginsDir, file);
|
|
57
|
+
const pluginUrl = pathToFileURL(pluginPath).href;
|
|
58
|
+
const module = await import(pluginUrl);
|
|
59
|
+
const PluginClass = module.default || module[file.replace(/\.[cm]?js$/, '')];
|
|
60
|
+
if (PluginClass && typeof PluginClass === 'function') {
|
|
61
|
+
const pluginName = PluginClass.name || file.replace(/\.[cm]?js$/, '');
|
|
62
|
+
customPlugins[pluginName] = () => {
|
|
63
|
+
const instance = new PluginClass();
|
|
64
|
+
instance.config.sourceFile = pluginPath;
|
|
65
|
+
return instance;
|
|
66
|
+
};
|
|
67
|
+
log(`Loaded custom plugin: ${pluginName}`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
catch (err) {
|
|
71
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
72
|
+
console.warn(`Failed to load plugin ${file}: ${message}`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
catch (err) {
|
|
77
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
78
|
+
console.warn(`Error loading custom plugins: ${message}`);
|
|
79
|
+
}
|
|
80
|
+
return customPlugins;
|
|
81
|
+
}
|
|
82
|
+
export function createPlugins(config, customPlugins = {}, verbose = false) {
|
|
83
|
+
const plugins = [];
|
|
84
|
+
const phases = ['discovery', 'indexing', 'analysis', 'enrichment', 'validation'];
|
|
85
|
+
for (const phase of phases) {
|
|
86
|
+
const names = config[phase] || [];
|
|
87
|
+
for (const name of names) {
|
|
88
|
+
// Check built-in first, then custom
|
|
89
|
+
const factory = BUILTIN_PLUGINS[name] || customPlugins[name];
|
|
90
|
+
if (factory) {
|
|
91
|
+
plugins.push(factory());
|
|
92
|
+
}
|
|
93
|
+
else if (verbose) {
|
|
94
|
+
// Only show plugin warning in verbose mode
|
|
95
|
+
console.warn(`Plugin not found: ${name} (skipping). Check .grafema/config.yaml or add to .grafema/plugins/`);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return plugins;
|
|
100
|
+
}
|
|
101
|
+
//# sourceMappingURL=pluginLoader.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pluginLoader.js","sourceRoot":"","sources":["../../src/plugins/pluginLoader.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAC5B,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,IAAI,CAAC;AAC7C,OAAO,EAAE,aAAa,EAAE,MAAM,KAAK,CAAC;AACpC,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAEvC,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAEtD;;;;;;;;;;GAUG;AACH,IAAI,wBAAwB,GAAG,KAAK,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Aa,CAAC;AACvB,CAAC;AAED,MAAM,UAAU,aAAa,CAC3B,MAAgC,EAChC,gBAA8C,EAAE,EAChD,UAAmB,KAAK;IAExB,MAAM,OAAO,GAAa,EAAE,CAAC;IAC7B,MAAM,MAAM,GAAuC,CAAC,WAAW,EAAE,UAAU,EAAE,UAAU,EAAE,YAAY,EAAE,YAAY,CAAC,CAAC;IAErH,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;QAC3B,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC;QAClC,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;YACzB,oCAAoC;YACpC,MAAM,OAAO,GAAG,eAAe,CAAC,IAAI,CAAC,IAAI,aAAa,CAAC,IAAI,CAAC,CAAC;YAC7D,IAAI,OAAO,EAAE,CAAC;gBACZ,OAAO,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC,CAAC;YAC1B,CAAC;iBAAM,IAAI,OAAO,EAAE,CAAC;gBACnB,2CAA2C;gBAC3C,OAAO,CAAC,IAAI,CAAC,qBAAqB,IAAI,qEAAqE,CAAC,CAAC;YAC/G,CAAC;QACH,CAAC;IACH,CAAC;IAED,OAAO,OAAO,CAAC;AACjB,CAAC"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ESM resolve hook for custom Grafema plugins.
|
|
3
|
+
*
|
|
4
|
+
* Allows plugins in .grafema/plugins/ to `import { Plugin } from '@grafema/core'`
|
|
5
|
+
* without requiring @grafema/core in the target project's node_modules/.
|
|
6
|
+
*
|
|
7
|
+
* The hook maps @grafema/* bare specifiers to the actual package URLs
|
|
8
|
+
* within the CLI's dependency tree.
|
|
9
|
+
*
|
|
10
|
+
* Registered via module.register() before loading custom plugins.
|
|
11
|
+
* Must be plain JS — loader hooks run in a separate thread.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** @type {Record<string, string>} package name → resolved file URL */
|
|
15
|
+
let grafemaPackages = {};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Called once when the hook is registered via module.register().
|
|
19
|
+
* @param {{ grafemaPackages: Record<string, string> }} data
|
|
20
|
+
*/
|
|
21
|
+
export function initialize(data) {
|
|
22
|
+
grafemaPackages = data.grafemaPackages;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Resolve hook — intercepts bare specifier imports for @grafema/* packages
|
|
27
|
+
* and redirects them to the CLI's bundled versions.
|
|
28
|
+
*
|
|
29
|
+
* Only exact package name matches are handled (e.g. '@grafema/core').
|
|
30
|
+
* All other specifiers pass through to the default resolver.
|
|
31
|
+
*/
|
|
32
|
+
export function resolve(specifier, context, next) {
|
|
33
|
+
if (grafemaPackages[specifier]) {
|
|
34
|
+
return { url: grafemaPackages[specifier], shortCircuit: true };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return next(specifier, context);
|
|
38
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"codePreview.d.ts","sourceRoot":"","sources":["../../src/utils/codePreview.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;
|
|
1
|
+
{"version":3,"file":"codePreview.d.ts","sourceRoot":"","sources":["../../src/utils/codePreview.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAKH,MAAM,WAAW,kBAAkB;IACjC,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,iBAAiB;IAChC,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;CACjB;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,kBAAkB,GAAG,iBAAiB,GAAG,IAAI,CA4BpF;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAC/B,OAAO,EAAE,iBAAiB,EAC1B,aAAa,CAAC,EAAE,MAAM,GACrB,MAAM,EAAE,CAeV"}
|
|
@@ -5,17 +5,19 @@
|
|
|
5
5
|
* for displaying in the explorer UI.
|
|
6
6
|
*/
|
|
7
7
|
import { readFileSync, existsSync } from 'fs';
|
|
8
|
+
import { isAbsolute, join } from 'path';
|
|
8
9
|
/**
|
|
9
10
|
* Get a code preview snippet from a source file.
|
|
10
11
|
* Returns lines around the specified line number with context.
|
|
11
12
|
*/
|
|
12
13
|
export function getCodePreview(options) {
|
|
13
|
-
const { file, line, contextBefore = 2, contextAfter = 12 } = options;
|
|
14
|
-
|
|
14
|
+
const { file, line, contextBefore = 2, contextAfter = 12, projectPath } = options;
|
|
15
|
+
const absoluteFile = projectPath && !isAbsolute(file) ? join(projectPath, file) : file;
|
|
16
|
+
if (!existsSync(absoluteFile)) {
|
|
15
17
|
return null;
|
|
16
18
|
}
|
|
17
19
|
try {
|
|
18
|
-
const content = readFileSync(
|
|
20
|
+
const content = readFileSync(absoluteFile, 'utf-8');
|
|
19
21
|
const allLines = content.split('\n');
|
|
20
22
|
// Calculate range (1-indexed)
|
|
21
23
|
const startLine = Math.max(1, line - contextBefore);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"codePreview.js","sourceRoot":"","sources":["../../src/utils/codePreview.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,IAAI,CAAC;
|
|
1
|
+
{"version":3,"file":"codePreview.js","sourceRoot":"","sources":["../../src/utils/codePreview.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,IAAI,CAAC;AAC9C,OAAO,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAgBxC;;;GAGG;AACH,MAAM,UAAU,cAA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M,CAAC;IAE/C,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE;QAC/B,MAAM,OAAO,GAAG,SAAS,GAAG,KAAK,CAAC;QAClC,MAAM,SAAS,GAAG,MAAM,CAAC,OAAO,CAAC,CAAC,QAAQ,CAAC,YAAY,EAAE,GAAG,CAAC,CAAC;QAC9D,MAAM,MAAM,GAAG,aAAa,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC;QAErD,2BAA2B;QAC3B,MAAM,WAAW,GAAG,IAAI,CAAC,MAAM,GAAG,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC;QAExE,OAAO,GAAG,MAAM,GAAG,SAAS,MAAM,WAAW,EAAE,CAAC;IAClD,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -25,7 +25,7 @@ export interface DisplayableNode {
|
|
|
25
25
|
type: string;
|
|
26
26
|
/** Human-readable name */
|
|
27
27
|
name: string;
|
|
28
|
-
/**
|
|
28
|
+
/** Source file path (relative to project root, or absolute for legacy) */
|
|
29
29
|
file: string;
|
|
30
30
|
/** Line number (optional) */
|
|
31
31
|
line?: number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"formatNode.d.ts","sourceRoot":"","sources":["../../src/utils/formatNode.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,2CAA2C;IAC3C,WAAW,EAAE,MAAM,CAAC;IACpB,4CAA4C;IAC5C,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,yCAAyC;IACzC,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,iFAAiF;IACjF,EAAE,EAAE,MAAM,CAAC;IACX,4CAA4C;IAC5C,IAAI,EAAE,MAAM,CAAC;IACb,0BAA0B;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,
|
|
1
|
+
{"version":3,"file":"formatNode.d.ts","sourceRoot":"","sources":["../../src/utils/formatNode.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,2CAA2C;IAC3C,WAAW,EAAE,MAAM,CAAC;IACpB,4CAA4C;IAC5C,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,yCAAyC;IACzC,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,iFAAiF;IACjF,EAAE,EAAE,MAAM,CAAC;IACX,4CAA4C;IAC5C,IAAI,EAAE,MAAM,CAAC;IACb,0BAA0B;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,0EAA0E;IAC1E,IAAI,EAAE,MAAM,CAAC;IACb,6BAA6B;IAC7B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,iDAAiD;IACjD,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,iDAAiD;IACjD,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,6BAA6B;IAC7B,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,eAAe,GAAG,MAAM,CAyBhE;AAED;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,eAAe,EAAE,OAAO,EAAE,iBAAiB,GAAG,MAAM,CAoB3F;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,eAAe,GAAG,MAAM,CAE9D;AAED;;GAEG;AACH,wBAAgB,cAAc,CAC5B,IAAI,EAAE,MAAM,GAAG,SAAS,EACxB,IAAI,EAAE,MAAM,GAAG,SAAS,EACxB,WAAW,EAAE,MAAM,GAClB,MAAM,CAIR"}
|
package/dist/utils/formatNode.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* Provides consistent formatting for node display across all CLI commands.
|
|
5
5
|
* Semantic IDs are shown as the PRIMARY identifier, with location as secondary.
|
|
6
6
|
*/
|
|
7
|
-
import { relative } from 'path';
|
|
7
|
+
import { isAbsolute, relative } from 'path';
|
|
8
8
|
/**
|
|
9
9
|
* Get the display name for a node based on its type.
|
|
10
10
|
*
|
|
@@ -77,7 +77,7 @@ export function formatNodeInline(node) {
|
|
|
77
77
|
export function formatLocation(file, line, projectPath) {
|
|
78
78
|
if (!file)
|
|
79
79
|
return '';
|
|
80
|
-
const relPath = relative(projectPath, file);
|
|
80
|
+
const relPath = isAbsolute(file) ? relative(projectPath, file) : file;
|
|
81
81
|
return line ? `${relPath}:${line}` : relPath;
|
|
82
82
|
}
|
|
83
83
|
//# sourceMappingURL=formatNode.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"formatNode.js","sourceRoot":"","sources":["../../src/utils/formatNode.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"formatNode.js","sourceRoot":"","sources":["../../src/utils/formatNode.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,MAAM,CAAC;AAoC5C;;;;;GAKG;AACH,MAAM,UAAU,kBAAkB,CAAC,IAAqB;IACtD,QAAQ,IAAI,CAAC,IAAI,EAAE,CAAC;QAClB,KAAK,YAAY;YACf,+BAA+B;YAC/B,IAAI,IAAI,CAAC,MAAM,IAAI,IAAI,CAAC,IAAI,EAAE,CAAC;gBAC7B,OAAO,GAAG,IAAI,CAAC,MAAM,IAAI,IAAI,CAAC,IAAI,EAAE,CAAC;YACvC,CAAC;YACD,MAAM;QACR,KAAK,cAAc;YACjB,yCAAyC;YACzC,IAAI,IAAI,CAAC,MAAM,IAAI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C,IAAI,CAAC,GAAG,MAAM,eAAe,GAAG,EAAE,CAAC,CAAC;QAC5C,CAAC;IACH,CAAC;IAED,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAC1B,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,gBAAgB,CAAC,IAAqB;IACpD,OAAO,IAAI,CAAC,EAAE,CAAC;AACjB,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,cAAc,CAC5B,IAAwB,EACxB,IAAwB,EACxB,WAAmB;IAEnB,IAAI,CAAC,IAAI;QAAE,OAAO,EAAE,CAAC;IACrB,MAAM,OAAO,GAAG,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,WAAW,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;IACtE,OAAO,IAAI,CAAC,CAAC,CAAC,GAAG,OAAO,IAAI,IAAI,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC;AAC/C,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pathUtils.d.ts","sourceRoot":"","sources":["../../src/utils/pathUtils.ts"],"names":[],"mappings":"AAMA,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,MAAM,CAE3E"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert a node file path to relative display format.
|
|
3
|
+
* Handles both legacy absolute paths and new relative paths (REG-408).
|
|
4
|
+
*/
|
|
5
|
+
import { relative, isAbsolute } from 'path';
|
|
6
|
+
export function toRelativeDisplay(file, projectPath) {
|
|
7
|
+
return isAbsolute(file) ? relative(projectPath, file) : file;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=pathUtils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pathUtils.js","sourceRoot":"","sources":["../../src/utils/pathUtils.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAE5C,MAAM,UAAU,iBAAiB,CAAC,IAAY,EAAE,WAAmB;IACjE,OAAO,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,WAAW,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;AAC/D,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@grafema/cli",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.6-beta",
|
|
4
4
|
"description": "CLI for Grafema code analysis toolkit",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/cli.js",
|
|
@@ -16,7 +16,8 @@
|
|
|
16
16
|
},
|
|
17
17
|
"files": [
|
|
18
18
|
"dist",
|
|
19
|
-
"src"
|
|
19
|
+
"src",
|
|
20
|
+
"skills"
|
|
20
21
|
],
|
|
21
22
|
"keywords": [
|
|
22
23
|
"grafema",
|
|
@@ -33,21 +34,18 @@
|
|
|
33
34
|
},
|
|
34
35
|
"dependencies": {
|
|
35
36
|
"commander": "^13.0.0",
|
|
36
|
-
"ink": "^6.6.0",
|
|
37
|
-
"ink-text-input": "^6.0.0",
|
|
38
|
-
"react": "^19.2.3",
|
|
39
37
|
"yaml": "^2.8.2",
|
|
40
|
-
"@grafema/
|
|
41
|
-
"@grafema/
|
|
42
|
-
"@grafema/
|
|
38
|
+
"@grafema/core": "0.2.6-beta",
|
|
39
|
+
"@grafema/types": "0.2.6-beta",
|
|
40
|
+
"@grafema/api": "0.2.6-beta"
|
|
43
41
|
},
|
|
44
42
|
"devDependencies": {
|
|
45
43
|
"@types/node": "^25.0.8",
|
|
46
|
-
"@types/react": "^19.2.9",
|
|
47
44
|
"typescript": "^5.9.3"
|
|
48
45
|
},
|
|
49
46
|
"scripts": {
|
|
50
47
|
"build": "tsc",
|
|
48
|
+
"postbuild": "mkdir -p dist/plugins && cp src/plugins/pluginResolver.js dist/plugins/",
|
|
51
49
|
"clean": "rm -rf dist",
|
|
52
50
|
"start": "node dist/cli.js",
|
|
53
51
|
"test": "node --import tsx --test test/*.test.ts"
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: grafema-codebase-analysis
|
|
3
|
+
description: >
|
|
4
|
+
Analyze codebases using a graph database instead of reading source files.
|
|
5
|
+
Use when understanding code architecture, finding functions or call patterns,
|
|
6
|
+
tracing data flow, checking dependencies, or answering "where is X used?"
|
|
7
|
+
questions. Grafema builds a queryable code graph from static analysis —
|
|
8
|
+
prefer querying the graph over reading files manually.
|
|
9
|
+
license: Apache-2.0
|
|
10
|
+
compatibility: Requires Grafema MCP server configured (grafema or @grafema/mcp package)
|
|
11
|
+
metadata:
|
|
12
|
+
author: Grafema
|
|
13
|
+
version: "0.2.5"
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
# Grafema: Graph-Based Codebase Analysis
|
|
17
|
+
|
|
18
|
+
## Core Principle
|
|
19
|
+
|
|
20
|
+
**Query the graph, not read code.**
|
|
21
|
+
|
|
22
|
+
Grafema builds a graph database from your codebase via static analysis.
|
|
23
|
+
Instead of reading dozens of files to understand how code connects,
|
|
24
|
+
query the graph to get structured, complete answers instantly.
|
|
25
|
+
|
|
26
|
+
```
|
|
27
|
+
BAD: Read 20 files hoping to find all callers of a function
|
|
28
|
+
GOOD: find_calls({ name: "processPayment" }) -> get all callers in one query
|
|
29
|
+
|
|
30
|
+
BAD: Grep for variable name across files, miss aliased references
|
|
31
|
+
GOOD: trace_dataflow({ source: "userInput", direction: "forward" }) -> complete data flow
|
|
32
|
+
|
|
33
|
+
BAD: Read file by file to understand module dependencies
|
|
34
|
+
GOOD: get_file_overview({ file: "src/api.ts" }) -> structured imports, exports, classes, functions
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### When to Use Grafema
|
|
38
|
+
|
|
39
|
+
- Finding where functions/methods are called
|
|
40
|
+
- Understanding module dependencies and imports
|
|
41
|
+
- Tracing data flow (forward or backward)
|
|
42
|
+
- Getting function details (signature, callers, callees)
|
|
43
|
+
- Checking code invariants with Datalog rules
|
|
44
|
+
- Exploring file structure and entity relationships
|
|
45
|
+
|
|
46
|
+
### When NOT to Use Grafema
|
|
47
|
+
|
|
48
|
+
- Reading a single specific file (use your editor/Read tool — faster)
|
|
49
|
+
- Editing code (Grafema is read-only analysis)
|
|
50
|
+
- Runtime behavior questions (Grafema is static analysis)
|
|
51
|
+
- Files not yet analyzed (run `analyze_project` first)
|
|
52
|
+
|
|
53
|
+
## Essential Tools (Tier 1)
|
|
54
|
+
|
|
55
|
+
These 5 tools handle ~80% of queries. Start here.
|
|
56
|
+
|
|
57
|
+
### find_nodes — Find entities by type, name, or file
|
|
58
|
+
|
|
59
|
+
```json
|
|
60
|
+
find_nodes({ type: "FUNCTION", name: "validateUser" })
|
|
61
|
+
find_nodes({ type: "CLASS", file: "src/auth.ts" })
|
|
62
|
+
find_nodes({ type: "http:request" })
|
|
63
|
+
find_nodes({ type: "MODULE" })
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
**Use when:** "Find all X", "What functions are in file Y", "List all routes"
|
|
67
|
+
|
|
68
|
+
**Node types:** MODULE, FUNCTION, METHOD, CLASS, VARIABLE, PARAMETER,
|
|
69
|
+
CALL, PROPERTY_ACCESS, METHOD_CALL, CALL_SITE,
|
|
70
|
+
http:route, http:request, db:query, socketio:emit, socketio:on
|
|
71
|
+
|
|
72
|
+
### find_calls — Find function/method call sites
|
|
73
|
+
|
|
74
|
+
```json
|
|
75
|
+
find_calls({ name: "processPayment" })
|
|
76
|
+
find_calls({ name: "query", className: "Database" })
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
**Use when:** "Where is X called?", "Who calls this function?", "Find all usages"
|
|
80
|
+
|
|
81
|
+
Returns call sites with file locations and whether the target is resolved.
|
|
82
|
+
|
|
83
|
+
### get_function_details — Complete function info
|
|
84
|
+
|
|
85
|
+
```json
|
|
86
|
+
get_function_details({ name: "handleRequest" })
|
|
87
|
+
get_function_details({ name: "validate", file: "src/auth.ts" })
|
|
88
|
+
get_function_details({ name: "processOrder", transitive: true })
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
**Use when:** "What does function X do?", "What does it call?", "Who calls it?"
|
|
92
|
+
|
|
93
|
+
Returns: signature, parameters, what it calls, who calls it.
|
|
94
|
+
Use `transitive: true` to follow call chains (A calls B calls C, max depth 5).
|
|
95
|
+
|
|
96
|
+
### get_context — Deep context for any node
|
|
97
|
+
|
|
98
|
+
```json
|
|
99
|
+
get_context({ semanticId: "src/api.ts:handleRequest#fn" })
|
|
100
|
+
get_context({ semanticId: "src/db.ts:Database#class", edgeType: "CALLS" })
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
**Use when:** "Tell me everything about this entity", "Show me its relationships"
|
|
104
|
+
|
|
105
|
+
Returns: node info, source code, ALL incoming/outgoing edges with code context.
|
|
106
|
+
Use after `find_nodes` to deep-dive into a specific result.
|
|
107
|
+
|
|
108
|
+
### trace_dataflow — Trace data flow
|
|
109
|
+
|
|
110
|
+
```json
|
|
111
|
+
trace_dataflow({ source: "userInput", file: "src/handler.ts", direction: "forward" })
|
|
112
|
+
trace_dataflow({ source: "dbResult", file: "src/query.ts", direction: "backward" })
|
|
113
|
+
trace_dataflow({ source: "config", direction: "both", max_depth: 5 })
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
**Use when:** "Where does this value end up?", "Where does this data come from?",
|
|
117
|
+
"Is user input reaching the database unsanitized?"
|
|
118
|
+
|
|
119
|
+
Directions: `forward` (where does it go?), `backward` (where did it come from?), `both`.
|
|
120
|
+
|
|
121
|
+
## Decision Tree
|
|
122
|
+
|
|
123
|
+
```
|
|
124
|
+
START: What do you need?
|
|
125
|
+
|
|
|
126
|
+
|-- "Find entities (functions, classes, routes)"
|
|
127
|
+
| -> find_nodes({ type, name, file })
|
|
128
|
+
|
|
|
129
|
+
|-- "Find who calls function X"
|
|
130
|
+
| -> find_calls({ name: "X" })
|
|
131
|
+
| -> For full details: get_function_details({ name: "X" })
|
|
132
|
+
|
|
|
133
|
+
|-- "Understand a specific entity deeply"
|
|
134
|
+
| -> First: find_nodes to get its semantic ID
|
|
135
|
+
| -> Then: get_context({ semanticId: "..." })
|
|
136
|
+
|
|
|
137
|
+
|-- "Trace data flow"
|
|
138
|
+
| -> trace_dataflow({ source, file, direction })
|
|
139
|
+
|
|
|
140
|
+
|-- "Understand a file's structure"
|
|
141
|
+
| -> get_file_overview({ file: "path/to/file.ts" })
|
|
142
|
+
|
|
|
143
|
+
|-- "Trace an alias/re-export chain"
|
|
144
|
+
| -> trace_alias({ variableName: "alias", file: "path.ts" })
|
|
145
|
+
|
|
|
146
|
+
|-- "Check a code rule/invariant"
|
|
147
|
+
| -> check_invariant({ rule: "violation(X) :- ..." })
|
|
148
|
+
|
|
|
149
|
+
|-- "Custom complex query"
|
|
150
|
+
| -> query_graph({ query: "violation(X) :- ..." })
|
|
151
|
+
| -> See references/query-patterns.md for Datalog syntax
|
|
152
|
+
|
|
|
153
|
+
|-- "Explore unknown codebase"
|
|
154
|
+
| -> get_stats() for high-level overview
|
|
155
|
+
| -> get_schema() for available node/edge types
|
|
156
|
+
| -> find_nodes({ type: "MODULE" }) for module list
|
|
157
|
+
| -> get_file_overview for specific files
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## Common Workflows
|
|
161
|
+
|
|
162
|
+
### 1. Impact Analysis: "What breaks if I change function X?"
|
|
163
|
+
|
|
164
|
+
```
|
|
165
|
+
get_function_details({ name: "X", transitive: true })
|
|
166
|
+
-> Check calledBy array for all callers (direct + transitive)
|
|
167
|
+
-> For critical callers: get_context({ semanticId }) for full picture
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
### 2. Security Audit: "Does user input reach the database?"
|
|
171
|
+
|
|
172
|
+
```
|
|
173
|
+
find_nodes({ type: "http:request" })
|
|
174
|
+
-> For each route, trace_dataflow({ source: requestParam, direction: "forward" })
|
|
175
|
+
-> Check if flow reaches db:query nodes
|
|
176
|
+
-> Use find_guards to check for sanitization
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### 3. Onboarding: "How is this codebase structured?"
|
|
180
|
+
|
|
181
|
+
```
|
|
182
|
+
get_stats() -> Node/edge counts by type
|
|
183
|
+
find_nodes({ type: "MODULE" }) -> All modules
|
|
184
|
+
get_file_overview({ file: "src/index.ts" }) -> Entry point structure
|
|
185
|
+
find_nodes({ type: "http:request" }) -> All API endpoints
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
### 4. Dependency Analysis: "What does module X depend on?"
|
|
189
|
+
|
|
190
|
+
```
|
|
191
|
+
get_file_overview({ file: "src/service.ts" })
|
|
192
|
+
-> Check imports section for dependencies
|
|
193
|
+
-> For each import: get_context for deeper relationships
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
### 5. Find Dead Code: "What functions have no callers?"
|
|
197
|
+
|
|
198
|
+
```
|
|
199
|
+
query_graph({
|
|
200
|
+
query: 'violation(X) :- node(X, "FUNCTION"), \\+ edge(_, X, "CALLS").'
|
|
201
|
+
})
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
## Anti-Patterns
|
|
205
|
+
|
|
206
|
+
**Don't read files to find call sites.** Use `find_calls` — it finds ALL callers across
|
|
207
|
+
the entire codebase, including indirect references you'd miss by grepping.
|
|
208
|
+
|
|
209
|
+
**Don't use `query_graph` for simple lookups.** `find_nodes`, `find_calls`, and
|
|
210
|
+
`get_function_details` are optimized for common queries. Reserve Datalog for
|
|
211
|
+
complex patterns (joins, transitive closure, invariant checks).
|
|
212
|
+
|
|
213
|
+
**Don't skip analysis status.** If you just ran `analyze_project`, check
|
|
214
|
+
`get_analysis_status` before querying — partial results are misleading.
|
|
215
|
+
|
|
216
|
+
**Don't request excessive depth.** `get_context` with no filters returns everything.
|
|
217
|
+
Use `edgeType` filter to focus on specific relationships (e.g., `"CALLS,ASSIGNED_FROM"`).
|
|
218
|
+
|
|
219
|
+
**Don't use Grafema for single-file questions.** If you only need to read one file,
|
|
220
|
+
use your editor. Grafema shines for cross-file relationships.
|
|
221
|
+
|
|
222
|
+
## Advanced Tools (Tier 2)
|
|
223
|
+
|
|
224
|
+
### query_graph — Custom Datalog queries
|
|
225
|
+
|
|
226
|
+
For complex patterns not covered by high-level tools.
|
|
227
|
+
See [references/query-patterns.md](references/query-patterns.md) for syntax and examples.
|
|
228
|
+
|
|
229
|
+
```json
|
|
230
|
+
query_graph({
|
|
231
|
+
query: "violation(X) :- node(X, \"CALL\"), attr(X, \"name\", \"eval\").",
|
|
232
|
+
explain: true
|
|
233
|
+
})
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
Available predicates: `node(Id, Type)`, `edge(Src, Dst, Type)`, `attr(Id, Name, Value)`.
|
|
237
|
+
Must define `violation/1` predicate for results. Use `explain: true` to debug empty results.
|
|
238
|
+
|
|
239
|
+
### get_file_overview — File-level structure
|
|
240
|
+
|
|
241
|
+
Structured overview of imports, exports, classes, functions, variables with relationships.
|
|
242
|
+
Recommended first step when exploring a specific file before using `get_context`.
|
|
243
|
+
|
|
244
|
+
### trace_alias — Resolve alias chains
|
|
245
|
+
|
|
246
|
+
For code like `const alias = obj.method; alias()` — traces "alias" back to "obj.method".
|
|
247
|
+
|
|
248
|
+
### get_schema — Available types
|
|
249
|
+
|
|
250
|
+
Returns all node and edge types in the graph. Use when you need exact type names.
|
|
251
|
+
|
|
252
|
+
### check_invariant — Code rule checking
|
|
253
|
+
|
|
254
|
+
Check if a Datalog rule has violations. For persistent rules, use `create_guarantee`.
|
|
255
|
+
|
|
256
|
+
## Specialized Tools (Tier 3)
|
|
257
|
+
|
|
258
|
+
| Tool | Purpose |
|
|
259
|
+
|------|---------|
|
|
260
|
+
| get_stats | Graph statistics (node/edge counts by type) |
|
|
261
|
+
| get_coverage | Analysis coverage for a path |
|
|
262
|
+
| find_guards | Conditional guards protecting a node |
|
|
263
|
+
| create_guarantee | Create persistent code invariant |
|
|
264
|
+
| list_guarantees | List all guarantees |
|
|
265
|
+
| check_guarantees | Check guarantee violations |
|
|
266
|
+
| delete_guarantee | Remove a guarantee |
|
|
267
|
+
| discover_services | Discover services without full analysis |
|
|
268
|
+
| analyze_project | Run/re-run analysis |
|
|
269
|
+
| get_analysis_status | Check analysis progress |
|
|
270
|
+
| read_project_structure | Directory tree |
|
|
271
|
+
| write_config | Update .grafema/config.yaml |
|
|
272
|
+
| get_documentation | Grafema usage docs |
|
|
273
|
+
| report_issue | Report bugs |
|
|
274
|
+
|
|
275
|
+
## Troubleshooting
|
|
276
|
+
|
|
277
|
+
**Query returns nothing?**
|
|
278
|
+
1. Check analysis ran: `get_analysis_status`
|
|
279
|
+
2. Check type names: `get_schema` for available types
|
|
280
|
+
3. Use `explain: true` in `query_graph` to debug
|
|
281
|
+
4. Check file paths match (relative to project root)
|
|
282
|
+
|
|
283
|
+
**Need help with Datalog syntax?**
|
|
284
|
+
- See [references/query-patterns.md](references/query-patterns.md)
|
|
285
|
+
- Use `get_documentation({ topic: "queries" })` for inline help
|
|
286
|
+
|
|
287
|
+
**Graph seems incomplete?**
|
|
288
|
+
- Run `get_coverage({ path: "src/" })` to check coverage
|
|
289
|
+
- Re-analyze with `analyze_project({ force: true })`
|
|
290
|
+
- Check `.grafema/config.yaml` for include/exclude patterns
|
|
291
|
+
|
|
292
|
+
## References
|
|
293
|
+
|
|
294
|
+
- [Node and Edge Types](references/node-edge-types.md) — Complete graph schema
|
|
295
|
+
- [Query Patterns](references/query-patterns.md) — Datalog cookbook with examples
|