This repository has been archived by the owner on Jun 27, 2023. It is now read-only.
forked from tfausak/hlint-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
97 lines (80 loc) · 2.7 KB
/
main.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
const core = require('@actions/core');
const exec = require('@actions/exec');
const fs = require('fs');
const glob = require('@actions/glob');
const toolCache = require('@actions/tool-cache');
const DEFAULT_CONFIG = '.hlint.yaml';
const DEFAULT_PATTERN = '**/*.hs';
const DEFAULT_VERSION = 'latest';
const LATEST_VERSION = '3.4';
const EXTENSIONS = {
darwin: '.tar.gz',
linux: '.tar.gz',
win32: '.zip'
};
const PLATFORMS = {
darwin: 'osx',
linux: 'linux',
win32: 'windows',
};
const buildUrl = (version, platform, extension) =>
`https://github.com/ndmitchell/hlint/releases/download/v${version}/hlint-${version}-x86_64-${platform}${extension}`;
const isFile = (path) =>
new Promise((resolve) => fs.stat(path, (error, stats) => resolve(!error && stats.isFile())));
const isUndefined = (x) =>
typeof x === 'undefined';
(async () => {
try {
const config = core.getInput('config') || DEFAULT_CONFIG;
const extension = EXTENSIONS[process.platform];
const pattern = core.getInput('pattern') || DEFAULT_PATTERN;
const platform = PLATFORMS[process.platform];
const rawVersion = core.getInput('version') || DEFAULT_VERSION;
const version = rawVersion === 'latest' ? LATEST_VERSION : rawVersion;
if (isUndefined(extension) || isUndefined(platform)) {
throw new Error(`unsupported platform: ${process.platform}`);
}
const hlint = await toolCache.downloadTool(buildUrl(version, platform, extension));
const path = await toolCache.extractTar(hlint);
core.addPath(`${path}/hlint-${version}`);
const globber = await glob.create(pattern);
const files = await globber.glob();
if (files.length > 0) {
const arguments = [
'--cmdthreads',
'--json',
'--no-exit-code',
...files.sort(),
];
const exists = await isFile(config);
if (exists) {
arguments.unshift('--hint', config);
}
const chunks = [];
await exec.exec('hlint', arguments, {
listeners: {
stdout: (chunk) => chunks.push(chunk),
},
});
const hints = JSON.parse(chunks.join(''));
for (const hint of hints) {
const message = hint.to
? `-- Found:\n${hint.from}\n-- Perhaps:\n${hint.to}`
: `-- Remove:\n${hint.from}`;
core.warning(message, {
endColumn: hint.endColumn,
endLine: hint.endLine,
file: hint.file,
startColumn: hint.startColumn,
startLine: hint.startLine,
title: `${hint.severity}: ${hint.hint}`,
});
}
if (hints.length > 0) {
throw new Error(`${hints.length} hint${hints.length === 1 ? '' : 's'}`);
}
}
} catch (error) {
core.setFailed(error);
}
})();