@veecode-platform/safira-cli
Version:
Generate a microservice project from your spec.
46 lines (45 loc) • 1.76 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.JsonUtils = void 0;
const file_exception_1 = require("../exception/file-exception");
const yaml_utils_1 = require("./yaml-utils");
const file_system_utils_1 = require("./file-system-utils");
class JsonUtils {
static loadJsonFile(filePath) {
if (!file_system_utils_1.FileSystemUtils.exists(filePath)) {
throw new file_exception_1.FileNotFoundException(`file not found, ${filePath}`);
}
const jsonData = file_system_utils_1.FileSystemUtils.loadFile(filePath);
return JSON.parse(jsonData);
}
static parse(jsonData) {
return JSON.parse(jsonData);
}
static stringify(jsonObject, pretty = false) {
return pretty ? JSON.stringify(jsonObject, null, 2) : JSON.stringify(jsonObject);
}
static writeFile(filePath, jsonObject, pretty = false) {
file_system_utils_1.FileSystemUtils.writeFile(filePath, this.stringify(jsonObject, pretty));
}
static parseToYaml(jsonFilePath, yamlFileToSave) {
const jsonData = this.loadJsonFile(jsonFilePath);
const yamlData = yaml_utils_1.YamlUtils.stringify(jsonData);
if (!yaml_utils_1.YamlUtils.isYamlFile(yamlFileToSave)) {
throw new Error("yaml file must have .yaml extension");
}
file_system_utils_1.FileSystemUtils.writeFile(yamlFileToSave, yamlData);
}
static isJsonFile(filePath) {
return (filePath?.toLowerCase().match(/\.[()jnos]+$/i) || []).length > 0;
}
static isJsonString(stringJson) {
try {
JSON.parse(stringJson);
}
catch {
return false;
}
return true;
}
}
exports.JsonUtils = JsonUtils;