Options
All
  • Public
  • Public/Protected
  • All
Menu

Class PgGenerator<O>

Base abstract class for for pg-generator classes.

Type parameters

Hierarchy

  • PgGenerator

Index

Constructors

constructor

  • new PgGenerator<O>(options: O, internalOptions: InternalOptions): PgGenerator<O>
  • Creates an instance of PgGenerator.

    Type parameters

    Parameters

    • options: O

      are parameters for several options.

    • internalOptions: InternalOptions

      are added by pg-generator functions and not for user.

    Returns PgGenerator<O>

Properties

Private #destinationRoot

#destinationRoot: string

Private #internalOptions

#internalOptions: InternalOptions

Protected options

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

Methods

Protected clear

  • clear(): 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>

Protected composeWith

  • composeWith(generator: string, options?: GeneratorOptions): Promise<void>
  • composeWith(generator: string, subGeneratorName?: 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

    • generator: string

      is the name or path of the generator. If it is a local path, use require.resolve or provide an absolute path.

    • Optional options: GeneratorOptions

      are the new options added on top of curent options.

    Returns 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

    • generator: string

      is the name or path of the generator. If it is a local path, use require.resolve or provide an absolute path.

    • Optional subGeneratorName: string

      is the name of the sub-generator to be executed.

    • Optional options: GeneratorOptions

      are the new options added on top of curent options.

    Returns Promise<void>

Protected context

  • context(): 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.

Private copyTemplate

  • copyTemplate(source: string, destination: string): Promise<void>
  • Copy the source file to the destination. Also creates the destination directory if it does not exist.

    Parameters

    • source: string

      is the source file to copy.

    • destination: string

      is the destination file to be created.

    Returns Promise<void>

Private deleteDestination

  • deleteDestination(path?: string): Promise<void>
  • Removes file or directory (and it's contents() from destination. If destination is equal to cwd() it throws error and aborts operation for a safety measure. Most probably cwd() contains other files too.

    Parameters

    • path: string = ""

      is the path relative to the destination root.

    Returns Promise<void>

Protected destinationPath

  • destinationPath(path?: string): string
  • Returns abosulte path for the given path relative to output directory.

    Parameters

    • path: string = ""

      is the path relative to output directory.

    Returns string

    the absolute path.

Private fetchContext

  • fetchContext(): Promise<Record<string, any>>
  • Fetches and merges context data from pg-generator instance, context file and options. The context file may be a JSON5 data file (may contain comments etc.) or a JavaScript file with a default export of object or a function. If it is a function, it is passed pg-structure db instance.

    Merge priorities are: options.context > context file > context() method.

    Returns Promise<Record<string, any>>

    merged context data.

Protected format

  • format(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

    • destination: string

      is the path of the destination file to be created.

    • Optional content: string

      is the content to format.

    Returns undefined | string | Promise<undefined | string>

    formatted code.

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>

Protected init

  • init(): any
  • Executed after clear operation.

    Returns any

Protected process

  • process(destination: string, content: string): any
  • Writes generated content from render to the destination file.

    Parameters

    • destination: string

      is the destionation path of the file to be cerated..

    • content: string

      is the content to process.

    Returns any

Protected render

  • render(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

    • 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.

Protected templatePath

  • templatePath(path?: string): string
  • Returns abosulte path for the given path relative to template root.

    Parameters

    • path: string = ""

      is the path relative to template root.

    Returns string

    the absolute path.

Private writeTemplate

  • writeTemplate(destination: string, content: string): Promise<void>
  • Writes given content to the file after creating it's directory.

    Parameters

    • destination: string
    • content: string

      is the content to write.

    Returns Promise<void>

Generated using TypeDoc