noodl-loader
Version:
Loader for noodl applications
52 lines • 1.72 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const fs_1 = require("fs");
const path_1 = require("path");
const noodl_core_1 = require("noodl-core");
const yml_1 = require("./yml");
/**
* Loads a file as a yaml string, object literal, or yaml document
* @param arg1
* @param arg2
* @param arg3
*/
function loadFile(arg1, arg2, arg3) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
let _filepath = '';
let _fsys;
let _as = 'yml';
if (noodl_core_1.is.str(arg1)) {
if (!(0, path_1.isAbsolute)(arg1)) {
arg1 = (0, path_1.resolve)((0, path_1.join)(process.cwd(), arg1));
}
if ((0, fs_1.existsSync)(arg1)) {
_filepath = arg1;
if (arg2 === 'doc') {
_as = 'doc';
}
else if (arg2 === 'json') {
_as = 'json';
}
}
else {
throw new Error(`The file at "${arg1}" does not exist`);
}
}
else if (noodl_core_1.is.obj(arg1)) {
_fsys = arg1;
_filepath = arg2 || '';
_as = (arg3 || 'yml');
}
let yml = '';
try {
yml = (yield (_fsys === null || _fsys === void 0 ? void 0 : _fsys.readFile(_filepath, 'utf8'))) || '';
}
catch (error) {
throw error instanceof Error ? error : new Error(String(error));
}
return (0, yml_1.parseAs)(_as, yml);
});
}
exports.default = loadFile;
//# sourceMappingURL=load-file.js.map