@manuth/generator-wsc-package
Version:
A Generator for WoltLab Suite Core Packages.
66 lines • 2.36 kB
JavaScript
import { fileURLToPath } from "node:url";
import { TSProjectSettingKey, TSProjectTypeScriptFileMapping } from "@manuth/generator-ts-project";
import { SyntaxKind, ts } from "ts-morph";
/**
* Provides the functionality to generate typescript files for the {@link WoltLabGenerator `WoltLabGenerator<TSettings, TOptions>`}.
*
* @template TSettings
* The type of the generator-settings.
*
* @template TOptions
* The type of the generator-options.
*/
export class WoltLabTypeScriptFileMapping extends TSProjectTypeScriptFileMapping {
/**
* Initializes a new instance of the {@link WoltLabTypeScriptFileMapping `WoltLabTypeScriptFileMapping<TSettings, TOptions>`} class.
*
* @param generator
* The component to create an instruction-file for.
*/
constructor(generator) {
super(generator);
this.woltLabGenerator = generator;
}
/**
* @inheritdoc
*/
get Generator() {
return this.woltLabGenerator;
}
/**
* Applies the prerequisites required for using the directory name in this file.
*
* @param file
* The file to apply the prerequisites to.
*/
ApplyDirname(file) {
if (this.Generator.Settings[TSProjectSettingKey.ESModule]) {
file.addImportDeclaration({
moduleSpecifier: "url",
namedImports: [
"fileURLToPath"
]
});
}
}
/**
* Gets a typescript syntax node for determining the name of the directory of the file.
*
* @returns
* A typescript syntax node for determining the name of the directory of the file.
*/
GetDirname() {
if (!this.Generator.Settings[TSProjectSettingKey.ESModule]) {
return ts.factory.createIdentifier("__dirname");
}
else {
return ts.factory.createCallExpression(ts.factory.createIdentifier("fileURLToPath"), [], [
ts.factory.createNewExpression(ts.factory.createIdentifier("URL"), [], [
ts.factory.createStringLiteral("."),
ts.factory.createPropertyAccessExpression(ts.factory.createMetaProperty(SyntaxKind.ImportKeyword, ts.factory.createIdentifier("meta")), "url")
])
]);
}
}
}
//# sourceMappingURL=WoltLabTypeScriptFileMapping.js.map