@cloudcare/guance-front-tools 1.0.13 → 1.0.15

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.
@@ -0,0 +1,147 @@
1
+ #!/usr/bin/env node
2
+
3
+ import fs from 'fs'
4
+ import path from 'path'
5
+ import { pathToFileURL } from 'url'
6
+ import Ajv from 'ajv'
7
+ import { convertDashboard } from './convert-grafana-dashboard-core.js'
8
+
9
+ if (isDirectExecution()) {
10
+ main()
11
+ }
12
+
13
+ function isDirectExecution() {
14
+ if (!process.argv[1]) return false
15
+ return import.meta.url === pathToFileURL(path.resolve(process.argv[1])).href
16
+ }
17
+
18
+ export function main() {
19
+ const { inputPath, outputPath, validateOutput, schemaId, guancePromqlCompatible, keepGrafanaMeta } = parseArgs(process.argv.slice(2))
20
+ const grafanaDashboard = readJson(inputPath)
21
+ const guanceDashboard = convertDashboard(grafanaDashboard, { guancePromqlCompatible, keepGrafanaMeta })
22
+
23
+ fs.mkdirSync(path.dirname(outputPath), { recursive: true })
24
+ fs.writeFileSync(outputPath, `${JSON.stringify(guanceDashboard, null, 2)}\n`, 'utf8')
25
+ console.log(`Converted ${inputPath} -> ${outputPath}`)
26
+
27
+ if (validateOutput) {
28
+ validateDashboardFile(outputPath, schemaId)
29
+ }
30
+ }
31
+
32
+ function parseArgs(args) {
33
+ let inputPath = ''
34
+ let outputPath = ''
35
+ let validateOutput = false
36
+ let schemaId = 'dashboard-schema.json'
37
+ let guancePromqlCompatible = false
38
+ let keepGrafanaMeta = false
39
+
40
+ for (let index = 0; index < args.length; index++) {
41
+ const value = args[index]
42
+ if ((value === '-i' || value === '--input') && args[index + 1]) {
43
+ inputPath = path.resolve(args[++index])
44
+ continue
45
+ }
46
+ if ((value === '-o' || value === '--output') && args[index + 1]) {
47
+ outputPath = path.resolve(args[++index])
48
+ continue
49
+ }
50
+ if (value === '--validate') {
51
+ validateOutput = true
52
+ continue
53
+ }
54
+ if (value === '--schema' && args[index + 1]) {
55
+ schemaId = args[++index]
56
+ continue
57
+ }
58
+ if (value === '--guance-promql-compatible') {
59
+ guancePromqlCompatible = true
60
+ continue
61
+ }
62
+ if (value === '--keep-grafana-meta') {
63
+ keepGrafanaMeta = true
64
+ continue
65
+ }
66
+ if (value === '-h' || value === '--help') {
67
+ printHelp()
68
+ process.exit(0)
69
+ }
70
+ }
71
+
72
+ if (!inputPath) {
73
+ printHelp()
74
+ process.exit(1)
75
+ }
76
+
77
+ if (!outputPath) {
78
+ const parsed = path.parse(inputPath)
79
+ outputPath = path.join(parsed.dir, `${parsed.name}.guance.json`)
80
+ }
81
+
82
+ return { inputPath, outputPath, validateOutput, schemaId, guancePromqlCompatible, keepGrafanaMeta }
83
+ }
84
+
85
+ function printHelp() {
86
+ console.error(
87
+ 'Usage: node convert-grafana-dashboard.mjs --input <grafana.json> [--output <guance.json>] [--validate] [--schema <schema-id>] [--guance-promql-compatible] [--keep-grafana-meta]'
88
+ )
89
+ }
90
+
91
+ export function validateDashboardFile(filePath, schemaId) {
92
+ const projectRoot = findProjectRoot()
93
+ const schemasDirectory = path.join(projectRoot, 'schemas')
94
+ const ajv = new Ajv({ allErrors: true })
95
+
96
+ forEachFile(schemasDirectory, (schemaPath) => {
97
+ if (!schemaPath.endsWith('.json')) return
98
+ ajv.addSchema(readJson(schemaPath))
99
+ })
100
+
101
+ const valid = ajv.validate(schemaId, readJson(filePath))
102
+ if (valid) {
103
+ console.log(`Validated ${filePath} against ${schemaId}`)
104
+ return
105
+ }
106
+
107
+ console.error(`Validation failed for ${filePath} against ${schemaId}:`)
108
+ for (const error of ajv.errors || []) {
109
+ const instancePath = error.instancePath || '/'
110
+ console.error(`- ${instancePath} ${error.message}`)
111
+ }
112
+ process.exit(1)
113
+ }
114
+
115
+ function findProjectRoot() {
116
+ let currentDirectory = process.cwd()
117
+
118
+ while (true) {
119
+ if (fs.existsSync(path.join(currentDirectory, 'schemas', 'dashboard-schema.json'))) {
120
+ return currentDirectory
121
+ }
122
+
123
+ const parentDirectory = path.dirname(currentDirectory)
124
+ if (parentDirectory === currentDirectory) {
125
+ throw new Error('Could not locate project root containing schemas/dashboard-schema.json')
126
+ }
127
+ currentDirectory = parentDirectory
128
+ }
129
+ }
130
+
131
+ function readJson(filePath) {
132
+ return JSON.parse(fs.readFileSync(filePath, 'utf8'))
133
+ }
134
+
135
+ function forEachFile(directoryPath, visitor) {
136
+ const entries = fs.readdirSync(directoryPath, { withFileTypes: true })
137
+ for (const entry of entries) {
138
+ const entryPath = path.join(directoryPath, entry.name)
139
+ if (entry.isDirectory()) {
140
+ forEachFile(entryPath, visitor)
141
+ continue
142
+ }
143
+ visitor(entryPath)
144
+ }
145
+ }
146
+
147
+ export { convertDashboard }
@@ -4,7 +4,7 @@ exports.covert = covert;
4
4
  // This script is the shared conversion source for the published API and CLI.
5
5
  // Keep it thin and delegate to the maintained converter implementation.
6
6
  // @ts-ignore
7
- var convert_grafana_dashboard_mjs_1 = require("../../skills/grafana-to-guance-dashboard/scripts/convert-grafana-dashboard.mjs");
7
+ var convert_grafana_dashboard_core_js_1 = require("./convert-grafana-dashboard-core.js");
8
8
  function covert(grafanaData) {
9
- return (0, convert_grafana_dashboard_mjs_1.convertDashboard)(grafanaData);
9
+ return (0, convert_grafana_dashboard_core_js_1.convertDashboard)(grafanaData);
10
10
  }