UNPKG

@manuth/generator-wsc-package

Version:

A Generator for WoltLab Suite Core Packages.

227 lines 7.59 kB
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 { posix } from "node:path"; import { PathPrompt } from "@manuth/generator-ts-project"; import path from "upath"; import { FileUploadComponentBase } from "../../../Components/FileUploadComponentBase.js"; import { WoltLabSettingKey } from "../../../Settings/WoltLabSettingKey.js"; import { PHPInstructionFileMapping } from "../FileMappings/PHPInstructionFileMapping.js"; import { SelfContainedPHPFileMapping } from "../FileMappings/SelfContainedPHPFileMapping.js"; import { PackageComponentType } from "../Settings/PackageComponentType.js"; const { join } = path; /** * Provides a component for uploading or executing php-scripts. * * @template TSettings * The type of the settings of the generator. * * @template TOptions * The type of the options of the generator. * * @template TComponentOptions * The type of the options of the component. */ export class PHPScriptComponent extends FileUploadComponentBase { /** * Initializes a new instance of the {@link PHPScriptComponent `PHPScriptComponent<TSettings, TOptions, TComponentOptions>`} class. * * @param generator * The generator of the component. */ constructor(generator) { super(generator); } /** * @inheritdoc */ get ID() { return PackageComponentType.PHPScript; } /** * @inheritdoc */ get DisplayName() { return "PHP-Scripts to Execute During the Installation"; } /** * @inheritdoc */ get DefaultSourceBaseName() { return "phpScripts"; } /** * Gets a question for asking whether the script-file is self-contained. */ get ScriptTypeQuestion() { return { type: "list", name: "SelfContained", message: "What kind of PHP-script do you want to execute?", default: false, choices: [ { value: true, name: "I want to create a new script" }, { value: false, name: "A script which already exists on the server" } ] }; } /** * @inheritdoc */ get AppQuestion() { return Object.assign(Object.assign({}, super.AppQuestion), { message: (options) => { return options.SelfContained ? "What application do you want to upload the php-file to?" : "What application's directory do you want to load the php-script from?"; } }); } /** * Gets a question for asking for the existing script's location. */ get LocationQuestion() { return { type: PathPrompt.TypeName, name: "FileName", message: (options) => { if (options.SelfContained) { return "Where do you want to upload the php-file to?"; } else { return `Where is the script located relative to the \`${options.Application}\` application-directory?`; } }, path: posix, default: (options, answers) => { if (options.SelfContained) { return join("lib", `install_${answers[WoltLabSettingKey.Identifier]}_0.0.0.php`); } else { return undefined; } } }; } /** * @inheritdoc */ get SourceQuestion() { return Object.assign(Object.assign({}, super.SourceQuestion), { message: "Where do you want to store the php-file?", when: (options) => __awaiter(this, void 0, void 0, function* () { return this.GetSelfContainedPredicate(options, true); }) }); } /** * @inheritdoc */ get ComponentOptionQuestionCollection() { let result = [ this.ScriptTypeQuestion, this.PathQuestion, this.AppQuestion, this.SourceQuestion, this.LocationQuestion ]; for (let question of super.ComponentOptionQuestionCollection) { if (!result.includes(question)) { result.push(question); } } return result; } /** * @inheritdoc */ get InstructionFileMapping() { return this.ComponentOptions.SelfContained ? new SelfContainedPHPFileMapping(this) : new PHPInstructionFileMapping(this); } /** * @inheritdoc */ get FileMappings() { return [ ...super.FileMappings, ...(this.ComponentOptions.SelfContained ? [ { Destination: () => this.ComponentOptions.Source, Processor: (target, generator) => __awaiter(this, void 0, void 0, function* () { generator.fs.write(target.Destination, ""); }) } ] : []) ]; } /** * @inheritdoc * * @param options * The options which have been provided by the user. * * @returns * The name of the instruction-class. */ GetClassName(options) { var _a; if ((_a = options === null || options === void 0 ? void 0 : options.SelfContained) !== null && _a !== void 0 ? _a : false) { return "SelfContainedPHPInstruction"; } else { return "PHPInstruction"; } } /** * @inheritdoc * * @param options * The options of the component. * * @returns * The default full name of the path to suggest in the {@link PHPScriptComponent.SourceQuestion `SourceQuestion`}. */ GetDefaultSource(options) { return join(super.GetDefaultSource(options), "install.php"); } /** * Gets a predicate for checking whether the php-script is self-contained. * * @param options * The options provided by the user. * * @param expected * A value indicating whether the php-script must be self-contained for passing the predicate. * * @param base * The base of the predicate. * * @returns * A predicate for checking whether the php-script is self-contained. */ GetSelfContainedPredicate(options, expected, base) { return __awaiter(this, void 0, void 0, function* () { let result; if (typeof base === "function") { result = yield base(options); } else { result = yield base; } return (result !== null && result !== void 0 ? result : true) && (options.SelfContained === expected); }); } } //# sourceMappingURL=PHPScriptComponent.js.map