diff --git a/index.js b/index.js index 535748f127ae311d21b56cfe690bf8333ce8dd9a..b47a67791da17235186587454175aaf0ac078e13 100644 --- a/index.js +++ b/index.js @@ -92,6 +92,8 @@ function Stack(dependencies) { const cli = require('./app/cli')(); const configLoadingCallbacks = []; const configBeforeSaveCallbacks = []; + const resetDataHooks = []; + const resetConfigHooks = []; const INPUT = new InputStream(); const PROCESS = new ProcessStream(); const loaded = {}; @@ -137,6 +139,20 @@ function Stack(dependencies) { } } + /** + * Reset data/config injection + * ----------------------- + */ + if (def.onReset) { + if (def.onReset.data) { + resetDataHooks.push(def.onReset.data); + } + // Before the configuration is saved, the module can make some injection/cleaning + if (def.onReset.config) { + resetConfigHooks.push(def.onReset.config); + } + } + /** * Wizard injection * ----------------------- @@ -186,6 +202,18 @@ function Stack(dependencies) { }); }); + // Config or Data reset hooks + server.resetDataHook = () => co(function*() { + for (const callback of resetDataHooks) { + yield callback(server.conf, program, logger, server.dal.confDAL); + } + }) + server.resetConfigHook = () => co(function*() { + for (const callback of resetConfigHooks) { + yield callback(server.conf, program, logger, server.dal.confDAL); + } + }) + // Initialize server (db connection, ...) try { server.onPluggedFSHook = () => co(function*() { diff --git a/server.js b/server.js index e8cb1a2dc305528b209ee64b42e59630c63e9253..edd2a6ac37bbda7da25221a19ef5122a6f80714f 100644 --- a/server.js +++ b/server.js @@ -225,22 +225,26 @@ function Server (home, memoryOnly, overrideConf) { }); this.resetAll = (done) => co(function*() { + yield that.resetDataHook() + yield that.resetConfigHook() const files = ['stats', 'cores', 'current', directory.DUNITER_DB_NAME, directory.DUNITER_DB_NAME + '.db', directory.DUNITER_DB_NAME + '.log', directory.WOTB_FILE, 'export.zip', 'import.zip', 'conf']; const dirs = ['blocks', 'blockchain', 'ud_history', 'branches', 'certs', 'txs', 'cores', 'sources', 'links', 'ms', 'identities', 'peers', 'indicators', 'leveldb']; return resetFiles(files, dirs, done); }); this.resetData = (done) => co(function*(){ + yield that.resetDataHook() const files = ['stats', 'cores', 'current', directory.DUNITER_DB_NAME, directory.DUNITER_DB_NAME + '.db', directory.DUNITER_DB_NAME + '.log', directory.WOTB_FILE]; const dirs = ['blocks', 'ud_history', 'branches', 'certs', 'txs', 'cores', 'sources', 'links', 'ms', 'identities', 'peers', 'indicators', 'leveldb']; yield resetFiles(files, dirs, done); }); - this.resetConf = (done) => { + this.resetConf = (done) => co(function*() { + yield that.resetConfigHook() const files = ['conf']; const dirs = []; return resetFiles(files, dirs, done); - }; + }); this.resetStats = (done) => { const files = ['stats']; @@ -455,10 +459,20 @@ function Server (home, memoryOnly, overrideConf) { */ this.generatorNewCertsToLinks = () => Promise.resolve({}) - /* + /** * Default hook on file system plugging. To be overriden by module system. */ this.onPluggedFSHook = () => Promise.resolve({}) + + /** + * Default hook on data reset. To be overriden by module system. + */ + this.resetDataHook = () => Promise.resolve({}) + + /** + * Default hook on data reset. To be overriden by module system. + */ + this.resetConfigHook = () => Promise.resolve({}) } util.inherits(Server, stream.Duplex);