Skip to content
Permalink
Browse files

fix(watcher): when watching symlinks specified in build option extra.…

…watch for windows

Removed dependency gulp-watch in favor of gulp's built in watch.
  • Loading branch information
jyounce committed Mar 20, 2018
1 parent a07fa39 commit c056c226f7487317d842021023927d1d28052609

Large diffs are not rendered by default.

@@ -95,7 +95,6 @@
"gulp-template": "5.0.0",
"gulp-typescript": "2.13.6",
"gulp-uglify": "3.0.0",
"gulp-watch": "5.0.0",
"gulp-zip": "4.1.0",
"inline-source": "5.2.7",
"jasmine": "2.9.0",
@@ -1,8 +1,9 @@
module.exports = (config, options) ->
path = require 'path'
log = require "#{config.req.helpers}/log"
isType = require "#{config.req.helpers}/isType"
test = require("#{config.req.helpers}/test")()
path = require 'path'
log = require "#{config.req.helpers}/log"
isType = require "#{config.req.helpers}/isType"
pathHelp = require "#{config.req.helpers}/path"
test = require("#{config.req.helpers}/test")()

# init extra.watch
# watch and copy additional static
@@ -25,6 +26,7 @@ module.exports = (config, options) ->
continue unless files.length
for file, i in files
files[i] = path.join config.src[app][loc].dir, files[i]
files[i] = pathHelp.format files[i]

formatCopyPaths 'app'

@@ -239,6 +239,7 @@ module.exports = (config) ->
ePaths = if ePaths then ePaths else excludes[k1][k2]
continue unless ePaths
continue unless ePaths.length
ePaths = pathHelp.formats ePaths
glob.src[appOrRb][loc][k1][k2] = v2.concat ePaths

addExcludeFromDist 'client'
@@ -254,7 +255,7 @@ module.exports = (config) ->
nodeModsGlob = '**/node_modules/**'
rbBowerGlob = "**/#{rbBowerDirName}/**"
appBowerGlob = "**/#{appBowerDirName}/**"
appLibsGlob = "#{appLibsDir}/**"
appLibsGlob = pathHelp.format "#{appLibsDir}/**"
extraGlobs = config.extra.watch.app.client
ignoreGlobs = [nodeModsGlob, rbBowerGlob, appLibsGlob]
ignoreGlobs.push appBowerGlob if appBowerDirName isnt rbBowerDirName
@@ -0,0 +1,85 @@
# WATCH HELPER
# introduced for chokidar
# =======================
module.exports = (config) ->
path = require 'path'
pathHelp = require "#{config.req.helpers}/path"

# API
# ===
getOptions: (opts={}) -> # chokidar options
watchOpts = ignoreInitial: true
return watchOpts unless Object.keys(opts).length
return watchOpts unless process.platform is 'win32'
return watchOpts unless opts.task and opts.delayTasks
# for windows when watching extra files
delayTask = opts.delayTasks.every (task) ->
opts.task.indexOf(task) isnt -1
return watchOpts unless delayTask
watchOpts.awaitWriteFinish = stabilityThreshold: 600
watchOpts

getGlobs: (glob, opts={}) ->
globs =
task: opts.task
files: []
paths: []
globstars: []

delete globs.task unless opts.task
return globs if !glob or !glob.length

for val in glob
continue if val.indexOf('!') isnt -1 # remove ignores
if val.endsWith '/' # absolute paths
globs.paths.push val
continue
if val.indexOf('*') isnt -1
globs.globstars.push val
continue
globs.files.push val # file paths
globs

getFileInfo: (_event, _path, globs) ->
_path = pathHelp.format _path
cwd = pathHelp.format process.cwd()
base = @_getBasepath _path, globs
relative = path.relative base, _path
file = {
event: _event
cwd
base
path: _path
relative
}

_getBasepath: (_path, globs) ->
basepath = @_getFileBase _path, globs.files
return basepath if basepath # files check

basepath = @_getPathBase _path, globs.paths
return basepath if basepath # absolute paths check

basepath = @_getGlobstarBase _path, globs.globstars
return basepath # globstar check

_getFileBase: (_path, globs) ->
basepath = globs.find (val) ->
return val.indexOf(_path) isnt -1
return unless basepath
path.dirname basepath

_getPathBase: (_path, globs) ->
for val in globs
if _path.indexOf(val) isnt -1
# remove trailing slash
basepath = val.substr 0, val.length - 1
break
basepath

_getGlobstarBase: (_path, globs) ->
for val in globs
val = val.split('/*')[0] # where a glob starts
if _path.indexOf(val) isnt -1
basepath = val; break
basepath
@@ -99,10 +99,10 @@ module.exports = (gulp, config) ->

# inline
# ======
taskManager.addTask 'inline-js-html-imports:dev', '/inline/js-html-imports', env: 'dev'
taskManager.addTask 'inline-js-html-imports:prod', '/inline/js-html-imports', env: 'prod'
taskManager.addTask 'inline-html-assets:dev', '/inline/html-assets', env: 'dev'
taskManager.addTask 'inline-html-assets:prod', '/inline/html-assets', env: 'prod'
taskManager.addTask 'inline-js-html-imports:dev', '/inline/js-html-imports', env: 'dev'
taskManager.addTask 'inline-js-html-imports:prod', '/inline/js-html-imports', env: 'prod'

# manage
# ======
@@ -19,7 +19,10 @@ module.exports = (config, gulp, Task) ->
.pipe inlineHtmlAssets config.inline.htmlAssets.options
.on 'error', (e) -> defer.reject e
.on 'data', ->
Task.opts.watchFile.rbLog() if forWatchFile
try
Task.opts.watchFile.rbLog() if forWatchFile
catch e
console.warn "#{Task.name}\n#{e.message}".warn
.pipe gulp.dest dest
.on 'end', ->
defer.resolve message: "completed task: #{Task.name}"
@@ -18,7 +18,10 @@ module.exports = (config, gulp, Task) ->
.pipe inlineJsHtmlImports()
.on 'error', (e) -> defer.reject e
.on 'data', ->
Task.opts.watchFile.rbLog() if forWatchFile
try
Task.opts.watchFile.rbLog() if forWatchFile
catch e
console.warn "#{Task.name}\n#{e.message}".warn
.pipe gulp.dest dest
.on 'end', ->
defer.resolve message: "completed task: #{Task.name}"
@@ -6,8 +6,8 @@ module.exports = (config, gulp, Task) ->
# ========
q = require 'q'
path = require 'path'
gWatch = require 'gulp-watch'
log = require "#{config.req.helpers}/log"
watchHelper = require("#{config.req.helpers}/watch") config
watchStore = require("#{config.req.manage}/watch-store") config
taskManager = require("#{config.req.manage}/task-manager") config, gulp

@@ -68,10 +68,11 @@ module.exports = (config, gulp, Task) ->
# event tasks
# ===========
changeTask = (taskName, file, opts) ->
Tasks.browserSync() if opts.bsReload is opts.event # see extra file watches
watchOpts = watchFile: file
watchOpts.keep = true if opts.keepWatchOpts
taskManager.runWatchTask taskName, watchOpts
taskManager.runWatchTask(taskName, watchOpts).then ->
return unless opts.bsReload is opts.event # see extra file watches
Tasks.browserSync()

addTask = (taskName, file, opts) ->
changeTask(taskName, file, opts).then ->
@@ -107,16 +108,37 @@ module.exports = (config, gulp, Task) ->
# watches
# =======
createWatch = (taskName, glob, opts={}) ->
defer = q.defer()
gOpts = read: false
gWatch glob, gOpts, (file) ->
defer = q.defer()
watchOpts = watchHelper.getOptions
task: taskName
delayTasks: ['copy-extra-files']

watcher = gulp.watch glob, watchOpts
watcherGlobs = watchHelper.getGlobs glob, task: taskName
# log.json watcherGlobs, 'WATCHER GLOBS:'

watcher.on 'add', (_path) ->
file = watchHelper.getFileInfo 'add', _path, watcherGlobs
events file, taskName, opts
.on 'ready', ->

watcher.on 'change', (_path) ->
file = watchHelper.getFileInfo 'change', _path, watcherGlobs
events file, taskName, opts

watcher.on 'unlink', (_path) ->
file = watchHelper.getFileInfo 'unlink', _path, watcherGlobs
events file, taskName, opts

watcher.on 'error', (e) ->
log.error e, "watcher task #{taskName}"

watcher.on 'ready', ->
loc = opts.loc or 'client'
loc = "#{loc} test" if opts.isTest
msg = if opts.lang.indexOf('.') isnt -1 then 'file' else 'files'
log.task "watching #{loc} #{opts.lang} #{msg}", 'minor'
defer.resolve()

defer.promise

# API
@@ -4,6 +4,7 @@ module.exports = (config) -> # :Object[]
# Requires
# ========
log = require "#{config.req.helpers}/log"
pathHelp = require "#{config.req.helpers}/path"
promiseHelp = require "#{config.req.helpers}/promise"

# Watches
@@ -43,13 +44,15 @@ module.exports = (config) -> # :Object[]
config.glob.dist.app.client.scripts.all
config.glob.dist.app.client.styles.all
config.glob.dist.app.client.views.all
config.spa.dist.path
pathHelp.format config.spa.dist.path
)
jsHtmlImports: [].concat(
config.glob.dist.app.client.scripts.all
config.glob.dist.app.client.views.all
)
spa: config.spa.src.path
spa: [
pathHelp.format config.spa.src.path
]
src: config.glob.src.app.client

# Callbacks
@@ -77,7 +77,7 @@ module.exports = (config) ->

_addReporter: ->
@jasmine.clearReporters() # remove default reporter (needed as of jasmine v2.5.2)

@jasmine.addReporter new Reporter
isVerbose: config.test.verbose.jasmine
showColors: true
@@ -10,16 +10,6 @@ module.exports =
return [] unless cache or cache.length
Object.keys cache

delete: (id) -> # recursively, id = module's absolute path
return unless id
files = require.cache[id]
isCached = !!files
# console.log "cached #{isCached}:".info, id
return unless isCached

children = files.children
if children.length
for file in children
@delete file.id

delete require.cache[id]
delete: (id) ->
Object.keys(require.cache).forEach (id) ->
delete require.cache[id]
@@ -7,6 +7,7 @@ module.exports =
'/common/common-server'
'/server/start-server'
# '/browser/open-browser' # TODO
'/server/stop-server'
]
dev: [
'/common/common'
@@ -2,9 +2,11 @@
# ===========
module.exports = (config, jasmine) ->
return unless config.test.watch
gWatch = require 'gulp-watch'
tests = require("#{config.paths.abs.test.helpers}/tests") config
specs = [ "#{config.paths.abs.test.tests.path}/**/*" ]
path = require 'path'
gulp = require 'gulp'
tests = require("#{config.paths.abs.test.helpers}/tests") config
testsPath = config.paths.abs.test.tests.path.replace /\\/g, '/' # replace for windows
specs = ["#{testsPath}/**/*"]

# helpers
# =======
@@ -16,19 +18,22 @@ module.exports = (config, jasmine) ->
# ====
createWatch = ->
new Promise (resolve, reject) ->
gWatch specs, read:false, (file) ->
event = file.event
test = tests.get.test file.relative
logMsg event, test
return if event is 'unlink'
watcher = gulp.watch specs

watcher.on 'change', (_path) ->
relative = path.relative testsPath, _path
test = tests.get.test relative
logMsg 'change', test
jasmine.init(test).reExecute()
.on 'ready', ->
console.log "watching tests".info
resolve()
.on 'error', (e) ->

watcher.on 'error', (e) ->
console.log "test watcher error".error
reject e.message

watcher.on 'ready', ->
console.log "watching tests".info
resolve()

# return
# ======
createWatch
@@ -0,0 +1,21 @@
# test results: stop-server
# =========================
task = 'stop-server'
request = require 'request'
config = require "#{process.cwd()}/extra/temp/config.json"
tests = require("#{config.paths.abs.test.helpers}/tests") config

# tests
# =====
describe task, ->
port = undefined
url = undefined

beforeAll ->
port = tests.get.app.config().ports.server
url = "http://localhost:#{port}/"

it 'should stop the server', (done) ->
request url, (e, res, body) ->
expect(e.code.toLowerCase()).toContain 'econnrefused'
done()
@@ -0,0 +1,12 @@
# test task: stop-server
# ======================
task = 'stop-server'
config = require "#{process.cwd()}/extra/temp/config.json"
tests = require("#{config.paths.abs.test.helpers}/tests") config
opts = track: true

# tests
# =====
describe task, ->
tests.test.task.async task, 'server stopped', opts
tests.test.results "/server/#{task}"

0 comments on commit c056c22

Please sign in to comment.
You can’t perform that action at this time.