forked from codeschool/sqlite-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlite-parser.js
More file actions
130 lines (117 loc) · 3.08 KB
/
sqlite-parser.js
File metadata and controls
130 lines (117 loc) · 3.08 KB
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
import parser from '../lib/index';
import {
stat,
writeFile,
mkdir,
readFile,
createReadStream,
createWriteStream
} from 'fs';
import {
normalize,
dirname
} from 'path';
const aliases = {
o: 'output',
v: 'version',
h: 'help',
x: 'stream'
};
const args = resolveArgs(process.argv.slice(2));
const error = function (err) {
console.error(err);
process.exit(1);
}
const done = checkThen(function () {
process.exit(0);
});
if (args['version']) {
console.log(`sqlite-parser v@@VERSION`);
process.exit(0);
}
if (args['help'] || args._.length === 0) {
console.log(`Usage:\tsqlite-parser [infile]\n`);
console.log(`Option\t\t\tAlias\tDescription`);
console.log(`--output [outfile]\t-o\tWrite output to a file instead of stdout`);
console.log(`--stream\t\t-x\tEnable streaming mode (default: infile >150kB)`);
console.log(`--version\t\t-v\tGet current parser version`);
process.exit(0);
}
let streaming = args['stream'];
const input = normalize(args._[0]);
let output = args['output'];
if (output) {
output = normalize(output);
}
stat(input, checkThen(function ({ size }) {
// If the file size is above a 150kB limit, switch to streaming mode
if (size / 1000 >= 150) {
streaming = true;
}
const startStream = streaming ? streamParser : standardParser;
if (output) {
const outDir = dirname(output);
stat(outDir, checkThen(startStream, function () {
mkdir(outDir, startStream);
}));
} else {
startStream();
}
}));
function resolveArgs(argv) {
const args = { _: [] };
let last = null;
const isNewArg = (arg) => !arg || arg.indexOf('-') === 0;
for (let i = 0; i < argv.length; i += 1) {
const arg = argv[i];
if (isNewArg(arg)) {
const cur = arg.indexOf('--') !== -1 ? arg.slice(2) : aliases[arg.slice(1)];
const peek = argv.length - 1 !== i ? argv[i + 1] : null;
const peekNew = isNewArg(peek);
args[cur] = peekNew ? true : peek;
if (!peekNew) { i += 1; }
} else {
args._.push(arg);
}
}
return args;
}
function checkThen(
resCallback = done,
errCallback = error
) {
return function (err, result) {
if (err) { return errCallback(err); }
resCallback(result);
};
}
function streamParser() {
const parserTransform = parser.createParser();
const singleNodeTransform = parser.createStitcher();
const readStream = createReadStream(input);
const writeStream = output ? createWriteStream(output) : process.stdout;
readStream.pipe(parserTransform);
parserTransform.pipe(singleNodeTransform);
singleNodeTransform.pipe(writeStream);
parserTransform.on('error', error);
singleNodeTransform.on('error', error);
writeStream.on('finish', done);
}
function standardParser() {
readFile(input, 'utf8', checkThen(function (data) {
parser(data, checkThen(function (ast) {
let result;
try {
result = JSON.stringify(ast, null, 2);
} catch (e) {
return error(e);
}
if (output) {
writeFile(output, result, checkThen(done));
} else {
process.stdout.write(`${result}\n`);
done();
}
}));
}));
};