-
Notifications
You must be signed in to change notification settings - Fork 0
/
kodesmell-run.js
178 lines (143 loc) · 4.5 KB
/
kodesmell-run.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
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
const program = require('commander');
const path = require('path')
const chalk = require('chalk')
const fs = require('graceful-fs')
const { flatten } = require('ramda')
const { promisify } = require('util')
const crypto = require('crypto')
const co = require('co')
const prompt = require('co-prompt')
const { createKodes } = require('./lib/upload')
const minimist = require('minimist')
const readdir = promisify(fs.readdir)
const readFile = promisify(fs.readFile)
const lstat = promisify(fs.lstat)
const writeFile = promisify(fs.writeFile)
const PROJECT_ROOT = __dirname
const KODESMELL_PROJECT_NAME = PROJECT_ROOT.split(path.sep).pop()
const KODESMELL_ROOT = path.resolve(PROJECT_ROOT, '.kodesmell')
const KODESMELL_CONFIG = path.resolve(KODESMELL_ROOT, 'kodesmell.json')
const KODESMELL_HASHKEY_CACHE = path.resolve(KODESMELL_ROOT, 'kodesmell_hashes.txt')
const finds = []
const configs = require('./lib/config')
const parser = async function kodeParser(file, project) {
try {
let text = await readFile(file, 'utf-8');
let lines = text.split(`\n`)
let badcode = false
let res = []
let trues = lines.map((line, i) => {
let isComment = /\/\//.test(line)
if (!isComment) return false;
let where = line.indexOf(';)')
if (where < 3) return false;
const success = chalk.red.bold
// console.log(`${success('[Smells]')} ${chalk.green.underline(file)}@${i}.`);
// Hashfinder
let message, hash
let hashFound = /\(#.+\)/.exec(line) // null | array
if (hashFound) {
hash = hashFound[0].slice(2, -1);
message = line.slice(where + 2, -hashFound[0].length)
} else {
message = line.slice(where + 2)
}
return {
hash,
fileName: file,
lineNumber: i,
line,
code: text,
project,
message: message.trim(),
}
return false;
}).filter(Boolean);
return trues;
} catch(err) {
console.error(err);
process.exit(0);
}
}
async function search(root, project) {
const info = chalk.hex('#aaa')(`searching in ${root}`)
try {
let stats = await lstat(root);
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write(info);
if (stats.isDirectory()) {
let paths = await readdir(root)
let results = paths.map(p => search(path.resolve(root, p)))
return await Promise.all(results)
} else if (stats.isFile()) {
return await parser(root, project)
}
} catch(err) {
console.error(err);
process.exit(0);
}
}
const inject = async function hashInjector(arr) {
let hashes = arr.reduce((hashes, json) => {
if (json.hash) {
hashes[json.hash] = true
}
return hashes;
}, {})
let injected = arr.map(async (json) => {
if (json.hash) return json;
let { line, code, fileName, lineNumber } = json
let hash = crypto.randomBytes(20).toString('hex').slice(0, 7);
while (hashes[hash]) {
hash = crypto.randomBytes(20).toString('hex').slice(0, 7);
}
let lines = [...code.split('\n')]
lines[lineNumber] = line + ` (#${hash})`
let newCode = lines.join('\n')
await writeFile(fileName, newCode)
return Object.assign({}, json, { hash });
})
return await Promise.all(injected);
}
async function readJson() {
try {
let json = await readFile(configs.KODESMELL_JSON, 'utf-8')
return JSON.parse(json);
} catch(e) {
if (e.code === 'ENOENT') {
console.error(`cannot run 'Kodesmell' in this project. You should run 'kodesmell init' first.`);
process.exit(0);
} else {
console.error(e);
process.exit(0);
}
}
}
(async function main() {
const argv = minimist(process.argv.slice(2))
let source = argv._[0]
if (!source) {
source = '.' // Default source is the root.
}
source = path.resolve(source)
let { project } = await readJson()
let found = flatten(await search(source, project))
process.stdout.write('\n')
let kodes = await inject(found)
let hashColor = chalk.bgRed
let formatKode = (kode) => {
console.log('\n')
console.log(`${hashColor(kode.hash)} ${chalk.gray.underline(kode.fileName + ' @' + kode.lineNumber)}`)
console.log(chalk.bold(kode.message))
console.log('\n')
}
kodes.map(formatKode)
console.log(chalk.inverse('Created Kode!'))
console.log('Sending generated \'kodes\' to server...\n')
await createKodes({ kodes })
console.log(chalk.inverse('Successfully sent to server!'))
})()
// [hash] fileName@lineNumber
// Should handle error cases
//