-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
47 lines (37 loc) · 1.62 KB
/
main.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
import { select } from '@inquirer/prompts'
import { deepReadDir } from './src/utils'
import { existsSync, mkdirSync, writeFile } from 'fs'
const bootstrap = async () => {
const scriptPaths: string[] = (await deepReadDir('src/scripts/')).flat(Number.POSITIVE_INFINITY)
const sanitizedScripts = scriptPaths.map((script) => script.replace('src/scripts/', ''))
const services = new Set(sanitizedScripts.map((script) => script.split('/')[0]))
const service = await select({
message: 'Select the service you want to use',
choices: [...services].map((service) => ({ name: service, value: service })),
})
const availableScripts = sanitizedScripts.filter((script) => script.includes(service))
const availableScriptsNoExt = availableScripts.map(
(script) => script.split('.ts')[0].split(`${service}/`)[1],
)
const script = await select({
message: 'Select the script you want to run',
choices: availableScriptsNoExt.map((script) => ({ name: script, value: script })),
})
const scriptPath = scriptPaths.find((scriptPath) => scriptPath.includes(script))
try {
const scriptFunction = await import(`./${scriptPath}`)
const result = await scriptFunction.default()
if (typeof result !== 'undefined') {
console.log(result)
const path = `./src/outputs/${service}/${script}.json`
const folderPath = path.split('/').slice(0, -1).join('/')
if (!existsSync(folderPath)) mkdirSync(folderPath, { recursive: true })
writeFile(path, JSON.stringify(result, null, 2), (err) => {
if (err) console.log(err)
})
}
} catch (err) {
console.error(err)
}
}
bootstrap()