@manuth/generator-wsc-package
Version:
A Generator for WoltLab Suite Core Packages.
149 lines • 5.92 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 { EOL } from "node:os";
import { join } from "node:path";
import { printNode, SyntaxKind, ts, VariableDeclarationKind } from "ts-morph";
import path from "upath";
import { WoltLabComponentSettingKey } from "../Settings/WoltLabComponentSettingKey.js";
import { WoltLabSettingKey } from "../Settings/WoltLabSettingKey.js";
import { WoltLabTypeScriptFileMapping } from "./WoltLabTypeScriptFileMapping.js";
const { dirname, relative, sep } = path;
/**
* Provides the functionality to generate instruction-files.
*
* @template TSettings
* The type of the generator-settings.
*
* @template TOptions
* The type of the generator-options.
*
* @template TComponentOptions
* The type of the component-options.
*/
export class InstructionFileMapping extends WoltLabTypeScriptFileMapping {
/**
* Initializes a new instance of the {@link InstructionFileMapping `InstructionFileMapping<TSettings, TOptions, TComponentOptions>`} class.
*
* @param component
* The component to create an instruction-file for.
*/
constructor(component) {
super(component.Generator);
this.component = component;
}
/**
* Gets the component to create an instruction-file for.
*/
get Component() {
return this.component;
}
/**
* Gets the options to pass to the instruction-constructor.
*/
get InstructionOptions() {
return this.WrapNode(ts.factory.createParenthesizedExpression(ts.factory.createObjectLiteralExpression())).getExpressionIfKindOrThrow(SyntaxKind.ObjectLiteralExpression);
}
/**
* @inheritdoc
*/
get Destination() {
return this.Generator.Settings[WoltLabSettingKey.ComponentOptions][this.Component.ID][WoltLabComponentSettingKey.Path];
}
/**
* @inheritdoc
*
* @param file
* The {@link SourceFile `SourceFile`} to transform.
*
* @returns
* The transformed file.
*/
Transform(file) {
const _super = Object.create(null, {
Transform: { get: () => super.Transform }
});
return __awaiter(this, void 0, void 0, function* () {
file = yield _super.Transform.call(this, file);
file.addImportDeclaration({
moduleSpecifier: "@manuth/woltlab-compiler",
namedImports: [
{
name: this.Component.ClassName
}
]
});
file.addVariableStatement({
isExported: true,
declarationKind: VariableDeclarationKind.Let,
docs: [
{
description: `${EOL}The instruction.`
}
],
declarations: [
{
name: this.Component.VariableName,
initializer: printNode(ts.factory.createNewExpression(ts.factory.createIdentifier(this.Component.ClassName), [], []))
}
]
});
let constructor = file.getVariableDeclaration(this.Component.VariableName).getInitializerIfKindOrThrow(SyntaxKind.NewExpression);
constructor.addArgument(`${EOL}${this.InstructionOptions.getFullText()}`);
file.formatText({
convertTabsToSpaces: true,
indentSize: 4,
placeOpenBraceOnNewLineForControlBlocks: true,
placeOpenBraceOnNewLineForFunctions: true,
insertSpaceAfterFunctionKeywordForAnonymousFunctions: false
});
return file;
});
}
/**
* Adds the prerequisites for using {@link join `path.join`} to the specified {@link sourceFile `sourceFile`}.
*
* @param sourceFile
* The file to add the prerequisites to.
*/
ApplyPathJoin(sourceFile) {
super.ApplyDirname(sourceFile);
sourceFile.addImportDeclaration({
moduleSpecifier: "path",
namedImports: [
{
name: "join"
}
]
});
}
/**
* Creates valid TypeScript-code for calling the {@link join `join`}-method.
*
* @param path
* The path to point to.
*
* @returns
* Valid TypeScript-code containing a {@link join `join`}-call for pointing to the specified {@link path `path`}.
*/
GetPathJoin(path) {
let call = this.WrapNode(ts.factory.createCallExpression(ts.factory.createIdentifier("join"), [], []));
call.addArgument(printNode(this.GetDirname()));
for (let pathComponent of relative(dirname(this.Generator.destinationPath(this.Destination)), this.Generator.destinationPath()).split(sep)) {
if (pathComponent.length > 0) {
call.addArgument(printNode(ts.factory.createStringLiteral(pathComponent)));
}
}
for (let pathComponent of relative(this.Generator.destinationPath(), this.Generator.destinationPath(path)).split(sep)) {
call.addArgument(printNode(ts.factory.createStringLiteral(pathComponent)));
}
return call;
}
}
//# sourceMappingURL=InstructionFileMapping.js.map