-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathflows.js
158 lines (117 loc) · 3.43 KB
/
flows.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
var tokenEntry = require ('dataflo.ws/initiator/token');
var EventEmitter = require ('events').EventEmitter;
var util = require ('util');
var path = require ('path');
var ayepromise = require ('ayepromise');
var common = require ("./common");
var flowJSON = require ("./flows.json");
function CuwireFlows () {
var self = this;
this.entry = new tokenEntry ({
flows: flowJSON.flows
});
this.flowTemplates = flowJSON.templates;
}
util.inherits (CuwireFlows, EventEmitter);
var cuwireFlows = new CuwireFlows ();
module.exports = cuwireFlows;
CuwireFlows.prototype.log = function () {
if (arguments[0] === 'error') {
console.error.apply (console, arguments);
}
}
CuwireFlows.prototype.launch = function (token, data, cb, errb) {
var deferred = ayepromise.defer();
var df = this.entry.process (token, {
autoRun: 0,
data: data || {},
templates: this.flowTemplates,
//_log: this.log.bind (this)
});
df.on ('completed', function (df) {
deferred.resolve (df.data);
cb && cb(df.data);
}.bind(this));
df.on ('failed', function (df) {
deferred.reject (df.data);
errb && errb (df.data);
});
df.run();
var promise = deferred.promise;
promise.flow = df;
return promise;
}
cuwireFlows.unzip = function (zipFile, targetFolder, options) {
var deferred = ayepromise.defer();
options = options || {};
var yauzl = require("yauzl");
var fs = require("fs");
var archiveFiles = [], archiveFolders = [];
var status = 'resolve';
yauzl.open (zipFile, function(err, zipfile) {
if (err) throw err;
var remains = 0;
zipfile.on ("entry", function(entry) {
if (/\/$/.test(entry.fileName)) {
// directory file names end with '/'
archiveFolders.push (entry.fileName);
return;
}
archiveFiles.push (entry);
zipfile.openReadStream (entry, function(err, readStream) {
remains++;
// console.log (targetFolder, entry.fileName);
var entryFile = path.join (targetFolder, entry.fileName);
if (options.replacePath) {
entryFile = entryFile.replace (options.replacePath[0], options.replacePath[1])
}
var entryFolder = path.dirname (entryFile);
common.mkdirParent (
entryFolder,
function () {
var writeStream = fs.createWriteStream (entryFile);
writeStream.on ('error', function (error) {
remains --;
status = 'reject';
console.log (error);
if (!remains) {
deferred[status] ();
}
});
readStream.on ('end', function () {
remains --;
if (!remains) {
deferred[status] ();
}
})
readStream.pipe (writeStream);
}
);
});
});
// archive read complete
zipfile.on ('end', function () {
});
});
return deferred.promise;
}
var zipData = {
url: "https://github.com/cuwire/RFduino/archive/master.zip",
folder: "/Users/apla/tmp/rfd"
};
//var promiseZip = cuwireFlows.launch ("installFromZip", zipData);
var arduinoSiteData = {
libraries: "http://downloads.arduino.cc/libraries/library_index.json",
packages: "http://downloads.arduino.cc/packages/package_index.json",
};
var promiseAData = cuwireFlows.launch ("arduinoSiteData", arduinoSiteData);
promiseAData.then (function () {
var flowData = promiseAData.flow.data;
console.log (JSON.parse (flowData.libraries.data));
console.log (JSON.parse (flowData.packages.data));
});
var gitData = {
url: "https://github.com/cuwire/RFduino",
folder: "/Users/apla/tmp/rfd"
};
//var promise = cuwireFlows.launch ("installFromGit", gitData);