-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
executable file
·108 lines (86 loc) · 2.73 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
/*
* gulp-screeps
* https://github.com/pcmulder/gulp-screeps
*
* Copyright (c) 2015 Patrick Mulder
* Licensed under the MIT license.
*/
'use strict';
var through = require('through2'),
gutil = require('gulp-util'),
https = require('https'),
util = require('util'),
path = require('path'),
fs = require("fs");
var PluginError = gutil.PluginError;
var PLUGIN_NAME = 'gulp-screeps';
module.exports = function (opt) {
opt = opt || {};
var files = [];
var modules = {};
if (typeof opt.email !== 'string' || typeof opt.password !== '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(null, file);
}
function endStream(cb) {
files.map(function (file) {
var name = path.basename(file.path).replace(/\.js$/,'');
modules[name] = file.contents.toString('utf-8');
});
var req = https.request({
hostname: 'screeps.com',
port: 443,
path: opt.ptr ? '/ptr/api/user/code' : '/api/user/code',
method: 'POST',
auth: opt.email + ':' + opt.password,
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
},
function (res) {
res.setEncoding('utf8');
var data = '';
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
data = JSON.parse(data);
if (data.ok) {
var msg = 'Commited to Screeps account "' + opt.email + '"';
if (opt.branch) {
msg += ' branch "' + opt.branch + '"';
}
msg += '.';
gutil.log(msg);
}
else {
gutil.log('Error while commiting to Screeps: ' + util.inspect(data));
}
});
});
var data = {
branch: opt.branch,
modules: modules
};
req.write(JSON.stringify(data));
req.end();
cb();
}
return through.obj(bufferContents, endStream);
};