From 5c764f3d94350bbca9bc25fb3b310ce5f05ff185 Mon Sep 17 00:00:00 2001 From: gregorybesson Date: Sun, 6 Aug 2017 23:16:42 +0200 Subject: [PATCH] enhancement: The scripts directory is watched in dev mode --- src/cli/core/config/config.json | 3 +++ src/cli/core/manager/Manager.js | 28 +++++++++++++++++++++++++++- src/cli/extend/plugins.js | 28 +++++++++++++++++++++++++++- src/tasks/nodemon.js | 4 ++-- 4 files changed, 59 insertions(+), 4 deletions(-) diff --git a/src/cli/core/config/config.json b/src/cli/core/config/config.json index 11672e98..1d861b2c 100755 --- a/src/cli/core/config/config.json +++ b/src/cli/core/config/config.json @@ -106,6 +106,9 @@ "hooks": { "url": "hooks" }, + "scripts": { + "path": "scripts" + }, "cookie": { "secure": false }, diff --git a/src/cli/core/manager/Manager.js b/src/cli/core/manager/Manager.js index b574f053..aecc98a0 100644 --- a/src/cli/core/manager/Manager.js +++ b/src/cli/core/manager/Manager.js @@ -14,7 +14,8 @@ import { cmsTemplates, cmsReference, cmsMedia, - cmsOperations + cmsOperations, + abeExtend } from '../../' let singleton = Symbol() @@ -74,6 +75,7 @@ class Manager { } } + this.pathScripts = path.join(config.root, config.scripts.path) this.pathStructure = path.join(config.root, config.structure.url) this.pathReference = path.join(config.root, config.reference.url) this.pathData = path.join(config.root, config.data.url) @@ -163,6 +165,7 @@ class Manager { this.events.template = new events.EventEmitter(0) this.events.structure = new events.EventEmitter(0) this.events.reference = new events.EventEmitter(0) + this.events.scripts = new events.EventEmitter(0) // watch template folder try { @@ -305,6 +308,29 @@ class Manager { } catch (e) { console.log('the directory ' + this.pathReference + ' does not exist') } + + try { + fse.accessSync(this.pathScripts, fse.F_OK) + this._watchScripts = watch.createMonitor( + this.pathScripts, + monitor => { + monitor.on('created', () => { + abeExtend.plugins.instance.updateScripts() + this.events.scripts.emit('update') + }) + monitor.on('changed', () => { + abeExtend.plugins.instance.updateScripts() + this.events.scripts.emit('update') + }) + monitor.on('removed', () => { + abeExtend.plugins.instance.updateScripts() + this.events.scripts.emit('update') + }) + } + ) + } catch (e) { + console.log('the directory ' + this.pathScripts + ' does not exist') + } } getKeysFromSelect() { diff --git a/src/cli/extend/plugins.js b/src/cli/extend/plugins.js index 6aa50d17..58214f3f 100755 --- a/src/cli/extend/plugins.js +++ b/src/cli/extend/plugins.js @@ -19,7 +19,6 @@ class Plugins { this.pluginsDir = path.join(config.root, 'node_modules') + path.sep this.scriptsDir = path.join(config.root, 'scripts') + path.sep this._plugins = [] - this.fn = [] // Plugins if (config.plugins && Array.isArray(config.plugins)) { @@ -79,6 +78,7 @@ class Plugins { var fileHook = fse.lstatSync(plugHook) if (fileHook.isFile()) { try { + delete require.cache[require.resolve(plugHook)] var h = require(plugHook) } catch (e) { console.log(e.stack) @@ -272,6 +272,32 @@ class Plugins { } } + updateScripts() { + this._plugins.length = 0 + this._scripts.length = 0 + + // Plugins + if (config.plugins && Array.isArray(config.plugins)) { + Array.prototype.forEach.call(config.plugins, pluginEntry => { + const plugin = this.getPluginConfig(this.pluginsDir, pluginEntry) + this._plugins.push(plugin) + }) + } + + // Scripts + try { + var directoryScripts = fse.lstatSync(this.scriptsDir) + if (directoryScripts.isDirectory()) { + this._scripts = coreUtils.file.getFoldersSync(this.scriptsDir, false) + Array.prototype.forEach.call(this._scripts, scriptEntry => { + const name = scriptEntry.path.replace(this.scriptsDir, '') + const script = this.getPluginConfig(this.scriptsDir, name) + this._plugins.push(script) + }) + } + } catch (e) {} + } + updatePlugin(plugin) { let json = {} let createLocalConfig = true diff --git a/src/tasks/nodemon.js b/src/tasks/nodemon.js index 440ae11e..1ae47afe 100755 --- a/src/tasks/nodemon.js +++ b/src/tasks/nodemon.js @@ -30,11 +30,11 @@ nodemon({ 'src/server/controllers/*', 'src/server/app.js', 'src/server/index.js', - process.env.ROOT + '/scripts/**/**/*.js', + //process.env.ROOT + '/scripts/**/**/*.js', process.env.ROOT + '/abe.json', process.env.ROOT + '/locales/*', process.env.ROOT + '/hooks/**/*.js', - process.env.ROOT + '/reference/**/*.json' + //process.env.ROOT + '/reference/**/*.json' ], stdin: true, runOnChangeOnly: false,