boats
Version:
Beautiful Open / Async Template System - Write less yaml with BOATS and Nunjucks.
29 lines (28 loc) • 1.38 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() {
const tplGlobals = this.env.globals;
// eslint-disable-next-line prefer-rest-params
const injectPath = upath_1.default.join(upath_1.default.dirname(tplGlobals.currentFilePointer), arguments[0]);
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);
}
const text = js_yaml_1.default.dump(lodash_1.default.omit(content, 'required'));
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');
}