templates-mo
Version:
Templates is a scaffolding framework that makes code generation simple, dynamic, and reusable. Generate files, parts of your app, or whole project structures—without the repetitive copy-pasting
163 lines (162 loc) • 4.84 kB
TypeScript
import { DirNode } from "../fileSystemTree";
import Prompter from "../prompter";
import { TemplateOptions } from "../types/templates";
import { AnswersHash, SettingsFile } from "../types/settings";
import { Build } from './build';
import File from './File';
interface BuildErrors {
error: Error;
build: Build;
didBuildPathExist: boolean;
}
export declare const DEFAULT_OPTIONS: TemplateOptions;
type RenderData = Record<string, any>;
/**
* @class
* @classdesc Create a new instance of a template
*/
export declare class Templates<TAnswers extends AnswersHash = AnswersHash> {
/**
* name of template
*/
template: string;
/**
* Templates options
*/
opts: TemplateOptions;
packages: Record<string, DirNode>;
packagesUsed: string[];
private _defs;
successfulBuilds: SuccessfulBuild;
buildErrors: BuildErrors[];
templateSettings: SettingsFile;
engine: any;
/**
* Path to the templates settings file
*/
templateSettingsPath: string;
/**
* Path to the templates directory
*/
src: string;
_prompts?: Prompter<TAnswers>;
compiledFiles: File[];
/**
* All tpsrc config file names.
*
* @example
*
* [
* '.tps/tps.config.cjs',
* '.tps/tps.config.ts',
* '.tps/tps.config.js',
* '.tps/.tpsrc.cjs',
* '.tps/.tpsrc.ts',
* '.tps/.tpsrc.js',
* '.tps/.tpsrc.yml',
* '.tps/.tpsrc.yaml',
* '.tps/.tpsrc.json',
* '.tps/.tpsrc',
* 'tps.config.cjs',
* 'tps.config.ts',
* 'tps.config.js',
* '.config/tpsrc.cjs',
* '.config/tpsrc.ts',
* '.config/tpsrc.js',
* '.config/tpsrc.yml',
* '.config/tpsrc.yaml',
* '.config/tpsrc.json',
* '.config/tpsrc',
* '.tpsrc.cjs',
* '.tpsrc.ts',
* '.tpsrc.js',
* '.tpsrc.yml',
* '.tpsrc.yaml',
* '.tpsrc.json',
* '.tpsrc',
* 'package.json'
* ]
*/
static readonly tpsrcConfigNames: string[];
/**
* Get all locations a template can be
*
* Templates can be in be:
* - any `.tps/` directory from the callers cwd and any directory above it
* - Any `node_module` directory from the callers cwd and any directory above it
*/
static getTemplateLocations(cwd?: string): string[];
/**
* Get the path to a template or null if template doesnt exist
*/
static findTemplate(templateName: string, cwd?: string): string | null;
/**
* Gets path to the global .tps/ directory
*/
static getGloablTpsPath(): string | null;
static getLocalTpsPath(): string | null;
static directoryIsTpsInitialized(dir: any): boolean;
static hasGloablTps(): boolean;
static hasLocalTps(): boolean;
constructor(templateName: string, opts?: Partial<TemplateOptions>);
hasGloablTps(): boolean;
hasLocalTps(): boolean;
/**
* Include packages to use in the render process
*/
loadPackages(newPackages: string | string[]): void;
/**
* @param {String} newPackage - package from the template you would like to use
*/
loadPackage(newPackageName: string): void;
/**
* Get directory tree representation of package
*/
pkg(packageName: string): DirNode;
/**
* Set answers for prompts
*/
hasPrompts(): boolean;
/**
* Get answers
*/
getAnswers(): TAnswers;
/**
* Set answers for prompts
* @param answers - object of prompts answers. Key should be the name of the prompt and value should be the answer to it
*/
setAnswers(answers: Partial<TAnswers>): void;
/**
* @param dest - destination to render your new template to
* @param buildPaths - Instances you would like to create
* @param data - data to pass to doT. This will be used when rendering dot files/syntax
* @returns {Promise}
*/
render<T extends string | string[]>(dest: string, buildPaths?: T, data?: RenderData): Promise<T extends string[] ? string[] : string>;
private _renderBuildPath;
_scheduleCleanUpForBuild(build: Build, err: Error, didBuildPathExist: boolean): void;
/**
* Compile all files that need to be made for render process
* @private
* @param {String} packageName - name of package
*/
_compileFilesFromPackage(packageName: string): void;
_answerRestOfPrompts(): Promise<void>;
/**
* Configurations
*/
_loadTpsrc(templateName: string): void;
private _loadTpsSpecificConfig;
private _emitEvent;
}
declare class SuccessfulBuild {
/**
* Paths of files that were successfully built
*/
files: string[];
/**
* Paths of directories that were successfully built
*/
dirs: string[];
}
export { TemplateOptions } from "../types/templates";