This repository was archived by the owner on Apr 10, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstacks.js
More file actions
executable file
·125 lines (102 loc) · 2.87 KB
/
stacks.js
File metadata and controls
executable file
·125 lines (102 loc) · 2.87 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
#!/usr/bin/env node
import { readFileSync, statSync } from 'fs'
import { spawn } from 'child_process'
import { load } from 'js-yaml'
function loadStacks() {
try {
const yamlContent = readFileSync("stacks.yaml", 'utf8')
const parsed = load(yamlContent)
return parsed.default || []
} catch (error) {
console.error("Error loading stacks.yaml:", error.message)
return []
}
}
function folderExists(path) {
try {
const stat = statSync(path)
return stat.isDirectory()
} catch {
return false
}
}
function runCommand(cmd, args = []) {
return new Promise((resolve) => {
const child = spawn(cmd, args, {
stdio: ['inherit', 'pipe', 'pipe']
})
let stdout = ''
let stderr = ''
child.stdout.on('data', (data) => {
stdout += data.toString()
})
child.stderr.on('data', (data) => {
stderr += data.toString()
})
child.on('close', (code) => {
if (code !== 0) {
console.error(`Command failed: ${cmd} ${args.join(' ')}`)
console.error(`Error: ${stderr.trim()}`)
resolve(false)
} else {
if (stdout.trim()) {
console.log(stdout.trim())
}
resolve(true)
}
})
child.on('error', (error) => {
console.error(`Failed to run command: ${cmd} ${args.join(' ')}`, error.message)
resolve(false)
})
})
}
async function processStacks() {
console.log(`[${new Date().toISOString()}] Checking stacks...`)
const stacks = loadStacks()
if (stacks.length === 0) {
console.log("No stacks found in configuration")
return
}
for (const stack of stacks) {
const { name, url } = stack
const stackPath = `/stacks/${name}`
console.log(`Checking stack: ${name}`)
if (!folderExists(stackPath)) {
console.log(`Stack folder ${stackPath} does not exist, cloning...`)
// Run jj clone with depth 10
const cloneSuccess = await runCommand("jj", [
"git", "clone",
"--depth", "10",
url,
stackPath
])
if (!cloneSuccess) {
console.error(`Failed to clone ${name}`)
continue
}
console.log(`Successfully cloned ${name}`)
} else {
console.log(`Stack folder ${stackPath} already exists`)
}
// TODO: hanlde watch mode
// Run docker compose up in the stack directory
console.log(`Running compose up for ${name}...`)
const composeSuccess = await runCommand("docker", [
"compose",
"-f", `${stackPath}/docker-compose.yaml`,
"up", "-d"
])
if (composeSuccess) {
console.log(`Successfully started ${name}`)
} else {
console.error(`Failed to start ${name}`)
}
}
console.log("Stack processing complete\n")
}
async function main() {
// Run immediately on start
await processStacks()
}
main().catch(console.error)