-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupload.js
executable file
·97 lines (86 loc) · 3.28 KB
/
upload.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
#!/usr/bin/env node
// emacs mode -*-JavaScript-*-
var fs = require('fs'),
https = require('https'),
getopt = require('node-getopt')
var util = require('./bracery-util')
var config = require('./bracery-config')
// parse command-line options
var opt = getopt.create([
['d' , 'data=PATH+' , 'upload data file(s) with format [{"name":"symbol1","rules":["option1","option2"...]},{"name":"symbol2","rules":[...]},...]'],
['D' , 'delete' , 'DELETE instead of PUT'],
['n' , 'name=NAME+' , 'include only named symbols'],
['b' , 'begin=NAME' , 'begin at named symbol'],
['c' , 'cookie=COOKIE' , 'use session cookie'],
['l' , 'lock' , 'set lock flag (requires session cookie to establish identity)'],
['s' , 'summary' , 'don\'t upload anything; instead, just output a summary in Markdown'],
['v' , 'verbose' , 'make lots of noise'],
['h' , 'help' , 'display this help message'],
]) // create Getopt instance
.bindHelp() // bind option 'help' to default action
.parseSystem() // parse command line
try {
const dataFiles = (opt.options.data || []).concat (opt.argv);
if (!(dataFiles && dataFiles.length)) {
console.error ('Please specify a data file');
process.exit();
}
let symbolAllowed = null, firstSymbol = null, begun = false;
if (opt.options.name) {
symbolAllowed = {};
opt.options.name.forEach ((sym) => (symbolAllowed[sym] = true));
}
if (opt.options.begin)
firstSymbol = opt.options.begin;
dataFiles.reduce (async (previousFiles, filename) => {
await previousFiles;
const file = fs.readFileSync(filename).toString();
const defs = JSON.parse (file);
await defs.reduce (async (previousDefs, def) => {
await previousDefs;
if (symbolAllowed && !symbolAllowed[def.name]) {
if (opt.options.verbose)
console.warn (def.name + ' not whitelisted');
return;
} if (firstSymbol) {
if (def.name === firstSymbol)
begun = true;
if (!begun) {
if (opt.options.verbose)
console.warn (def.name + ' skipped');
return;
}
}
if (opt.options.summary) {
console.log ("- ~[" + def.name + "](/" + def.name + ") " + def.summary);
return;
}
const reqOpts = {
hostname: 'bracery.org',
port: 443,
path: '/store/' + def.name,
headers: {
'Content-Type': 'application/json;charset=UTF-8'
},
};
let content = '';
if (opt.options['delete']) {
reqOpts.method = 'DELETE'
} else {
reqOpts.method = 'PUT'
if (opt.options.cookie)
reqOpts.headers['Cookie'] = config.cookieName + '=' + opt.options.cookie;
const body = { bracery: '[' + def.rules.join('|') + ']' };
if (opt.options.lock)
body.locked = true;
content = JSON.stringify (body);
}
console.warn (reqOpts.method + ' ' + reqOpts.path + ' (' + def.rules.length + ')');
let [res, data] = await util.httpsRequest (reqOpts, content);
if (res.statusCode != 200)
console.warn ('Error ' + res.statusCode + ' (' + def.name + ') ' + (data || ''));
}, Promise.resolve());
}, Promise.resolve());
} catch (e) {
console.error (e);
}