-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
90 lines (72 loc) · 2.46 KB
/
main.js
File metadata and controls
90 lines (72 loc) · 2.46 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
#!/usr/bin/env node
import { readFileSync, existsSync, unlinkSync } from 'fs'
import { dirname, resolve } from 'path'
import { fileURLToPath } from 'url'
import { spawn } from 'child_process'
// packages
import chalk from 'chalk'
import inquirer from 'inquirer'
// utils
import { config } from './utils/config.js'
import { MODULES } from './utils/modules.js'
import { getProjectRoot } from './utils/getProjectRoot.js'
import { installPackages, uninstallPackages } from './utils/pkg.js'
const __dirname = dirname(fileURLToPath(import.meta.url))
async function main() {
console.clear()
console.log(`
${chalk.hex('#00ffff').bold(' ⚡ STYLE-FORGE ⚡ ')}
`)
const projectRoot = getProjectRoot()
if (!projectRoot) {
console.error('\n❌ Project root not found. Make sure you are inside a valid project with package.json\n')
process.exit(1)
}
const pkg = JSON.parse(readFileSync(resolve(projectRoot, 'package.json'), 'utf-8'))
const deps = pkg.dependencies || {}
const choices = MODULES.map(name => {
const version = deps[name] ? ` (${deps[name]})` : ''
return {
name: name + version,
value: name,
checked: !!deps[name],
}
})
const { selected } = await inquirer.prompt([
{
type: 'checkbox',
name: 'selected',
message: 'Select modules to install:',
choices,
},
])
const toInstall = selected.filter(m => !deps[m])
const toUninstall = MODULES.filter(m => deps[m] && !selected.includes(m))
if (toInstall.length > 0) await installPackages(toInstall)
if (toUninstall.length > 0) await uninstallPackages(toUninstall)
if (selected.length > 0) {
const buildPath = resolve(__dirname, './utils/builder.js')
const child = spawn('node', [buildPath], { stdio: 'inherit' })
child.on('exit', code => {
if (code !== 0) {
console.error('\n❌ Failed to build style-forge.css.\n')
}
})
} else {
const outputPath = resolve(projectRoot, `${config.output.dir}/${config.output.name}.css`)
if (existsSync(outputPath)) {
unlinkSync(outputPath)
console.log(`\n🧹 Removed ${config.output.name}.css (no modules selected).\n`)
} else {
console.log('\n❗ No modules selected, nothing to build.\n')
}
}
}
main().catch(err => {
if (err?.isTtyError || err?.message?.includes('SIGINT') || err.name === 'ExitPromptError') {
console.log('\n👋 Bye!\n')
process.exit(0)
}
console.error('\n❌ Unhandled error:', err)
process.exit(1)
})