Skip to content

Commit

Permalink
feat: 添加自动清理旧缓存能力
Browse files Browse the repository at this point in the history
  • Loading branch information
edenhliu committed May 21, 2019
1 parent 324811f commit 6e71140
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/a8k/src/plugins/command-build.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import logger from '@a8k/cli-utils/logger';
import { logWithSpinner, stopSpinner } from '@a8k/cli-utils/spinner';
import fs from 'fs-extra';
import { ENV_PROD, TYPE_CLIENT, TYPE_SERVER, ENV_DEV } from '../const';
import { ENV_DEV, ENV_PROD, TYPE_CLIENT, TYPE_SERVER } from '../const';
import cleanUnusedCache from '../utils/clean-old-cache.js';

export default {
apply: context => {
Expand All @@ -17,7 +18,8 @@ export default {
.option('--dev', '环境变量使用 development')
.option('--inspectWebpack', '输出webpack配置信息')
.action(async ({ dev, analyzer, inspectWebpack, sourceMap, mini, silent }) => {
// 为了让react这样的库不要使用压缩代码
await cleanUnusedCache(context);
// 为了让react这样的库不要使用压缩代码;
process.env.NODE_ENV = dev ? ENV_DEV : ENV_PROD;

context.options.inspectWebpack = inspectWebpack;
Expand Down
2 changes: 2 additions & 0 deletions packages/a8k/src/plugins/command-dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import os from 'os';
import WebpackDevServer from 'webpack-dev-server';
import { ENV_DEV, TYPE_CLIENT, TYPE_SERVER } from '../const';
import { printInstructions, setProxy } from '../utils/helper';
import cleanUnusedCache from '../utils/clean-old-cache';

const isInteractive = process.stdout.isTTY;

Expand Down Expand Up @@ -36,6 +37,7 @@ export default {
.option('-c, --css-source-map', '使用cssSourceMap ,但会导致开发模式 FOUC')
.option('--inspectWebpack', '输出webpack配置信息')
.action(async ({ ssr, port, eslint, stylelint, cssSourceMap, inspectWebpack }) => {
await cleanUnusedCache(context);
process.env.NODE_ENV = ENV_DEV;
context.options.inspectWebpack = inspectWebpack;
context.internals.mode = ENV_DEV;
Expand Down
19 changes: 19 additions & 0 deletions packages/a8k/src/utils/clean-old-cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { logWithSpinner, stopSpinner } from '@a8k/cli-utils/spinner';
import fs from 'fs-extra';
import path from 'path';

async function cleanUnusedCache(context) {
const { cacheBase, cache } = context.config;
if (cacheBase) {
logWithSpinner('clean old cache');
const list = await fs.readdir(cacheBase);
await Promise.all(
list
.filter(i => path.basename(cache) !== i)
.map(i => path.join(cacheBase, i))
.map(filepath => fs.remove(filepath))
);
stopSpinner();
}
}
export default cleanUnusedCache;

0 comments on commit 6e71140

Please sign in to comment.