UNPKG

noodl-loader

Version:

Loader for noodl applications

239 lines 8.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.unwrap = exports.withYmlExt = exports.toNode = exports.toDocument = exports.stringify = exports.getScalars = exports.toYml = exports.toJson = exports.toDoc = exports.parseAs = exports.parse = exports.merge = exports.isNode = exports.getNodeTypeLabel = exports.fetchYml = exports.ensureYmlExt = exports.visit = exports.isDocument = exports.isCollection = exports.isPair = exports.isScalar = exports.isSeq = exports.isMap = void 0; const tslib_1 = require("tslib"); const noodl_core_1 = require("noodl-core"); const axios_1 = tslib_1.__importDefault(require("axios")); const yaml_1 = tslib_1.__importStar(require("yaml")); var yaml_2 = require("yaml"); Object.defineProperty(exports, "isMap", { enumerable: true, get: function () { return yaml_2.isMap; } }); Object.defineProperty(exports, "isSeq", { enumerable: true, get: function () { return yaml_2.isSeq; } }); Object.defineProperty(exports, "isScalar", { enumerable: true, get: function () { return yaml_2.isScalar; } }); Object.defineProperty(exports, "isPair", { enumerable: true, get: function () { return yaml_2.isPair; } }); Object.defineProperty(exports, "isCollection", { enumerable: true, get: function () { return yaml_2.isCollection; } }); Object.defineProperty(exports, "isDocument", { enumerable: true, get: function () { return yaml_2.isDocument; } }); Object.defineProperty(exports, "visit", { enumerable: true, get: function () { return yaml_2.visit; } }); function ensureYmlExt(value) { if (!value.endsWith('.yml')) return `${value}.yml`; return value; } exports.ensureYmlExt = ensureYmlExt; function fetchYml(url, as = 'yml') { return tslib_1.__awaiter(this, void 0, void 0, function* () { try { const isJson = as === 'json'; const isDoc = as === 'doc'; const yml = (yield axios_1.default.get(url)).data; return isJson ? yaml_1.default.parse(yml) : isDoc ? toDocument(yml) : yml; } catch (error) { const err = error instanceof Error ? error : new Error(String(error)); throw err; } }); } exports.fetchYml = fetchYml; function getNodeTypeLabel(node) { if (yaml_1.default.isScalar(node)) return 'Scalar'; if (yaml_1.default.isPair(node)) return 'Scalar'; if (yaml_1.default.isMap(node)) return 'YAMLMap'; if (yaml_1.default.isSeq(node)) return 'YAMLSeq'; if (yaml_1.default.isNode(node)) return 'Node'; return 'unknown'; } exports.getNodeTypeLabel = getNodeTypeLabel; function isNode(value) { return (value !== null && typeof value === 'object' && (yaml_1.default.isNode(value) || yaml_1.default.isPair(value) || yaml_1.default.isDocument(value))); } exports.isNode = isNode; function merge(node, value) { if (node instanceof Map) { if (yaml_1.default.isDocument(value) && yaml_1.default.isMap(value.contents)) { value = value.contents; } if (yaml_1.default.isMap(value)) { value.items.forEach((pair) => node.set(pair.key, pair.value)); } } else if (yaml_1.default.isMap(node)) { if (yaml_1.default.isDocument(value) && yaml_1.default.isMap(value.contents)) { value = value.contents; } if (yaml_1.default.isMap(value)) { value.items.forEach((pair) => node.set(pair.key, pair.value)); } } else if (yaml_1.default.isSeq(node)) { if (yaml_1.default.isSeq(value)) { value.items.forEach((item) => node.add(item)); } } else if (yaml_1.default.isPair(node)) { if (yaml_1.default.isPair(value)) { node.value = value.value; } } else if (yaml_1.default.isScalar(node)) { if (yaml_1.default.isScalar(value)) { node.value = value.value; } else if (yaml_1.default.isPair(value)) { node.value = value.value; } } else if (noodl_core_1.is.obj(node)) { if (yaml_1.default.isDocument(value) && yaml_1.default.isMap(value.contents)) { value = value.contents; } if (yaml_1.default.isMap(value)) { value.items.forEach((pair) => (node[String(pair.key)] = pair.value)); } } return node; } exports.merge = merge; function parse(dataType, yml = '', opts) { const options = Object.assign({ logLevel: 'debug', prettyErrors: true, strict: false, toStringDefaults: { indent: 2, singleQuote: true, } }, opts); return dataType === 'map' ? yaml_1.default.parseDocument(yml, options) : yaml_1.default.parse(yml, options); } exports.parse = parse; function parseAs(arg1, arg2) { let as = 'yml'; let value; if (arg2 != undefined) { as = arg1; value = arg2; } else { value = arg1; } if (Buffer.isBuffer(value)) { value = value.toString('utf8'); } switch (as) { case 'doc': return toDocument(value); case 'json': return yaml_1.default.parse(noodl_core_1.is.str(value) ? value : stringify(value)); default: return (noodl_core_1.is.str(value) ? value : stringify(value)); } } exports.parseAs = parseAs; const toDoc = (value) => parseAs('doc', value); exports.toDoc = toDoc; const toJson = (value) => parseAs('json', value); exports.toJson = toJson; const toYml = (value) => parseAs('yml', value); exports.toYml = toYml; function getScalars(node, fn = (_, node) => yaml_1.default.isScalar(node)) { const scalars = []; if (yaml_1.default.isNode(node) || yaml_1.default.isDocument(node)) { yaml_1.default.visit(node, function onVisitNode(key, n, path) { const scalar = fn(key, n, path); if (scalar) scalars.push(n); }); } else if (yaml_1.default.isPair(node)) { return scalars.concat(getScalars(node.value, fn)); } return scalars; } exports.getScalars = getScalars; /** * Returns the stringified output of the yaml document or object. * If there are errors when parsing yaml documents, it returns a stringified yaml output of the errors instead * @param { y.Document } doc */ function stringify(value, opts) { let result = ''; let options = {}; options.collectionStyle = 'block'; options.indent = 2; options.logLevel = 'debug'; options.prettyErrors = true; if (value) { if (yaml_1.default.isDocument(value)) { if (value.errors.length) { result = yaml_1.default.stringify(value.errors, options); } else { result = value.toString(options); } } else { result = yaml_1.default.stringify(value, options); } } return result; } exports.stringify = stringify; /** * Will convert value to a yaml document * @param value The value to convert. Supports yaml string or an object literal * @returns A yaml document */ function toDocument(value, opts) { if (value != undefined) { return yaml_1.default.parseDocument(typeof value === 'string' ? value : yaml_1.default.stringify(value), opts); } return new yaml_1.default.Document(value, opts); } exports.toDocument = toDocument; function toNode(value) { if (isNode(value) || (0, yaml_1.isPair)(value)) return value; switch (typeof value) { case 'boolean': case 'number': case 'string': case 'undefined': return new yaml_1.default.Scalar(value); default: { if (value === null) { return new yaml_1.default.Scalar(null); } if (noodl_core_1.is.arr(value)) { const seq = new yaml_1.default.YAMLSeq(); value.forEach((v) => seq.items.push(toNode(v))); return seq; } else if (noodl_core_1.is.obj(value)) { const map = new yaml_1.default.YAMLMap(); noodl_core_1.fp.entries(value).forEach(([k, v]) => map.set(k, toNode(v))); return map; } return value; } } } exports.toNode = toNode; function withYmlExt(s = '') { return !s.endsWith('.yml') && (s += '.yml'); } exports.withYmlExt = withYmlExt; function unwrap(node) { if (node !== null && typeof node === 'object') { if (yaml_1.default.isScalar(node)) return node.value; if (yaml_1.default.isDocument(node)) return node.contents; } return node; } exports.unwrap = unwrap; //# sourceMappingURL=yml.js.map