boats
Version:
Beautiful Open / Async Template System - Write less yaml with BOATS and Nunjucks.
33 lines (32 loc) • 1.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = default_1;
const tslib_1 = require("tslib");
const upath_1 = tslib_1.__importDefault(require("upath"));
const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
const js_yaml_1 = tslib_1.__importDefault(require("js-yaml"));
const lodash_1 = tslib_1.__importDefault(require("lodash"));
function default_1(defPath, ...rest) {
const tplGlobals = this.env.globals;
// eslint-disable-next-line prefer-rest-params
const injectPath = upath_1.default.join(upath_1.default.dirname(tplGlobals.currentFilePointer), defPath);
if (!fs_extra_1.default.pathExistsSync(injectPath)) {
throw new Error('Path not found when trying to make optional: ' + injectPath);
}
const content = js_yaml_1.default.load(fs_extra_1.default.readFileSync(injectPath, 'utf-8'));
if (content && content['type'] !== 'object') {
throw new TypeError('Referenced item must be an object: ' + injectPath);
}
content.properties = lodash_1.default.pick(content.properties, lodash_1.default.flatten(rest));
if (Array.isArray(content.required)) {
content.required = content.required.filter((i) => lodash_1.default.flatten(rest).includes(i));
}
const text = js_yaml_1.default.dump(content);
const parts = text.split('\n');
const first = parts.pop();
for (let i = 1; i < parts.length; i++) {
parts[i] = tplGlobals.indentObject[tplGlobals.indentNumber].linePadding + parts[i];
}
++this.env.globals.indentNumber;
return first + parts.join('\n');
}