forked from coder-on-deck/mavenmon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch.js
executable file
·162 lines (140 loc) · 4.24 KB
/
watch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env node
/**
*
* Setup watched files.
*
* Invoke the watcher on each detected change.
*
*/
/**
* arguments from command line
*/
var argv = require('minimist')(process.argv.slice(2))
/**
* a library to watch the files
*/
var chokidar = require('chokidar')
/**
* path library
*/
var path = require('path')
/**
* logger
*/
var logger = require('log4js').getLogger('mavemon')
logger.setLevel(argv.verbose ? 'ALL' : 'INFO')
/**
* collection utility
*/
var _ = require('lodash')
/**
* helps with parsing the pom. not to be confused with the pom-parser library.
*/
var mavenmonPomParser = require('./pom-parser')
/**
*
* @type {"fs"}
*/
var fs = require('fs')
/**
* register global exceptions
*/
require('./register-exceptions')
/**
* a function to decide if folder should be watched.
*/
var shouldWatchFolder = require(argv.filter || './should-watch-folder')
// read configuration
var config = {}
var configFile = path.join(process.cwd(), '.nodemon.json')
function reloadConfig () { // support auto reload
logger.debug('reloading configuration from', configFile)
try {
config = _.merge(config, require(configFile)) // merge to keep reference. use null so lodash will remove key
logger.debug('config is', config)
} catch (e) {
logger.error('error reading configuration', e)
}
}
chokidar.watch(configFile).on('all', reloadConfig) // reload on config change
if (fs.existsSync(configFile)) {
reloadConfig()
}
// list watched files
var watchRoot = argv._[0] || path.resolve('.') // by default use cwd
logger.debug('watching args', argv._)
var onchangeLocation = path.resolve(path.join(watchRoot, argv._[1] || './onchange.js'))
if (fs.existsSync(onchangeLocation)) {
logger.debug('onchangeLocation', onchangeLocation)
} else {
logger.info('using default onchange handler')
onchangeLocation = './default-on-change.js'
}
var onchange = require(onchangeLocation)
var ready = false
var queue = {}
/**
* @typedef {object} QueueItem
* @property {string} dir
* @property {object} pom. Contains project.version, project.parent.version etc.. like a pom.xml model. NOTE! keys are in lower-case
* @property {string} jarfile - an estimation where the jarfile will be. (does not support custom maven configuration)
* @property {string} classfile - estimation where the classfile will be. (does not support custom maven configuration)
*/
/**
*
* @param {QueueItem} data
*/
function executeQueueItem (data) {
logger.debug('handling change on ', path)
if (!ready) {
return
}
var promise = onchange(data, argv.buildNumber, argv.commitNumber)
if (!promise || !promise.then) {
throw new Error('onchange must return a promise')
}
promise.then(function () {
delete queue[data.dir]
}, function () {
})
}
function executeAllItems () {
var beforeLength = Object.keys(queue).length
var promises = _.map(queue, item => executeQueueItem(item))
Promise.all(promises, () => {
if (beforeLength !== Object.keys(queue).length) {
executeAllItems()
}
})
}
// on change, register the item on the queue and execute all items
function handleChange (type) {
return function (path) {
logger.info(type, path)
mavenmonPomParser(path).then(function (data) {
data.path = path
data.type = type
queue[data.dir] = data
executeAllItems()
})
}
}
logger.debug('starting walk')
// One-liner for current directory, ignores .dotfiles
var watcher = chokidar.watch('.', {
ignored: function (filepath) {
// console.log('should watch', filepath, shouldWatchFolder(filepath))
return !shouldWatchFolder(filepath)
},
usePolling: true // after a lot of investigating.. polling behaves nicer when using `mvn clean`
}).on('ready', function () {
ready = true
setTimeout(function () { // should happen on a different tick or otherwise we will get all the add events.
logger.debug('adding on change handler')
}, 0)
setTimeout(function () {
watcher.on('change', handleChange('change')).on('add', handleChange('add')).on('unlink', handleChange('unlink')) // 'change' when code changed. 'add' for .class files that were removed with 'clean' and added by 'install'
logger.info('watching... you can start writing code')
}, 1000)
})
logger.info('setting up watch for [' + watchRoot + '] please wait..')