UNPKG

yaml-js-include

Version:

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

99 lines 3.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.YamlInclude = void 0; const tslib_1 = require("tslib"); const fs_1 = require("fs"); const promises_1 = require("fs/promises"); const yaml = require("js-yaml"); const p = require("path"); const dir_1 = require("./dir"); const file_1 = require("./file"); const seq_1 = require("./seq"); /** A wrapper around YAML loader to enable including of files or directories */ class YamlInclude { /** * Creates a new instance * @param _directoryOptions Default options for directory include * @param _seqOptions Default options for directory as array include * @param _encoding Encoding of files. */ constructor(_directoryOptions, _seqOptions, _encoding = 'utf-8') { this._directoryOptions = _directoryOptions; this._seqOptions = _seqOptions; this._encoding = _encoding; this._baseFile = ''; } /** * Reads a YAML file and parses it's content using include schema * @param filePath Path to the file to read * @param baseSchema Determines the base schema for YAML parse. @default yaml.DEFAULT_SCHEMA * @typeParam T - Type of the expected result object * @returns Parsed file content */ load(filePath, baseSchema) { const src = (0, fs_1.readFileSync)(filePath, this._encoding); return this.parse(src, filePath, baseSchema); } /** * Reads a YAML file asynchronously and parses it's content using include schema * @param filePath Path to the file to read * @param baseSchema Determines the base schema for YAML parse. @default yaml.DEFAULT_SCHEMA * @typeParam T - Type of the expected result object * @returns Parsed file content */ loadAsync(filePath, baseSchema) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const src = yield (0, promises_1.readFile)(filePath, this._encoding); return this.parse(src, filePath, baseSchema); }); } /** * Parses a YAML content using include schema * @param src YAML as string * @param basePath Base path for the include schema * @param baseSchema Determines the base schema for YAML parse. @default yaml.DEFAULT_SCHEMA * @typeParam T - Type of the expected result object * @returns Parsed file content */ parse(src, basePath, baseSchema) { baseSchema = baseSchema || yaml.DEFAULT_SCHEMA; this.basePath = basePath; return yaml.load(src, { schema: baseSchema.extend(this.types), filename: basePath, }); } /** Sets a base file path for resolving files or directories */ set basePath(filePath) { this._baseFile = p.resolve(filePath); } /** Gets a base file path for resolving files or directories */ get basePath() { const dir = (0, fs_1.statSync)(this._baseFile).isDirectory() ? this._baseFile : p.dirname(this._baseFile); return dir; } /** Gets default encoding for reading the files */ get encoding() { return this._encoding; } /** Gets default directory include options */ get directoryOptions() { return this._directoryOptions; } /** Gets default directory as array include options */ get seqOptions() { return this._seqOptions; } /** Gets include types for the YAML schema */ get types() { return [(0, dir_1.getDirectoryIncludeType)(this), (0, seq_1.getSeqIncludeType)(this), (0, file_1.getFileIncludeType)(this)]; } /** Gets a schema for YAML */ get schema() { return new yaml.Schema(this.types); } } exports.YamlInclude = YamlInclude; //# sourceMappingURL=include.js.map