@player-tools/metrics-output-plugin 0.12.1-next.1 → 0.12.1-next.3

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.
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../../../../../../../../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":["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 );\n }\n }\n\n return result; // Always returns an object, even if empty\n}\n"],"mappings":";A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,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/metrics-output.ts","../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/language/metrics-output-plugin/src/utils.ts"],"sourcesContent":["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 and append to 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 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.`,\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,YAAY,QAAQ;AACpB,YAAY,UAAU;AACtB,SAAS,SAAS,iBAAiB;AA0CnC,SAAS,cAAc,UAA0B;AAE/C,QAAM,aAAa,SAAS,QAAQ,OAAO,GAAG;AAG9C,SAAO,WAAW,QAAQ,cAAc,EAAE;AAC5C;AAGA,IAAM,QAAQ;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,CAAC;AACjD,SAAK,QAAQ,QAAQ,SAAS,CAAC;AAC/B,SAAK,WAAW,QAAQ,YAAY,CAAC;AAAA,EACvC;AAAA,EAEA,MAAM,SAAsC;AAE1C,YAAQ,MAAM,cAAc;AAAA,MAC1B,KAAK;AAAA,MACL,CACE,aACA,EAAE,gBAAgB,MACD;AAEjB,YAAO,cAAW,KAAK,cAAc,GAAG;AACtC,eAAK,oBAAoB;AAAA,QAC3B;AAEA,aAAK,aAAa,aAAa,eAAe;AAE9C,eAAO;AAAA,MACT;AAAA,IACF;AAAA,EACF;AAAA,EAEQ,s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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"]}
package/package.json CHANGED
@@ -6,16 +6,17 @@
6
6
  "types"
7
7
  ],
8
8
  "name": "@player-tools/metrics-output-plugin",
9
- "version": "0.12.1-next.1",
9
+ "version": "0.12.1-next.3",
10
10
  "main": "dist/cjs/index.cjs",
11
11
  "dependencies": {
12
- "@player-tools/json-language-service": "0.12.1-next.1",
12
+ "@player-tools/json-language-service": "0.12.1-next.3",
13
13
  "@player-ui/player": "0.12.0-next.1",
14
14
  "jsonc-parser": "^2.3.1",
15
15
  "typescript-template-language-service-decorator": "^2.3.1",
16
16
  "vscode-languageserver-types": "^3.15.1",
17
17
  "vscode-languageserver-textdocument": "^1.0.1",
18
18
  "typescript": "5.5.4",
19
+ "ts-deepmerge": "^7.0.2",
19
20
  "tslib": "^2.6.2"
20
21
  },
21
22
  "devDependencies": {