-
Notifications
You must be signed in to change notification settings - Fork 35
/
index.js
68 lines (56 loc) · 1.56 KB
/
index.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
var os = require('os')
var path = require('path')
var fs = require('fs-extra')
var mkdirp = require('mkdirp')
var guid = require('guid').raw
var o = require('octal')
var mv = require('mv')
var walk = require('walk').walk
module.exports = function (outDir, tmpDir) {
outDir = outDir || 'ice-box'
var tmpDirHead = guid()
tmpDir = path.join(tmpDir || path.join(os.tmpdir(), 'ice-box'), tmpDirHead)
return function (work, finish) {
mkdirp.sync(tmpDir)
work(path.resolve(tmpDir), function (err) {
if (err) {
// clean up!
return fs.remove(tmpDir, function (err2) {
if (err2) return finish(err2)
return finish(err)
})
}
// Copy tmpdir to outdir
var outFull = path.join(outDir, tmpDirHead)
fs.mkdirs(outDir, function (err) {
if (err) return finish(err)
mv(tmpDir, outFull, function (err) {
if (err) return finish(err)
// Set outdir as read-only
recursiveChmod(outFull, function (err) {
if (err) return finish(err)
finish(err, path.resolve(outFull))
})
})
})
})
}
}
function recursiveChmod (dir, done) {
var walker = walk(dir)
walker.on('directory', function (root, dirStatsArray, next) {
fs.chmodSync(root, o(755))
next()
})
walker.on('file', function (root, fileStats, next) {
var _path = path.join(root, fileStats.name)
fs.chmodSync(_path, o(555))
next()
})
walker.on('errors', function (err) {
done(err)
})
walker.on('end', function () {
done()
})
}