From 128c9c5998dcc8db9d4c9472ce451ff40f4732ee Mon Sep 17 00:00:00 2001 From: Roy Li Date: Sun, 6 Sep 2020 18:41:05 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=85=81=E8=AE=B8=E9=80=9A=E8=BF=87?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=E8=A1=8C=E5=BC=80=E5=90=AF=E8=BF=9C=E7=A8=8B?= =?UTF-8?q?=E7=89=87=E6=AE=B5=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/command/generate.ts | 7 ++++++- lib/generate.ts | 8 ++++---- lib/utils/remote-snippet.ts | 7 ++++--- lib/utils/tmp-helper.ts | 5 ++--- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/lib/command/generate.ts b/lib/command/generate.ts index 83b61d2f4..ac189e643 100644 --- a/lib/command/generate.ts +++ b/lib/command/generate.ts @@ -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, @@ -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 diff --git a/lib/generate.ts b/lib/generate.ts index d44a4d614..eb7a9878c 100644 --- a/lib/generate.ts +++ b/lib/generate.ts @@ -16,11 +16,11 @@ import { loadRemoteSnippetList } from './utils/remote-snippet'; const spinner = ora(); -async function run(config: CommandConfig, skipFail?: boolean): Promise { +async function run(config: CommandConfig, skipFail?: boolean, snippetCache?: boolean): Promise { const artifactList: ReadonlyArray = 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); @@ -79,9 +79,9 @@ export async function generate( return artifactInstance.render(templateEngine); } -export default async function(config: CommandConfig, skipFail?: boolean): Promise { +export default async function(config: CommandConfig, skipFail?: boolean, snippetCache?: boolean): Promise { logger.info('开始生成规则'); - await run(config, skipFail) + await run(config, skipFail, snippetCache) .catch(err => { // istanbul ignore next if (spinner.isSpinning) { diff --git a/lib/utils/remote-snippet.ts b/lib/utils/remote-snippet.ts index 1797bab60..dc4138a73 100644 --- a/lib/utils/remote-snippet.ts +++ b/lib/utils/remote-snippet.ts @@ -47,7 +47,9 @@ export const addProxyToSurgeRuleSet = (str: string, proxyName: string): string = .join('\n'); }; -export const loadRemoteSnippetList = (remoteSnippetList: ReadonlyArray): Promise> => { +export const loadRemoteSnippetList = (remoteSnippetList: ReadonlyArray, snippetCache = true): Promise> => { + const tmpFactory = createTmpFactory('remote-snippets'); + function load(url: string): Promise { // logger.info(`正在下载远程片段: ${url}`); @@ -63,11 +65,10 @@ export const loadRemoteSnippetList = (remoteSnippetList: ReadonlyArray { - 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; diff --git a/lib/utils/tmp-helper.ts b/lib/utils/tmp-helper.ts index 4b89f680e..168a807a5 100644 --- a/lib/utils/tmp-helper.ts +++ b/lib/utils/tmp-helper.ts @@ -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; @@ -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); }