-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·321 lines (291 loc) · 10.8 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#!/usr/bin/env node
const path = require('path')
const fs = require('fs')
const utils = require(path.join(__dirname, '/src/utils'))
global.qualscanCLI = require.main === module || process.env.qualscanCLI
const run = async (defaultConf, defaultPath) => {
const qualscanCLI = global.qualscanCLI
const cmdListDefault = process.env.TASKS_LIST ? process.env.TASKS_LIST.split(',') : ['code-duplication', 'security-audit', 'updates', 'package-check', 'dependencies-exact-version', 'project-size', 'dependencies-check', 'dependencies-size', 'require-time']
let scriptListDefault = process.env.SCRIPTS_LIST ? process.env.SCRIPTS_LIST.split(',') : []
if (defaultConf && defaultConf.scripts) {
scriptListDefault = defaultConf.scripts
}
const knownScripts = ['test', 'lint', 'linter']
process.env.QUALSCAN_PROJECT_PATH = defaultPath || process.env.QUALSCAN_PROJECT_PATH || process.cwd()
if (qualscanCLI) {
console.log('\x1b[33m%s\x1b[0m', ' ____ __ \r\n / __ \\__ ______ _/ /_____________ _____ \r\n / / / / / / / __ `/ / ___/ ___/ __ `/ __ \\\r\n/ /_/ / /_/ / /_/ / (__ ) /__/ /_/ / / / /\r\n\\___\\_\\__,_/\\__,_/_/____/\\___/\\__,_/_/ /_/ ')
console.log('\n')
}
// -----------------------------
// Get scripts from package.json
// -----------------------------
try {
global.packagefile = require(path.join(process.env.QUALSCAN_PROJECT_PATH, 'package.json'))
if (global.packagefile.scripts && scriptListDefault.length === 0 && !defaultConf) {
for (let index = 0; index < knownScripts.length; index++) {
const script = knownScripts[index]
if (global.packagefile.scripts[script]) {
scriptListDefault.push(script)
}
}
}
} catch (err) {
if (qualscanCLI) {
console.error('No package.json file found!')
console.error('Qualscan requires a valid Javascript project structure with a package.json file.')
process.exit(1)
} else {
throw err
}
}
// -----------------------------
// Load .qualscanrc file
// -----------------------------
const rcFilePath = path.join(process.env.QUALSCAN_PROJECT_PATH, '.qualscanrc')
let conf = defaultConf
if (fs.existsSync(rcFilePath)) {
try {
conf = JSON.parse(fs.readFileSync(rcFilePath))
} catch (err) {
console.warn('Invalid .qualscanrc file format, Qualscan requires a valid JSON file!')
console.warn('.qualscanrc file has been ignored.')
}
}
const defaults = require(path.join(__dirname, 'src/defaults.json'))
if (conf) {
utils.deepMerge(defaults, conf)
}
// -----------------------------
// Load conf
// -----------------------------
const init = !conf ? require('yargs')(process.argv.slice(2)).config() : require('yargs')(process.argv.slice(2)).config(conf)
// -----------------------------
// Get args from command line
// -----------------------------
global.argv = init
.help('h')
.alias('h', 'help')
.options('level', {
alias: 'l',
type: 'string',
description: 'Level of information to display (error, warnings, info, all)',
default: 'all'
})
.option('verbose', {
alias: 'v',
type: 'boolean',
description: 'Display detailed result for all tasks'
})
.option('tasks', {
alias: 't',
type: 'array',
description: 'List of tasks to run'
})
.option('scripts', {
alias: 's',
type: 'array',
description: 'List of scripts to run'
})
.option('budget-info', {
alias: 'bi',
type: 'boolean',
default: true,
description: 'Display budgets info?'
})
.option('security-audit.budget', {
alias: 'nab',
type: 'object',
default: defaults['security-audit'].budget,
description: 'Set the budget for npm audit plugin.'
})
.option('code-duplication.args', {
alias: 'cda',
type: 'string',
description: 'Args for code duplication plugins'
})
.option('code-duplication.budget', {
alias: 'cdb',
type: 'object',
default: defaults['code-duplication'].budget,
description: 'Set the budget for code duplication plugin (in percentage).'
})
.option('dependencies-exact-version.budget', {
alias: 'devb',
type: 'object',
default: defaults['dependencies-exact-version'].budget,
description: 'Set the budget for npm audit plugin.'
})
.option('dependencies-exact-version.devDependencies', {
alias: 'devd',
type: 'boolean',
description: 'Check dev dependencies exact version'
})
.option('updates.budget', {
alias: 'nob',
type: 'object',
default: defaults.updates.budget,
description: 'Set the budget for updates plugin.'
})
.option('updates.devDependencies', {
alias: 'nod',
type: 'boolean',
default: false,
description: 'Take into account dev dependencies for updates?'
})
.option('project-size.budget', {
alias: 'npb',
type: 'object',
default: defaults['project-size'].budget,
description: 'Set the budget for project size plugin.'
})
.option('dependencies-check.budget', {
alias: 'dcb',
type: 'object',
default: defaults['dependencies-check'].budget,
description: 'Set the budget for dependencies check.'
})
.option('dependencies-check.args', {
alias: 'dca',
type: 'object',
default: defaults['dependencies-check'].args,
description: 'Send custom args to dependencies check plugin.'
})
.option('dependencies-size.budget', {
alias: 'dsb',
type: 'object',
default: defaults['dependencies-size'].budget,
description: 'Set the budget for dependencies size plugin.'
})
.option('require-time.budget', {
alias: 'rtb',
type: 'object',
default: defaults['require-time'].budget,
description: 'Set the budget for require time plugin (in nanoseconds).'
})
.option('require-time.entrypoint', {
alias: 'rte',
type: 'string',
default: utils.getEntrypoint(),
description: 'Path to the entrypoint of your project.'
})
.option('reporters', {
alias: 'r',
type: 'array',
description: 'List of reporters to use',
default: ['text']
})
.option('reportPath', {
alias: 'rp',
type: 'string',
description: 'Path where to save report',
default: path.join(process.env.QUALSCAN_PROJECT_PATH, '/report')
})
.option('devMode', {
alias: 'dm',
type: 'boolean',
description: 'Manage project as in development mode',
default: false
})
.command('exportConf', 'Export qualscan conf!', () => {}, (argv) => {
console.log(argv)
process.exit(0)
})
.argv
global.reporters = {}
for (let i = 0; i < global.argv.reporters.length; i++) {
const reporter = global.argv.reporters[i]
try {
global.reporters[reporter] = require(path.join(__dirname, './src/reporters/', reporter))
} catch (err) {
console.error(`Reporter ${reporter} does not exist!`)
}
}
const levels = ['all', 'info', 'warn', 'fail']
const currentLevel = { error: 3, warn: 2, info: 1, all: 0 }[global.argv.level]
// -----------------------------
// Launch all commands
// -----------------------------
utils.display('start', [])
const allCmds = []
const cmdList = global.argv.tasks || cmdListDefault
const scriptList = global.argv.scripts || scriptListDefault
const skipped = []
const skippedScripts = []
const report = { data: {}, time: 0 }
const time = process.hrtime()
// -----------------------------
// Prepare commands list
// -----------------------------
for (const index in cmdList) {
utils.prepareCmd(cmdList[index], allCmds, skipped)
}
// ---------------------------------
// Prepare scripts & skipped scripts
// ---------------------------------
for (let index = 0; index < scriptList.length; index++) {
const script = scriptList[index]
if (!global.packagefile.scripts || !global.packagefile.scripts[script]) {
skippedScripts.push(script)
} else {
utils.prepareCmd(script, allCmds, skipped, true)
}
}
const res = await Promise.all(allCmds)
let hasError = false
const budgetInfo = []
let score = 0
for (const i in res) {
const cmd = res[i]
hasError = hasError || cmd.level === 'fail'
if (global.argv.budgetInfo && cmd.budget) {
budgetInfo.push(cmd.budget)
}
if (cmd.level === 'succeed' || cmd.level === 'info') {
score++
}
// -----------------------------
// Display verbose messages
// -----------------------------
if (global.argv.verbose &&
(levels.indexOf(cmd.level) >= currentLevel ||
(global.argv.level === 'all' && cmd.level === 'succeed'))) {
utils.display('displayVerbose', [cmd])
}
}
score = Math.round(score * 100 / res.length)
report.data.cmds = res
report.data.score = score
report.data.skipped = {
plugins: skipped,
scripts: skippedScripts
}
const diff = process.hrtime(time)
report.time = diff[0] * 1e9 + diff[1]
utils.display('displayScore', [score])
// -----------------------------
// Display bugets information
// -----------------------------
utils.display('displayBudgets', [budgetInfo])
// -------------------------------
// Print skipped scripts & plugins
// -------------------------------
if (skipped.length > 0) {
utils.display('displaySkippedPlugins', [skipped])
}
if (skippedScripts.length > 0) {
utils.display('displaySkippedScripts', [skippedScripts])
}
await utils.display('end', [report])
if (qualscanCLI) {
process.exit(hasError ? 1 : 0)
} else {
return report
}
}
if (global.qualscanCLI) {
run()
} else {
module.exports = {
run
}
}