@cloudcare/guance-front-tools 1.0.15 → 1.0.16

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