-
Notifications
You must be signed in to change notification settings - Fork 2
/
commit.js
83 lines (71 loc) · 1.73 KB
/
commit.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
const {readdirSync, statSync} = require('fs')
const {join, sep} = require('path')
//
// Common scopes
/**
* Build scopes
* - commit
* - deps
* - deps-dev
* - format
* - lint
*/
const build = () => ['commit', 'deps', 'deps-dev', 'format', 'lint']
const scopes = {build}
//
// Scope helpers
/**
* Enumerate config files
*
* @example
*
* - `dir/foo.js`
* - `dir/bar.config.js`
* - `dir/baz.json`
* - `dir/qux.ts`
*
* `ls.config('./dir') → ['dir/foo', 'dir/bar', 'dir/baz', 'dir/qux']`
*
* @param {string} path - directory to enumerate
*/
const configs = path =>
readdirSync(path)
.filter(f => statSync(join(path, f)).isFile())
.map(
f =>
`${path.split(sep).reverse()[0]}/${f.replace(
/(\.config)?.(json|js|ts)/,
'',
)}`,
)
/**
* @typedef DirsOptions
* @property {string} [prefix] prefix to prepend to each scope entry (e.g:
* `{ prefix: 'prefix' }` becomes `type(prefix/scope)`)
* @property {RegExp | string | null} [exclude] expression for excluding
* directories, defaults to `^node_modules`
*/
/**
* Enumerate one level of directories
*
* @param {string} path - directory to enumerate
* @param {DirsOptions} [options] - options
*/
const dirs = (path, options) => {
const {exclude, prefix} = {exclude: /^node_modules/, ...options}
/**
* @param {string} f filename
*/
const test = f => {
if (!exclude) return true
return !(typeof exclude === 'string'
? f.includes(exclude)
: exclude.test(f))
}
return readdirSync(path)
.filter(f => statSync(join(path, f)).isDirectory() && test(f))
.map(item => (prefix ? `${prefix}/${item}` : item))
.map(item => item.toLowerCase())
}
const ls = {configs, dirs}
module.exports = {ls, scopes}