-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup.js
More file actions
176 lines (153 loc) · 7.8 KB
/
setup.js
File metadata and controls
176 lines (153 loc) · 7.8 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env node
/**
* Kompl one-command setup
* Usage: node setup.js
*
* Does everything:
* 1. Checks Docker Desktop is running
* 2. Creates .env from .env.example (if not already present)
* 3. Prompts for Gemini + Firecrawl API keys and writes them to .env
* 4. Installs and globally links the kompl CLI (npm link)
* 5. Writes ~/.kompl/config.json so `kompl` commands work immediately
* 6. Runs `docker compose up --build -d` to start the full stack
*
* Requires: Docker Desktop running, Node >= 18
*/
'use strict'
const { execSync } = require('child_process')
const fs = require('fs')
const os = require('os')
const path = require('path')
const readline = require('readline')
const ROOT = __dirname
const green = (s) => `\x1b[32m${s}\x1b[0m`
const yellow = (s) => `\x1b[33m${s}\x1b[0m`
const bold = (s) => `\x1b[1m${s}\x1b[0m`
const dim = (s) => `\x1b[2m${s}\x1b[0m`
function ask(rl, question) {
return new Promise(resolve => rl.question(question, resolve))
}
function run(cmd, cwd = ROOT) {
execSync(cmd, { cwd, stdio: 'inherit', shell: true })
}
async function main() {
console.log(bold('\n Kompl — setup\n'))
// ── 1. Check Docker ────────────────────────────────────────────────────────
process.stdout.write(' Checking Docker... ')
try {
execSync('docker info', { stdio: 'pipe' })
console.log(green('running'))
} catch (err) {
console.log('')
const stderr = (err && err.stderr) ? err.stderr.toString() : ''
if (stderr.includes('permission denied')) {
console.error(' Error: Docker daemon is running but you do not have permission to use it.')
console.error(' Fix: add your user to the docker group, then log out and back in:')
console.error(' sudo usermod -aG docker $USER\n')
} else {
console.error(' Error: Docker is not running.')
console.error(' Start Docker (Docker Desktop on Windows/Mac, or the Docker daemon on Linux) and run this script again.\n')
}
process.exit(1)
}
// ── 2. Create .env ─────────────────────────────────────────────────────────
const envSrc = path.join(ROOT, '.env.example')
const envDest = path.join(ROOT, '.env')
if (!fs.existsSync(envDest)) {
fs.copyFileSync(envSrc, envDest)
console.log(' Created .env from .env.example')
} else {
console.log(' .env already exists — keeping it')
}
// ── 3. Prompt for API keys ─────────────────────────────────────────────────
let env = fs.readFileSync(envDest, 'utf8')
const alreadyHasGemini = /^GEMINI_API_KEY=.+$/m.test(env)
const alreadyHasFirecrawl = /^FIRECRAWL_API_KEY=.+$/m.test(env)
if (alreadyHasGemini && alreadyHasFirecrawl) {
console.log(' API keys already set in .env — skipping prompts')
} else {
console.log('\n You need two API keys (both have free tiers):')
console.log(dim(' Gemini: https://aistudio.google.com/apikey (free works for the demo; paid Tier 1 strongly recommended for real use)'))
console.log(dim(' Firecrawl: https://firecrawl.dev (500 scrapes/month free)\n'))
const rl = readline.createInterface({ input: process.stdin, output: process.stdout })
if (!alreadyHasGemini) {
const key = (await ask(rl, ' Gemini API key: ')).trim()
if (!key) { console.error(' Error: Gemini API key is required.'); rl.close(); process.exit(1) }
env = env.replace(/^GEMINI_API_KEY=.*$/m, `GEMINI_API_KEY=${key}`)
}
if (!alreadyHasFirecrawl) {
const key = (await ask(rl, ' Firecrawl API key: ')).trim()
if (!key) { console.error(' Error: Firecrawl API key is required.'); rl.close(); process.exit(1) }
env = env.replace(/^FIRECRAWL_API_KEY=.*$/m, `FIRECRAWL_API_KEY=${key}`)
}
rl.close()
fs.writeFileSync(envDest, env, 'utf8')
console.log(green('\n API keys saved to .env'))
}
// ── 4. Install + link CLI ──────────────────────────────────────────────────
console.log('\n Installing kompl CLI...')
const cliDir = path.join(ROOT, 'cli')
run('npm install', cliDir)
try {
run('npm link', cliDir)
} catch (err) {
const msg = (err && err.message) ? err.message : String(err)
if (msg.includes('EACCES') || msg.includes('permission denied')) {
console.error('\n Error: npm link failed — permission denied.')
console.error(' Your npm global prefix requires elevated access. Fix with:')
console.error(' mkdir -p ~/.npm-global')
console.error(' npm config set prefix ~/.npm-global')
console.error(' echo \'export PATH="$HOME/.npm-global/bin:$PATH"\' >> ~/.bashrc # or ~/.zshrc')
console.error(' source ~/.bashrc')
console.error(' Then re-run: node setup.js\n')
} else {
console.error('\n Error: npm link failed:', msg)
}
process.exit(1)
}
// Verify the binary is reachable in PATH
try {
execSync('kompl --version', { stdio: 'ignore' })
console.log(green(' kompl CLI installed'))
} catch {
console.log(yellow(' kompl CLI installed but not in PATH.'))
console.log(dim(' You may need to open a new terminal, or add the npm global bin to your PATH.'))
console.log(dim(' Run: npm config get prefix → then add <prefix>/bin to PATH.\n'))
}
// ── 5. Write ~/.kompl/config.json ──────────────────────────────────────────
const configDir = path.join(os.homedir(), '.kompl')
const configFile = path.join(configDir, 'config.json')
fs.mkdirSync(configDir, { recursive: true })
fs.writeFileSync(configFile, JSON.stringify({ projectDir: ROOT, port: 3000 }, null, 2), 'utf8')
console.log(' CLI configured → ' + dim(configFile))
// ── 5b. Write KOMPL_TIMEZONE to .env ───────────────────────────────────────
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone
let envContent = fs.readFileSync(envDest, 'utf8')
if (envContent.includes('KOMPL_TIMEZONE=')) {
envContent = envContent.replace(/^KOMPL_TIMEZONE=.*/m, `KOMPL_TIMEZONE=${timezone}`)
} else {
const prefix = envContent.length > 0 && !envContent.endsWith('\n') ? '\n' : ''
envContent += `${prefix}KOMPL_TIMEZONE=${timezone}\n`
}
fs.writeFileSync(envDest, envContent, 'utf8')
console.log(' Timezone → ' + dim(timezone))
// ── 6. Start stack ─────────────────────────────────────────────────────────
console.log('\n Starting Kompl stack...')
console.log(dim(' (First run builds Docker images — ~5–10 min)'))
console.log(dim(' Subsequent starts take ~15 seconds.\n'))
// Prefer docker compose v2 plugin; fall back to v1 standalone binary
let compose = 'docker compose'
try { execSync('docker compose version', { stdio: 'ignore' }) }
catch { compose = 'docker-compose' }
run(`${compose} up --build -d`)
console.log(bold(green('\n Kompl is starting!')))
console.log('\n Check when it\'s ready: ' + bold('kompl status'))
console.log(' Open in browser: ' + bold('kompl open'))
console.log(' Stream logs: ' + bold('kompl logs'))
console.log(' Stop: ' + bold('kompl stop') + '\n')
console.log(dim(' App will be available at http://localhost:3000\n'))
}
main().catch(err => {
console.error('\n Setup failed:', err.message)
process.exit(1)
})