ts-morph#Writers TypeScript Examples
The following examples show how to use
ts-morph#Writers.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConfigGenerator.ts From gengen with MIT License | 6 votes |
private getProperties(controllers: Record<string, Record<string, string>>): OptionalKind<PropertyDeclarationStructure>[] {
return Object.entries(controllers).map(([controller, actions]) => {
const propertyValue = Object.entries(actions).reduce<Record<string, string>>((store, [name, path]) => {
store[name] = `'${path}'`;
return store;
}, {});
return {
name: `${controller}Service`,
scope: Scope.Public,
isStatic: true,
initializer: Writers.object(propertyValue)
};
});
}
Example #2
Source File: AngularServicesGenerator.ts From gengen with MIT License | 6 votes |
protected getServices(services: IServiceModel[]): ClassDeclarationStructure[] {
return services.map(
(service): ClassDeclarationStructure => ({
kind: StructureKind.Class,
isExported: true,
name: `${service.name}Service`,
extends: service.methods.some((x) => x.kind === MethodKind.Download) ? DOWNLOAD_SERVICE : BASE_SERVICE,
decorators: [
{
kind: StructureKind.Decorator,
name: 'Injectable',
arguments: Writers.object({ providedIn: "'root'" })
}
],
ctors: [this.getConstructorStatement(service)],
methods: this.methodGenerator.getMethodsCodeStructures(service.methods)
})
);
}
Example #3
Source File: instructions.ts From adonis5-jwt with MIT License | 4 votes |
/**
* Makes the auth config file
*/
async function editConfig(
projectRoot: string,
app: ApplicationContract,
sink: typeof sinkStatic,
state: InstructionsState
) {
const configDirectory = app.directoriesMap.get("config") || "config";
const configPath = join(configDirectory, "auth.ts");
let tokenProvider;
if (state.tokensProvider === "redis") {
tokenProvider = Writers.object({
type: "'jwt'",
driver: "'redis'",
redisConnection: "'local'",
foreignKey: "'user_id'",
});
} else {
tokenProvider = Writers.object({
type: "'api'",
driver: "'database'",
table: "'jwt_tokens'",
foreignKey: "'user_id'",
});
}
let provider;
if (state.provider === "database") {
provider = Writers.object({
driver: "'database'",
identifierKey: "'id'",
uids: "['email']",
usersTable: `'${state.usersTableName}'`,
});
} else if (state.provider === "lucid") {
provider = Writers.object({
driver: '"lucid"',
identifierKey: '"id"',
uids: "[]",
model: `() => import('${state.usersModelNamespace}')`,
});
} else {
throw new Error(`Invalid state.provider: ${state.provider}`);
}
//Instantiate ts-morph
const project = await getTsMorphProject(projectRoot);
const authConfigFile = project.getSourceFileOrThrow(configPath);
//Remove Env import, if already present
authConfigFile.getImportDeclaration("@ioc:Adonis/Core/Env")?.remove();
//Add Env import
authConfigFile.addImportDeclaration({
defaultImport: "Env",
moduleSpecifier: "@ioc:Adonis/Core/Env",
});
const variable = authConfigFile
?.getVariableDeclarationOrThrow("authConfig")
.getInitializerIfKindOrThrow(SyntaxKind.ObjectLiteralExpression);
let guardsProperty = variable?.getPropertyOrThrow("guards") as PropertyAssignment;
let guardsObject = guardsProperty.getInitializerIfKindOrThrow(SyntaxKind.ObjectLiteralExpression);
//Remove JWT config, if already present
guardsObject.getProperty("jwt")?.remove();
//Add JWT config
guardsObject?.addPropertyAssignment({
name: "jwt",
initializer: Writers.object({
driver: '"jwt"',
publicKey: `Env.get('JWT_PUBLIC_KEY', '').replace(/\\\\n/g, '\\n')`,
privateKey: `Env.get('JWT_PRIVATE_KEY', '').replace(/\\\\n/g, '\\n')`,
persistJwt: `${state.persistJwt ? "true" : "false"}`,
jwtDefaultExpire: `'${state.jwtDefaultExpire}'`,
refreshTokenDefaultExpire: `'${state.refreshTokenDefaultExpire}'`,
tokenProvider: tokenProvider,
provider: provider,
}),
});
authConfigFile.formatText();
await authConfigFile?.save();
sink.logger.action("update").succeeded(configPath);
}