@manuth/woltlab-compiler
Version:
A compiler for generating WoltLab-Package `.tar` Archives and other WoltLab-Package Components
120 lines • 3.7 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import fs from "fs-extra";
import { create as createFS } from "mem-fs";
import { create as createEditor } from "mem-fs-editor";
import { create as createArchive } from "tar";
import path from "upath";
const { ensureDir, readdir } = fs;
const { dirname, join, resolve } = path;
/**
* Provides the functionality to compile a component.
*
* @template T
* The type of the item which can be compiled by this compiler.
*/
export class Compiler {
/**
* Initializes a new instance of the {@link Compiler `Compiler<T>`} class.
*
* @param item
* The item to compile.
*/
constructor(item) {
/**
* The path to save the compiled item to.
*/
this.destinationPath = "";
this.item = item;
}
/**
* Gets the item to compile.
*/
get Item() {
return this.item;
}
/**
* Gets or sets the path to save the compiled item to.
*/
get DestinationPath() {
return this.destinationPath;
}
/**
* @inheritdoc
*/
set DestinationPath(value) {
this.destinationPath = value;
}
/**
* Compiles the item.
*/
Execute() {
return __awaiter(this, void 0, void 0, function* () {
yield this.Compile();
});
}
/**
* Copies files using `ejs`.
*
* @param source
* The source to copy the files from.
*
* @param destination
* The destination to copy the files to.
*
* @param context
* The context to use.
*
* @param delimiter
* The delimiter of the ejs-tags.
*/
CopyTemplate(source, destination, context, delimiter) {
return __awaiter(this, void 0, void 0, function* () {
let fileStoreEditor = createEditor(createFS());
fileStoreEditor.copyTpl(source, destination, context, { delimiter }, { globOptions: { dot: true } });
yield new Promise((resolve) => {
fileStoreEditor.commit([], () => {
resolve();
});
});
});
}
/**
* Joins the paths and returns the path contained by the destination-folder.
*
* @param path
* The path that is to be joined.
*
* @returns
* The joined path relative to the destination-folder.
*/
MakeDestinationPath(...path) {
return join(this.DestinationPath, ...path);
}
/**
* Compresses a folder and saves the result to a specified file.
*
* @param source
* The folder that is to be compressed.
*
* @param destination
* The filename to save the compressed file to.
*/
Compress(source, destination) {
return __awaiter(this, void 0, void 0, function* () {
yield ensureDir(dirname(destination));
yield createArchive({
cwd: resolve(source),
file: resolve(destination)
}, yield readdir(source));
});
}
}
//# sourceMappingURL=Compiler.js.map