-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
64 lines (53 loc) · 1.63 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
#! /usr/bin/env node
var fs = require('fs');
const { connect } = require('http2');
var path = require('path');
console.log("SnowJS-Helpers");
console.log("=================");
if (process.argv.length < 4)
{
console.log("Usage: snowjs-helpers input-sql-file outdir");
process.exit(1);
}
var filename = process.argv[2];
var outdir = process.argv[3];
try {
fs.accessSync(filename,fs.constants.R_OK);
}
catch
{
console.log("No read access to input file " + filename);
process.exit(2);
}
try {
fs.accessSync(outdir,fs.constants.W_OK);
}
catch
{
console.log("No write access to " + outdir);
process.exit(3);
}
var only_filename = path.basename(filename);
var target_file = path.join(outdir,only_filename);
console.log("Processing " + filename + " ==> " + target_file);
var contents = fs.readFileSync(filename, 'utf8');
var tagStart = contents.indexOf("$$");
var spheader = contents.substring(0,tagStart).trim();
var tagEnd = contents.indexOf("$$", tagStart + 10);
var spbody = contents.substr(contents.indexOf("$$") + 2,tagEnd - tagStart - 2).trim();
var usings = spbody.match(/"@USING_(\w+)";/g);
for (let index = 0; index < usings.length; index++) {
const using = usings[index];
try {
var snippet_name = using.replace("\"@USING_","").replace('";','') + ".snippet";
var regexp = new RegExp(using,"g");
var snippet = fs.readFileSync(path.join(".",snippet_name));
spbody = spbody.replace(regexp,snippet);
}
catch(ex)
{
console.error(`Snippet ${snippet_name} invalid`);
}
}
var newsp = spheader + "\n$$\n" + spbody + "\n$$;";
fs.writeFileSync(target_file, newsp);