-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathparams.js
82 lines (71 loc) · 1.87 KB
/
params.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
var fs = require('fs')
var path = require('path')
if (!process) {
process = require('process') // > v0.4.0
}
var rc = require('rc-loader')
var repeatString = require('repeat-string')
var editorconfig = require('editorconfig')
var defaultIndentWidth = repeatString(' ', 2)
function loadStylelint(from) {
var configName = 'stylelint'
return rc(configName, {}, { config: path.join(from, configName) }).rules || {}
}
function getIndentWidthFromEditorConfig(from, hasScss) {
var config
if (hasScss) {
config = editorconfig.parseSync(path.resolve(process.cwd(), '*.scss'))
} else {
config = editorconfig.parseSync(path.resolve(process.cwd(), '*.css'))
}
if (config == undefined) {
return defaultIndentWidth
}
switch (config.indent_style) {
case 'tab':
return '\t'
case 'space':
return repeatString(' ', config.indent_size)
default:
return defaultIndentWidth
}
}
function isEmptyObject(obj) {
for (var name in obj) {
return false
}
return true
}
function hasScssInWorkingDir(wd) {
var dirs = fs.readdirSync(wd)
return dirs.some(function (dir) {
return dir.match(/.*\.scss/)
})
}
function newParmas(from) {
var wd = path.dirname(from) || process.cwd()
var params = {}
var hasScss = hasScssInWorkingDir(wd)
params.hasScss = hasScss
var stylelintConfig = loadStylelint(wd)
if (isEmptyObject(stylelintConfig)) {
params.indentWidth = getIndentWidthFromEditorConfig(wd, hasScss)
params.stylelint = {}
return params
}
var indentaiton = stylelintConfig['indentation']
switch (typeof indentaiton) {
case 'string':
params.indentWidth = '\t'
break
case 'number':
params.indentWidth = repeatString(' ', indentaiton)
break
default:
params.indentWidth = defaultIndentWidth
break
}
params.stylelint = stylelintConfig
return params
}
module.exports = newParmas