-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
103 lines (85 loc) · 2.62 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
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
"use strict";
var fs = require('nano-fs'),
Promise = require('nano-promise'),
Path = require('path');
module.exports = {
load: function (log, data) {
var opts = data.opts,
src = Path.join(data.sources_folder || opts.sources_folder, data.name);
return fs.readFile(src, data.encoding = 'utf8')
.then(function (text) {
data.content = text;
});
},
'load-bin': function (log, data) {
var opts = data.opts,
src = Path.join(data.sources_folder || opts.sources_folder, data.name);
return fs.readFile(src, data.encoding = null)
.then(function (text) {
data.content = text;
});
},
'load-json': function (log, data) {
var opts = data.opts,
src = Path.join(data.sources_folder || opts.sources_folder, data.name);
return fs.readFile(src, 'utf8')
.then(function (text) {
data.encoding = 'json';
data.content = JSON.parse(text);
});
},
'dont-overwrite': function (log, data) {
var opts = data.opts,
dest = Path.join(data.dist_folder || opts.dist_folder, data.dest || data.name);
return fs.stat(dest).then(function (stat) {
throw Promise.CANCEL_REASON;
}, function (err) {
/* istanbul ignore if */
if (err.code !== 'ENOENT')
throw err;
});
},
'load-dist': function (log, data) {
var opts = data.opts,
src = Path.join(data.dist_folder || opts.dist_folder, data.name);
return fs.readFile(src, data.encoding = 'utf8')
.then(function (text) {
data.content = text;
});
},
rename: function sync(log, data) {
var dest = data.dest || data.name;
if (dest.indexOf('\\') >= 0)
dest = data.name.replace(/^(.*\/)?([^/]+)(\.[a-z0-9_]+)$/, dest.replace(/\\/g, '$'));
data.dest = dest;
},
save: function (log, data) {
var opts = data.opts,
dest = Path.join(data.dist_folder || data.opts.dist_folder, data.dest || data.name);
if (data.encoding === 'utf8' && typeof data.content !== 'string')
throw Error('data content is not a string');
return fs.mkpath(Path.dirname(dest))
/* .catch(function (err) {
if (err.code !== 'EEXIST')
throw err;
})*/
.then(function () {
switch (data.encoding) {
case 'utf8':
case null:
return fs.writeFile(dest, data.content, { encoding: data.encoding });
case 'json':
var text = require('nano-json').render(data.content, { });
return fs.writeFile(dest, text, { encoding: 'utf8' });
default:
throw Error('unknown data encoding: '+require('nano-json').js2str(data.encoding)+'');
}
});
},
copy: function (job, data, done) {
var opts = data.opts,
src = Path.join(data.sources_folder || opts.sources_folder, data.name),
dst = Path.join(data.dist_folder || opts.dist_folder, data.name);
return fs.copy(src, dst);
}
};