-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdumpStringsFromJsFile.js
More file actions
84 lines (73 loc) · 2.34 KB
/
dumpStringsFromJsFile.js
File metadata and controls
84 lines (73 loc) · 2.34 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
const path = require('path')
const chalk = require('chalk')
const stringify = require('json-stringify-safe')
const Promise = require('bluebird')
const fs = require('fs')
Promise.promisifyAll(fs)
const findStrings = require('./findStrings')
/* eslint-disable no-unused-vars */
function dumpJSON(filename, v) {
fs.writeFileAsync(
filename,
stringify(v, null, 4)
).then(_ => {
console.log(chalk.blue(`${chalk.bold(path.basename(filename))} written`))
}).catch(err => {
console.log(chalk.red('Error:'), err)
})
}
/* eslint-enable no-unused-vars */
function isSQL(str) {
const patterns = [
/SELECT.+FROM/im,
/INSERT.+INTO/im,
/UPDATE\w.+.+/im,
/DELETE.+.+/im,
/CALL.+\w/im
] // [^-]+[(]|\w+[^-]*
for (const rx of patterns) {
// if (!(rx instanceof RegExp)) { throw new Error(util.inspect(rx) + 'is not a RegExp') }
if (str.startsWith('SELECT')) {
console.log(rx, ' --> ', str.substr(0, Math.min(30, str.length)) + '...')
}
if (rx.test(str)) {
// console.log(chalk.red(rx.toString()), rx.exec(str) )
// console.log(chalk.green('passed'))
return true
}
}
// console.log(chalk.red('NotMatched'), str.substr(0, Math.min(str.length, 20)))
return false
}
function appendLastSemicolon(str) {
if (!/[;][\s\S]$/.test(str)) { return str + ';' }
}
function dumpStringsFromJsFiles(files, destDir) {
files.forEach(file => {
file = path.resolve(file)
console.log(chalk.bold.yellow('Opening file'), chalk.bold.cyan(file))
let fileContent = fs.readFileSync(file, 'utf8')
let strings = findStrings(fileContent)
console.log('%s strings found', strings.length)
let sqls = []// strings.filter(isSQL)
for (var str of strings) {
// console.log(str.substr(0, Math.min(30, str.length)))
if (isSQL(str)) {
sqls.push(appendLastSemicolon(str))
}
}
console.log('\t\t%s/%s sql/str', chalk.bold(sqls.length), strings.length)
let filename = path.basename(file)
filename = filename.substr(0, filename.lastIndexOf('.')) + '.sql'
fs.writeFileAsync(
path.join(destDir, filename),
sqls.join('\n\n')
).then(_ => {
console.log(chalk.bold.green(filename), 'Written')
}).catch(err => {
console.log(chalk.red('Error: '), chalk.bold.red(filename))
console.log(err)
})
})
}
module.exports = dumpStringsFromJsFiles