-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
51 lines (42 loc) · 1.54 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
const { existsSync, statSync } = require('fs')
const { join } = require('path')
const glob = require('fast-glob')
const Lintspaces = require('lintspaces')
const issues = require('./lib/issues')
// lintspaces configuration
const ignores = [
'js-comments',
'c-comments',
'java-comments',
'as-comments',
'xml-comments',
'html-comments',
'python-comments',
'ruby-comments',
'applescript-comments'
]
module.exports = (config = {}, output = process.stdout, cwd = '/code') => {
// default values
let exclude = config.exclude_paths || []
let include = config.include_paths || ['**']
const editorconfig = config.editorconfig || join(cwd, '.editorconfig')
// prep paths for use with glob
// include = include.map(path => path.replace(/^\/+/, ''))
include = include.map(path => existsSync(path) && statSync(path).isDirectory() ? join(path, '**') : path)
exclude = exclude.map(path => `!${join(path, '**')}`)
return new Promise((resolve, reject) => {
if (!existsSync(editorconfig) || !statSync(editorconfig).isFile()) {
return reject(new Error('.editorconfig file required'))
}
// setup validator
const lintspaces = new Lintspaces({ editorconfig, ignores })
const stream = glob.stream(include.concat(exclude), { cwd, absolute: true })
stream.once('error', reject)
stream.on('data', file => lintspaces.validate(file))
stream.once('end', () => {
const files = lintspaces.getInvalidFiles()
issues(files, cwd).forEach(issue => output.write(JSON.stringify(issue) + '\0'))
resolve()
})
})
}