pg-generator / PgGenerator

# Class: PgGenerator<O>

Base abstract class for for pg-generator classes.

# Type parameters

Name Type Default
O GeneratorOptions GeneratorOptions

# Constructors

# constructor

+ new PgGenerator<O>(options: O, internalOptions: InternalOptions): PgGenerator<O>

Creates an instance of PgGenerator.

# Type parameters:

Name Type Default
O GeneratorOptions GeneratorOptions

# Parameters:

Name Type Description
options O are parameters for several options.
internalOptions InternalOptions are added by pg-generator functions and not for user.

Returns: PgGenerator<O>

Defined in: pg-generator.ts:23

# Properties

# options

Protected options: O & { context: Record<string, any> ; envPrefix: string }

Defined in: pg-generator.ts:23

# Methods

# clear

Protectedclear(): Promise<any>

If clear option is true, this method is called. It deletes destination path. User may override this method to tailor clear operation according to generator's purposes.

Returns: Promise<any>

Defined in: pg-generator.ts:85


# composeWith

ProtectedcomposeWith(generator: string, options?: GeneratorOptions): Promise<void>

Executes the default sub-generator app from a generator. If options are provided, they are combined with current options.

# Example

this.composeWith("generator-from-npm");
this.composeWith("generator-from-npm", options);
this.composeWith(require.resolve("./local-generator"), options);

throws error if generator can not be found.

# Parameters:

Name Type Description
generator string is the name or path of the generator. If it is a local path, use require.resolve or provide an absolute path.
options? GeneratorOptions are the new options added on top of curent options.

Returns: Promise<void>

Defined in: pg-generator.ts:232

ProtectedcomposeWith(generator: string, subGeneratorName?: string, options?: GeneratorOptions): Promise<void>

Executes a sub-generator from a generator. If options are provided, they are merged with current options. Also some of the options are changed to sensible default values for sub-generators (e.g. clear is changed to false), but they can be overridden by newly provided options.

# Example

this.composeWith("generator-from-npm", "sub-generator");
this.composeWith(require.resolve("./local-generator"), "sub-generator", options);

throws error if generator or sub-generator can not be found.

# Parameters:

Name Type Description
generator string is the name or path of the generator. If it is a local path, use require.resolve or provide an absolute path.
subGeneratorName? string is the name of the sub-generator to be executed.
options? GeneratorOptions are the new options added on top of curent options.

Returns: Promise<void>

Defined in: pg-generator.ts:248


# context

Protectedcontext(): Record<string, any> | Promise<Record<string, any>>

Provides generator specific extra context to templates. This data is deeply merged with context provided by user.

# Example

export default class App extends PgGenerator {
  protected context(): Record<string, any> {
    return { global: { addSchemaName: true } };
  }
}

Returns: Record<string, any> | Promise<Record<string, any>>

generator spesific extra context data.

Defined in: pg-generator.ts:109


# destinationPath

ProtecteddestinationPath(path?: string): string

Returns abosulte path for the given path relative to output directory.

# Parameters:

Name Type Default value Description
path string "" is the path relative to output directory.

Returns: string

the absolute path.

Defined in: pg-generator.ts:197


# format

Protectedformat(destination: string, content?: string): undefined | string | Promise<undefined | string>

Formats given content. By default prettier is used. Plase note that at this stage file is not written to disk yet.

# Parameters:

Name Type Description
destination string is the path of the destination file to be created.
content? string is the content to format.

Returns: undefined | string | Promise<undefined | string>

formatted code.

Defined in: pg-generator.ts:142


# generate

generate(): Promise<void>

Renders all templates in [rootDir]/templates directory with the render function using related context data, and writes generated contents to the files in output directory. If render function returns undefined for a template/database object pair no file is not written for that pair.

Template file names may contain variables and basic filter functions to change names of generated files.

Additionally copies all files in [rootDir]/files to the output directory.

Returns: Promise<void>

Defined in: pg-generator.ts:49


# init

Protectedinit(): any

Executed after clear operation.

Returns: any

Defined in: pg-generator.ts:92


# process

Protectedprocess(destination: string, content: string): any

Writes generated content from render to the destination file.

# Parameters:

Name Type Description
destination string is the destionation path of the file to be cerated..
content string is the content to process.

Returns: any

Defined in: pg-generator.ts:153


# render

Protectedrender(templatePath: string, context: Context): undefined | string | Promise<undefined | string>

Generates content from the template with provided path using the context.

# Example

import engine from "my-favorite-template-engine";

export default class Md extends PgGenerator {
  protected async render(templatePath: string, context: Context): Promise<string> {
    return engine.render(templatePath, context);
  }
}

# Parameters:

Name Type Description
templatePath string is the path of the template file to generate content.
context Context is the data to be used for generating content.

Returns: undefined | string | Promise<undefined | string>

generated content.

Defined in: pg-generator.ts:129


# templatePath

ProtectedtemplatePath(path?: string): string

Returns abosulte path for the given path relative to template root.

# Parameters:

Name Type Default value Description
path string "" is the path relative to template root.

Returns: string

the absolute path.

Defined in: pg-generator.ts:207