-
Notifications
You must be signed in to change notification settings - Fork 1
/
effects.js
141 lines (111 loc) · 3.37 KB
/
effects.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
var fs = require('fs');
var path = require('path');
var gift = require('gift');
var funkit = require('funkit');
var partial = funkit.partial;
var zfill = funkit.string.zfill;
var filewalker = require('filewalker');
var conf = require('./conf.json');
var repo = gift(conf.repoPath);
var effectPath = partial(getPath, effectsPath(), 'js');
var effectMetaPath = partial(getPath, effectsMetaPath(), 'json');
var commitEffect = partial(commit, 'effects', effectPath, 'js');
var commitEffectMeta = partial(commit, 'effects_meta', effectMetaPath, 'json');
exports.commitEffect = commitEffect;
exports.commitEffectMeta = commitEffectMeta;
function commit(pathPrefix, pathFn, ext, msg, id, data, cb) {
var p = pathFn(id);
fs.writeFile(p, data, 'utf8', function(err, d) {
if(err) return cb(err);
repo.add(path.join(pathPrefix, id + '.' + ext), function(err) {
if(err) {
console.warn('add failed', err);
return cb(err);
}
repo.commit(msg, {
all: true // XXXXX: sketchy, better commit just specific files
}, function(err) {
if(err) {
console.warn('commit failed', err);
return cb(err);
}
cb();
});
});
});
}
function create(o, cb) {
var name = o.name || 'untitled';
var id;
count(effectsPath(), function(err, count) {
if(err) return console.error(err);
id = zfill(5, count + 1) + '_' + name;
commitEffect('New effect', id, o.code, function(err, d) {
if(err) return cb(err);
commitEffectMeta('New effect', id, JSON.stringify({
name: name,
author: o.author,
description: '',
parent: o.parent
}), function(err, d) {
if(err) return cb(err);
cb(null, {
id: id
});
});
});
});
}
exports.create = create;
function count(p, cb) {
fs.readdir(p, function(err, files) {
if(err) return console.error(err);
cb(null, files.length);
});
}
exports.getAll = partial(walk, effectsMetaPath(), function(f, p) {
var d = require(path.join(p, f));
d.id = path.basename(f, '.json');
return d;
});
exports.getAllMeta = partial(walk, effectsPath(), function(f) {
return path.basename(f, '.js');
});
function walk(p, fileCb, cb) {
var data = [];
// doesn't work with ../../ ???
filewalker(p).
on('file', function(f) {
data.push(fileCb(f, p));
}).
on('done', function() {
cb(null, data);
}).
on('error', function(err) {
cb(err, data);
}).walk();
}
function read(id, cb) {
fs.readFile(effectPath(id), 'utf8', cb);
}
exports.read = read;
function getMeta(id, cb) {
try {
var d = require(effectMetaPath(id));
cb(null, d);
} catch(e) {
cb(e);
}
}
exports.getMeta = getMeta;
function getPath(p, ext, id) {
// avoid exposing FS (perhaps there's a neater way?)
if(id.indexOf('/') >= 0 || id.indexOf('\\') >= 0) return;
return path.join(p, id + '.' + ext);
}
function effectsPath() {
return path.join(conf.repoPath, 'effects');
}
function effectsMetaPath() {
return path.join(conf.repoPath, 'effects_meta');
}