From bc9748f31479ea0e2311ebf4437bb6289b1b658b Mon Sep 17 00:00:00 2001 From: dailyrandomphoto Date: Wed, 23 Oct 2019 16:06:06 +0800 Subject: [PATCH] should not clear database when use 'hexo new', 'hexo --help', etc. commands --- lib/plugins/filter/before_exit/save_database.js | 2 +- test/scripts/filters/save_database.js | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/plugins/filter/before_exit/save_database.js b/lib/plugins/filter/before_exit/save_database.js index d1c2f9519c..a5388e399e 100644 --- a/lib/plugins/filter/before_exit/save_database.js +++ b/lib/plugins/filter/before_exit/save_database.js @@ -1,7 +1,7 @@ 'use strict'; function saveDatabaseFilter() { - if (!this.env.init) return; + if (!this.env.init || !this._dbLoaded) return; return this.database.save().then(() => { this.log.debug('Database saved'); diff --git a/test/scripts/filters/save_database.js b/test/scripts/filters/save_database.js index 641cdfb8b5..a33482e3ac 100644 --- a/test/scripts/filters/save_database.js +++ b/test/scripts/filters/save_database.js @@ -11,6 +11,7 @@ describe('Save database', () => { it('default', () => { hexo.env.init = true; + hexo._dbLoaded = true; return saveDatabase().then(() => fs.exists(dbPath)).then(exist => { exist.should.be.true; @@ -20,6 +21,16 @@ describe('Save database', () => { it('do nothing if hexo is not initialized', () => { hexo.env.init = false; + hexo._dbLoaded = true; + + return saveDatabase().then(() => fs.exists(dbPath)).then(exist => { + exist.should.be.false; + }); + }); + + it('do nothing if database is not loaded', () => { + hexo.env.init = true; + hexo._dbLoaded = false; return saveDatabase().then(() => fs.exists(dbPath)).then(exist => { exist.should.be.false;