UNPKG

yaml-js-include

Version:

Extension for yaml-js library to be able to include files/directories in yaml files

98 lines 4.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getSeqIncludeType = void 0; const fs = require("fs"); const p = require("path"); const yaml = require("js-yaml"); const dir_1 = require("./dir"); const DefaultIncludeDirSeqOptions = { include: [], exclude: [], allowEmpty: false, recursive: true, extensions: ['.yaml', '.yml'], ignoreIndicator: '_', }; /** * Gets a schema for including directories as sequence (array) * @param yamlInclude YAML include object * @returns {yaml.Type} YAML type for a schema */ const getSeqIncludeType = (yamlInclude) => { return new yaml.Type('tag:yaml.org,2002:inc/seq', { kind: 'sequence', // Data should be an array of 1 or 2 items (path and optional options) resolve: (data) => Array.isArray(data) && data.length > 0 && data.length < 3, construct: (data) => { var _a; const basePath = yamlInclude.basePath; let files = []; if (!data[1]) { data[1] = {}; } const options = Object.assign(Object.assign(Object.assign({}, DefaultIncludeDirSeqOptions), ((_a = yamlInclude.directoryOptions) !== null && _a !== void 0 ? _a : {})), data[1]); const result = []; options.extensions.sort((a, b) => b.length - a.length); if (Array.isArray(data[0])) { files = data[0]; } else { const fullPath = p.join(basePath, data[0]); files = (0, dir_1.recursiveReaddirSync)(fullPath, options.recursive); files = files.map((filePath) => filePath.replace(basePath + p.sep, '')); } // sort the by length of filepath files.sort(function (a, b) { return a.length - b.length; }); const getKeepingFileName = (filePath) => { const extension = options.extensions.find((ext) => filePath.endsWith(ext)); if (!extension) { return undefined; } const filename = p.basename(filePath, extension); // check whitelist for filepath and file name if (options.include && (options.include.indexOf(filePath) !== -1 || options.include.indexOf(filename) !== -1 || options.include.indexOf(filename + extension) !== -1)) { return filename; } // if ANY part of the path has an ignorePrefix, // skip it if (filePath.indexOf(p.sep + options.ignoreIndicator) !== -1) { return undefined; } // check blacklist for filepath and file name if (options.exclude && (options.exclude.indexOf(filePath) !== -1 || options.exclude.indexOf(filename) !== -1 || options.exclude.indexOf(filename + extension) !== -1)) { return undefined; } return filename; }; files.forEach(function (filePath) { const filename = getKeepingFileName(filePath); if (!filename) { return; } // get the source at last let included = {}; const fullFilePath = p.join(basePath, filePath); const src = fs.readFileSync(fullFilePath, yamlInclude.encoding); if (src.length > 0) { included = yamlInclude.parse(src, fullFilePath); yamlInclude.basePath = basePath; } if (options.allowEmpty || Object.getOwnPropertyNames(included).length > 0) { result.push(included); } }); return result; }, instanceOf: Array, }); }; exports.getSeqIncludeType = getSeqIncludeType; //# sourceMappingURL=seq.js.map