1 | fs = require "fs"
|
2 |
|
3 | watchr = require "watchr"
|
4 |
|
5 | class 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 |
|
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 |
|
37 | path: @jsFilesRoot
|
38 |
|
39 |
|
40 | listener: (evt, filePath, fileStat, filePrevStat) =>
|
41 | @_loadJSFileOrDirectory filePath
|
42 | fileChangedCallback?(null, filePath)
|
43 |
|
44 |
|
45 | next: (err, watchr) ->
|
46 | done(err, watchr)
|
47 |
|
48 |
|
49 | watchr.watch watchOptions
|
50 |
|
51 | module.exports = FileLoader
|