|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { readFileSync, existsSync, unlinkSync } from 'fs' |
| 4 | +import { dirname, resolve } from 'path' |
| 5 | +import { fileURLToPath } from 'url' |
| 6 | +import { spawn } from 'child_process' |
| 7 | + |
| 8 | +// packages |
| 9 | +import chalk from 'chalk' |
| 10 | +import inquirer from 'inquirer' |
| 11 | + |
| 12 | +// utils |
| 13 | +import { config } from './utils/config.js' |
| 14 | +import { MODULES } from './utils/modules.js' |
| 15 | +import { getProjectRoot } from './utils/getProjectRoot.js' |
| 16 | +import { installPackages, uninstallPackages } from './utils/pkg.js' |
| 17 | + |
| 18 | +const __filename = fileURLToPath(import.meta.url) |
| 19 | +const __dirname = dirname(__filename) |
| 20 | + |
| 21 | +async function main() { |
| 22 | + console.clear() |
| 23 | + |
| 24 | + console.log(` |
| 25 | + ${chalk.hex('#00ffff').bold(' ⚡ STYLE-FORGE ⚡ ')} |
| 26 | + `) |
| 27 | + |
| 28 | + const projectRoot = getProjectRoot() |
| 29 | + |
| 30 | + if (!projectRoot) { |
| 31 | + console.error('\n❌ Project root not found. Make sure you are inside a valid project with package.json\n') |
| 32 | + process.exit(1) |
| 33 | + } |
| 34 | + |
| 35 | + const pkg = JSON.parse(readFileSync(resolve(projectRoot, 'package.json'), 'utf8')) |
| 36 | + const deps = pkg.dependencies || {} |
| 37 | + |
| 38 | + const choices = MODULES.map(name => { |
| 39 | + const version = deps[name] ? ` (${deps[name]})` : '' |
| 40 | + return { |
| 41 | + name: name + version, |
| 42 | + value: name, |
| 43 | + checked: !!deps[name], |
| 44 | + } |
| 45 | + }) |
| 46 | + |
| 47 | + const { selected } = await inquirer.prompt([ |
| 48 | + { |
| 49 | + type: 'checkbox', |
| 50 | + name: 'selected', |
| 51 | + message: 'Select modules to install:', |
| 52 | + choices, |
| 53 | + }, |
| 54 | + ]) |
| 55 | + |
| 56 | + const toInstall = selected.filter(m => !deps[m]) |
| 57 | + const toUninstall = MODULES.filter(m => deps[m] && !selected.includes(m)) |
| 58 | + |
| 59 | + if (toInstall.length > 0) await installPackages(toInstall) |
| 60 | + if (toUninstall.length > 0) await uninstallPackages(toUninstall) |
| 61 | + |
| 62 | + if (selected.length > 0) { |
| 63 | + const buildPath = resolve(__dirname, './utils/builder.js') |
| 64 | + const child = spawn('node', [buildPath], { stdio: 'inherit' }) |
| 65 | + |
| 66 | + child.on('exit', code => { |
| 67 | + if (code !== 0) { |
| 68 | + console.error('\n❌ Failed to build style-forge.css.\n') |
| 69 | + } |
| 70 | + }) |
| 71 | + } else { |
| 72 | + const outputPath = resolve(projectRoot, `${config.output.dir}/${config.output.name}.css`) |
| 73 | + |
| 74 | + if (existsSync(outputPath)) { |
| 75 | + unlinkSync(outputPath) |
| 76 | + console.log(`\n🧹 Removed ${config.output.name}.css (no modules selected).\n`) |
| 77 | + } else { |
| 78 | + console.log('\n❗ No modules selected, nothing to build.\n') |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +main().catch(err => { |
| 84 | + if (err?.isTtyError || err?.message?.includes('SIGINT') || err.name === 'ExitPromptError') { |
| 85 | + console.log('\n👋 Bye!\n') |
| 86 | + process.exit(0) |
| 87 | + } |
| 88 | + |
| 89 | + console.error('\n❌ Unhandled error:', err) |
| 90 | + process.exit(1) |
| 91 | +}) |
0 commit comments