-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanage.mjs
executable file
·213 lines (191 loc) · 6.5 KB
/
manage.mjs
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env node
// Command line tool to add and delete tokens, or wipe db (tokens + interactions)
// Intended to be run directly on the server (no networking)
import { readFileSync } from 'fs';
import path from 'path';
import readline from 'readline';
import os from 'os';
import * as db from './db.mjs';
import * as util from './util.mjs';
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const CONFIG = JSON.parse(readFileSync('./config/main.config.json'));
const DB = JSON.parse(readFileSync(CONFIG.db_config));
const KEYWORDS = JSON.parse(readFileSync('./config/keywords.json'));
const EXPORT_BATCH = 1000;
const EXPORT_TMP_DIR = '.tmp/';
let args = process.argv.slice(2); // 0 .. node, 1 .. module
args = args.map( str => str.toLowerCase() );
const script = path.basename( process.argv[1] );
function exit() {
rl.close();
process.exit();
}
function usage() {
console.log(`Usage: ${script}
add <num>
delete <id> [ <id> ... ]
export
wipe-tokens
wipe-interactions
wipe-all`);
exit();
}
if (args.length == 0) usage();
if (args[0] == 'add') {
if (!args[1]) {
console.log('need <num> number of tokens to generate');
usage();
}
const num = parseInt(args[1]);
if (Number.isNaN(num)) usage();
console.log(`creating ${num} mock tokens:`);
for (let i=0; i<num; i++) {
if (i>0) await util.sleep(1);
process.stdout.write( (i+1) + '...' );
const keywords = [ KEYWORDS[util.rndint(KEYWORDS.length)], KEYWORDS[util.rndint(KEYWORDS.length)], KEYWORDS[util.rndint(KEYWORDS.length)] ];
const generated = util.timestamp();
const res = await db.put_token({
svg: util.random_svg(`tfcc:generated="${generated}" tfcc:keywords="${keywords.join(',')}"`),
generated,
keywords
});
console.log(res.id);
}
exit();
} else if (args[0] == 'delete') {
if (!args[1]) {
console.log('need <id> id or ids to delete');
usage();
}
const arg = args.slice(1).join(','); // join all remaining args
const ids = arg.split(',').filter(str => str != '').map(str => str.trim().toLowerCase());
rl.question(`Are you sure you want to delete ${ids.length} token(s): \n${ids.join('\n')}\n? `, async response => {
if (!response.toLowerCase() == 'yes') {
console.log('Aborting.');
exit();
}
rl.close();
for (let id of ids) {
try {
process.stdout.write(`deleting ${id}...`);
await db.delete_token(id);
console.log('OK');
} catch (e) {
console.log(e.response.body);
}
}
});
} else if (args[0] == 'export') {
const json_dir = EXPORT_TMP_DIR + '/json';
const svg_dir = EXPORT_TMP_DIR + '/svg';
util.rmdir(json_dir); util.rmdir(svg_dir);
util.mkdir(json_dir); util.mkdir(svg_dir);
let is_first_batch = true;
let pad_len = 32; // maximum pad length
try {
let res = null;
while (!res || res.next != null) {
if (!res) {
res = await db.get_tokens(0, null, null, EXPORT_BATCH);
} else {
res = await db.get_tokens(null, res.next, null, EXPORT_BATCH);
}
// determine pad length (first batch only)
if (is_first_batch) {
if (res.rows.length > 0) {
pad_len = res.rows[0].id.length; // this is the latest token i.e. the longest
}
}
// console.log(res);
console.log(`retrieved batch of ${res.rows.length} tokens...`);
for (let token of res.rows) {
const id = token.id.toUpperCase().padStart(pad_len, '0');
util.save_json( `${json_dir}/${id}.json`, token);
util.save_text( `${svg_dir}/${id}.svg`, token.svg);
}
is_first_batch = false;
}
let ts = util.timestamp().replace(/[Z:]/g, '').replace(/[T\.]/g, '-');
let zipfile = `export-${ts}.zip`
util.zip(EXPORT_TMP_DIR, zipfile);
util.rmdir(EXPORT_TMP_DIR);
console.log(`created ${zipfile}`);
} catch (e) {
console.log('error: ', e.response.body);
}
exit();
} else if (args[0] == 'wipe-all') {
const challenge = os.hostname() + '-all-' + util.rnd_hash(4).toUpperCase();
rl.question(`WARNING: You are about to wipe ALL TOKENS and INTERACTIONS from the database on ${os.hostname()}! To confirm type \'${challenge}\': `, async response => {
if (response !== challenge) {
console.log('Aborted.');
exit();
}
rl.close();
console.log('Wiping:');
process.stdout.write(`deleting ${DB.tokens_db}...`);
try {
await db.delete_db(DB.tokens_db);
console.log('OK');
} catch (e) {
console.log('error: ', e.response.body);
}
process.stdout.write(`deleting ${DB.interactions_db}...`);
try {
await db.delete_db(DB.interactions_db);
console.log('OK');
} catch (e) {
console.log('error: ', e.response.body);
}
console.log('creating dbs...');
await db.create_db(DB.tokens_db);
await db.create_db(DB.interactions_db);
console.log('creating design doc...');
await db.create_design_docs();
});
} else if (args[0] == 'wipe-tokens') {
const challenge = os.hostname() + '-tokens-' + util.rnd_hash(4).toUpperCase();
rl.question(`WARNING: You are about to wipe ALL TOKENS from the database on ${os.hostname()}! To confirm type \'${challenge}\': `, async response => {
if (response !== challenge) {
console.log('Aborted.');
exit();
}
rl.close();
console.log('Wiping:');
process.stdout.write(`deleting ${DB.tokens_db}...`);
try {
await db.delete_db(DB.tokens_db);
console.log('OK');
} catch (e) {
console.log('error: ', e.response.body);
}
console.log('creating db...');
await db.create_db(DB.tokens_db);
});
} else if (args[0] == 'wipe-interactions') {
const challenge = os.hostname() + '-interactions-' + util.rnd_hash(4).toUpperCase();
rl.question(`WARNING: You are about to wipe ALL INTERACTIONS from the database on ${os.hostname()}! To confirm type \'${challenge}\': `, async response => {
if (response !== challenge) {
console.log('Aborted.');
exit();
}
rl.close();
console.log('Wiping:');
process.stdout.write(`deleting ${DB.interactions_db}...`);
try {
await db.delete_db(DB.interactions_db);
console.log('OK');
} catch (e) {
console.log('error: ', e.response.body);
}
console.log('creating db...');
await db.create_db(DB.interactions_db);
console.log('creating design doc...');
await db.create_design_docs();
});
} else {
usage();
}