diff --git a/lib/generator/artifact.ts b/lib/generator/artifact.ts index 8f2f3b6ab..2866ab465 100644 --- a/lib/generator/artifact.ts +++ b/lib/generator/artifact.ts @@ -49,6 +49,7 @@ export class Artifact extends EventEmitter { private customFilters?: ProviderConfig['customFilters']; private netflixFilter?: ProviderConfig['netflixFilter']; private youtubePremiumFilter?: ProviderConfig['youtubePremiumFilter']; + private templateEngine?: Environment; constructor( public surgioConfig: CommandConfig, @@ -159,11 +160,15 @@ export class Artifact extends EventEmitter { }; } - public async init(): Promise { + public async init(templateEngine?: Environment): Promise { if (this.isReady) { throw new Error('Artifact 已经初始化完成'); } + if (templateEngine) { + this.templateEngine = templateEngine; + } + this.emit('initArtifact:start', { artifact: this.artifact }); await Bluebird.map( @@ -190,19 +195,25 @@ export class Artifact extends EventEmitter { this.emit('initArtifact:end', { artifact: this.artifact }); } - public render(templateEngine: Environment): string { + public render(templateEngine?: Environment): string { if (!this.isReady) { throw new Error('Artifact 还未初始化'); } + const targetTemplateEngine = templateEngine || this.templateEngine; + + if (!targetTemplateEngine) { + throw new Error('没有可用的 Nunjucks 环境'); + } + const renderContext = this.renderContext; const { templateString, template, } = this.artifact; const result = templateString - ? templateEngine.renderString(templateString, renderContext) - : templateEngine.render(`${template}.tpl`, renderContext); + ? targetTemplateEngine.renderString(templateString, renderContext) + : targetTemplateEngine.render(`${template}.tpl`, renderContext); this.emit('renderArtifact', { artifact: this.artifact, result });