UNPKG

@throw-out-error/minecraft-mcfunction

Version:

A simple way to create your mcfunction files using Typescript syntax.

104 lines 4.2 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); } var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.McFunction = void 0; const utility_1 = require("./utility"); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const commands_1 = require("./commands"); const stream_1 = require("stream"); const util_1 = require("util"); const transpiler_1 = require("./transpiler"); const pipeline = util_1.promisify(stream_1.pipeline); function isIterable(iter) { return typeof iter[Symbol.iterator] === 'function'; } class McFunction { constructor(nameOrSource, optsOrCmds = {}) { var _a, _b; this.dependencies = new Set(); if (Array.isArray(optsOrCmds) || isIterable(optsOrCmds)) { optsOrCmds = { commands: optsOrCmds }; } if (typeof nameOrSource === 'function') { this.name = nameOrSource.name; this.commands = new Set(); const transpiler = new transpiler_1.Transpiler(); const { rootFunction: self, functions } = transpiler.transpile(nameOrSource, optsOrCmds.name); self.dependencies = new Set(functions.values()); return self; } this.name = (_a = optsOrCmds.name) !== null && _a !== void 0 ? _a : nameOrSource; this.commands = new Set((_b = optsOrCmds.commands) !== null && _b !== void 0 ? _b : []); } compile(path) { if (!path) { return this.generate(); } const functionPath = path_1.default.join(path, `${this.name}.mcfunction`); utility_1.mkdirIfNotExist(path_1.default.dirname(functionPath)); const writeStream = fs_1.default.createWriteStream(functionPath); const compiling = []; // pipeline accepts a generator, but typescript doesn't know that compiling.push(pipeline(stream_1.Readable.from(this.generate()), writeStream)); for (let sub of this.dependencies) { if (sub === this) continue; compiling.push(sub.compile(path)); } return Promise.all(compiling); } async *generate() { const deleteSubCommands = (cmd) => { const cmds = cmd[commands_1.Command.ARGUMENTS].filter((a) => a instanceof commands_1.Command); for (let c of cmds) { deleteSubCommands(c); this.commands.delete(c); } }; this.commands.forEach(deleteSubCommands); for (let cmd of this.commands) { for await (let s of cmd.compile()) { yield s; } yield '\n'; } } /** * Add a command to the function * @param {Command} command the command to be added * @deprecated pass the commands as an array to the constructor instead */ addCommand(command) { } /** * Copies the function * @param {McFunction} funct the function to be copied * @returns {McFunction} a reference to the function * @deprecated This implementation only creates a shallow copy and copies aren't necessary anyways */ static copy(funct) { return funct; } /** * @deprecated Use the constructor instead */ static from(source, opts = {}) { return new McFunction(source, opts); } } exports.McFunction = McFunction; __exportStar(require("./arguments"), exports); __exportStar(require("./commands"), exports); //# sourceMappingURL=index.js.map