Skip to content

Commit

Permalink
feat: 支持在 Artifact 初始化时传入 Environment
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed Feb 27, 2020
1 parent 28c39eb commit 75ae51f
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions lib/generator/artifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -159,11 +160,15 @@ export class Artifact extends EventEmitter {
};
}

public async init(): Promise<void> {
public async init(templateEngine?: Environment): Promise<void> {
if (this.isReady) {
throw new Error('Artifact 已经初始化完成');
}

if (templateEngine) {
this.templateEngine = templateEngine;
}

this.emit('initArtifact:start', { artifact: this.artifact });

await Bluebird.map(
Expand All @@ -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 });

Expand Down

0 comments on commit 75ae51f

Please sign in to comment.