forked from mockee/istatic
-
Notifications
You must be signed in to change notification settings - Fork 1
/
istatic.js
379 lines (316 loc) · 8.89 KB
/
istatic.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/usr/bin/env node
/*jshint node:true loopfunc:true*/
var fs = require('fs')
, util = require('util')
, path = require('path')
, mkdirp = require('mkdirp')
, difflib = require('difflib')
, cp = require('child_process')
, eventMaster = require('./lib/event')
, slice = [].slice
, keys = Object.keys
, logger = Object.create(console)
, PATH_STATIC = path.resolve('.statictmp/')
, basename = path.basename
, extname = path.extname
, availableFiles = {}
, cwd = process.cwd()
, sep = path.sep
, defaultHostDict = {
github: 'git://github.com/'
, bitbucket: 'git://bitbucket.org/'
, local: ''
}
cp.exec = (function(fn) {
var self = this, seq = 0
return function(command) {
var promise = new eventMaster.Promise()
fn.call(self, command, function(err) {
promise[err ? 'reject' : 'resolve'](arguments)
})
return promise
}
})(cp.exec.bind(cp))
logger.istatic = function() {
var args = slice.call(arguments).join(' ')
return logger.info('istatic', args)
}
function extend(obj) {
slice.call(arguments, 1)
.forEach(function(source) {
for (var prop in source) {
obj[prop] = source[prop]
}
})
return obj
}
function colour(color) {
var colors = {
red: '\033[31m'
, cyan: '\033[36m'
, grey: '\033[90m'
, white: '\033[37m'
, yellow: '\033[33m'
}
return [colors[color]
, slice.call(arguments, 1).join(' ')
, '\033[0m'].join('')
}
function makeTempGitReposDir() {
fs.existsSync(PATH_STATIC, function(exists) {
if (exists) { return }
fs.mkdir(PATH_STATIC)
})
}
function getGitPath(url) {
var lastItem = url.split(sep).pop()
, isLocal = lastItem.indexOf('.git') === -1
, repoName = isLocal ? lastItem
: basename(url.trim(), '.git')
return path.resolve(PATH_STATIC, repoName)
}
function shortenName(repoName) {
repoName = /\//g.test(repoName)
? repoName.split('/').pop() : repoName
return repoName
}
function normalizeName(name){
return name.trim().replace(/^\//, './');
}
function getConfigFile(filename) {
var promise = new eventMaster.Promise()
fs.readFile(filename, 'utf8', function(err, data) {
if (!err) {
// Convert yaml to json
var yaml = require('js-yaml')
, json = yaml.load(data)
promise.resolve([json])
} else {
var errInfo = "There's no `" + filename
+ "` in your app root directory."
promise.reject([errInfo])
}
})
return promise
}
function outputs(err, stdout, stderr) {
logger.info(stdout.split('\n')[0])
}
function filterFiles(src) {
var fileType = extname(src).slice(1)
, allowedFileTypes = [
'html', 'jade', 'js', 'coffee'
, 'css', 'styl', 'sass', 'scss'
, 'png', 'jpg', 'jpeg', 'gif'
]
return fs.statSync(src).isFile()
? allowedFileTypes.indexOf(fileType) !== -1
: !/^\./.test(src.split('/').pop())
}
function copy(src, dst, cb) {
function copyHelper(err) {
var is, os
fs.stat(src, function (err, stat) {
if (err) { return cb(err) }
is = fs.createReadStream(src)
os = fs.createWriteStream(dst)
is.pipe(os)
is.on('end', function() {
fs.utimes(dst, stat.atime, stat.mtime, cb)
}).on('error', function(err) {
if (err) { return cb(err) }
})
})
}
cb = cb || function(){}
fs.stat(dst, copyHelper)
}
function copyFile(src, dst) {
var pathArr = dst.split(sep)
, isLegalType = filterFiles(src)
if (!isLegalType) { return }
if (!fs.existsSync(dst)) {
mkdirp.sync(!pathArr.slice(-1)[0]
? dst : pathArr.slice(0, -1).join(sep))
}
if (fs.statSync(src).isFile()) {
var dstIsDir = dst.substr(-1) === sep
, dstFile = dstIsDir ? dst
+ src.split(sep).pop() : dst
if (!(src in availableFiles)
&& fs.existsSync(dstFile)
&& localModified(src, dstFile)) {
return logger.info(
colour('red', ' IGNORED (file)', dstFile))
}
copy(src, dstFile, function(err) {
if (err) { return }
logger.info(' \033[90m'
, src.replace(path.resolve(cwd), '').replace(/^\//, '')
, '\033[0m->\033[90m', dst, '\033[0m')
})
} else if (fs.statSync(src).isDirectory()) {
fs.readdirSync(src).forEach(function(name) {
var srcPath = [src.replace(/\/$/g, ''), name].join(sep)
, isDir = fs.statSync(srcPath).isDirectory()
copyFile(srcPath, isDir ? dst + name + sep : dst)
})
}
}
function diffFile(src, dst) {
var sep = path.sep
, pathArr = dst.split(sep)
, isLegalType = filterFiles(src)
, noDstFile = fs.existsSync(dst)
if (!isLegalType || !noDstFile) { return }
if (fs.statSync(src).isFile()) {
var dstIsDir = dst.substr(-1) === sep
, dstFile = dstIsDir ? dst
+ src.split(sep).slice(-1)[0] : dst
if (!localModified(src, dstFile)) {
availableFiles[src] = dstFile
}
} else if (fs.statSync(src).isDirectory()) {
fs.readdirSync(src).forEach(function(name) {
var srcPath = [src, name].join(sep)
, isDir = fs.statSync(srcPath).isDirectory()
diffFile(srcPath, isDir ? dst + name + sep : dst)
})
}
}
function outputFilesNum(files) {
var dirNum = 0, fileNum = 0
function combinText(typeNum) {
var type, num, t, text = ''
for (t in typeNum) {
num = typeNum[t]
text += (!num ? '' : (!text ? '' : ' and ')
+ [num, num > 1 ? t + 's' : t].join(' '))
}
return text
}
keys(files).forEach(function(n) {
if (!!extname(n)) { fileNum++ }
else { dirNum++ }
})
return combinText({
file: fileNum, dir: dirNum
})
}
function copy2app(repoName, files) {
repoName = shortenName(repoName)
logger.istatic(colour('cyan', 'copying')
, colour('white', outputFilesNum(files)))
for (var src in files) {
copyFile(path.resolve(PATH_STATIC, repoName, normalizeName(src))
, normalizeName(files[src]))
}
}
function clone(url, path) {
var cmd = ['git clone', url, path].join(' ')
return cp.exec(cmd).done(function(a) {
logger.istatic(colour('cyan', 'cloning')
, colour('yellow', url))
})
}
function fetch(name) {
name = shortenName(name)
process.chdir(path.resolve(PATH_STATIC, name))
return cp.exec('git fetch --all')
.done(function(err, stdout) {
process.chdir(cwd)
})
}
function reset(name, commit) {
name = shortenName(name)
commit = commit || 'origin/master'
process.chdir(path.resolve(PATH_STATIC, name))
return cp.exec('git reset --hard ' + commit)
.done(function() {
logger.istatic(colour('cyan', 'pulling')
, colour('yellow', name, '(' + commit + ')'))
})
}
// Get modified time that used for compare files
function getMtime(file) {
return (new Date(fs.statSync(file).mtime)).getTime()
}
function localModified(src, dst) {
if (!fs.existsSync(dst)) {
return false;
}
var srcStr = fs.readFileSync(src, 'UTF-8')
, dstStr = fs.readFileSync(dst, 'UTF-8')
, diffRatio = (new difflib
.SequenceMatcher(null, srcStr, dstStr))
.quickRatio()
return diffRatio !== 1
}
function pullAction(config) {
var commit, files
, repos = config.repos
, repo, repoUrl, repoPath
, customHostDict = config.hostDict
, defaultHost = config.host || 'github'
, hostDict = extend(defaultHostDict, customHostDict)
function generateRepoUrl(host, name) {
return [hostDict[host], name, '.git'].join('')
}
function copyAfterReset(name) {
repo = repos[name]
commit = repo.tag || repo.commit
return reset(name, commit).done(function() {
process.chdir(cwd)
copy2app(name, repos[name].file)
})
}
function hasCloned(repo) {
process.chdir(cwd)
return fs.existsSync(repo)
}
var promises = []
for (var name in repos) {
(function(name) {
repo = repos[name]
repoUrl = 'host' in repo
? repo.host === 'local'
? name : generateRepoUrl(repo.host, name)
: generateRepoUrl(defaultHost, name)
repoPath = getGitPath(repoUrl)
if (hasCloned(repoPath)) {
files = repos[name].file
for (var src in files) {
diffFile(path.resolve(PATH_STATIC, shortenName(name),
normalizeName(src)), normalizeName(files[src]))
}
promises.push(fetch(name).done(function() {
return copyAfterReset(name)
}).follow())
} else {
promises.push(clone(repoUrl, repoPath).done(function() {
return copyAfterReset(name)
}).follow())
}
})(name)
}
return eventMaster.when.apply(this, promises)
}
function pull(config, cb) {
makeTempGitReposDir()
if (config) { return pullAction(config).then(cb) }
getConfigFile('static.yaml')
.done(pullAction)
.fail(function(err) {
logger.error(err)
}).follow().then(cb)
}
function clear(name) {
var rimraf = require('rimraf')
rimraf(PATH_STATIC, function(err) {
if (!err) { return }
console.error(err)
})
}
exports.pull = pull
exports.clear = clear
exports.version = '0.3.1'