This repository was archived by the owner on Jan 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.js
67 lines (56 loc) · 2.19 KB
/
runner.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
// Copyright 2018 VMware, Inc.
// SPDX-License-Identifier: BSD-2-Clause
const { spawn } = require('child_process')
const fs = require('fs')
const path = require('path')
const merge = require('./merge_reports')
const linters = require('./linters')
/**
* Run all linters on specified files
* @param {string[]} files Files to analyze
* @param {string} repoID
* @param {string} prID
*/
async function runLinters (files, repoID, prID) {
// TODO: Sync directory with file download location resolution
const reports = Object.values(linters).map((linter) => run(linter, linter.workingDirectoryForPR(repoID, prID), files))
return merge(await Promise.all(reports))
}
/**
* Linter driver logic: spawn a child process, gather the results and build
* a report
* @param {*} linter A linter instance
* @param {string} workingDirectory The path to the process working directory
* @param {string[]} files Files to analyze
* @returns {Promise<any>} A promise for the report object with the analysis results
*/
function run (linter, workingDirectory, files) {
const filtered = linter.filter(files)
if (filtered.length === 0) { return linter.defaultReport }
const reportFilePath = path.join(workingDirectory, '..', linter.reportFile)
const process = spawn(linter.name, linter.args(filtered, path.join('..', linter.reportFile)), { cwd: workingDirectory })
let errorLogs = ''
process.stderr.on('data', (chunk) => {
errorLogs += chunk.toString()
})
// Promise report generation
return new Promise((resolve, reject) => {
process.on('error', reject)
process.on('close', () => reportHandler(linter, path.resolve(workingDirectory), reportFilePath, resolve, reject, errorLogs))
})
}
function reportHandler (linter, workingDirectory, reportFilePath, resolve, reject, logs) {
fs.readFile(reportFilePath, 'utf8', (err, data) => {
if (err) {
console.log('Could not read linter results: ' + reportFilePath)
console.log('stderr: ' + logs)
return reject(err)
} else {
const results = linter.parseResults(data)
const report = linter.generateReport(results, workingDirectory)
return resolve(report)
}
})
}
module.exports.runLinters = runLinters
module.exports.run = run