UNPKG

ng-alain-plugin-theme

Version:
66 lines (65 loc) 2.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.mergePath = exports.d = exports.getJSON = exports.deepMergeKey = void 0; const jsonc_parser_1 = require("jsonc-parser"); const fs_1 = require("fs"); function deepMergeKey(original, ingoreArray, ...objects) { if (Array.isArray(original) || typeof original !== 'object') return original; const isObject = (v) => typeof v === 'object' || typeof v === 'function'; const merge = (target, obj) => { Object.keys(obj) .filter(key => key !== '__proto__' && Object.prototype.hasOwnProperty.call(obj, key)) .forEach(key => { const oldValue = obj[key]; const newValue = target[key]; if (!ingoreArray && Array.isArray(newValue)) { target[key] = [...newValue, ...oldValue]; } else if (oldValue != null && isObject(oldValue) && newValue != null && isObject(newValue)) { target[key] = merge(newValue, oldValue); } else { target[key] = oldValue; } }); return target; }; objects.filter(v => isObject(v)).forEach(v => merge(original, v)); return original; } exports.deepMergeKey = deepMergeKey; function getJSON(jsonFile) { const content = (0, fs_1.readFileSync)(jsonFile, 'utf-8'); try { const errors = []; const result = (0, jsonc_parser_1.parse)(content, errors, { allowTrailingComma: true }); if (errors.length) { const { error, offset } = errors[0]; throw new Error(`Failed to parse "${jsonFile}" as JSON AST Object. ${(0, jsonc_parser_1.printParseErrorCode)(error)} at location: ${offset}.`); } return result; } catch (ex) { console.log(`Can't parse json file (${jsonFile}), pls check for comments or trailing commas, or validate json via https://jsonlint.com/`); throw ex; } } exports.getJSON = getJSON; function d(config, message, data) { if (config.debug === true) { console.log(`[debug] ${message}`); if (data) { console.log(JSON.stringify(data)); } } } exports.d = d; function mergePath(...args) { const res = args .map(v => (v.startsWith('/') ? v.substring(1) : v)) .map(v => (v.endsWith('/') ? v.substring(0, v.length - 1) : v)) .join('/'); return res; } exports.mergePath = mergePath;