forked from wk-cof/gulp-screeps
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
executable file
·147 lines (122 loc) · 4.25 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
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
/*
* gulp-screeps
* https://github.com/pcmulder/gulp-screeps
*
* Copyright (c) 2015 Patrick Mulder
* Licensed under the MIT license.
*/
'use strict';
var through = require('through2'),
PluginError = require('plugin-error'),
log = require('fancy-log'),
http = require('http'),
https = require('https'),
util = require('util'),
path = require('path'),
fs = require('fs');
var PLUGIN_NAME = 'gulp-screeps';
module.exports = function (opt) {
opt = opt || {};
var files = [];
var modules = {};
if (typeof opt.host !== 'string'){
opt.host = 'screeps.com';
}
if (typeof opt.port !== 'number'){
opt.port = 443;
}
if (typeof opt.secure !== 'boolean'){
opt.secure = opt.port === 443;
}
if ((typeof opt.email !== 'string' || typeof opt.password !== 'string') && typeof opt.token !== 'string') {
throw new PluginError(PLUGIN_NAME, 'Please provide account information');
}
if (typeof opt.branch !== 'string'){
opt.branch = 'default';
}
function bufferContents(file, enc, cb) {
// ignore empty files
if (file.isNull()) {
cb();
return;
}
if (file.isStream()) {
this.emit('error', new PluginError(PLUGIN_NAME, 'Streaming not supported'));
cb();
return;
}
files.push(file);
cb();
}
function endStream(cb) {
files.map(function (file) {
var name = path.basename(file.path).replace(/\.js$/,'');
modules[name] = file.contents.toString('utf-8');
});
var request = (opt.secure ? https : http).request;
var api = '/api/user/code';
var reqOpts = {
hostname: opt.host,
port: opt.port,
path: opt.path ? opt.path + api : api,
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
};
if(opt.token) {
reqOpts.headers['x-token'] = opt.token;
} else if(opt.email && opt.password) {
reqOpts.auth = opt.email + ':' + opt.password;
}
var req = request(reqOpts,
function (res) {
res.setEncoding('utf8');
var data = '';
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
try {
data = JSON.parse(data);
} catch(e) {}
if (data.ok) {
var msg = 'Committed to Screeps account';
if (opt.email) {
msg += ' "' + opt.email + '"';
}
if (opt.token) {
msg += ' with token';
}
if (opt.branch) {
msg += ' branch "' + opt.branch + '"';
}
if (opt.host !== "screeps.com") {
msg += ' on server "' + opt.host + '"';
}
msg += '.';
if (res.headers['x-ratelimit-limit']) {
var limit = res.headers['x-ratelimit-limit'];
var remaining = res.headers['x-ratelimit-remaining'];
var reset = res.headers['x-ratelimit-reset'];
msg += ' RateLimiting: (' + remaining + '/' + limit + ' ' + ((parseInt(reset) * 1000) - Date.now()) + 'ms to reset)';
}
log(msg);
cb();
}
else {
log('Error while committing to Screeps: ' + util.inspect(data));
cb('Error while committing to Screeps: ' + util.inspect(data));
}
});
res.on('error', cb);
});
var data = {
branch: opt.branch,
modules: modules
};
req.write(JSON.stringify(data));
req.end();
}
return through.obj(bufferContents, endStream);
};