1 | var fs = require('fs')
|
2 | var glob = require('glob')
|
3 | var path = require('path')
|
4 |
|
5 |
|
6 | var exports = {
|
7 | intersect : intersect,
|
8 | openModificationsRecord : openModificationsRecord,
|
9 | closeModificationsRecord : closeModificationsRecord,
|
10 | fileIsNewer : fileIsNewer,
|
11 | fileNeedsUpdating : fileNeedsUpdating,
|
12 | getLastModifiedTime : getLastModifiedTime,
|
13 | fileIsDirectory : fileIsDirectory,
|
14 | fileExists : fileExists,
|
15 | getDestPath : getDestPath,
|
16 | mkdirIfNotExist : mkdirIfNotExist
|
17 | }
|
18 |
|
19 | module.exports = function(deps){
|
20 | if(deps){
|
21 | fs = deps.fs
|
22 | glob = deps.glob
|
23 | path = deps.path
|
24 | }
|
25 | return exports
|
26 | }
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | function intersect(all, exclusions) {
|
32 |
|
33 |
|
34 | var allFiles = []
|
35 | if(Array.isArray(all)) {
|
36 | for(var i in all) {
|
37 | var files = glob.sync(all[i])
|
38 | for(var f in files) {
|
39 | allFiles.push(files[f])
|
40 | }
|
41 | }
|
42 | }
|
43 | else {
|
44 | allFiles = glob.sync(all)
|
45 | }
|
46 |
|
47 | var exclude = []
|
48 |
|
49 | exclusions.forEach(function(exclusion, index, array){
|
50 | var files = glob.sync(exclusion)
|
51 |
|
52 | if(exclusion.match(/\/?\*[^\/]*$/)) {
|
53 | files.push(exclusion.replace(/\/?\*[^\/]*$/, ''))
|
54 | }
|
55 |
|
56 | files.forEach(function(file, index, array){
|
57 | var matchIndex = allFiles.indexOf(file)
|
58 | if(matchIndex !== -1) {
|
59 | allFiles.splice(matchIndex, 1)
|
60 | }
|
61 | })
|
62 | })
|
63 | return allFiles
|
64 | }
|
65 |
|
66 | function openModificationsRecord() {
|
67 | if(exports.fileExists(path.dirname(__filename) + '/../modificationsRecord.json')) return require(path.dirname(__filename) + '/../modificationsRecord.json')
|
68 | else return {}
|
69 | }
|
70 |
|
71 | function closeModificationsRecord(record) {
|
72 | var output = JSON.stringify(record)
|
73 | fs.writeFileSync(path.dirname(__filename) + '/../modificationsRecord.json', output)
|
74 | }
|
75 |
|
76 |
|
77 |
|
78 | function fileIsNewer(filepath, compareTo) {
|
79 |
|
80 | var lastModified = exports.getLastModifiedTime(filepath)
|
81 |
|
82 | if(compareTo > lastModified) {
|
83 | return false
|
84 | }
|
85 | else {
|
86 | return true
|
87 | }
|
88 | }
|
89 |
|
90 | function fileNeedsUpdating(record, fileSrc, fileDest) {
|
91 |
|
92 |
|
93 |
|
94 | var lastModified = exports.getLastModifiedTime(fileSrc)
|
95 | var destExists = exports.fileExists(fileDest)
|
96 |
|
97 | if(!destExists) {
|
98 | record[fileSrc+ '###' + fileDest] = lastModified
|
99 | return true
|
100 | }
|
101 |
|
102 | if(record[fileSrc+ '###' + fileDest]) {
|
103 | if(record[fileSrc+ '###' + fileDest] < lastModified) {
|
104 | record[fileSrc+ '###' + fileDest] = lastModified
|
105 | return true
|
106 | }
|
107 | else {
|
108 | return false
|
109 | }
|
110 | }
|
111 | else {
|
112 | record[fileSrc+ '###' + fileDest] = lastModified
|
113 | return true
|
114 | }
|
115 | }
|
116 |
|
117 |
|
118 | function getLastModifiedTime(filepath) {
|
119 | return fs.statSync(filepath).mtime.getTime()
|
120 | }
|
121 |
|
122 | function fileIsDirectory(filepath) {
|
123 | return fs.statSync(filepath).isDirectory()
|
124 | }
|
125 |
|
126 | function getDestPath(file, basename, destDir) {
|
127 | var relativePath = path.relative(basename, file)
|
128 | if(destDir !== "") destDir += "/"
|
129 | var output = destDir + relativePath
|
130 | return output.replace(/\\/g, '/')
|
131 | }
|
132 |
|
133 | function mkdirIfNotExist(file) {
|
134 |
|
135 | var base = process.cwd()
|
136 | if(base !== "") base += '/'
|
137 | file = base + file
|
138 | file = file.replace(/\\/g, '/')
|
139 | file = file.replace(/\/$/g, '')
|
140 |
|
141 | if(path.extname(file).length > 0) {
|
142 | file = file.replace(/\/?[^\/]+$/, '')
|
143 | }
|
144 |
|
145 | var dirsToMake = []
|
146 |
|
147 | while(file.length > 0 && !exports.fileExists(file)) {
|
148 | var dirToMake = file.match(/\/?([^\/]+)$/)[1]
|
149 | dirsToMake.unshift(dirToMake)
|
150 | file = file.replace(/\/?[^\/]+$/, '')
|
151 | }
|
152 |
|
153 | for(var i in dirsToMake) {
|
154 | file += "/" + dirsToMake[i]
|
155 | fs.mkdirSync(file)
|
156 | }
|
157 |
|
158 | }
|
159 |
|
160 |
|
161 | function fileExists(filepath) {
|
162 |
|
163 | try {
|
164 | fs.accessSync(filepath, fs.F_OK);
|
165 | } catch (e) {
|
166 | return false
|
167 | }
|
168 | return true
|
169 | }
|
170 |
|