-
Notifications
You must be signed in to change notification settings - Fork 21
/
localsource.js
272 lines (237 loc) · 7.38 KB
/
localsource.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
;(function() {
'use strict';
var gui = require('nw.gui');
var fs = require('fs');
var path = require('path');
var thunkify = require('thunkify');
var co = require('co');
var cofs = require('co-fs');
var shortId = require('shortid');
var fsExtra = require('fs-extra');
var cocopy = thunkify(fsExtra.copy);
var mkdirp = require('mkdirp');
var config = {};
var data = {};
var setLoaded = function(obj) {
config = {type: 'local', scriptPath: obj.scriptPath};
data = {script: obj.script, dataPath: obj.dataPath};
console.log('setLoaded');
};
var getSource = function() {
return {config: config, data: data};
};
var getFullDataPath = function() {
return path.dirname(config.scriptPath) + '/' + data.dataPath;
};
var settings = function() {
return {
AUTO_UPLOAD_LAYER_TIME: 4,
AUTO_UPLOAD_FLAT_TIME: 5
}
};
// generate a unique name for and make a data directory
var makeDataPath = function *(basepath, name) {
var alt = '';
while (true) {
var path = basepath + '/' + name + alt + '_data';
if (yield cofs.exists(path)) {
alt = '_' + shortId.generate();
}
else {
yield cofs.mkdir(path);
yield cofs.mkdir(path + '/images');
return './' + name + alt + '_data';
}
}
};
/**
* Create a new script.
*
* pass a callback function
* callback is called with:
* - TODO
*/
var create = function(next) {
co(function *() {
var name = shortId.generate();
var path = autosavesPath();
var dataPath = yield makeDataPath(path, name);
var script = example.fountainText(dataPath);
var scriptPath = path + '/' + name + '.fountain';
// write the script file
yield cofs.writeFile(scriptPath, script, 'utf8');
// ok, we've successfully created it, set the module vars
setLoaded({scriptPath: scriptPath, dataPath: dataPath, script: script});
return getSource();
})(next);
};
/**
* Open an existing script.
*
* config: {type: 'local', scriptPath: path to script file}
* next expects (err, {config, data})
*/
var load = function(config, next) {
co(function *() {
if (config.type != 'local' || !config.scriptPath) return next('invalid config');
var scriptPath = config.scriptPath;
var script = (yield cofs.readFile(scriptPath)).toString();
setLoaded({script: script, scriptPath: scriptPath});
return getSource();
})(next);
};
var imageUrl = function(name) {
return 'file://' + getFullDataPath() + '/images/' + name;
};
var saveScript = function(script, next) {
data.script = script;
fs.writeFile(config.scriptPath, script, next);
};
var saveImage = function(filename, imageData, contentType) {
var dfd = new $.Deferred();
if (data.dataPath) {
imageData = imageData.replace(/^data:image\/\w+;base64,/, '');
var file = getFullDataPath() + '/images/' + filename;
fs.writeFile(file, imageData, 'base64', function(err) {
if (err) dfd.reject(); else dfd.resolve();
});
}
else {
dfd.reject();
}
return dfd.promise();
};
var saveAs = function(toPath, next) {
co(function *() {
if (!data.dataPath) {
return new Error('invalid data directory');
}
if (toPath == config.scriptPath) {
// don't need to move
return getSource();
}
var src, dst;
// copy the script file over
src = config.scriptPath;
console.log('src', src);
var fileExists = yield cofs.exists(toPath);
var dirExists = yield cofs.exists(path.dirname(toPath));
var dirIsDirectory = (yield cofs.stat(path.dirname(toPath))).isDirectory();
if (fileExists) {
var stat = yield cofs.stat(toPath);
if (stat.isDirectory()) {
dst = toPath + '/' + path.basename(src);
}
else if (stat.isFile()) {
dst = toPath;
}
else {
console.log('invalid destination1');
throw new Error('invalid destination');
}
}
else if (dirExists && dirIsDirectory) {
dst = toPath;
}
else {
console.log('invalid destination');
throw new Error('invalid destination');
}
console.log('copying script: ', src, dst);
yield cocopy(src, dst);
var scriptPath = dst;
// copy the data dir over
src = getFullDataPath();
dst = path.dirname(scriptPath) + '/' + path.basename(src);
console.log('copying data: ', src, dst);
if (path.normalize(src) != path.normalize(dst)) {
yield cocopy(src, dst);
}
setLoaded({script: data.script, scriptPath: scriptPath, dataPath: data.dataPath});
return getSource();
})(next);
}
// assume they haven't saved if we are saving to the autosave directory
var hasSaved = function() {
return path.dirname(config.scriptPath) != autosavesPath();
};
var getDataPath = function() {
return data.dataPath;
};
var getScriptpath = function() {
return path.dirname(config.scriptPath);
};
var setDataPath = function(fullpath, isAbsolute, ensureExists) {
if (isAbsolute) {
data.dataPath = path.relative(path.dirname(config.scriptPath), fullpath);
}
else {
data.dataPath = fullpath;
}
if (ensureExists) {
// make the images dir (and therefore the data dir)
mkdirp(getFullDataPath() + '/images');
}
console.log('setDataPath', fullpath, data.dataPath);
};
var checkDataPath = function(next) {
co(function *() {
var dataPath = getFullDataPath() + '/images';
return (
(yield cofs.exists(dataPath)) &&
(yield cofs.stat(dataPath)).isDirectory()
);
})(next);
};
var tmp = require('tmp');
var co_tmpName = thunkify(tmp.tmpName);
var childProcess = require('child_process');
var co_exec = thunkify(childProcess.exec);
var fixPath = function(_path) {
return _path.replace(/(\s)/g, "\\ ");
};
var createPlayerZip = function(next) {
co(function *() {
var tmpPath = yield co_tmpName({prefix: 'files-', postfix: '.zip'});
var _tmpPath = fixPath(tmpPath);
console.log('tmp name created', tmpPath);
var scriptPathDir = fixPath(path.dirname(config.scriptPath));
var scriptPathName = fixPath(path.basename(config.scriptPath));
var cmd = "cd " + scriptPathDir + " && zip -r -X " + _tmpPath + " " + scriptPathName;
console.log('zipping', cmd);
yield co_exec(cmd);
var dataPath = fixPath(getFullDataPath());
var cmd = "cd " + dataPath + " && zip -r -X " + _tmpPath + " images/*-large.jpeg";
console.log('zipping', cmd);
yield co_exec(cmd);
return tmpPath;
})(next);
};
var localSource = window.localSource = {
settings: settings,
create: create,
load: load,
saveAs: saveAs,
imageUrl: imageUrl,
saveScript: saveScript,
saveImage: saveImage,
hasSaved: hasSaved,
getDataPath: getDataPath,
getFullDataPath: getFullDataPath,
setDataPath: setDataPath,
checkDataPath: checkDataPath,
getSource: getSource,
getScriptpath: getScriptpath,
createPlayerZip: createPlayerZip
};
function autosavesPath() {
return gui.App.dataPath + '/Autosaves';
}
function checkAutosavesPath() {
var path = autosavesPath();
fs.exists(path, function(exists) {
if (!exists) fs.mkdir(path);
});
}
checkAutosavesPath();
}).call(this);