Repository navigation
Expand file tree
/
Copy pathimportBuilder.js
More file actions
92 lines (83 loc) · 2.58 KB
/
Copy pathimportBuilder.js
File metadata and controls
92 lines (83 loc) · 2.58 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
const lineReader = require('line-reader')
const fs = require('fs')
const Promise = require('bluebird')
const { promisify } = require('util')
const writeFileAsync = promisify(fs.writeFile)
const buildImports = () => {
const path = `${__dirname}/components`
const output = `${__dirname}/components/index.js`
const getDirectories = (source) =>
fs
.readdirSync(source, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name)
const dirs = getDirectories(path)
fs.unlink(output, (err) => {
if (err) {
console.log(err)
}
}) // delete file
const writeExports = () => {
fs.appendFile(output, `\nexport {\n`, (err) => {
if (err) {
console.error(err)
throw err
}
})
dirs.forEach((directory, index) => {
const eachLine = Promise.promisify(lineReader.eachLine)
eachLine(`${path}/${directory}/index.js`, (line, notLast) => {
const startIndex = line.indexOf('default as') + 'default as'.length
const endIndex = line.indexOf('}')
const component = line.substring(startIndex, endIndex).trim()
fs.appendFile(output, `\t${component},\n`, (err) => {
if (err) throw err
})
})
.then(() => {
if (index === dirs.length - 1) {
setTimeout(() => {
fs.appendFile(output, `}`, (err) => {
if (err) throw err
})
}, 200)
}
})
.catch((err) => {
console.error(err)
})
})
}
const writeImports = () => {
dirs.forEach((directory, index) => {
const eachLine = Promise.promisify(lineReader.eachLine)
let result = 'import {'
eachLine(`${path}/${directory}/index.js`, (line, notLast) => {
const startIndex = line.indexOf('default as') + 'default as'.length
const endIndex = line.indexOf('}')
const component = line.substring(startIndex, endIndex).trim()
if (!notLast) result += ` ${component},`
if (notLast) result += ` ${component}`
})
.then(() => {
console.log(`Importing /component/${directory}`)
result += ` } from './${directory}'`
fs.appendFile(output, `${result}\n`, (err) => {
if (err) throw err
})
if (index === dirs.length - 1) {
setTimeout(() => {
console.log('writing exports')
writeExports()
}, 500)
}
})
.catch((err) => {
console.error(err)
})
})
}
writeImports()
//
}
buildImports()