-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidentify-entities.ts
48 lines (43 loc) · 1.54 KB
/
identify-entities.ts
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
import fs from 'fs'
import path from 'path'
const srcDir = 'src'
function findFilesRecursive(dir: string): string[] {
const files: string[] = []
fs.readdirSync(dir).forEach(file => {
const filePath = path.join(dir, file) || ''
if (fs.statSync(filePath).isDirectory()) {
files.push(...findFilesRecursive(filePath))
} else {
files.push(filePath)
}
})
return files
}
function relativeToRoot(file): string {
let offset = 0
for(let i = file.indexOf(srcDir); i < file.length; i++) {
offset += file[i] === path.sep ? 1 : 0
}
if (offset === 1) {
return './'
} else if (offset > 1) {
return Array(offset - 1).fill('../').join('')
}
throw new Error('could not determine offset')
}
function identifyEntities() {
const files = findFilesRecursive(srcDir)
console.log(files)
let output = ''
const outputFile = files.find(file => file.endsWith(`${path.sep}entities.ts`))
console.log(outputFile)
const relativeRoot = relativeToRoot(outputFile)
for (let file of files.filter(file => file.endsWith('.entity.ts'))) {
const className = (fs.readFileSync(file, 'utf-8').match(/class (\S+)\s?{/))![1]
const pathSep = (path.sep === '/') ? path.sep : '\\\\'
const relativePath = file.slice(file.indexOf(srcDir) + srcDir.length + 1, -3).replace(new RegExp(pathSep, 'g'), '/')
output += `export { ${className } } from '${relativeRoot + relativePath}'\n`
}
fs.writeFileSync(outputFile!, output, 'utf-8')
}
identifyEntities()