@nmmty/lazycanvas
Version:
A simple way to interact with @napi-rs/canvas in an advanced way!
84 lines (83 loc) • 3.44 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.YAMLReader = void 0;
const JSONReader_1 = require("./JSONReader");
const _yaml = __importStar(require("js-yaml"));
const fs = __importStar(require("fs"));
const LazyUtil_1 = require("../../../utils/LazyUtil");
const path = __importStar(require("path"));
class YAMLReader {
/**
* Reads a YAML string and converts it to a LazyCanvas object.
* @param {string} [data] - The YAML string to read.
* @param {Object} [opts] - Optional parameters for debugging.
* @returns A Promise that resolves to a LazyCanvas object.
*/
static read(data, opts) {
const yamlContent = _yaml.load(data);
if (opts?.debug) {
LazyUtil_1.LazyLog.log('info', 'YAML content loaded:', yamlContent);
}
if (typeof yamlContent === 'object' && yamlContent !== null) {
return JSONReader_1.JSONReader.read(yamlContent, opts);
}
else {
throw new LazyUtil_1.LazyError("Invalid YAML content: Expected an object.");
}
}
/**
* Reads a YAML file and converts it to a LazyCanvas object.
* @param {string} [filePath] - The path to the YAML file.
* @param {Object} [opts] - Optional parameters for debugging.
* @returns A Promise that resolves to a LazyCanvas object.
* @throws LazyError if the file does not exist or has an invalid extension.
*/
static readFile(filePath, opts) {
if (!fs.existsSync(filePath)) {
throw new LazyUtil_1.LazyError(`File not found: ${filePath}`);
}
const ext = path.extname(filePath).toLowerCase();
if (ext !== '.yaml' && ext !== '.yml') {
throw new LazyUtil_1.LazyError(`Invalid file extension: ${ext}. Expected .yaml or .yml.`);
}
const data = fs.readFileSync(filePath, 'utf8');
if (opts?.debug) {
LazyUtil_1.LazyLog.log('info', `Reading YAML file: ${filePath}`);
}
return this.read(data, opts);
}
}
exports.YAMLReader = YAMLReader;