This repository has been archived by the owner on Mar 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgithub-examples.js
190 lines (156 loc) · 5.69 KB
/
github-examples.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
var path = require("path");
var ipfsd = require("ipfsd-ctl");
var IPFSHost = require("../hosts/ipfshost");
var MemoryRegistry = require("../registries/memoryregistry");
var fs = require("fs-extra");
var request = require("request");
var parallel = require("async/parallel");
var series = require("async/series");
var whilst = require("async/whilst");
var eachSeries = require("async/eachSeries");
// These examples are ordered such that any package that depends on another in this list
// will come after the one it depends on (i.e., transferable depends on owned). We do this
// because we edit the lockfiles that contain deployed packages, which means we have to
// update dependency's build_dependencies to reference the new lockfiles.
var examples = [
"owned",
"transferable",
"standard-token",
"safe-math-lib",
"piper-coin",
"escrow",
"wallet"
];
module.exports = {
initialize: function(options, callback) {
var self = this;
if (typeof options == "function") {
callback = options;
options = {};
}
var lockfiles = {};
var sourcefiles = {};
var ipfs_daemon;
var ipfs_api;
var host;
var registry;
var lockfile_uris = {};
series([
// Download all lockfiles
function(c) {
var lockfiles_requests = {};
examples.forEach(function(name) {
lockfiles_requests[name] = self.get.bind(self, "/" + name + "/1.0.0.json");
});
parallel(lockfiles_requests, function(err, results) {
if (err) return c(err);
Object.keys(results).forEach(function(package_name) {
lockfiles[package_name] = JSON.parse(results[package_name]);
});
c();
});
},
// Download all source files
function(c) {
var sourcefile_requests = {};
Object.keys(lockfiles).forEach(function(package_name) {
var lockfile = lockfiles[package_name];
Object.keys(lockfile.sources || {}).forEach(function(file_path) {
file_path = "/" + package_name + "/" + file_path;
sourcefile_requests[file_path] = self.get.bind(self, file_path);
});
});
parallel(sourcefile_requests, function(err, results) {
if (err) return c(err);
sourcefiles = results;
c();
});
},
// Create host and registry
function(c) {
ipfsd.disposable(function (err, node) {
if (err) return c(err);
ipfs_daemon = node;
node.startDaemon(function(err, ipfs) {
ipfs_api = ipfs;
host = new IPFSHost(ipfs_api.apiHost, ipfs_api.apiPort);
registry = new MemoryRegistry();
c();
});
});
},
// Put all source files on host
function(c) {
var puts = [];
Object.keys(sourcefiles).forEach(function(sourcefile_path) {
puts.push(host.putContents(sourcefiles[sourcefile_path]));
});
Promise.all(puts).then(function(results) {
c();
}).catch(c);
},
// Put all lockfiles on host and register versions
// Do this serially so we can override build dependencies of lockfiles
// that are edited.
function(c) {
var pushed_lockfiles = {};
var package_names = Object.keys(lockfiles);
eachSeries(package_names, function(package_name, finished) {
var lockfile = lockfiles[package_name];
// If we've specified a specific blockchain, override deployments to point to that blockchain.
if (options.blockchain != null && lockfile.deployments != null && Object.keys(lockfile.deployments).length > 0) {
var deployments = {};
// This is naive, but will coerce down to a single blockchain, which is good enough for now.
Object.keys(lockfile.deployments || {}).forEach(function(blockchain) {
deployments[options.blockchain] = lockfile.deployments[blockchain];
});
lockfile.deployments = deployments;
}
// Override any build dependencies of previously uploaded lockfiles.
Object.keys(lockfile.build_dependencies || {}).forEach(function(dependency_name) {
if (pushed_lockfiles[dependency_name] != null) {
lockfile.build_dependencies[dependency_name] = pushed_lockfiles[dependency_name];
}
});
var raw_lockfile = JSON.stringify(lockfile, null, 2);
host.putContents(raw_lockfile).then(function(uri) {
pushed_lockfiles[package_name] = uri;
finished();
}).catch(finished);
}, function(err) {
if (err) return c(err);
var registrations = [];
package_names.forEach(function(package_name) {
var uri = pushed_lockfiles[package_name];
registrations.push(registry.register(package_name, "1.0.0", uri));
});
Promise.all(registrations).then(function(results) {
c();
}).catch(c);
});
}
], function(err) {
if (err) return callback(err);
callback(null, {
host: host,
registry: registry,
examples: examples,
ipfs_daemon: ipfs_daemon,
ipfs_api: ipfs_api
});
});
},
get: function(examples_path, callback) {
var fullPath = path.join("/ethpm/epm-spec/master/examples", examples_path);
var url = "https://raw.githubusercontent.com" + fullPath;
request(url, function(error, response, body) {
if(error) {
callback(error);
} else if (response.statusCode !== 200) {
callback(new Error(`Unexpected server response ${response.statusCode}`));
} else {
callback(null, body);
};
});
}
};