Skip to content

Commit

Permalink
enhancement: The scripts directory is watched in dev mode
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorybesson committed Aug 6, 2017
1 parent ee0ec58 commit 5c764f3
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/cli/core/config/config.json
Expand Up @@ -106,6 +106,9 @@
"hooks": {
"url": "hooks"
},
"scripts": {
"path": "scripts"
},
"cookie": {
"secure": false
},
Expand Down
28 changes: 27 additions & 1 deletion src/cli/core/manager/Manager.js
Expand Up @@ -14,7 +14,8 @@ import {
cmsTemplates,
cmsReference,
cmsMedia,
cmsOperations
cmsOperations,
abeExtend
} from '../../'

let singleton = Symbol()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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() {
Expand Down
28 changes: 27 additions & 1 deletion src/cli/extend/plugins.js
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/tasks/nodemon.js
Expand Up @@ -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,
Expand Down

0 comments on commit 5c764f3

Please sign in to comment.