Skip to content

Commit

Permalink
refactor(hexo): merge theme config during watch
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Jun 30, 2020
1 parent 49bd031 commit 28af5bc
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
21 changes: 15 additions & 6 deletions lib/hexo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ const routeCache = new WeakMap();

const castArray = obj => { return Array.isArray(obj) ? obj : [obj]; };

const mergeCtxThemeConfig = ctx => {
// Merge hexo.config.theme_config into hexo.theme.config before post rendering & generating
// config.theme_config has "_config.[theme].yml" merged in load_theme_config.js
if (ctx.config.theme_config) {
ctx.theme.config = deepMerge(ctx.theme.config, ctx.config.theme_config);
}
};

const createLoadThemeRoute = function(generatorResult, locals, ctx) {
const { log, theme } = ctx;
const { path, cache: useCache } = locals;
Expand Down Expand Up @@ -312,11 +320,7 @@ class Hexo extends EventEmitter {
this.theme.process()
]);
}).then(() => {
// Merge hexo.config.theme_config into hexo.theme.config before post rendering & generating
// config.theme_config has "_config.[theme].yml" merged in load_theme_config.js
if (this.config.theme_config) {
this.theme.config = deepMerge(this.theme.config, this.config.theme_config);
}
mergeCtxThemeConfig(this);
}).then(() => this._generate({cache: false})).asCallback(callback);
}

Expand All @@ -340,9 +344,14 @@ class Hexo extends EventEmitter {
this.source.watch(),
this.theme.watch()
]);
}).then(() => {
mergeCtxThemeConfig(this);
}).then(() => {
this.source.on('processAfter', this._watchBox);
this.theme.on('processAfter', this._watchBox);
this.theme.on('processAfter', () => {
this._watchBox();
mergeCtxThemeConfig(this);
});

return this._generate({cache: useCache});
}).asCallback(callback);
Expand Down
35 changes: 35 additions & 0 deletions test/scripts/hexo/hexo.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,41 @@ describe('Hexo', () => {

it('watch() - theme', async () => await testWatch(join(hexo.theme_dir, 'source')));

it('watch() - merge theme config', () => {
const theme_config_1 = [
'a:',
' b: 1',
' c: 2'
].join('\n');
const theme_config_2 = [
'a:',
' b: 1',
' c: 3'
].join('\n');

const hexo = new Hexo(__dirname, { silent: true });
hexo.config.theme_config = { a: { b: 3, d: 4 } };
const theme_config_path = join(hexo.theme_dir, '_config.yml');

return fs.writeFile(theme_config_path, theme_config_1)
.then(() => hexo.init())
.then(() => hexo.watch())
.then(() => {
hexo.theme.config.a.should.have.own.property('d');
hexo.theme.config.a.d.should.eql(4);
})
.then(() => fs.writeFile(theme_config_path, theme_config_2))
.delay(300)
.then(() => {
hexo.theme.config.a.should.have.own.property('d');
hexo.theme.config.a.d.should.eql(4);
})
.then(() => hexo.unwatch())
.delay(300)
.then(() => fs.unlink(theme_config_path))
.delay(300);
});

// it('unwatch()'); missing-unit-test

it('exit()', async () => {
Expand Down

0 comments on commit 28af5bc

Please sign in to comment.