-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
58 lines (52 loc) · 1.68 KB
/
index.js
File metadata and controls
58 lines (52 loc) · 1.68 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
'use strict'
//----------------------------------------------------------
// modules
//----------------------------------------------------------
// node
const fs = require('fs')
const p = require('path')
// npm
const globby = require('globby')
//----------------------------------------------------------
// logic
//----------------------------------------------------------
/**
* @func writeOut
* @desc generate import statements and write to file
* @param {string} dir - top level directory containing dirs to index
* @param {string} subdir - directory to index contents of
* @returns {undefined}
*/
function writeOut(dir, subdir) {
return fs.writeFileSync(
p.join(dir, `${subdir}.js`),
globby.sync(p.join(dir, subdir, `*.js`))
.map(file => file.replace(dir, '.'))
.map(file => file.replace(`.js`, ''))
.map(file => `import '${file}'`)
.join('\n')
.concat('\n')
)
}
/**
* @func autoImport
* @desc get dirs to index and iteratively call writeOut
* @param {string} dir - top level directory containing dirs to index
* @param {string|array} ignore - subdirectories to ignore
* @returns {undefined}
*/
function autoImport(dir, ignore) {
if (ignore) {
const formatter = toIgnore => p.join(dir, toIgnore, p.sep)
ignore = typeof ignore === 'string'
? [formatter(ignore)]
: ignore.map(toIgnore => formatter(toIgnore))
}
return globby.sync(p.join(dir, '*', p.sep))
.filter(path => ignore ? ignore.every(_ => path !== _) : path)
.map(path => writeOut(dir, p.basename(path)))
}
//----------------------------------------------------------
// exports
//----------------------------------------------------------
module.exports = autoImport