-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathlint-merge-report.js
More file actions
156 lines (142 loc) · 4 KB
/
lint-merge-report.js
File metadata and controls
156 lines (142 loc) · 4 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
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
/* eslint-disable no-console */
import { spawn } from 'child_process';
/* eslint-disable no-param-reassign */
import fs from 'fs';
import { getEnv } from '../utils/env.js';
import { getPresetApi } from '../utils/preset.js';
const reports = ['eslint-report.json', 'stylelint-report.json'];
let buff = [];
async function run(cmd, opts = {}) {
if (opts.verbose) {
console.log(`\n#### RUNNER: ${cmd.name} ${cmd.args.join(' ')}`);
}
const start = Date.now();
return new Promise(async (resolve, reject) => {
const out = spawn(cmd.name, cmd.args);
let stdout = '';
let stderr = '';
out.on('error', error => {
console.error(error);
reject(error);
});
out.on('close', () => {
resolve(stdout);
});
out.on('exit', code => {
if (opts.verbose && stderr) {
console.error(`#### RUNNER: Child Process STDERR: ${stderr}`);
}
if (opts.verbose && stdout) {
console.error(`#### RUNNER: Child Process STDOUT: ${stdout}`);
}
if (code > 0) {
run.exitCode += 1;
console.error(`#### RUNNER: ${cmd.name} ${cmd.args.join(' ')} exit code ${code}`);
reject(`STDOUT: ${stdout}\n\nSTDERR: ${stderr}`);
return;
}
const end = Date.now();
console.log(
`#### RUNNER: ${cmd.name} ${cmd.args.join(' ')} exit code ${code} in ${
(end - start) / 1000
} seconds`,
);
resolve(stdout);
});
out.stdout.on('data', data => {
const datastr = data.toString();
if (data && datastr) {
stdout += datastr;
}
});
out.stderr.on('data', data => {
const datastr = data.toString();
if (data && datastr) {
stderr += datastr;
}
});
});
}
function transform(item) {
if (item.source && !item.filePath) {
item.filePath = item.source;
delete item.source;
}
if (item.warnings && !item.messages) {
item.messages = item.warnings.map(w => ({
...w,
severity: 1,
message: w.text,
ruleId: w.rule,
}));
item.warningCount = item.warnings.length;
delete item.warning;
} else if (item.messages) {
item.messages = item.messages.map(w => ({ ...w, severity: 1 }));
item.warningCount += item.errorCount;
item.errorCount = 0;
}
return item;
}
function getPackages(packageDirs = []) {
return packageDirs.flatMap(dir =>
fs.readdirSync(dir).map(subDir => ({
name: subDir,
location: `${dir}/${subDir}`,
})),
);
}
export default function mergeReport(options) {
// current env vars and talend scripts configuration in <project-folder>/talend-scripts.(js/json)
const env = getEnv(options);
env.TALEND_MODE = 'production';
console.log(`Talend scripts mode : ${env.TALEND_MODE}`);
if (env.TALEND_SCRIPTS_CONFIG) {
console.log('Talend scripts configuration file found and loaded');
} else {
console.log('Talend scripts configuration file not found');
}
const presetApi = getPresetApi(env);
const rootPackageDirs = presetApi.getUserConfig('lintMergeReport', {})?.packageDirs || [];
const packages = getPackages(rootPackageDirs);
if (packages.length === 0) {
throw new Error(
'No packages has been retrieved, check if the talend-scripts.json is well configured',
);
}
// https://stackoverflow.com/questions/65944700/how-to-run-git-diff-in-github-actions
const diff = run({
name: 'git',
args: ['diff', '--name-only', `origin/${options[0]}`, `origin/${options[1]}`],
})
.then(out =>
out
.split('\n')
.map(str => str.trim())
.filter(Boolean),
)
.catch(e => console.error(e));
diff.then(files => {
function onlyIfInDiff(lint) {
return !!files.find(f => lint.filePath.endsWith(`/${f}`));
}
packages.forEach(pkg => {
reports.forEach(report => {
const fpath = `${pkg.location}/${report}`;
if (fs.existsSync(fpath)) {
try {
buff = buff.concat(
JSON.parse(fs.readFileSync(fpath)).map(transform).filter(onlyIfInDiff),
);
} catch (e) {
console.error(e);
}
}
});
});
const target = `${process.cwd()}/eslint-report.json`;
// eslint-disable-next-line no-console
console.log(`report merge into ${target}`);
fs.writeFileSync(target, JSON.stringify(buff, null, 2));
});
}