@manuth/generator-wsc-package
Version:
A Generator for WoltLab Suite Core Packages.
128 lines • 5.89 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 { WoltLabTypeScriptFileMapping } from "../../../FileMappings/WoltLabTypeScriptFileMapping.js";
const { relative, sep } = path;
/**
* Provides the functionality to create an entry-point.
*
* @template TSettings
* The type of the settings of the generator.
*
* @template TOptions
* The type of the options of the generator.
*/
export class EntryPointFileMapping extends WoltLabTypeScriptFileMapping {
/**
* Initializes a new instance of the {@link EntryPointFileMapping `EntryPointFileMapping`} class.
*
* @param generator
* The component to create an instruction-file for.
*/
constructor(generator) {
super(generator);
this.packageGenerator = generator;
}
/**
* @inheritdoc
*/
get Generator() {
return this.packageGenerator;
}
/**
* @inheritdoc
*/
get Destination() {
return this.Generator.sourcePath("index.ts");
}
/**
* @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* () {
let compilerVariableName = "compiler";
file = yield _super.Transform.call(this, file);
this.ApplyDirname(file);
file.addImportDeclarations([
{
moduleSpecifier: "path",
namedImports: [
{
name: "join"
}
]
},
{
moduleSpecifier: "@manuth/woltlab-compiler",
namedImports: [
{
name: "PackageCompiler"
}
]
},
Object.assign(Object.assign({}, yield this.GetImportDeclaration(this.Generator.WoltLabPackageFileMapping.Destination)), { namedImports: [
this.Generator.PackageVariableName
] })
]);
file.addVariableStatement({
declarationKind: VariableDeclarationKind.Let,
declarations: [
{
name: compilerVariableName,
initializer: printNode(ts.factory.createNewExpression(ts.factory.createIdentifier("PackageCompiler"), [], [
ts.factory.createIdentifier(this.Generator.PackageVariableName)
]))
}
]
});
let pathComponents = [
this.GetDirname()
];
for (let pathComponent of relative(file.getDirectoryPath(), this.Generator.destinationPath()).split(sep)) {
pathComponents.push(ts.factory.createStringLiteral(pathComponent));
}
file.addStatements(`${EOL}(${EOL}` +
printNode(ts.factory.createArrowFunction([
ts.factory.createModifier(SyntaxKind.AsyncKeyword)
], [], [], undefined, undefined, ts.factory.createBlock([
ts.factory.createExpressionStatement(ts.factory.createAssignment(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier(compilerVariableName), ts.factory.createIdentifier("DestinationPath")), ts.factory.createCallExpression(ts.factory.createIdentifier("join"), [], [
...pathComponents,
ts.factory.createTemplateExpression(ts.factory.createTemplateHead(""), [
ts.factory.createTemplateSpan(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier(this.Generator.PackageVariableName), ts.factory.createIdentifier("Identifier")), ts.factory.createTemplateMiddle("-")),
ts.factory.createTemplateSpan(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier(this.Generator.PackageVariableName), ts.factory.createIdentifier("Version")), ts.factory.createTemplateTail(".tar"))
])
]))),
ts.factory.createExpressionStatement(ts.factory.createAwaitExpression(ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier(compilerVariableName), ts.factory.createIdentifier("Execute")), [], [])))
], true))) +
")();");
file.formatText({
convertTabsToSpaces: true,
indentSize: 4,
placeOpenBraceOnNewLineForControlBlocks: true,
placeOpenBraceOnNewLineForFunctions: true,
insertSpaceAfterFunctionKeywordForAnonymousFunctions: false
});
return file;
});
}
}
//# sourceMappingURL=EntryPointFileMapping.js.map