File size: 1,869 Bytes
5c2ed06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env node

/**
 * This script can be used to convert databases from one format to another
 * Format:
 * `node tools/modlog/convert --db path/to/database --logdir path/to/modlogs --from <database format> --to <database format>`
 * Usage example:
 * `node tools/modlog/convert --db databases/modlog.db --logdir logs/modlog --from txt --to sqlite`
 * @author jetou
 */

'use strict';

// support TypeScript
require('child_process').execSync('node ' + __dirname + "/../build");
const ModlogConverter = require(`${__dirname}/converter.ts`).ModlogConverter;
const path = require('path');

function parseFlags() {
	const args = process.argv.slice(2);
	const flags = Object.create(null);
	for (const [idx, arg] of args.entries()) {
		if (idx % 2 === 0) {
			flags[arg] = args[idx + 1];
		}
	}
	return flags;
}

const flags = parseFlags();

const databasePath = flags['--db'];
const textLogPath = flags['--logdir'];
const outputLogPath = flags['--outputlogdir'];
const from = flags['--from'];
const to = flags['--to'];
const newestAllowedTimestamp = parseInt(flags['--newest-allowed-timestamp']);

if (newestAllowedTimestamp && isNaN(newestAllowedTimestamp)) {
	throw new Error(`The newest allowed timestamp must be a number of milliseconds.`);
}

if (
	!((databasePath || outputLogPath) && textLogPath && from && to) ||
	!['txt', 'sqlite'].includes(from) ||
	!['txt', 'sqlite'].includes(to)
) {
	throw new Error(`Invalid arguments specified.\nUsage: node tools/modlog/convert --db path/to/database --logdir path/to/modlogs --from <database format> --to <database format>.`);
}

console.log(`Converting ${from === 'txt' ? `the text modlog files in ${textLogPath}` : `${databasePath}`} from ${from} to ${to}...`);
ModlogConverter.convert(from, to, path.resolve(databasePath || ""), path.resolve(textLogPath), path.resolve(outputLogPath || ""), newestAllowedTimestamp);