-
Notifications
You must be signed in to change notification settings - Fork 59
/
lint.js
118 lines (108 loc) · 3.1 KB
/
lint.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
/* eslint-disable no-console */
import path from 'path'
import { fileURLToPath } from 'url'
import { ESLint } from 'eslint'
import { execa } from 'execa'
import fs from 'fs-extra'
import { globby } from 'globby'
import kleur from 'kleur'
import Listr from 'listr'
import merge from './utils/merge-options.js'
import { fromRoot, readJson, hasTsconfig, isTypescript, findBinary, hasDocCheck } from './utils.js'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
/**
* @typedef {import("./types").GlobalOptions} GlobalOptions
* @typedef {import("./types").LintOptions} LintOptions
* @typedef {import("listr").ListrTaskWrapper} Task
* @typedef {import("./types").TSOptions} TSOptions
*/
const tasks = new Listr(
[
{
title: 'eslint',
/**
*
* @param {GlobalOptions & LintOptions} ctx
* @param {Task} task
*/
task: async (ctx, task) => {
const eslint = new ESLint({
fix: ctx.fix,
baseConfig: { extends: 'ipfs' },
useEslintrc: true
})
const results = await eslint.lintFiles(await globby(ctx.files))
const formatter = await eslint.loadFormatter('unix')
const hasErrors = ESLint.getErrorResults(results).length > 0
if (ctx.fix) {
await ESLint.outputFixes(results)
}
if (!ctx.silent && hasErrors) {
const output = await formatter.format(results)
console.error(output
.split('\n')
.map(line => {
if (line.includes('[Error')) {
return kleur.red(line)
}
return line
})
.join('\n'))
}
if (hasErrors) {
throw new Error('Lint errors')
}
}
},
{
title: 'tsc',
/**
* @param {GlobalOptions & LintOptions} ctx
*/
enabled: ctx => hasTsconfig && !isTypescript,
/**
* @param {GlobalOptions & LintOptions} ctx
* @param {Task} task
*/
task: async (ctx, task) => {
const configPath = fromRoot('tsconfig-check.aegir.json')
const userTSConfig = readJson(fromRoot('tsconfig.json'))
try {
fs.writeJsonSync(
configPath,
merge.apply({ concatArrays: true }, [
userTSConfig,
{
compilerOptions: {
noEmit: true,
emitDeclarationOnly: false
}
}
])
)
await execa(findBinary('tsc'), ['--build', configPath], {
localDir: path.join(__dirname, '../..'),
preferLocal: true,
stdio: 'inherit'
})
} finally {
fs.removeSync(configPath)
fs.removeSync(fromRoot('dist', 'tsconfig-check.aegir.tsbuildinfo'))
}
}
},
{
title: 'doc-check',
enabled: () => hasDocCheck,
task: async () => {
await execa('npm', ['run', 'doc-check', '--if-present', '--', '--publish', 'false'], {
stdio: 'inherit'
})
}
}
],
{
renderer: 'verbose'
}
)
export default tasks