@player-tools/metrics-output-plugin 0.12.1-next.2 → 0.12.1-next.4
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/cjs/index.cjs +66 -31
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/index.legacy-esm.js +66 -31
- package/dist/index.mjs +66 -31
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -2
- package/src/__tests__/metrics-output-plugin.test.ts +642 -0
- package/src/index.ts +1 -0
- package/src/metrics-output.ts +103 -65
- package/src/types.ts +26 -0
- package/types/index.d.ts +1 -0
- package/types/metrics-output.d.ts +6 -15
- package/types/types.d.ts +17 -0
package/dist/cjs/index.cjs
CHANGED
|
@@ -39,10 +39,12 @@ module.exports = __toCommonJS(src_exports);
|
|
|
39
39
|
// ../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/language/metrics-output-plugin/src/metrics-output.ts
|
|
40
40
|
var fs = __toESM(require("fs"));
|
|
41
41
|
var path = __toESM(require("path"));
|
|
42
|
+
var import_ts_deepmerge = require("ts-deepmerge");
|
|
42
43
|
function normalizePath(filePath) {
|
|
43
44
|
const normalized = filePath.replace(/\\/g, "/");
|
|
44
45
|
return normalized.replace(/^file:\/\//, "");
|
|
45
46
|
}
|
|
47
|
+
var merge = import_ts_deepmerge.merge;
|
|
46
48
|
var MetricsOutput = class {
|
|
47
49
|
name = "metrics-output-plugin";
|
|
48
50
|
outputDir;
|
|
@@ -51,7 +53,12 @@ var MetricsOutput = class {
|
|
|
51
53
|
stats;
|
|
52
54
|
features;
|
|
53
55
|
// In-memory storage of all results
|
|
54
|
-
aggregatedResults = {
|
|
56
|
+
aggregatedResults = {
|
|
57
|
+
content: {}
|
|
58
|
+
};
|
|
59
|
+
get outputFilePath() {
|
|
60
|
+
return path.resolve(this.outputDir, `${this.fileName}.json`);
|
|
61
|
+
}
|
|
55
62
|
constructor(options = {}) {
|
|
56
63
|
this.outputDir = options.outputDir || process.cwd();
|
|
57
64
|
let fileName = options.fileName || "metrics";
|
|
@@ -62,36 +69,36 @@ var MetricsOutput = class {
|
|
|
62
69
|
this.rootProperties = options.rootProperties || {};
|
|
63
70
|
this.stats = options.stats || {};
|
|
64
71
|
this.features = options.features || {};
|
|
65
|
-
this.aggregatedResults = {
|
|
66
|
-
content: {}
|
|
67
|
-
};
|
|
68
72
|
}
|
|
69
73
|
apply(service) {
|
|
70
74
|
service.hooks.onValidateEnd.tap(
|
|
71
75
|
this.name,
|
|
72
76
|
(diagnostics, { documentContext }) => {
|
|
77
|
+
if (fs.existsSync(this.outputFilePath)) {
|
|
78
|
+
this.loadExistingMetrics();
|
|
79
|
+
}
|
|
73
80
|
this.generateFile(diagnostics, documentContext);
|
|
74
81
|
return diagnostics;
|
|
75
82
|
}
|
|
76
83
|
);
|
|
77
84
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
try {
|
|
84
|
-
const result = this.rootProperties(diagnostics, documentContext);
|
|
85
|
-
if (typeof result === "object" && result !== null) {
|
|
86
|
-
return result;
|
|
87
|
-
}
|
|
88
|
-
return { dynamicRootValue: result };
|
|
89
|
-
} catch (error) {
|
|
90
|
-
documentContext.log.error(`Error evaluating root properties: ${error}`);
|
|
91
|
-
return { error: `Root properties evaluation failed: ${error}` };
|
|
85
|
+
loadExistingMetrics() {
|
|
86
|
+
try {
|
|
87
|
+
const fileContent = fs.readFileSync(this.outputFilePath, "utf-8");
|
|
88
|
+
if (!fileContent.trim()) {
|
|
89
|
+
return;
|
|
92
90
|
}
|
|
91
|
+
const parsed = JSON.parse(fileContent);
|
|
92
|
+
const existingMetrics = parsed && typeof parsed === "object" ? parsed : {};
|
|
93
|
+
this.aggregatedResults = merge(
|
|
94
|
+
existingMetrics,
|
|
95
|
+
this.aggregatedResults
|
|
96
|
+
);
|
|
97
|
+
} catch (error) {
|
|
98
|
+
console.warn(
|
|
99
|
+
`Warning: Could not parse existing metrics file ${this.outputFilePath}. Continuing with current metrics. Error: ${error}`
|
|
100
|
+
);
|
|
93
101
|
}
|
|
94
|
-
return this.rootProperties;
|
|
95
102
|
}
|
|
96
103
|
/**
|
|
97
104
|
* Evaluates a value, executing it if it's a function
|
|
@@ -108,9 +115,10 @@ var MetricsOutput = class {
|
|
|
108
115
|
return value;
|
|
109
116
|
}
|
|
110
117
|
generateMetrics(diagnostics, documentContext) {
|
|
111
|
-
|
|
118
|
+
const statsSource = this.stats;
|
|
119
|
+
if (typeof statsSource === "function") {
|
|
112
120
|
try {
|
|
113
|
-
const result2 =
|
|
121
|
+
const result2 = statsSource(diagnostics, documentContext);
|
|
114
122
|
if (typeof result2 === "object" && result2 !== null) {
|
|
115
123
|
return result2;
|
|
116
124
|
}
|
|
@@ -121,15 +129,16 @@ var MetricsOutput = class {
|
|
|
121
129
|
}
|
|
122
130
|
}
|
|
123
131
|
const result = {};
|
|
124
|
-
Object.entries(
|
|
132
|
+
Object.entries(statsSource).forEach(([key, value]) => {
|
|
125
133
|
result[key] = this.evaluateValue(value, diagnostics, documentContext);
|
|
126
134
|
});
|
|
127
135
|
return result;
|
|
128
136
|
}
|
|
129
137
|
generateFeatures(diagnostics, documentContext) {
|
|
130
|
-
|
|
138
|
+
const featuresSource = this.features;
|
|
139
|
+
if (typeof featuresSource === "function") {
|
|
131
140
|
try {
|
|
132
|
-
const result2 =
|
|
141
|
+
const result2 = featuresSource(diagnostics, documentContext);
|
|
133
142
|
if (typeof result2 === "object" && result2 !== null) {
|
|
134
143
|
return result2;
|
|
135
144
|
}
|
|
@@ -142,7 +151,7 @@ var MetricsOutput = class {
|
|
|
142
151
|
}
|
|
143
152
|
}
|
|
144
153
|
const result = {};
|
|
145
|
-
Object.entries(
|
|
154
|
+
Object.entries(featuresSource).forEach(([key, value]) => {
|
|
146
155
|
result[key] = this.evaluateValue(value, diagnostics, documentContext);
|
|
147
156
|
});
|
|
148
157
|
return result;
|
|
@@ -151,18 +160,44 @@ var MetricsOutput = class {
|
|
|
151
160
|
const fullOutputDir = path.resolve(process.cwd(), this.outputDir);
|
|
152
161
|
fs.mkdirSync(fullOutputDir, { recursive: true });
|
|
153
162
|
const filePath = normalizePath(documentContext.document.uri);
|
|
154
|
-
const stats = this.generateMetrics(
|
|
155
|
-
|
|
156
|
-
|
|
163
|
+
const stats = this.generateMetrics(
|
|
164
|
+
diagnostics,
|
|
165
|
+
documentContext
|
|
166
|
+
);
|
|
167
|
+
const features = this.generateFeatures(
|
|
168
|
+
diagnostics,
|
|
169
|
+
documentContext
|
|
170
|
+
);
|
|
171
|
+
const newEntry = {
|
|
157
172
|
stats,
|
|
158
173
|
...Object.keys(features).length > 0 ? { features } : {}
|
|
159
174
|
};
|
|
160
|
-
|
|
161
|
-
|
|
175
|
+
let rootProps;
|
|
176
|
+
if (typeof this.rootProperties === "function") {
|
|
177
|
+
try {
|
|
178
|
+
const result = this.rootProperties(diagnostics, documentContext);
|
|
179
|
+
if (typeof result === "object" && result !== null) {
|
|
180
|
+
rootProps = result;
|
|
181
|
+
} else {
|
|
182
|
+
rootProps = { dynamicRootValue: result };
|
|
183
|
+
}
|
|
184
|
+
} catch (error) {
|
|
185
|
+
documentContext.log.error(`Error evaluating root properties: ${error}`);
|
|
186
|
+
rootProps = { error: `Root properties evaluation failed: ${error}` };
|
|
187
|
+
}
|
|
188
|
+
} else {
|
|
189
|
+
rootProps = this.rootProperties;
|
|
190
|
+
}
|
|
191
|
+
this.aggregatedResults = merge(
|
|
192
|
+
this.aggregatedResults,
|
|
193
|
+
rootProps,
|
|
194
|
+
{ content: { [filePath]: newEntry } }
|
|
195
|
+
);
|
|
162
196
|
const outputFilePath = path.join(fullOutputDir, `${this.fileName}.json`);
|
|
197
|
+
const { content, ...root } = this.aggregatedResults;
|
|
163
198
|
fs.writeFileSync(
|
|
164
199
|
outputFilePath,
|
|
165
|
-
JSON.stringify(
|
|
200
|
+
JSON.stringify({ ...root, content }, null, 2),
|
|
166
201
|
"utf-8"
|
|
167
202
|
);
|
|
168
203
|
return outputFilePath;
|
package/dist/cjs/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/language/metrics-output-plugin/src/index.ts","../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/language/metrics-output-plugin/src/metrics-output.ts","../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/language/metrics-output-plugin/src/utils.ts"],"sourcesContent":["export * from \"./metrics-output\";\nexport * from \"./utils\";\n","import * as fs from \"fs\";\nimport * as path from \"path\";\nimport { Diagnostic } from \"vscode-languageserver-types\";\nimport type {\n PlayerLanguageService,\n PlayerLanguageServicePlugin,\n DocumentContext,\n} from \"@player-tools/json-language-service\";\n\n/**\n * Function that will be called with diagnostics and document context\n * @param diagnostics - Array of diagnostics from validation\n * @param documentContext - Context of the current document\n * @returns Any value that will be included in the metrics output\n */\nexport type MetricFunction = (...args: any[]) => any;\n\nexport type MetricValue =\n | Record<string, any>\n | number\n | string\n | boolean\n | MetricFunction;\n\nexport interface MetricsOutputConfig {\n /** Directory where the output file will be written */\n outputDir?: string;\n\n /** Name of the JSON output file */\n fileName?: string;\n\n /**\n * Custom properties to include at the root level of the output\n */\n rootProperties?: Record<string, any> | MetricFunction;\n\n /**\n * Content-specific stats\n */\n stats?: Record<string, MetricValue> | MetricFunction;\n\n /**\n * Content-specific features\n */\n features?: Record<string, MetricValue> | MetricFunction;\n}\n\n/**\n * Normalizes a file path to use consistent separators and format\n */\nfunction normalizePath(filePath: string): string {\n // Convert backslashes to forward slashes for consistency\n const normalized = filePath.replace(/\\\\/g, \"/\");\n\n // Remove file:// protocol if present\n return normalized.replace(/^file:\\/\\//, \"\");\n}\n\n/**\n * A plugin that writes diagnostic results to a JSON file in a specified output directory.\n * NOTE: This plugin is designed for CLI usage only and should not be used in an IDE.\n */\nexport class MetricsOutput implements PlayerLanguageServicePlugin {\n name = \"metrics-output-plugin\";\n\n private outputDir: string;\n private fileName: string;\n private rootProperties: Record<string, any> | MetricFunction;\n private stats: Record<string, MetricValue> | MetricFunction;\n private features: Record<string, MetricValue> | MetricFunction;\n\n // In-memory storage of all results\n private aggregatedResults: Record<string, any> = {};\n\n constructor(options: MetricsOutputConfig = {}) {\n this.outputDir = options.outputDir || process.cwd();\n\n // Handle file name, stripping .json extension if provided\n let fileName = options.fileName || \"metrics\";\n if (fileName.endsWith(\".json\")) {\n fileName = fileName.split(\".\")[0]; // Remove extension\n }\n this.fileName = fileName;\n this.rootProperties = options.rootProperties || {};\n this.stats = options.stats || {};\n this.features = options.features || {};\n\n // Initialize with empty content\n this.aggregatedResults = {\n content: {},\n };\n }\n\n apply(service: PlayerLanguageService): void {\n // Hook into the validation end to capture diagnostics and write output\n service.hooks.onValidateEnd.tap(\n this.name,\n (\n diagnostics: Diagnostic[],\n { documentContext }: { documentContext: DocumentContext },\n ): Diagnostic[] => {\n this.generateFile(diagnostics, documentContext);\n return diagnostics;\n },\n );\n }\n\n /**\n * Evaluates root properties, executing it if it's a function\n */\n private evaluateRootProperties(\n diagnostics: Diagnostic[],\n documentContext: DocumentContext,\n ): Record<string, any> {\n if (typeof this.rootProperties === \"function\") {\n try {\n const result = this.rootProperties(diagnostics, documentContext);\n if (typeof result === \"object\" && result !== null) {\n return result;\n }\n return { dynamicRootValue: result };\n } catch (error) {\n documentContext.log.error(`Error evaluating root properties: ${error}`);\n return { error: `Root properties evaluation failed: ${error}` };\n }\n }\n return this.rootProperties;\n }\n\n /**\n * Evaluates a value, executing it if it's a function\n */\n private evaluateValue(\n value: MetricValue,\n diagnostics: Diagnostic[],\n documentContext: DocumentContext,\n ) {\n if (typeof value === \"function\") {\n try {\n return value(diagnostics, documentContext);\n } catch (error) {\n documentContext.log.error(`Error evaluating value: ${error}`);\n return { error: `Value evaluation failed: ${error}` };\n }\n }\n return value;\n }\n\n private generateMetrics(\n diagnostics: Diagnostic[],\n documentContext: DocumentContext,\n ): Record<string, any> {\n // If stats is a function, evaluate it directly\n if (typeof this.stats === \"function\") {\n try {\n const result = this.stats(diagnostics, documentContext);\n if (typeof result === \"object\" && result !== null) {\n return result;\n }\n return { dynamicStatsValue: result };\n } catch (error) {\n documentContext.log.error(`Error evaluating stats function: ${error}`);\n return { error: `Stats function evaluation failed: ${error}` };\n }\n }\n\n // Otherwise process each metric in the record\n const result: Record<string, any> = {};\n Object.entries(this.stats).forEach(([key, value]) => {\n result[key] = this.evaluateValue(value, diagnostics, documentContext);\n });\n\n return result;\n }\n\n private generateFeatures(\n diagnostics: Diagnostic[],\n documentContext: DocumentContext,\n ): Record<string, any> {\n // If features is a function, evaluate it directly\n if (typeof this.features === \"function\") {\n try {\n const result = this.features(diagnostics, documentContext);\n if (typeof result === \"object\" && result !== null) {\n return result;\n }\n return { dynamicFeaturesValue: result };\n } catch (error) {\n documentContext.log.error(\n `Error evaluating features function: ${error}`,\n );\n return { error: `Features function evaluation failed: ${error}` };\n }\n }\n\n // Otherwise process each feature in the record\n const result: Record<string, any> = {};\n Object.entries(this.features).forEach(([key, value]) => {\n result[key] = this.evaluateValue(value, diagnostics, documentContext);\n });\n\n return result;\n }\n\n private generateFile(\n diagnostics: Diagnostic[],\n documentContext: DocumentContext,\n ): string {\n // Ensure the output directory exists\n const fullOutputDir = path.resolve(process.cwd(), this.outputDir);\n fs.mkdirSync(fullOutputDir, { recursive: true });\n\n // Get the file path from the document URI and normalize it\n const filePath = normalizePath(documentContext.document.uri);\n\n // Generate metrics\n const stats = this.generateMetrics(diagnostics, documentContext);\n const features = this.generateFeatures(diagnostics, documentContext);\n\n // Update content for this file\n this.aggregatedResults.content[filePath] = {\n stats,\n ...(Object.keys(features).length > 0 ? { features } : {}),\n };\n\n // Evaluate root properties with current diagnostics and context\n const rootProps = this.evaluateRootProperties(diagnostics, documentContext);\n\n // Apply root properties to the aggregated results\n Object.assign(this.aggregatedResults, rootProps);\n\n // Write the aggregated results to a file\n const outputFilePath = path.join(fullOutputDir, `${this.fileName}.json`);\n fs.writeFileSync(\n outputFilePath,\n JSON.stringify(this.aggregatedResults, null, 2),\n \"utf-8\",\n );\n\n return outputFilePath;\n }\n}\n","import { Diagnostic } from \"vscode-languageserver-types\";\n\n/**\n * Extracts data from diagnostic messages using a pattern\n */\nexport function extractFromDiagnostics<T>(\n pattern: RegExp,\n parser: (value: string) => T,\n): (diagnostics: Diagnostic[]) => T | undefined {\n return (diagnostics: Diagnostic[]): T | undefined => {\n for (const diagnostic of diagnostics) {\n const match = diagnostic.message.match(pattern);\n if (match && match[1]) {\n try {\n const result = parser(match[1]);\n // Check if result is NaN (only relevant for numeric parsers)\n if (typeof result === \"number\" && isNaN(result)) {\n console.warn(`Failed to parse diagnostic value: ${match[1]}`);\n return undefined;\n }\n return result;\n } catch (e) {\n console.warn(`Failed to parse diagnostic value: ${match[1]}`, e);\n return undefined;\n }\n }\n }\n return undefined;\n };\n}\n\n/**\n * Extracts data from diagnostics\n */\nexport function extractByData(\n data: string | symbol,\n diagnostics: Diagnostic[],\n parser?: (diagnostic: Diagnostic) => any,\n): Record<string, any> {\n const filteredDiagnostics = diagnostics.filter(\n (diagnostic) => diagnostic.data === data,\n );\n if (filteredDiagnostics.length === 0) {\n return {};\n }\n\n // Default parser that attempts to parse the message as JSON or returns the raw message\n const defaultParser = (diagnostic: Diagnostic): any => {\n try {\n if (diagnostic.message) {\n return JSON.parse(diagnostic.message);\n }\n return diagnostic.message || {};\n } catch (e) {\n return diagnostic.message || {};\n }\n };\n\n const actualParser = parser || defaultParser;\n\n // Collect all information from the specified source\n const result: Record<string, any> = {};\n for (const diagnostic of filteredDiagnostics) {\n try {\n const extractedData = actualParser(diagnostic);\n\n if (typeof extractedData === \"object\" && extractedData !== null) {\n // If object, merge with existing data\n Object.assign(result, extractedData);\n } else {\n // Otherwise store as separate entries\n const key = `entry_${Object.keys(result).length}`;\n result[key] = extractedData;\n }\n } catch (e) {\n console.warn(\n `Failed to process diagnostic from data ${String(data)}:`,\n e,\n );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,OAAO,MAAM,CAAC,CAAC;AAE9B,cAAI,OAAO,WAAW,YAAY,MAAM,MAAM,GAAG;AAC/C,oBAAQ,KAAK,qCAAqC,MAAM,CAAC,CAAC,EAAE;AAC5D,mBAAO;AAAA,UACT;AACA,iBAAO;AAAA,QACT,SAAS,GAAG;AACV,kBAAQ,KAAK,qCAAqC,MAAM,CAAC,CAAC,IAAI,CAAC;AAC/D,iBAAO;AAAA,QACT;AAAA,MACF;AAAA,IACF;AACA,WAAO;AAAA,EACT;AACF;AAKO,SAAS,cACd,MACA,aACA,QACqB;AACrB,QAAM,sBAAsB,YAAY;AAAA,IACtC,CAAC,eAAe,WAAW,SAAS;AAAA,EACtC;AACA,MAAI,oBAAoB,WAAW,GAAG;AACpC,WAAO,CAAC;AAAA,EACV;AAGA,QAAM,gBAAgB,CAAC,eAAgC;AACrD,QAAI;AACF,UAAI,WAAW,SAAS;AACtB,eAAO,KAAK,MAAM,WAAW,OAAO;AAAA,MACtC;AACA,aAAO,WAAW,WAAW,CAAC;AAAA,IAChC,SAAS,GAAG;AACV,aAAO,WAAW,WAAW,CAAC;AAAA,IAChC;AAAA,EACF;AAEA,QAAM,eAAe,UAAU;AAG/B,QAAM,SAA8B,CAAC;AACrC,aAAW,cAAc,qBAAqB;AAC5C,QAAI;AACF,YAAM,gBAAgB,aAAa,UAAU;AAE7C,UAAI,OAAO,kBAAkB,YAAY,kBAAkB,MAAM;AAE/D,eAAO,OAAO,QAAQ,aAAa;AAAA,MACrC,OAAO;AAEL,cAAM,MAAM,SAAS,OAAO,KAAK,MAAM,EAAE,MAAM;AAC/C,eAAO,GAAG,IAAI;AAAA,MAChB;AAAA,IACF,SAAS,GAAG;AACV,cAAQ;AAAA,QACN,0CAA0C,OAAO,IAAI,CAAC;AAAA,QACtD;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AACT;","names":["result"]}
|
|
1
|
+
{"version":3,"sources":["../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/language/metrics-output-plugin/src/index.ts","../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/language/metrics-output-plugin/src/metrics-output.ts","../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/language/metrics-output-plugin/src/utils.ts"],"sourcesContent":["export * from \"./metrics-output\";\nexport * from \"./utils\";\nexport * from \"./types\";\n","import * as fs from \"fs\";\nimport * as path from \"path\";\nimport { merge as deepMerge } from \"ts-deepmerge\";\nimport { Diagnostic } from \"vscode-languageserver-types\";\nimport type {\n PlayerLanguageService,\n PlayerLanguageServicePlugin,\n DocumentContext,\n} from \"@player-tools/json-language-service\";\nimport type {\n MetricsRoot,\n MetricsStats,\n MetricsFeatures,\n MetricsContent,\n MetricsReport,\n MetricValue,\n} from \"./types\";\n\nexport interface MetricsOutputConfig {\n /** Directory where the output file will be written */\n outputDir?: string;\n\n /** Name of the JSON output file */\n fileName?: string;\n\n /**\n * Custom properties to include at the root level of the output\n */\n rootProperties?: MetricsRoot;\n\n /**\n * Content-specific stats\n */\n stats?: MetricsStats;\n\n /**\n * Content-specific features\n */\n features?: MetricsFeatures;\n}\n\n/**\n * Normalizes a file path to use consistent separators and format\n */\nfunction normalizePath(filePath: string): string {\n // Convert backslashes to forward slashes for consistency\n const normalized = filePath.replace(/\\\\/g, \"/\");\n\n // Remove file:// protocol if present\n return normalized.replace(/^file:\\/\\//, \"\");\n}\n\n// Narrow ts-deepmerge’s generic return type to what's needed\nconst merge = deepMerge as <T>(...objects: Array<Partial<T>>) => T;\n\n/**\n * A plugin that writes diagnostic results to a JSON file in a specified output directory.\n * NOTE: This plugin is designed for CLI usage only and should not be used in an IDE.\n */\nexport class MetricsOutput implements PlayerLanguageServicePlugin {\n name = \"metrics-output-plugin\";\n\n private outputDir: string;\n private fileName: string;\n private rootProperties: MetricsRoot;\n private stats: MetricsStats;\n private features: MetricsFeatures;\n\n // In-memory storage of all results\n private aggregatedResults: MetricsReport = {\n content: {},\n };\n\n private get outputFilePath(): string {\n return path.resolve(this.outputDir, `${this.fileName}.json`);\n }\n\n constructor(options: MetricsOutputConfig = {}) {\n this.outputDir = options.outputDir || process.cwd();\n\n // Handle file name, stripping .json extension if provided\n let fileName = options.fileName || \"metrics\";\n if (fileName.endsWith(\".json\")) {\n fileName = fileName.split(\".\")[0]; // Remove extension\n }\n this.fileName = fileName;\n this.rootProperties = options.rootProperties || {};\n this.stats = options.stats || {};\n this.features = options.features || {};\n }\n\n apply(service: PlayerLanguageService): void {\n // Hook into the validation end to capture diagnostics and write output\n service.hooks.onValidateEnd.tap(\n this.name,\n (\n diagnostics: Diagnostic[],\n { documentContext }: { documentContext: DocumentContext },\n ): Diagnostic[] => {\n // If metrics file exists, load it\n if (fs.existsSync(this.outputFilePath)) {\n this.loadExistingMetrics();\n }\n\n this.generateFile(diagnostics, documentContext);\n\n return diagnostics;\n },\n );\n }\n\n private loadExistingMetrics(): void {\n try {\n const fileContent = fs.readFileSync(this.outputFilePath, \"utf-8\");\n\n // Handle empty file case - treat it as if no file exists\n if (!fileContent.trim()) {\n return;\n }\n\n const parsed: unknown = JSON.parse(fileContent);\n const existingMetrics: Partial<MetricsReport> =\n parsed && typeof parsed === \"object\"\n ? (parsed as Partial<MetricsReport>)\n : {};\n\n // Recursively merge existing metrics with current aggregated results\n this.aggregatedResults = merge<MetricsReport>(\n existingMetrics,\n this.aggregatedResults,\n );\n } catch (error) {\n // If we can't parse existing file, continue with current state\n console.warn(\n `Warning: Could not parse existing metrics file ${this.outputFilePath}. Continuing with current metrics. Error: ${error}`,\n );\n }\n }\n\n /**\n * Evaluates a value, executing it if it's a function\n */\n private evaluateValue(\n value: MetricValue,\n diagnostics: Diagnostic[],\n documentContext: DocumentContext,\n ) {\n if (typeof value === \"function\") {\n try {\n return value(diagnostics, documentContext);\n } catch (error) {\n documentContext.log.error(`Error evaluating value: ${error}`);\n return { error: `Value evaluation failed: ${error}` };\n }\n }\n return value;\n }\n\n private generateMetrics(\n diagnostics: Diagnostic[],\n documentContext: DocumentContext,\n ): MetricsStats {\n const statsSource = this.stats;\n // If stats is a function, evaluate it directly\n if (typeof statsSource === \"function\") {\n try {\n const result = statsSource(diagnostics, documentContext);\n if (typeof result === \"object\" && result !== null) {\n return result;\n }\n return { dynamicStatsValue: result };\n } catch (error) {\n documentContext.log.error(`Error evaluating stats function: ${error}`);\n return { error: `Stats function evaluation failed: ${error}` };\n }\n }\n\n // Otherwise process each metric in the record\n const result: MetricsStats = {};\n Object.entries(statsSource).forEach(([key, value]) => {\n result[key] = this.evaluateValue(value, diagnostics, documentContext);\n });\n\n return result;\n }\n\n private generateFeatures(\n diagnostics: Diagnostic[],\n documentContext: DocumentContext,\n ): Record<string, MetricValue> {\n const featuresSource = this.features;\n // If features is a function, evaluate it directly\n if (typeof featuresSource === \"function\") {\n try {\n const result = featuresSource(diagnostics, documentContext);\n if (typeof result === \"object\" && result !== null) {\n return result;\n }\n return { dynamicFeaturesValue: result };\n } catch (error) {\n documentContext.log.error(\n `Error evaluating features function: ${error}`,\n );\n return { error: `Features function evaluation failed: ${error}` };\n }\n }\n\n // Otherwise process each feature in the record\n const result: Record<string, MetricValue> = {};\n Object.entries(featuresSource).forEach(([key, value]) => {\n result[key] = this.evaluateValue(value, diagnostics, documentContext);\n });\n\n return result;\n }\n\n private generateFile(\n diagnostics: Diagnostic[],\n documentContext: DocumentContext,\n ): string {\n // Ensure the output directory exists\n const fullOutputDir = path.resolve(process.cwd(), this.outputDir);\n fs.mkdirSync(fullOutputDir, { recursive: true });\n\n // Get the file path from the document URI and normalize it\n const filePath = normalizePath(documentContext.document.uri);\n\n // Generate metrics\n const stats: MetricsStats = this.generateMetrics(\n diagnostics,\n documentContext,\n );\n const features: MetricsFeatures = this.generateFeatures(\n diagnostics,\n documentContext,\n );\n\n // Build this file's entry\n const newEntry: MetricsContent = {\n stats,\n ...(Object.keys(features).length > 0 ? { features } : {}),\n };\n\n // Evaluate root properties\n let rootProps: MetricsRoot;\n if (typeof this.rootProperties === \"function\") {\n try {\n const result = this.rootProperties(diagnostics, documentContext);\n if (typeof result === \"object\" && result !== null) {\n rootProps = result as Record<string, any>;\n } else {\n rootProps = { dynamicRootValue: result };\n }\n } catch (error) {\n documentContext.log.error(`Error evaluating root properties: ${error}`);\n rootProps = { error: `Root properties evaluation failed: ${error}` };\n }\n } else {\n rootProps = this.rootProperties as Record<string, any>;\n }\n\n // Single deep merge of root properties and content for this file\n this.aggregatedResults = merge<MetricsReport>(\n this.aggregatedResults,\n rootProps,\n { content: { [filePath]: newEntry } },\n );\n\n // Write ordered results: all root properties first, then content last\n const outputFilePath = path.join(fullOutputDir, `${this.fileName}.json`);\n const { content, ...root } = this.aggregatedResults;\n fs.writeFileSync(\n outputFilePath,\n JSON.stringify({ ...root, content }, null, 2),\n \"utf-8\",\n );\n\n return outputFilePath;\n }\n}\n","import { Diagnostic } from \"vscode-languageserver-types\";\n\n/**\n * Extracts data from diagnostic messages using a pattern\n */\nexport function extractFromDiagnostics<T>(\n pattern: RegExp,\n parser: (value: string) => T,\n): (diagnostics: Diagnostic[]) => T | undefined {\n return (diagnostics: Diagnostic[]): T | undefined => {\n for (const diagnostic of diagnostics) {\n const match = diagnostic.message.match(pattern);\n if (match && match[1]) {\n try {\n const result = parser(match[1]);\n // Check if result is NaN (only relevant for numeric parsers)\n if (typeof result === \"number\" && isNaN(result)) {\n console.warn(`Failed to parse diagnostic value: ${match[1]}`);\n return undefined;\n }\n return result;\n } catch (e) {\n console.warn(`Failed to parse diagnostic value: ${match[1]}`, e);\n return undefined;\n }\n }\n }\n return undefined;\n };\n}\n\n/**\n * Extracts data from diagnostics\n */\nexport function extractByData(\n data: string | symbol,\n diagnostics: Diagnostic[],\n parser?: (diagnostic: Diagnostic) => any,\n): Record<string, any> {\n const filteredDiagnostics = diagnostics.filter(\n (diagnostic) => diagnostic.data === data,\n );\n if (filteredDiagnostics.length === 0) {\n return {};\n }\n\n // Default parser that attempts to parse the message as JSON or returns the raw message\n const defaultParser = (diagnostic: Diagnostic): any => {\n try {\n if (diagnostic.message) {\n return JSON.parse(diagnostic.message);\n }\n return diagnostic.message || {};\n } catch (e) {\n return diagnostic.message || {};\n }\n };\n\n const actualParser = parser || defaultParser;\n\n // Collect all information from the specified source\n const result: Record<string, any> = {};\n for (const diagnostic of filteredDiagnostics) {\n try {\n const extractedData = actualParser(diagnostic);\n\n if (typeof extractedData === \"object\" && extractedData !== null) {\n // If object, merge with existing data\n Object.assign(result, extractedData);\n } else {\n // Otherwise store as separate entries\n const key = `entry_${Object.keys(result).length}`;\n result[key] = extractedData;\n }\n } catch (e) {\n console.warn(\n `Failed to process diagnostic from data ${String(data)}:`,\n e,\n );\n }\n }\n\n return result; // Always returns an object, even if empty\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,SAAoB;AACpB,WAAsB;AACtB,0BAAmC;AA0CnC,SAAS,cAAc,UAA0B;AAE/C,QAAM,aAAa,SAAS,QAAQ,OAAO,GAAG;AAG9C,SAAO,WAAW,QAAQ,cAAc,EAAE;AAC5C;AAGA,IAAM,QAAQ,oBAAAA;AAMP,IAAM,gBAAN,MAA2D;AAAA,EAChE,OAAO;AAAA,EAEC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA;AAAA,EAGA,oBAAmC;AAAA,IACzC,SAAS,CAAC;AAAA,EACZ;AAAA,EAEA,IAAY,iBAAyB;AACnC,WAAY,aAAQ,KAAK,WAAW,GAAG,KAAK,QAAQ,OAAO;AAAA,EAC7D;AAAA,EAEA,YAAY,UAA+B,CAAC,GAAG;AAC7C,SAAK,YAAY,QAAQ,aAAa,QAAQ,IAAI;AAGlD,QAAI,WAAW,QAAQ,YAAY;AACnC,QAAI,SAAS,SAAS,OAAO,GAAG;AAC9B,iBAAW,SAAS,MAAM,GAAG,EAAE,CAAC;AAAA,IAClC;AACA,SAAK,WAAW;AAChB,SAAK,iBAAiB,QAAQ,kBAAkB,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;AAC5D,mBAAO;AAAA,UACT;AACA,iBAAO;AAAA,QACT,SAAS,GAAG;AACV,kBAAQ,KAAK,qCAAqC,MAAM,CAAC,CAAC,IAAI,CAAC;AAC/D,iBAAO;AAAA,QACT;AAAA,MACF;AAAA,IACF;AACA,WAAO;AAAA,EACT;AACF;AAKO,SAAS,cACd,MACA,aACA,QACqB;AACrB,QAAM,sBAAsB,YAAY;AAAA,IACtC,CAAC,eAAe,WAAW,SAAS;AAAA,EACtC;AACA,MAAI,oBAAoB,WAAW,GAAG;AACpC,WAAO,CAAC;AAAA,EACV;AAGA,QAAM,gBAAgB,CAAC,eAAgC;AACrD,QAAI;AACF,UAAI,WAAW,SAAS;AACtB,eAAO,KAAK,MAAM,WAAW,OAAO;AAAA,MACtC;AACA,aAAO,WAAW,WAAW,CAAC;AAAA,IAChC,SAAS,GAAG;AACV,aAAO,WAAW,WAAW,CAAC;AAAA,IAChC;AAAA,EACF;AAEA,QAAM,eAAe,UAAU;AAG/B,QAAM,SAA8B,CAAC;AACrC,aAAW,cAAc,qBAAqB;AAC5C,QAAI;AACF,YAAM,gBAAgB,aAAa,UAAU;AAE7C,UAAI,OAAO,kBAAkB,YAAY,kBAAkB,MAAM;AAE/D,eAAO,OAAO,QAAQ,aAAa;AAAA,MACrC,OAAO;AAEL,cAAM,MAAM,SAAS,OAAO,KAAK,MAAM,EAAE,MAAM;AAC/C,eAAO,GAAG,IAAI;AAAA,MAChB;AAAA,IACF,SAAS,GAAG;AACV,cAAQ;AAAA,QACN,0CAA0C,OAAO,IAAI,CAAC;AAAA,QACtD;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AACT;","names":["deepMerge","result"]}
|
package/dist/index.legacy-esm.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
// ../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/language/metrics-output-plugin/src/metrics-output.ts
|
|
2
2
|
import * as fs from "fs";
|
|
3
3
|
import * as path from "path";
|
|
4
|
+
import { merge as deepMerge } from "ts-deepmerge";
|
|
4
5
|
function normalizePath(filePath) {
|
|
5
6
|
const normalized = filePath.replace(/\\/g, "/");
|
|
6
7
|
return normalized.replace(/^file:\/\//, "");
|
|
7
8
|
}
|
|
9
|
+
var merge = deepMerge;
|
|
8
10
|
var MetricsOutput = class {
|
|
9
11
|
name = "metrics-output-plugin";
|
|
10
12
|
outputDir;
|
|
@@ -13,7 +15,12 @@ var MetricsOutput = class {
|
|
|
13
15
|
stats;
|
|
14
16
|
features;
|
|
15
17
|
// In-memory storage of all results
|
|
16
|
-
aggregatedResults = {
|
|
18
|
+
aggregatedResults = {
|
|
19
|
+
content: {}
|
|
20
|
+
};
|
|
21
|
+
get outputFilePath() {
|
|
22
|
+
return path.resolve(this.outputDir, `${this.fileName}.json`);
|
|
23
|
+
}
|
|
17
24
|
constructor(options = {}) {
|
|
18
25
|
this.outputDir = options.outputDir || process.cwd();
|
|
19
26
|
let fileName = options.fileName || "metrics";
|
|
@@ -24,36 +31,36 @@ var MetricsOutput = class {
|
|
|
24
31
|
this.rootProperties = options.rootProperties || {};
|
|
25
32
|
this.stats = options.stats || {};
|
|
26
33
|
this.features = options.features || {};
|
|
27
|
-
this.aggregatedResults = {
|
|
28
|
-
content: {}
|
|
29
|
-
};
|
|
30
34
|
}
|
|
31
35
|
apply(service) {
|
|
32
36
|
service.hooks.onValidateEnd.tap(
|
|
33
37
|
this.name,
|
|
34
38
|
(diagnostics, { documentContext }) => {
|
|
39
|
+
if (fs.existsSync(this.outputFilePath)) {
|
|
40
|
+
this.loadExistingMetrics();
|
|
41
|
+
}
|
|
35
42
|
this.generateFile(diagnostics, documentContext);
|
|
36
43
|
return diagnostics;
|
|
37
44
|
}
|
|
38
45
|
);
|
|
39
46
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
try {
|
|
46
|
-
const result = this.rootProperties(diagnostics, documentContext);
|
|
47
|
-
if (typeof result === "object" && result !== null) {
|
|
48
|
-
return result;
|
|
49
|
-
}
|
|
50
|
-
return { dynamicRootValue: result };
|
|
51
|
-
} catch (error) {
|
|
52
|
-
documentContext.log.error(`Error evaluating root properties: ${error}`);
|
|
53
|
-
return { error: `Root properties evaluation failed: ${error}` };
|
|
47
|
+
loadExistingMetrics() {
|
|
48
|
+
try {
|
|
49
|
+
const fileContent = fs.readFileSync(this.outputFilePath, "utf-8");
|
|
50
|
+
if (!fileContent.trim()) {
|
|
51
|
+
return;
|
|
54
52
|
}
|
|
53
|
+
const parsed = JSON.parse(fileContent);
|
|
54
|
+
const existingMetrics = parsed && typeof parsed === "object" ? parsed : {};
|
|
55
|
+
this.aggregatedResults = merge(
|
|
56
|
+
existingMetrics,
|
|
57
|
+
this.aggregatedResults
|
|
58
|
+
);
|
|
59
|
+
} catch (error) {
|
|
60
|
+
console.warn(
|
|
61
|
+
`Warning: Could not parse existing metrics file ${this.outputFilePath}. Continuing with current metrics. Error: ${error}`
|
|
62
|
+
);
|
|
55
63
|
}
|
|
56
|
-
return this.rootProperties;
|
|
57
64
|
}
|
|
58
65
|
/**
|
|
59
66
|
* Evaluates a value, executing it if it's a function
|
|
@@ -70,9 +77,10 @@ var MetricsOutput = class {
|
|
|
70
77
|
return value;
|
|
71
78
|
}
|
|
72
79
|
generateMetrics(diagnostics, documentContext) {
|
|
73
|
-
|
|
80
|
+
const statsSource = this.stats;
|
|
81
|
+
if (typeof statsSource === "function") {
|
|
74
82
|
try {
|
|
75
|
-
const result2 =
|
|
83
|
+
const result2 = statsSource(diagnostics, documentContext);
|
|
76
84
|
if (typeof result2 === "object" && result2 !== null) {
|
|
77
85
|
return result2;
|
|
78
86
|
}
|
|
@@ -83,15 +91,16 @@ var MetricsOutput = class {
|
|
|
83
91
|
}
|
|
84
92
|
}
|
|
85
93
|
const result = {};
|
|
86
|
-
Object.entries(
|
|
94
|
+
Object.entries(statsSource).forEach(([key, value]) => {
|
|
87
95
|
result[key] = this.evaluateValue(value, diagnostics, documentContext);
|
|
88
96
|
});
|
|
89
97
|
return result;
|
|
90
98
|
}
|
|
91
99
|
generateFeatures(diagnostics, documentContext) {
|
|
92
|
-
|
|
100
|
+
const featuresSource = this.features;
|
|
101
|
+
if (typeof featuresSource === "function") {
|
|
93
102
|
try {
|
|
94
|
-
const result2 =
|
|
103
|
+
const result2 = featuresSource(diagnostics, documentContext);
|
|
95
104
|
if (typeof result2 === "object" && result2 !== null) {
|
|
96
105
|
return result2;
|
|
97
106
|
}
|
|
@@ -104,7 +113,7 @@ var MetricsOutput = class {
|
|
|
104
113
|
}
|
|
105
114
|
}
|
|
106
115
|
const result = {};
|
|
107
|
-
Object.entries(
|
|
116
|
+
Object.entries(featuresSource).forEach(([key, value]) => {
|
|
108
117
|
result[key] = this.evaluateValue(value, diagnostics, documentContext);
|
|
109
118
|
});
|
|
110
119
|
return result;
|
|
@@ -113,18 +122,44 @@ var MetricsOutput = class {
|
|
|
113
122
|
const fullOutputDir = path.resolve(process.cwd(), this.outputDir);
|
|
114
123
|
fs.mkdirSync(fullOutputDir, { recursive: true });
|
|
115
124
|
const filePath = normalizePath(documentContext.document.uri);
|
|
116
|
-
const stats = this.generateMetrics(
|
|
117
|
-
|
|
118
|
-
|
|
125
|
+
const stats = this.generateMetrics(
|
|
126
|
+
diagnostics,
|
|
127
|
+
documentContext
|
|
128
|
+
);
|
|
129
|
+
const features = this.generateFeatures(
|
|
130
|
+
diagnostics,
|
|
131
|
+
documentContext
|
|
132
|
+
);
|
|
133
|
+
const newEntry = {
|
|
119
134
|
stats,
|
|
120
135
|
...Object.keys(features).length > 0 ? { features } : {}
|
|
121
136
|
};
|
|
122
|
-
|
|
123
|
-
|
|
137
|
+
let rootProps;
|
|
138
|
+
if (typeof this.rootProperties === "function") {
|
|
139
|
+
try {
|
|
140
|
+
const result = this.rootProperties(diagnostics, documentContext);
|
|
141
|
+
if (typeof result === "object" && result !== null) {
|
|
142
|
+
rootProps = result;
|
|
143
|
+
} else {
|
|
144
|
+
rootProps = { dynamicRootValue: result };
|
|
145
|
+
}
|
|
146
|
+
} catch (error) {
|
|
147
|
+
documentContext.log.error(`Error evaluating root properties: ${error}`);
|
|
148
|
+
rootProps = { error: `Root properties evaluation failed: ${error}` };
|
|
149
|
+
}
|
|
150
|
+
} else {
|
|
151
|
+
rootProps = this.rootProperties;
|
|
152
|
+
}
|
|
153
|
+
this.aggregatedResults = merge(
|
|
154
|
+
this.aggregatedResults,
|
|
155
|
+
rootProps,
|
|
156
|
+
{ content: { [filePath]: newEntry } }
|
|
157
|
+
);
|
|
124
158
|
const outputFilePath = path.join(fullOutputDir, `${this.fileName}.json`);
|
|
159
|
+
const { content, ...root } = this.aggregatedResults;
|
|
125
160
|
fs.writeFileSync(
|
|
126
161
|
outputFilePath,
|
|
127
|
-
JSON.stringify(
|
|
162
|
+
JSON.stringify({ ...root, content }, null, 2),
|
|
128
163
|
"utf-8"
|
|
129
164
|
);
|
|
130
165
|
return outputFilePath;
|
package/dist/index.mjs
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
// ../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/language/metrics-output-plugin/src/metrics-output.ts
|
|
2
2
|
import * as fs from "fs";
|
|
3
3
|
import * as path from "path";
|
|
4
|
+
import { merge as deepMerge } from "ts-deepmerge";
|
|
4
5
|
function normalizePath(filePath) {
|
|
5
6
|
const normalized = filePath.replace(/\\/g, "/");
|
|
6
7
|
return normalized.replace(/^file:\/\//, "");
|
|
7
8
|
}
|
|
9
|
+
var merge = deepMerge;
|
|
8
10
|
var MetricsOutput = class {
|
|
9
11
|
name = "metrics-output-plugin";
|
|
10
12
|
outputDir;
|
|
@@ -13,7 +15,12 @@ var MetricsOutput = class {
|
|
|
13
15
|
stats;
|
|
14
16
|
features;
|
|
15
17
|
// In-memory storage of all results
|
|
16
|
-
aggregatedResults = {
|
|
18
|
+
aggregatedResults = {
|
|
19
|
+
content: {}
|
|
20
|
+
};
|
|
21
|
+
get outputFilePath() {
|
|
22
|
+
return path.resolve(this.outputDir, `${this.fileName}.json`);
|
|
23
|
+
}
|
|
17
24
|
constructor(options = {}) {
|
|
18
25
|
this.outputDir = options.outputDir || process.cwd();
|
|
19
26
|
let fileName = options.fileName || "metrics";
|
|
@@ -24,36 +31,36 @@ var MetricsOutput = class {
|
|
|
24
31
|
this.rootProperties = options.rootProperties || {};
|
|
25
32
|
this.stats = options.stats || {};
|
|
26
33
|
this.features = options.features || {};
|
|
27
|
-
this.aggregatedResults = {
|
|
28
|
-
content: {}
|
|
29
|
-
};
|
|
30
34
|
}
|
|
31
35
|
apply(service) {
|
|
32
36
|
service.hooks.onValidateEnd.tap(
|
|
33
37
|
this.name,
|
|
34
38
|
(diagnostics, { documentContext }) => {
|
|
39
|
+
if (fs.existsSync(this.outputFilePath)) {
|
|
40
|
+
this.loadExistingMetrics();
|
|
41
|
+
}
|
|
35
42
|
this.generateFile(diagnostics, documentContext);
|
|
36
43
|
return diagnostics;
|
|
37
44
|
}
|
|
38
45
|
);
|
|
39
46
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
try {
|
|
46
|
-
const result = this.rootProperties(diagnostics, documentContext);
|
|
47
|
-
if (typeof result === "object" && result !== null) {
|
|
48
|
-
return result;
|
|
49
|
-
}
|
|
50
|
-
return { dynamicRootValue: result };
|
|
51
|
-
} catch (error) {
|
|
52
|
-
documentContext.log.error(`Error evaluating root properties: ${error}`);
|
|
53
|
-
return { error: `Root properties evaluation failed: ${error}` };
|
|
47
|
+
loadExistingMetrics() {
|
|
48
|
+
try {
|
|
49
|
+
const fileContent = fs.readFileSync(this.outputFilePath, "utf-8");
|
|
50
|
+
if (!fileContent.trim()) {
|
|
51
|
+
return;
|
|
54
52
|
}
|
|
53
|
+
const parsed = JSON.parse(fileContent);
|
|
54
|
+
const existingMetrics = parsed && typeof parsed === "object" ? parsed : {};
|
|
55
|
+
this.aggregatedResults = merge(
|
|
56
|
+
existingMetrics,
|
|
57
|
+
this.aggregatedResults
|
|
58
|
+
);
|
|
59
|
+
} catch (error) {
|
|
60
|
+
console.warn(
|
|
61
|
+
`Warning: Could not parse existing metrics file ${this.outputFilePath}. Continuing with current metrics. Error: ${error}`
|
|
62
|
+
);
|
|
55
63
|
}
|
|
56
|
-
return this.rootProperties;
|
|
57
64
|
}
|
|
58
65
|
/**
|
|
59
66
|
* Evaluates a value, executing it if it's a function
|
|
@@ -70,9 +77,10 @@ var MetricsOutput = class {
|
|
|
70
77
|
return value;
|
|
71
78
|
}
|
|
72
79
|
generateMetrics(diagnostics, documentContext) {
|
|
73
|
-
|
|
80
|
+
const statsSource = this.stats;
|
|
81
|
+
if (typeof statsSource === "function") {
|
|
74
82
|
try {
|
|
75
|
-
const result2 =
|
|
83
|
+
const result2 = statsSource(diagnostics, documentContext);
|
|
76
84
|
if (typeof result2 === "object" && result2 !== null) {
|
|
77
85
|
return result2;
|
|
78
86
|
}
|
|
@@ -83,15 +91,16 @@ var MetricsOutput = class {
|
|
|
83
91
|
}
|
|
84
92
|
}
|
|
85
93
|
const result = {};
|
|
86
|
-
Object.entries(
|
|
94
|
+
Object.entries(statsSource).forEach(([key, value]) => {
|
|
87
95
|
result[key] = this.evaluateValue(value, diagnostics, documentContext);
|
|
88
96
|
});
|
|
89
97
|
return result;
|
|
90
98
|
}
|
|
91
99
|
generateFeatures(diagnostics, documentContext) {
|
|
92
|
-
|
|
100
|
+
const featuresSource = this.features;
|
|
101
|
+
if (typeof featuresSource === "function") {
|
|
93
102
|
try {
|
|
94
|
-
const result2 =
|
|
103
|
+
const result2 = featuresSource(diagnostics, documentContext);
|
|
95
104
|
if (typeof result2 === "object" && result2 !== null) {
|
|
96
105
|
return result2;
|
|
97
106
|
}
|
|
@@ -104,7 +113,7 @@ var MetricsOutput = class {
|
|
|
104
113
|
}
|
|
105
114
|
}
|
|
106
115
|
const result = {};
|
|
107
|
-
Object.entries(
|
|
116
|
+
Object.entries(featuresSource).forEach(([key, value]) => {
|
|
108
117
|
result[key] = this.evaluateValue(value, diagnostics, documentContext);
|
|
109
118
|
});
|
|
110
119
|
return result;
|
|
@@ -113,18 +122,44 @@ var MetricsOutput = class {
|
|
|
113
122
|
const fullOutputDir = path.resolve(process.cwd(), this.outputDir);
|
|
114
123
|
fs.mkdirSync(fullOutputDir, { recursive: true });
|
|
115
124
|
const filePath = normalizePath(documentContext.document.uri);
|
|
116
|
-
const stats = this.generateMetrics(
|
|
117
|
-
|
|
118
|
-
|
|
125
|
+
const stats = this.generateMetrics(
|
|
126
|
+
diagnostics,
|
|
127
|
+
documentContext
|
|
128
|
+
);
|
|
129
|
+
const features = this.generateFeatures(
|
|
130
|
+
diagnostics,
|
|
131
|
+
documentContext
|
|
132
|
+
);
|
|
133
|
+
const newEntry = {
|
|
119
134
|
stats,
|
|
120
135
|
...Object.keys(features).length > 0 ? { features } : {}
|
|
121
136
|
};
|
|
122
|
-
|
|
123
|
-
|
|
137
|
+
let rootProps;
|
|
138
|
+
if (typeof this.rootProperties === "function") {
|
|
139
|
+
try {
|
|
140
|
+
const result = this.rootProperties(diagnostics, documentContext);
|
|
141
|
+
if (typeof result === "object" && result !== null) {
|
|
142
|
+
rootProps = result;
|
|
143
|
+
} else {
|
|
144
|
+
rootProps = { dynamicRootValue: result };
|
|
145
|
+
}
|
|
146
|
+
} catch (error) {
|
|
147
|
+
documentContext.log.error(`Error evaluating root properties: ${error}`);
|
|
148
|
+
rootProps = { error: `Root properties evaluation failed: ${error}` };
|
|
149
|
+
}
|
|
150
|
+
} else {
|
|
151
|
+
rootProps = this.rootProperties;
|
|
152
|
+
}
|
|
153
|
+
this.aggregatedResults = merge(
|
|
154
|
+
this.aggregatedResults,
|
|
155
|
+
rootProps,
|
|
156
|
+
{ content: { [filePath]: newEntry } }
|
|
157
|
+
);
|
|
124
158
|
const outputFilePath = path.join(fullOutputDir, `${this.fileName}.json`);
|
|
159
|
+
const { content, ...root } = this.aggregatedResults;
|
|
125
160
|
fs.writeFileSync(
|
|
126
161
|
outputFilePath,
|
|
127
|
-
JSON.stringify(
|
|
162
|
+
JSON.stringify({ ...root, content }, null, 2),
|
|
128
163
|
"utf-8"
|
|
129
164
|
);
|
|
130
165
|
return outputFilePath;
|