Skip to content

Commit

Permalink
feat: 修改 Artifact 初始化接口
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed Feb 28, 2020
1 parent fb2e886 commit 4c3ac84
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 11 deletions.
56 changes: 56 additions & 0 deletions lib/generator/__tests__/artifact.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import test from 'ava';
import { join } from 'path';
import { loadConfig } from '../../utils/config';
import { Artifact } from '../artifact';
import { getEngine } from '../template';

const resolve = p => join(__dirname, '../../../test/fixture/', p);

test('new Artifact()', async t => {
const fixture = resolve('plain');
const config = loadConfig(fixture, './surgio.conf.js');
const artifact = new Artifact(config, {
name: 'new_path.conf',
template: 'test',
provider: 'ss_json',
});

t.is(artifact.isReady, false);
await artifact.init();
t.is(artifact.isReady, true);

await t.throwsAsync(async () => {
await artifact.init();
});
});

test('Artifact without templateEngine', async t => {
const fixture = resolve('plain');
const config = loadConfig(fixture, './surgio.conf.js');
const artifact = new Artifact(config, {
name: 'new_path.conf',
template: 'test',
provider: 'ss_json',
});
const templateEngine = getEngine(config.templateDir, config.publicUrl);

await artifact.init();

t.throws(() => {
artifact.render();
});
t.notThrows(() => {
artifact.render(templateEngine);
});
await t.notThrowsAsync(async () => {
const instance = await (
new Artifact(config, {
name: 'new_path.conf',
template: 'test',
provider: 'ss_json',
}, { templateEngine })
)
.init();
instance.render();
});
});
15 changes: 5 additions & 10 deletions lib/generator/artifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { prependFlag } from '../utils/flag';

export interface ArtifactOptions {
readonly remoteSnippetList?: ReadonlyArray<RemoteSnippet>;
readonly templateEngine?: Environment;
}

export class Artifact extends EventEmitter {
Expand All @@ -49,12 +50,11 @@ export class Artifact extends EventEmitter {
private customFilters?: ProviderConfig['customFilters'];
private netflixFilter?: ProviderConfig['netflixFilter'];
private youtubePremiumFilter?: ProviderConfig['youtubePremiumFilter'];
private templateEngine?: Environment;

constructor(
public surgioConfig: CommandConfig,
public artifact: ArtifactConfig,
public options: ArtifactOptions = {}
private options: ArtifactOptions = {}
) {
super();

Expand Down Expand Up @@ -160,15 +160,11 @@ export class Artifact extends EventEmitter {
};
}

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

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

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

await Bluebird.map(
Expand Down Expand Up @@ -202,7 +198,7 @@ export class Artifact extends EventEmitter {
throw new Error('Artifact 还未初始化');
}

const targetTemplateEngine = templateEngine || this.templateEngine;
const targetTemplateEngine = templateEngine || this.options.templateEngine;

if (!targetTemplateEngine) {
throw new Error('没有可用的 Nunjucks 环境');
Expand Down Expand Up @@ -332,7 +328,7 @@ export class Artifact extends EventEmitter {
) {
try {
nodeConfig.hostnameIp = await resolveDomain(nodeConfig.hostname);
} /* istanbul ignore next */ catch (err) {
} /* istanbul ignore next */catch (err) {
logger.warn(`${nodeConfig.hostname} 无法解析,将忽略该域名的解析结果`);
}
}
Expand All @@ -344,7 +340,6 @@ export class Artifact extends EventEmitter {
})
.filter(item => !!item);


this.nodeConfigListMap.set(providerName, nodeConfigList);
this.initProgress++;

Expand Down
2 changes: 2 additions & 0 deletions lib/provider/Provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,12 @@ export default class Provider {
return globalPort++;
}

// istanbul ignore next
public async getSubscriptionUserInfo(): Promise<SubscriptionUserinfo> {
throw new Error('此 Provider 不支持该功能');
}

// istanbul ignore next
public getNodeList(): Promise<ReadonlyArray<PossibleNodeConfigType>> {
return Promise.resolve([]);
};
Expand Down
2 changes: 1 addition & 1 deletion test/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const cli = path.join(__dirname, '../bin/surgio.js');
const fixture = path.join(__dirname, './fixture');
const resolve = p => path.join(fixture, p);

test.only('cli works', async t => {
test('cli works', async t => {
await coffee.fork(cli, ['generate', '-h'], {
cwd: resolve('plain'),
})
Expand Down

0 comments on commit 4c3ac84

Please sign in to comment.