|
1 |
| -/** |
2 |
| - * Documentation goes here |
3 |
| - * @public |
4 |
| - */ |
5 |
| -export const HELLO = "Hello"; |
| 1 | +import { cloneNode, CloneNodeOptions } from "@wessberg/ts-clone-node"; |
| 2 | +import { |
| 3 | + ScriptTarget, |
| 4 | + ScriptKind, |
| 5 | + createSourceFile, |
| 6 | + Statement, |
| 7 | + Expression, |
| 8 | + VariableStatement, |
| 9 | + VariableDeclaration, |
| 10 | + SourceFile, |
| 11 | + NodeArray |
| 12 | +} from "typescript"; |
| 13 | + |
| 14 | +interface Replacements { |
| 15 | + [key: string]: any; |
| 16 | +} |
| 17 | + |
| 18 | +export class TemplateFactory { |
| 19 | + public constructor( |
| 20 | + protected source: string, |
| 21 | + protected target: ScriptTarget, |
| 22 | + protected kind: ScriptKind, |
| 23 | + protected cloneOpts?: CloneNodeOptions |
| 24 | + ) {} |
| 25 | + |
| 26 | + /** |
| 27 | + * Replaces all occurrences of all keys of the given replacement map with its corresponding values. |
| 28 | + * @param replacements - The map of strings to replace within the source |
| 29 | + * @param source - Optionally you can provide your own source instead of the internal source |
| 30 | + * @return the processed source string |
| 31 | + * @public |
| 32 | + */ |
| 33 | + public replace(replacements?: Replacements, source: string = this.source): string { |
| 34 | + if (!replacements) return source; |
| 35 | + return Object.keys(replacements).reduce((source, key) => source.replace(key, replacements[key]), source); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Creates a virtual source file from the internal source |
| 40 | + * @param fileName - The name of the file to create |
| 41 | + * @param replacements - The replacement map to apply to the source {@link replace} |
| 42 | + * @param source - Optionally you can provide your own source instead of the internal source |
| 43 | + * @return A typescript SourceFile instance |
| 44 | + * @public |
| 45 | + */ |
| 46 | + public file(fileName = "", replacements?: Replacements, source: string = this.source): SourceFile { |
| 47 | + return createSourceFile(fileName, this.replace(replacements, source), this.target, true, this.kind); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Builds a list of statements from the internal source. |
| 52 | + * @param replacements - The replacement map to apply to the source {@link replace} |
| 53 | + * @param source - Optionally you can provide your own source instead of the internal source |
| 54 | + * @return An array of typescript statements, cloned and ready for insertion / replacement |
| 55 | + * @public |
| 56 | + */ |
| 57 | + public statements<T extends Statement = Statement>(replacements?: Replacements, source: string = this.source): T[] { |
| 58 | + return this.unclonedStatements<T>(replacements, source).map(this.cloner); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Builds a statement from the internal source. |
| 63 | + * @param replacements - The replacement map to apply to the source {@link replace} |
| 64 | + * @param source - Optionally you can provide your own source instead of the internal source |
| 65 | + * @return A typescript statement, cloned and ready for insertion / replacement |
| 66 | + * @public |
| 67 | + */ |
| 68 | + public statement<T extends Statement = Statement>(replacements?: Replacements, source: string = this.source): T { |
| 69 | + return this.clone(this.unclonedStatement<T>(replacements, source)); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Builds a declaration from the internal source. |
| 74 | + * @param replacements - The replacement map to apply to the source {@link replace} |
| 75 | + * @param source - Optionally you can provide your own source instead of the internal source |
| 76 | + * @return A typescript variable declaration, cloned and ready for insertion / replacement |
| 77 | + * @public |
| 78 | + */ |
| 79 | + public declaration<T extends VariableDeclaration = VariableDeclaration>(replacements?: Replacements, source: string = this.source): T { |
| 80 | + return this.clone(this.unclonedDeclaration<T>(replacements, source)); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Builds an expression from the internal source. |
| 85 | + * @param replacements - The replacement map to apply to the source {@link replace} |
| 86 | + * @param source - Optionally you can provide your own source instead of the internal source |
| 87 | + * @return A typescript expression, cloned and ready for insertion / replacement |
| 88 | + * @public |
| 89 | + */ |
| 90 | + public expression<T extends Expression = Expression>(replacements?: Replacements, source: string = this.source): T { |
| 91 | + return this.clone(this.unclonedExpression<T>(replacements, source)); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Builds a list of statements from the internal source. |
| 96 | + * @param replacements - The replacement map to apply to the source {@link replace} |
| 97 | + * @param source - Optionally you can provide your own source instead of the internal source |
| 98 | + * @return An array of typescript statements, not fit for insertion yet |
| 99 | + * @internal |
| 100 | + */ |
| 101 | + protected unclonedStatements<T extends Statement = Statement>(replacements?: Replacements, source: string = this.source): NodeArray<T> { |
| 102 | + return this.file("", replacements, source).statements as NodeArray<T>; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Builds a statement from the internal source. |
| 107 | + * @param replacements - The replacement map to apply to the source {@link replace} |
| 108 | + * @param source - Optionally you can provide your own source instead of the internal source |
| 109 | + * @return A typescript statement, not fit for insertion yet |
| 110 | + * @internal |
| 111 | + */ |
| 112 | + protected unclonedStatement<T extends Statement = Statement>(replacements?: Replacements, source: string = this.source): T { |
| 113 | + return this.unclonedStatements<T>(replacements, source)[0]; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Builds a declaration from the internal source. |
| 118 | + * @param replacements - The replacement map to apply to the source {@link replace} |
| 119 | + * @param source - Optionally you can provide your own source instead of the internal source |
| 120 | + * @return A typescript variable declaration, not fit for insertion yet |
| 121 | + * @internal |
| 122 | + */ |
| 123 | + protected unclonedDeclaration<T extends VariableDeclaration = VariableDeclaration>( |
| 124 | + replacements?: Replacements, |
| 125 | + source: string = this.source |
| 126 | + ): T { |
| 127 | + return this.unclonedStatement<VariableStatement>(replacements, source).declarationList.declarations[0] as T; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Builds an expression from the internal source. |
| 132 | + * @param replacements - The replacement map to apply to the source {@link replace} |
| 133 | + * @param source - Optionally you can provide your own source instead of the internal source |
| 134 | + * @return A typescript expression, not fit for insertion yet |
| 135 | + * @internal |
| 136 | + */ |
| 137 | + protected unclonedExpression<T extends Expression = Expression>(replacements?: Replacements, source: string = this.source): T { |
| 138 | + const wrapped = `const __TEMPLATE_REPLACE__${Date.now()}_${0 | (Math.random() * 10000)} = ${source};`; |
| 139 | + return this.unclonedDeclaration(replacements, wrapped).initializer as T; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * A function for use in {@link Array#map}. |
| 144 | + * Clones all passed elements. |
| 145 | + * {@link clone}, {@link cloneOpts} |
| 146 | + * @internal |
| 147 | + */ |
| 148 | + protected get cloner() { |
| 149 | + return (node: any) => this.clone(node); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * A function that clones all passed nodes using {@link cloneOpts}. |
| 154 | + * @param node - The node to clone |
| 155 | + * @returns The cloned node |
| 156 | + * {@link clone}, {@link cloneOpts} |
| 157 | + * @internal |
| 158 | + */ |
| 159 | + protected clone<T extends any>(node: T): T { |
| 160 | + return cloneNode(node, this.cloneOpts) as T; |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +export interface TemplateOptions { |
| 165 | + target?: ScriptTarget; |
| 166 | + kind?: ScriptKind; |
| 167 | + clone?: CloneNodeOptions; |
| 168 | +} |
| 169 | + |
| 170 | +export function template(source: string, { target = ScriptTarget.Latest, kind = ScriptKind.TS, clone }: TemplateOptions = {}) { |
| 171 | + return new TemplateFactory(source, target, kind, clone); |
| 172 | +} |
0 commit comments