Skip to content

Commit

Permalink
feat: add cache
Browse files Browse the repository at this point in the history
  • Loading branch information
zyao89 committed Mar 15, 2020
1 parent 3ef2751 commit fe988ab
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/commands/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ module.exports = (api, argv, opts) => {
return vuepressConfig[name];
},
}),
vuepressDir: vuepressConfig.vuepressDir,
};

return OPTIONS;
Expand Down
20 changes: 16 additions & 4 deletions src/commands/create/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
module.exports = function(api, argv, opts, BASE_ROOT) {
const logger = api.logger;

const path = require('path');
const { fs, prompt, chalk } = require('@micro-app/shared-utils');
const { fs, prompt, chalk, path, _ } = require('@micro-app/shared-utils');
const moment = require('moment');

const selfVuepressConfig = api.selfVuepressConfig || {};
const commandOpts = selfVuepressConfig.command || {};
const createCommand = commandOpts.create || {};
const cacheDir = path.resolve(api.tempDir, '.cache');

let chain = Promise.resolve();

Expand All @@ -33,7 +33,13 @@ module.exports = function(api, argv, opts, BASE_ROOT) {
// categories
chain = chain.then(() => {
// 提供可选项,没有则自定义。
const categoriesOpts = [].concat(createCommand.categories || []);
// 从缓存获取
const categoriesCachePath = path.resolve(cacheDir, 'categories.json');
let categoriesCache = [];
if (fs.existsSync(categoriesCachePath)) {
categoriesCache = fs.readJSONSync(categoriesCachePath);
}
const categoriesOpts = _.uniq([].concat(createCommand.categories || []).concat(categoriesCache || []));
let _chain = Promise.resolve(CUSTOM_KEY);
if (categoriesOpts.length) {
_chain = _chain.then(() => prompt.select('Select Categories:', {
Expand All @@ -59,7 +65,13 @@ module.exports = function(api, argv, opts, BASE_ROOT) {
// tags
chain = chain.then(() => {
// 提供可选项,没有则自定义。
const tagsOpts = [].concat(createCommand.tags || []);
// 从缓存获取
const tagsCachePath = path.resolve(cacheDir, 'tags.json');
let tagsCache = [];
if (fs.existsSync(tagsCachePath)) {
tagsCache = fs.readJSONSync(tagsCachePath);
}
const tagsOpts = _.uniq([].concat(createCommand.tags || []).concat(tagsCache || []));
let _chain = Promise.resolve([]);
if (tagsOpts.length) {
_chain = _chain.then(() => prompt.check('Select Tags:', {
Expand Down
5 changes: 5 additions & 0 deletions src/commands/createConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ module.exports = function createConfig(api, args, opts) {

const loadConfig = require('@vuepress/core/lib/node/loadConfig');

// const vuepressDir = api.resolveWorkspace('.vuepress'); // 不能使用,vuepress内部代码太乱
const vuepressDir = path.resolve(root, vuepressConfig.sourceDir, '.vuepress');
const siteConfig = loadConfig(vuepressDir) || {};

const customConfig = require('../config');
const config = smartMerge({}, customConfig, siteConfig, vuepressConfig, opts, _.pick(args, [ 'base' ]));
config.root = root;
// reset vuepressDir
config.vuepressDir = vuepressDir;
config.cacheDir = path.resolve(api.tempDir, '.cache');
// reset dest
config.dest = path.resolve(vuepressConfig.sourceDir, config.dest);

Expand Down
37 changes: 37 additions & 0 deletions src/commands/lib/core.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

const { eject, version, createApp } = require('@vuepress/core');

function changeVuepressDir(app, options) {
if (options.vuepressDir) {
app.vuepressDir = options.vuepressDir;
}
}

async function dev(options) {
if (process.env.NODE_ENV === undefined) {
process.env.NODE_ENV = 'development';
}
const app = createApp(options);
// change vuepressDir
changeVuepressDir(app, options);
await app.process();
return app.dev();
}

async function build(options) {
if (process.env.NODE_ENV === undefined) {
process.env.NODE_ENV = 'production';
}
const app = createApp(options);
// change vuepressDir
changeVuepressDir(app, options);
await app.process();
return app.build();
}

exports.version = version;
exports.createApp = createApp;
exports.dev = dev;
exports.build = build;
exports.eject = eject;
2 changes: 1 addition & 1 deletion src/commands/lib/registerCoreCommands.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

const envinfo = require('envinfo');
const { dev, build, eject } = require('@vuepress/core');
const { dev, build, eject } = require('./core');
const { chalk, path, logger, env } = require('@vuepress/shared-utils');
const { wrapCommand } = require('./util');

Expand Down
48 changes: 48 additions & 0 deletions theme/plugins/blog/createCache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// cache
module.exports = (options, ctx) => {
const { fs, _, logger, path } = require('@micro-app/shared-utils');

return {
name: 'createCache',

ready() {
const { pages, siteConfig } = ctx;

const categories = [];
const tags = [];
pages
.filter(page => !!page.frontmatter)
.filter(page => {
const { title, frontmatter: { home, private: isPrivate } } = page;
return !(home === true || title === undefined || isPrivate === true);
})
.map(page => ({ ...page, date: new Date(page.frontmatter.date || page.birthTimestamp || '') }))
.sort((a, b) => b.date - a.date)
.map(page => {
const frontmatter = page.frontmatter || {};
const categories = Array.isArray(frontmatter.categories) ? frontmatter.categories : _.isString(frontmatter.categories) && [ frontmatter.categories ];
const tags = Array.isArray(frontmatter.tags) ? frontmatter.tags : _.isString(frontmatter.tags) && [ frontmatter.tags ];
return {
categories: [].concat(categories || []),
tags: [].concat(tags || []),
};
})
.forEach(item => {
categories.push(...item.categories);
tags.push(...item.tags);
});

const cacheDir = siteConfig.cacheDir;
fs.ensureDirSync(cacheDir);
fs.writeJSONSync(
path.resolve(cacheDir, 'categories.json'),
_.uniq(categories)
);
fs.writeJSONSync(
path.resolve(cacheDir, 'tags.json'),
_.uniq(tags)
);
logger.info('Categories & Tags has been cached!');
},
};
};
1 change: 1 addition & 0 deletions theme/plugins/blog/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const registerPlugins = function(ctx) {
}

// TODO more blog plugins...
plugins.push([ require('./createCache'), true ]);

return plugins;
};
Expand Down
3 changes: 1 addition & 2 deletions theme/plugins/rss.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// rss
module.exports = (options, ctx) => {
const { fs, _, logger } = require('@micro-app/shared-utils');
const path = require('path');
const { fs, _, logger, path } = require('@micro-app/shared-utils');
const RSS = require('rss');

if (!_.isPlainObject(options)) {
Expand Down

0 comments on commit fe988ab

Please sign in to comment.