UNPKG

1.37 kBtext/coffeescriptView Raw
1fs = require "fs"
2
3watchr = require "watchr"
4
5class FileLoader
6 constructor: (assetsModule, @log, skipHidden) ->
7 @assets = assetsModule.instance
8 @assetJS = @assets.options.helperContext.js
9 @jsFilesRoot = @assets.options.src + "/js"
10
11 loadFiles: ->
12 @_loadJSFileOrDirectory @jsFilesRoot
13
14 _loadJSDirectory: (dirPath) ->
15 paths = fs.readdirSync dirPath
16
17 @_loadJSFileOrDirectory "#{dirPath}/#{path}" for path in paths
18 true
19
20 _loadJSFileOrDirectory: (path) ->
21 stat = fs.statSync path
22 if stat?.isDirectory()
23 @_loadJSDirectory path
24 else
25 assetName = (((path.replace @jsFilesRoot, "").replace ".coffee", "").replace ".js", "").slice 1
26
27 # Skip if a hidden file
28 return if "." == assetName.split("/")[assetName.split("/").length - 1][0]
29
30 @log?("Assetizing #{assetName}")
31 @assetJS assetName
32
33 watchFiles: (fileChangedCallback, done) ->
34
35 watchOptions =
36 # Watch our js root
37 path: @jsFilesRoot
38
39 # When a file is changed, run this
40 listener: (evt, filePath, fileStat, filePrevStat) =>
41 @_loadJSFileOrDirectory filePath
42 fileChangedCallback?(null, filePath)
43
44 # Wait til we're ready before continuing
45 next: (err, watchr) ->
46 done(err, watchr)
47
48 # Let's light this candle
49 watchr.watch watchOptions
50
51module.exports = FileLoader