Skip to content

Commit

Permalink
feat: 允许通过命令行开启远程片段缓存
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed Sep 6, 2020
1 parent 3f4fb3b commit 128c9c5
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
7 changes: 6 additions & 1 deletion lib/command/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ class GenerateCommand extends Command {
alias: 'config',
default: './surgio.conf.js',
},
'snippet-cache': {
type: 'boolean',
default: false,
description: '缓存远程片段',
},
'skip-fail': {
type: 'boolean',
default: false,
Expand Down Expand Up @@ -48,7 +53,7 @@ class GenerateCommand extends Command {
output: path.resolve(ctx.cwd, ctx.argv.output),
} : null)
});
await generate(config, ctx.argv.skipFail);
await generate(config, ctx.argv.skipFail, ctx.argv.snippetCache);
}

// istanbul ignore next
Expand Down
8 changes: 4 additions & 4 deletions lib/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import { loadRemoteSnippetList } from './utils/remote-snippet';

const spinner = ora();

async function run(config: CommandConfig, skipFail?: boolean): Promise<void> {
async function run(config: CommandConfig, skipFail?: boolean, snippetCache?: boolean): Promise<void> {
const artifactList: ReadonlyArray<ArtifactConfig> = config.artifacts;
const distPath = config.output;
const remoteSnippetsConfig = config.remoteSnippets || [];
const remoteSnippetList = await loadRemoteSnippetList(remoteSnippetsConfig);
const remoteSnippetList = await loadRemoteSnippetList(remoteSnippetsConfig, snippetCache);
const templateEngine = getEngine(config.templateDir);

await fs.remove(distPath);
Expand Down Expand Up @@ -79,9 +79,9 @@ export async function generate(
return artifactInstance.render(templateEngine);
}

export default async function(config: CommandConfig, skipFail?: boolean): Promise<void> {
export default async function(config: CommandConfig, skipFail?: boolean, snippetCache?: boolean): Promise<void> {
logger.info('开始生成规则');
await run(config, skipFail)
await run(config, skipFail, snippetCache)
.catch(err => {
// istanbul ignore next
if (spinner.isSpinning) {
Expand Down
7 changes: 4 additions & 3 deletions lib/utils/remote-snippet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ export const addProxyToSurgeRuleSet = (str: string, proxyName: string): string =
.join('\n');
};

export const loadRemoteSnippetList = (remoteSnippetList: ReadonlyArray<RemoteSnippetConfig>): Promise<ReadonlyArray<RemoteSnippet>> => {
export const loadRemoteSnippetList = (remoteSnippetList: ReadonlyArray<RemoteSnippetConfig>, snippetCache = true): Promise<ReadonlyArray<RemoteSnippet>> => {
const tmpFactory = createTmpFactory('remote-snippets');

function load(url: string): Promise<string> {
// logger.info(`正在下载远程片段: ${url}`);

Expand All @@ -63,11 +65,10 @@ export const loadRemoteSnippetList = (remoteSnippetList: ReadonlyArray<RemoteSni
}

return Bluebird.map(remoteSnippetList, item => {
const tmpFactory = createTmpFactory('remote-snippets');
const fileMd5 = crypto.createHash('md5').update(item.url).digest('hex');

return (async () => {
if (isNow()) {
if (snippetCache || isNow()) {
const tmp = tmpFactory(fileMd5, REMOTE_SNIPPET_CACHE_MAXAGE);
const tmpContent = await tmp.getContent();
let snippet: string;
Expand Down
5 changes: 2 additions & 3 deletions lib/utils/tmp-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import fs from 'fs-extra';
const logger = createLogger({ service: 'surgio:utils:tmp-helper' });
const tmpDir = path.join(os.tmpdir(), 'surgio-config');

logger.debug('tmpDir: %s', tmpDir);

export class TmpFile {
public filename: string;
public extname: string;
Expand Down Expand Up @@ -61,9 +59,10 @@ export class TmpFile {
export interface TmpContent { readonly content: string, readonly lastEditTime: number, readonly maxAge?: number }

export const createTmpFactory = (baseDir: string): ((filePath: string, maxAge?: number) => TmpFile) => {

baseDir = path.join(tmpDir, baseDir);

logger.debug('tmpDir: %s', baseDir);

if (!fs.existsSync(baseDir)) {
fs.mkdirpSync(baseDir);
}
Expand Down

0 comments on commit 128c9c5

Please sign in to comment.