forked from ahmadnassri/echint
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
137 lines (109 loc) · 3.48 KB
/
index.js
File metadata and controls
137 lines (109 loc) · 3.48 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
'use strict'
const debuglog = require('util').debuglog
const dotenv = require('dotenv')
const fs = require('fs')
const glob = require('glob')
const Lintspaces = require('lintspaces')
const minimatch = require('minimatch')
const path = require('path')
const pkg = require('pkg-config')
const debug = debuglog('echint')
const config = pkg('echint', { root: false })
const DEFAULT_PATTERN = '**/*'
const DEFAULT_IGNORE_PATTERNS = [
'coverage/**',
'node_modules/**',
'bower_components/**',
'**/*.{jpg,png,gif,ico}'
]
dotenv.config({ silent: true })
dotenv.config({ silent: true, path: '.env.' + process.env.NODE_ENV })
module.exports = function echint (files, options, cb) {
// organize arguments
if (typeof arguments[1] === 'function') {
cb = arguments[1]
options = {}
}
// organize arguments
if (!Array.isArray(arguments[0]) && typeof arguments[0] === 'object') {
options = arguments[0]
files = undefined
}
// default values
const defaults = {
config: process.env.ECHINT_CONFIG || '.editorconfig',
ignore: process.env.ECHINT_IGNORE ? [process.env.ECHINT_IGNORE] : DEFAULT_IGNORE_PATTERNS,
pattern: process.env.ECHINT_PATTERN || DEFAULT_PATTERN,
readPackage: process.env.ECHINT_READ_PACKAGE ? (process.env.ECHINT_READ_PACKAGE === 'true') : (options ? options.readPackage : true)
}
// initialize options
const opts = Object.assign({}, defaults)
// overwrite from package.json?
if (opts.readPackage && config) {
debug('package.json config found')
Object.assign(opts, config)
}
// overwrite from local options again
Object.assign(opts, options)
if (opts.extends) {
debug('extends found')
const extendsDir = path.join(process.cwd(), 'node_modules', opts.extends.replace(/^(echint-config-)?/, 'echint-config-'))
const extendsMain = pkg('main', { cwd: extendsDir, root: false })
opts.config = path.join(extendsDir, extendsMain || '.editorconfig')
}
debug('starting with options: config=%s ignore=%j', opts.config, opts.ignore)
// setup validator
const lintspaces = new Lintspaces({
editorconfig: opts.config,
ignores: [
'js-comments',
'c-comments',
'java-comments',
'as-comments',
'xml-comments',
'html-comments',
'python-comments',
'ruby-comments',
'applescript-comments'
]
})
// files to check
if (!files || !files.length) {
debug('no file list given, using match pattern instead: "%s"', opts.pattern)
files = glob.sync(opts.pattern, {
ignore: opts.ignore.concat(DEFAULT_IGNORE_PATTERNS),
nodir: true
})
// since glob already ignored files for us, do NOT check later
opts.ignore = []
}
// Run validation
files.forEach(file => {
if (opts.ignore.some(pattern => minimatch(file, pattern))) {
debug('[%s] is ignored"', file)
return
}
// confirm file exists
try {
if (!fs.statSync(file).isFile()) throw Error('is not a file')
} catch (e) {
return debug('✖ [%s] is not a file ', file)
}
// where the magic happens
lintspaces.validate(file)
// debugging
if (Object.keys(lintspaces.getInvalidLines(file)).length) {
debug('✖ [%s] is invalid', file)
} else {
debug('✓ [%s] is valid', file)
}
})
// get full list of errors
const errors = lintspaces.getInvalidFiles()
// determine if test was a success
const valid = Object.keys(errors).length === 0
if (typeof cb === 'function') {
cb(valid ? null : errors, valid)
}
return valid
}