@zabaca/zbc 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/zbc.js +2 -0
- package/package.json +27 -0
- package/src/commands/add.ts +139 -0
- package/src/commands/apply.ts +47 -0
- package/src/commands/destroy.ts +42 -0
- package/src/commands/init.ts +129 -0
- package/src/engine/apply.ts +35 -0
- package/src/engine/destroy.ts +29 -0
- package/src/engine/discover.ts +33 -0
- package/src/engine/resolve.ts +78 -0
- package/src/engine/secrets.ts +33 -0
- package/src/index.ts +22 -0
- package/src/infra-types.ts +38 -0
- package/src/utils/copy-template.ts +82 -0
- package/src/utils/detect-mode.ts +54 -0
- package/src/utils/find-project-root.ts +18 -0
- package/src/utils/load-config.ts +12 -0
- package/templates/gitignore +8 -0
- package/templates/infra/modules/turso/index.ts +118 -0
- package/templates/infra/modules/turso/registry.json +17 -0
- package/templates/infra/modules/vercel/index.ts +358 -0
- package/templates/infra/modules/vercel/registry.json +15 -0
- package/templates/infra/package.json +15 -0
- package/templates/infra/src/config.ts +8 -0
- package/templates/infra/src/define-module.ts +34 -0
- package/templates/infra/src/index.ts +3 -0
- package/templates/infra/src/types.ts +36 -0
- package/templates/infra/tsconfig.json +9 -0
- package/templates/root/package.json +10 -0
- package/templates/root/tsconfig.json +17 -0
- package/templates/sops.yaml +11 -0
- package/templates/workflows/preview.yml +52 -0
- package/templates/workflows/production.yml +28 -0
- package/templates/zbc.config.ts +6 -0
package/bin/zbc.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@zabaca/zbc",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Zabaca stack CLI — scaffold and deploy infrastructure",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"bin": {
|
|
8
|
+
"zbc": "./bin/zbc.js"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"bin",
|
|
12
|
+
"src",
|
|
13
|
+
"templates"
|
|
14
|
+
],
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"citty": "^0.1.6",
|
|
20
|
+
"sops-age": "^4.0.2",
|
|
21
|
+
"yaml": "^2.8.3"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@types/bun": "^1.3.12",
|
|
25
|
+
"vercel": "^50.44.0"
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import * as path from 'node:path'
|
|
2
|
+
import * as fs from 'node:fs/promises'
|
|
3
|
+
import { spawn } from 'node:child_process'
|
|
4
|
+
import { defineCommand } from 'citty'
|
|
5
|
+
import { findProjectRoot } from '../utils/find-project-root'
|
|
6
|
+
import { bundledModulesCandidates, copyTemplateFile } from '../utils/copy-template'
|
|
7
|
+
|
|
8
|
+
interface RegistryFile {
|
|
9
|
+
path: string
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface RegistryManifest {
|
|
13
|
+
name: string
|
|
14
|
+
description?: string
|
|
15
|
+
files: RegistryFile[]
|
|
16
|
+
dependencies?: Record<string, string>
|
|
17
|
+
devDependencies?: Record<string, string>
|
|
18
|
+
optionalDependencies?: Record<string, string>
|
|
19
|
+
secrets?: string[]
|
|
20
|
+
signupUrl?: string
|
|
21
|
+
tokenUrl?: string
|
|
22
|
+
instructions?: string
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async function resolveBundledModuleDir(name: string): Promise<string> {
|
|
26
|
+
for (const candidate of bundledModulesCandidates()) {
|
|
27
|
+
const dir = path.join(candidate, name)
|
|
28
|
+
const registry = path.join(dir, 'registry.json')
|
|
29
|
+
if (await Bun.file(registry).exists()) return dir
|
|
30
|
+
}
|
|
31
|
+
throw new Error(`Module "${name}" not found in built-in registry. Available: turso, vercel.`)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function bunAdd(
|
|
35
|
+
cwd: string,
|
|
36
|
+
deps: Record<string, string>,
|
|
37
|
+
flag?: '--dev' | '--optional',
|
|
38
|
+
): Promise<void> {
|
|
39
|
+
const entries = Object.entries(deps)
|
|
40
|
+
if (entries.length === 0) return Promise.resolve()
|
|
41
|
+
|
|
42
|
+
const args = ['add']
|
|
43
|
+
if (flag) args.push(flag)
|
|
44
|
+
for (const [name, version] of entries) {
|
|
45
|
+
args.push(`${name}@${version}`)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
console.log(` bun ${args.join(' ')} (in ${path.relative(process.cwd(), cwd) || '.'})`)
|
|
49
|
+
|
|
50
|
+
return new Promise((resolve, reject) => {
|
|
51
|
+
const proc = spawn('bun', args, { cwd, stdio: 'inherit' })
|
|
52
|
+
proc.on('close', (code) => {
|
|
53
|
+
if (code === 0) resolve()
|
|
54
|
+
else reject(new Error(`bun add exited with code ${code}`))
|
|
55
|
+
})
|
|
56
|
+
proc.on('error', reject)
|
|
57
|
+
})
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export const addCommand = defineCommand({
|
|
61
|
+
meta: {
|
|
62
|
+
name: 'add',
|
|
63
|
+
description: 'Vendor a built-in infra module into packages/infra/modules/',
|
|
64
|
+
},
|
|
65
|
+
args: {
|
|
66
|
+
module: {
|
|
67
|
+
type: 'positional',
|
|
68
|
+
description: 'Module name (e.g. turso, vercel)',
|
|
69
|
+
required: true,
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
async run({ args }) {
|
|
73
|
+
const moduleName = args.module
|
|
74
|
+
const projectRoot = await findProjectRoot()
|
|
75
|
+
const infraDir = path.join(projectRoot, 'packages/infra')
|
|
76
|
+
|
|
77
|
+
if (!(await Bun.file(path.join(infraDir, 'package.json')).exists())) {
|
|
78
|
+
console.error('✗ packages/infra/ not found. Run `zbc init` first.')
|
|
79
|
+
process.exit(1)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const sourceDir = await resolveBundledModuleDir(moduleName)
|
|
83
|
+
const destDir = path.join(infraDir, 'modules', moduleName)
|
|
84
|
+
|
|
85
|
+
if (await Bun.file(path.join(destDir, 'registry.json')).exists()) {
|
|
86
|
+
console.log(
|
|
87
|
+
`✓ ${moduleName} already installed at packages/infra/modules/${moduleName}/ — skipping`,
|
|
88
|
+
)
|
|
89
|
+
return
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const registry = (await Bun.file(
|
|
93
|
+
path.join(sourceDir, 'registry.json'),
|
|
94
|
+
).json()) as RegistryManifest
|
|
95
|
+
|
|
96
|
+
console.log(`zbc add: ${moduleName}`)
|
|
97
|
+
|
|
98
|
+
// Copy module files
|
|
99
|
+
await fs.mkdir(destDir, { recursive: true })
|
|
100
|
+
for (const f of registry.files) {
|
|
101
|
+
await copyTemplateFile(path.join(sourceDir, f.path), path.join(destDir, f.path))
|
|
102
|
+
}
|
|
103
|
+
// Always copy registry.json (used as install marker + future upgrade ref)
|
|
104
|
+
await copyTemplateFile(
|
|
105
|
+
path.join(sourceDir, 'registry.json'),
|
|
106
|
+
path.join(destDir, 'registry.json'),
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
// Install deps in packages/infra
|
|
110
|
+
if (registry.dependencies) {
|
|
111
|
+
await bunAdd(infraDir, registry.dependencies)
|
|
112
|
+
}
|
|
113
|
+
if (registry.devDependencies) {
|
|
114
|
+
await bunAdd(infraDir, registry.devDependencies, '--dev')
|
|
115
|
+
}
|
|
116
|
+
if (registry.optionalDependencies) {
|
|
117
|
+
await bunAdd(infraDir, registry.optionalDependencies, '--optional')
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Post-install message
|
|
121
|
+
console.log('')
|
|
122
|
+
console.log(`✓ ${moduleName} installed`)
|
|
123
|
+
console.log('')
|
|
124
|
+
if (registry.secrets && registry.secrets.length > 0) {
|
|
125
|
+
console.log('Required secrets:')
|
|
126
|
+
for (const s of registry.secrets) console.log(` - ${s}`)
|
|
127
|
+
}
|
|
128
|
+
if (registry.signupUrl) console.log(`Sign up: ${registry.signupUrl}`)
|
|
129
|
+
if (registry.tokenUrl) console.log(`Get token: ${registry.tokenUrl}`)
|
|
130
|
+
if (registry.instructions) {
|
|
131
|
+
console.log('')
|
|
132
|
+
console.log(registry.instructions)
|
|
133
|
+
}
|
|
134
|
+
console.log('')
|
|
135
|
+
console.log(
|
|
136
|
+
`Next: create an instance file under packages/infra/environments/<env>/ that imports from ../../modules/${moduleName}.`,
|
|
137
|
+
)
|
|
138
|
+
},
|
|
139
|
+
})
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import * as path from 'node:path'
|
|
2
|
+
import { defineCommand } from 'citty'
|
|
3
|
+
import { findProjectRoot } from '../utils/find-project-root'
|
|
4
|
+
import { loadConfig } from '../utils/load-config'
|
|
5
|
+
import { applyEnvironment } from '../engine/apply'
|
|
6
|
+
|
|
7
|
+
export const applyCommand = defineCommand({
|
|
8
|
+
meta: {
|
|
9
|
+
name: 'apply',
|
|
10
|
+
description: 'Apply infrastructure for an environment',
|
|
11
|
+
},
|
|
12
|
+
args: {
|
|
13
|
+
env: {
|
|
14
|
+
type: 'positional',
|
|
15
|
+
description: 'Environment name (e.g., production, preview)',
|
|
16
|
+
required: true,
|
|
17
|
+
},
|
|
18
|
+
instance: {
|
|
19
|
+
type: 'positional',
|
|
20
|
+
description: 'Specific instance to apply (+ its dependencies)',
|
|
21
|
+
required: false,
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
async run({ args }) {
|
|
25
|
+
const projectRoot = await findProjectRoot()
|
|
26
|
+
const config = await loadConfig(projectRoot)
|
|
27
|
+
|
|
28
|
+
if (!config.environments.includes(args.env)) {
|
|
29
|
+
console.error(
|
|
30
|
+
`Unknown environment: "${args.env}". Available: ${config.environments.join(', ')}`,
|
|
31
|
+
)
|
|
32
|
+
process.exit(1)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const envDir = path.join(
|
|
36
|
+
projectRoot,
|
|
37
|
+
'packages',
|
|
38
|
+
'infra',
|
|
39
|
+
'environments',
|
|
40
|
+
args.env,
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
console.log(`Applying ${args.env}${args.instance ? ` (instance: ${args.instance})` : ''}...`)
|
|
44
|
+
await applyEnvironment(projectRoot, envDir, args.instance)
|
|
45
|
+
console.log('\nDone.')
|
|
46
|
+
},
|
|
47
|
+
})
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import * as path from 'node:path'
|
|
2
|
+
import { defineCommand } from 'citty'
|
|
3
|
+
import { findProjectRoot } from '../utils/find-project-root'
|
|
4
|
+
import { loadConfig } from '../utils/load-config'
|
|
5
|
+
import { destroyEnvironment } from '../engine/destroy'
|
|
6
|
+
|
|
7
|
+
export const destroyCommand = defineCommand({
|
|
8
|
+
meta: {
|
|
9
|
+
name: 'destroy',
|
|
10
|
+
description: 'Tear down ephemeral resources for an environment',
|
|
11
|
+
},
|
|
12
|
+
args: {
|
|
13
|
+
env: {
|
|
14
|
+
type: 'positional',
|
|
15
|
+
description: 'Environment name (e.g., preview)',
|
|
16
|
+
required: true,
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
async run({ args }) {
|
|
20
|
+
const projectRoot = await findProjectRoot()
|
|
21
|
+
const config = await loadConfig(projectRoot)
|
|
22
|
+
|
|
23
|
+
if (!config.environments.includes(args.env)) {
|
|
24
|
+
console.error(
|
|
25
|
+
`Unknown environment: "${args.env}". Available: ${config.environments.join(', ')}`,
|
|
26
|
+
)
|
|
27
|
+
process.exit(1)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const envDir = path.join(
|
|
31
|
+
projectRoot,
|
|
32
|
+
'packages',
|
|
33
|
+
'infra',
|
|
34
|
+
'environments',
|
|
35
|
+
args.env,
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
console.log(`Destroying ${args.env}...`)
|
|
39
|
+
await destroyEnvironment(projectRoot, envDir)
|
|
40
|
+
console.log('\nDone.')
|
|
41
|
+
},
|
|
42
|
+
})
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import * as path from 'node:path'
|
|
2
|
+
import * as fs from 'node:fs/promises'
|
|
3
|
+
import { defineCommand } from 'citty'
|
|
4
|
+
import { detectMode } from '../utils/detect-mode'
|
|
5
|
+
import { copyTemplateDir, copyTemplateFile, templatesRoot } from '../utils/copy-template'
|
|
6
|
+
|
|
7
|
+
async function ensureDir(dir: string): Promise<void> {
|
|
8
|
+
await fs.mkdir(dir, { recursive: true })
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
async function writeIfMissing(filePath: string, body: string): Promise<void> {
|
|
12
|
+
const file = Bun.file(filePath)
|
|
13
|
+
if (await file.exists()) {
|
|
14
|
+
console.log(` skip ${path.relative(process.cwd(), filePath)} (exists)`)
|
|
15
|
+
return
|
|
16
|
+
}
|
|
17
|
+
await ensureDir(path.dirname(filePath))
|
|
18
|
+
await Bun.write(filePath, body)
|
|
19
|
+
console.log(` write ${path.relative(process.cwd(), filePath)}`)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const initCommand = defineCommand({
|
|
23
|
+
meta: {
|
|
24
|
+
name: 'init',
|
|
25
|
+
description: 'Scaffold zbc infrastructure into the current repo',
|
|
26
|
+
},
|
|
27
|
+
args: {
|
|
28
|
+
project: {
|
|
29
|
+
type: 'positional',
|
|
30
|
+
description: 'Project name (defaults to current directory name)',
|
|
31
|
+
required: false,
|
|
32
|
+
},
|
|
33
|
+
ci: {
|
|
34
|
+
type: 'string',
|
|
35
|
+
description: 'CI provider to scaffold (e.g. "github")',
|
|
36
|
+
required: false,
|
|
37
|
+
},
|
|
38
|
+
'no-sops': {
|
|
39
|
+
type: 'boolean',
|
|
40
|
+
description: 'Skip writing .sops.yaml',
|
|
41
|
+
default: false,
|
|
42
|
+
},
|
|
43
|
+
'non-interactive': {
|
|
44
|
+
type: 'boolean',
|
|
45
|
+
description: 'Fail instead of prompting',
|
|
46
|
+
default: false,
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
async run({ args }) {
|
|
50
|
+
const cwd = process.cwd()
|
|
51
|
+
const projectName = args.project ?? path.basename(cwd)
|
|
52
|
+
const tplRoot = templatesRoot()
|
|
53
|
+
|
|
54
|
+
console.log(`zbc init: ${projectName}`)
|
|
55
|
+
console.log(` cwd: ${cwd}`)
|
|
56
|
+
|
|
57
|
+
const mode = await detectMode(cwd)
|
|
58
|
+
if (mode.kind === 'incompatible') {
|
|
59
|
+
console.error(`✗ ${mode.reason}`)
|
|
60
|
+
process.exit(1)
|
|
61
|
+
}
|
|
62
|
+
console.log(` mode: ${mode.kind}`)
|
|
63
|
+
|
|
64
|
+
const vars = { PROJECT_NAME: projectName }
|
|
65
|
+
|
|
66
|
+
// 1. Greenfield-only root files
|
|
67
|
+
if (mode.kind === 'greenfield') {
|
|
68
|
+
await copyTemplateFile(
|
|
69
|
+
path.join(tplRoot, 'root/package.json'),
|
|
70
|
+
path.join(cwd, 'package.json'),
|
|
71
|
+
{ vars },
|
|
72
|
+
)
|
|
73
|
+
await copyTemplateFile(
|
|
74
|
+
path.join(tplRoot, 'root/tsconfig.json'),
|
|
75
|
+
path.join(cwd, 'tsconfig.json'),
|
|
76
|
+
{ vars },
|
|
77
|
+
)
|
|
78
|
+
await copyTemplateFile(path.join(tplRoot, 'gitignore'), path.join(cwd, '.gitignore'))
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// 2. zbc.config.ts (both modes)
|
|
82
|
+
await copyTemplateFile(path.join(tplRoot, 'zbc.config.ts'), path.join(cwd, 'zbc.config.ts'), {
|
|
83
|
+
vars,
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
// 3. .sops.yaml (unless --no-sops)
|
|
87
|
+
if (!args['no-sops']) {
|
|
88
|
+
await copyTemplateFile(path.join(tplRoot, 'sops.yaml'), path.join(cwd, '.sops.yaml'))
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// 4. packages/infra/ skeleton (always). Exclude modules — those are
|
|
92
|
+
// added on demand via `zbc add`.
|
|
93
|
+
const infraDest = path.join(cwd, 'packages/infra')
|
|
94
|
+
await copyTemplateDir(path.join(tplRoot, 'infra'), infraDest, {
|
|
95
|
+
vars,
|
|
96
|
+
exclude: ['modules'],
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
// 5. environments/ dirs with .gitkeep
|
|
100
|
+
for (const env of ['production', 'preview']) {
|
|
101
|
+
const envDir = path.join(infraDest, 'environments', env)
|
|
102
|
+
await ensureDir(envDir)
|
|
103
|
+
await writeIfMissing(path.join(envDir, '.gitkeep'), '')
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// 6. CI workflows
|
|
107
|
+
if (args.ci === 'github') {
|
|
108
|
+
const wfDest = path.join(cwd, '.github/workflows')
|
|
109
|
+
await copyTemplateDir(path.join(tplRoot, 'workflows'), wfDest)
|
|
110
|
+
} else if (args.ci) {
|
|
111
|
+
console.warn(` warn: unknown --ci=${args.ci} (only "github" supported)`)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
console.log('')
|
|
115
|
+
console.log('✓ init done')
|
|
116
|
+
console.log('')
|
|
117
|
+
if (mode.kind === 'greenfield') {
|
|
118
|
+
console.log('Next steps:')
|
|
119
|
+
console.log(' 1. bun install')
|
|
120
|
+
console.log(' 2. Add your age public keys to .sops.yaml')
|
|
121
|
+
console.log(' 3. bunx @zabaca/zbc add turso # or vercel')
|
|
122
|
+
} else {
|
|
123
|
+
console.log('Next steps:')
|
|
124
|
+
console.log(' 1. bun install # pick up packages/infra')
|
|
125
|
+
console.log(' 2. Add your age public keys to .sops.yaml')
|
|
126
|
+
console.log(' 3. bunx @zabaca/zbc add turso # or vercel')
|
|
127
|
+
}
|
|
128
|
+
},
|
|
129
|
+
})
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { ApplyContext } from '../infra-types'
|
|
2
|
+
import { discoverInstances } from './discover'
|
|
3
|
+
import { resolveOrder } from './resolve'
|
|
4
|
+
import { loadSecrets } from './secrets'
|
|
5
|
+
|
|
6
|
+
export async function applyEnvironment(
|
|
7
|
+
projectRoot: string,
|
|
8
|
+
envDir: string,
|
|
9
|
+
target?: string,
|
|
10
|
+
): Promise<void> {
|
|
11
|
+
const instances = await discoverInstances(envDir)
|
|
12
|
+
const sorted = resolveOrder(instances, target)
|
|
13
|
+
const secrets = await loadSecrets(envDir)
|
|
14
|
+
const outputs = new Map<string, unknown>()
|
|
15
|
+
|
|
16
|
+
for (const instance of sorted) {
|
|
17
|
+
const importOutputs: Record<string, unknown> = {}
|
|
18
|
+
for (const dep of instance.imports) {
|
|
19
|
+
importOutputs[dep.name] = outputs.get(dep.name)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const ctx: ApplyContext = { secrets, imports: importOutputs, projectRoot }
|
|
23
|
+
|
|
24
|
+
const validatedConfig = instance._definition.configSchema.parse(instance.config)
|
|
25
|
+
|
|
26
|
+
console.log(`\n→ ${instance.moduleName}:${instance.name}`)
|
|
27
|
+
|
|
28
|
+
const result = await instance._definition.apply(validatedConfig, ctx)
|
|
29
|
+
|
|
30
|
+
instance._definition.outputsSchema.parse(result)
|
|
31
|
+
|
|
32
|
+
outputs.set(instance.name, result)
|
|
33
|
+
console.log(`✓ ${instance.moduleName}:${instance.name} applied`)
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { ApplyContext } from '../infra-types'
|
|
2
|
+
import { discoverInstances } from './discover'
|
|
3
|
+
import { resolveOrder } from './resolve'
|
|
4
|
+
import { loadSecrets } from './secrets'
|
|
5
|
+
|
|
6
|
+
export async function destroyEnvironment(projectRoot: string, envDir: string): Promise<void> {
|
|
7
|
+
const instances = await discoverInstances(envDir)
|
|
8
|
+
const sorted = resolveOrder(instances)
|
|
9
|
+
const reversed = [...sorted].reverse()
|
|
10
|
+
const secrets = await loadSecrets(envDir)
|
|
11
|
+
|
|
12
|
+
for (const instance of reversed) {
|
|
13
|
+
const { destroy } = instance._definition
|
|
14
|
+
if (!destroy) {
|
|
15
|
+
console.log(`⊘ ${instance.moduleName}:${instance.name} has no destroy — skipping`)
|
|
16
|
+
continue
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const ctx: ApplyContext = { secrets, imports: {}, projectRoot }
|
|
20
|
+
|
|
21
|
+
const validatedConfig = instance._definition.configSchema.parse(instance.config)
|
|
22
|
+
|
|
23
|
+
console.log(`\n→ ${instance.moduleName}:${instance.name}`)
|
|
24
|
+
|
|
25
|
+
await destroy(validatedConfig, ctx)
|
|
26
|
+
|
|
27
|
+
console.log(`✓ ${instance.moduleName}:${instance.name} destroyed`)
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import * as path from 'node:path'
|
|
2
|
+
import type { ModuleInstance } from '../infra-types'
|
|
3
|
+
|
|
4
|
+
function isModuleInstance(value: unknown): value is ModuleInstance {
|
|
5
|
+
return (
|
|
6
|
+
typeof value === 'object' &&
|
|
7
|
+
value !== null &&
|
|
8
|
+
'name' in value &&
|
|
9
|
+
'moduleName' in value &&
|
|
10
|
+
'_definition' in value &&
|
|
11
|
+
'imports' in value
|
|
12
|
+
)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export async function discoverInstances(envDir: string): Promise<ModuleInstance[]> {
|
|
16
|
+
const glob = new Bun.Glob('*.ts')
|
|
17
|
+
const files = Array.from(glob.scanSync({ cwd: envDir }))
|
|
18
|
+
const instances: ModuleInstance[] = []
|
|
19
|
+
|
|
20
|
+
for (const file of files) {
|
|
21
|
+
const absolutePath = path.resolve(envDir, file)
|
|
22
|
+
const mod = await import(absolutePath)
|
|
23
|
+
const instance = mod.default
|
|
24
|
+
|
|
25
|
+
if (!isModuleInstance(instance)) {
|
|
26
|
+
throw new Error(`${file} does not export a valid module instance as its default export`)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
instances.push(instance)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return instances
|
|
33
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { ModuleInstance } from '../infra-types'
|
|
2
|
+
|
|
3
|
+
export function resolveOrder(instances: ModuleInstance[], target?: string): ModuleInstance[] {
|
|
4
|
+
let toProcess = instances
|
|
5
|
+
|
|
6
|
+
if (target) {
|
|
7
|
+
const targetInstance = instances.find((i) => i.name === target)
|
|
8
|
+
if (!targetInstance) {
|
|
9
|
+
throw new Error(
|
|
10
|
+
`Instance "${target}" not found. Available: ${instances.map((i) => i.name).join(', ')}`,
|
|
11
|
+
)
|
|
12
|
+
}
|
|
13
|
+
toProcess = collectTransitiveDeps(targetInstance, instances)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return topologicalSort(toProcess)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function collectTransitiveDeps(instance: ModuleInstance, _all: ModuleInstance[]): ModuleInstance[] {
|
|
20
|
+
const collected = new Set<string>()
|
|
21
|
+
const result: ModuleInstance[] = []
|
|
22
|
+
|
|
23
|
+
function walk(inst: ModuleInstance) {
|
|
24
|
+
if (collected.has(inst.name)) return
|
|
25
|
+
collected.add(inst.name)
|
|
26
|
+
for (const dep of inst.imports) {
|
|
27
|
+
walk(dep)
|
|
28
|
+
}
|
|
29
|
+
result.push(inst)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
walk(instance)
|
|
33
|
+
return result
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function topologicalSort(instances: ModuleInstance[]): ModuleInstance[] {
|
|
37
|
+
const nameToInstance = new Map<string, ModuleInstance>()
|
|
38
|
+
const inDegree = new Map<string, number>()
|
|
39
|
+
const adjacency = new Map<string, string[]>()
|
|
40
|
+
|
|
41
|
+
for (const inst of instances) {
|
|
42
|
+
nameToInstance.set(inst.name, inst)
|
|
43
|
+
inDegree.set(inst.name, 0)
|
|
44
|
+
adjacency.set(inst.name, [])
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
for (const inst of instances) {
|
|
48
|
+
for (const dep of inst.imports) {
|
|
49
|
+
if (nameToInstance.has(dep.name)) {
|
|
50
|
+
adjacency.get(dep.name)!.push(inst.name)
|
|
51
|
+
inDegree.set(inst.name, (inDegree.get(inst.name) ?? 0) + 1)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const queue: string[] = []
|
|
57
|
+
for (const [name, degree] of inDegree) {
|
|
58
|
+
if (degree === 0) queue.push(name)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const sorted: ModuleInstance[] = []
|
|
62
|
+
while (queue.length > 0) {
|
|
63
|
+
const name = queue.shift()!
|
|
64
|
+
sorted.push(nameToInstance.get(name)!)
|
|
65
|
+
|
|
66
|
+
for (const dependent of adjacency.get(name) ?? []) {
|
|
67
|
+
const newDegree = (inDegree.get(dependent) ?? 1) - 1
|
|
68
|
+
inDegree.set(dependent, newDegree)
|
|
69
|
+
if (newDegree === 0) queue.push(dependent)
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (sorted.length !== instances.length) {
|
|
74
|
+
throw new Error('Circular dependency detected between module instances')
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return sorted
|
|
78
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import * as path from 'node:path'
|
|
2
|
+
import { parse as parseYaml } from 'yaml'
|
|
3
|
+
|
|
4
|
+
export async function loadSecrets(envDir: string): Promise<Record<string, string>> {
|
|
5
|
+
const secretsPath = path.join(envDir, 'secrets.yaml')
|
|
6
|
+
const file = Bun.file(secretsPath)
|
|
7
|
+
|
|
8
|
+
if (!(await file.exists())) {
|
|
9
|
+
return {}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const raw = await file.text()
|
|
13
|
+
const parsed = parseYaml(raw)
|
|
14
|
+
|
|
15
|
+
// Check if this is a SOPS-encrypted file (has a "sops" key)
|
|
16
|
+
if (parsed?.sops) {
|
|
17
|
+
const { decryptSops } = await import('sops-age')
|
|
18
|
+
const decrypted = await decryptSops({ path: secretsPath })
|
|
19
|
+
|
|
20
|
+
if (typeof decrypted !== 'object' || decrypted === null) {
|
|
21
|
+
throw new Error(`secrets.yaml in ${envDir} did not decrypt to an object`)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return decrypted as Record<string, string>
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Plain YAML (unencrypted) — used in development
|
|
28
|
+
if (typeof parsed !== 'object' || parsed === null) {
|
|
29
|
+
throw new Error(`secrets.yaml in ${envDir} is not a valid object`)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return parsed as Record<string, string>
|
|
33
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
import { defineCommand, runMain } from 'citty'
|
|
3
|
+
import { addCommand } from './commands/add'
|
|
4
|
+
import { applyCommand } from './commands/apply'
|
|
5
|
+
import { destroyCommand } from './commands/destroy'
|
|
6
|
+
import { initCommand } from './commands/init'
|
|
7
|
+
|
|
8
|
+
const main = defineCommand({
|
|
9
|
+
meta: {
|
|
10
|
+
name: 'zbc',
|
|
11
|
+
version: '0.1.0',
|
|
12
|
+
description: 'Zabaca stack CLI',
|
|
13
|
+
},
|
|
14
|
+
subCommands: {
|
|
15
|
+
apply: applyCommand,
|
|
16
|
+
destroy: destroyCommand,
|
|
17
|
+
init: initCommand,
|
|
18
|
+
add: addCommand,
|
|
19
|
+
},
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
runMain(main)
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// Mirror of packages/infra/src/types.ts. Inlined here so the published CLI
|
|
2
|
+
// has no runtime/workspace dependency on @zbc/infra. Keep in sync.
|
|
3
|
+
import type { z } from 'zod'
|
|
4
|
+
|
|
5
|
+
export type ApplyFn<TConfig, TOutputs> = (config: TConfig, ctx: ApplyContext) => Promise<TOutputs>
|
|
6
|
+
|
|
7
|
+
export type DestroyFn<TConfig> = (config: TConfig, ctx: ApplyContext) => Promise<void>
|
|
8
|
+
|
|
9
|
+
export interface ApplyContext {
|
|
10
|
+
secrets: Record<string, string>
|
|
11
|
+
imports: Record<string, unknown>
|
|
12
|
+
projectRoot: string
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface ModuleDefinition<TConfig extends z.ZodType, TOutputs extends z.ZodType> {
|
|
16
|
+
name: string
|
|
17
|
+
configSchema: TConfig
|
|
18
|
+
outputsSchema: TOutputs
|
|
19
|
+
apply: ApplyFn<z.infer<TConfig>, z.infer<TOutputs>>
|
|
20
|
+
destroy?: DestroyFn<z.infer<TConfig>>
|
|
21
|
+
instance: (opts: InstanceOptions<TConfig>) => ModuleInstance<TOutputs>
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface InstanceOptions<TConfig extends z.ZodType> {
|
|
25
|
+
name: string
|
|
26
|
+
config: z.input<TConfig>
|
|
27
|
+
imports?: ModuleInstance[]
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface ModuleInstance<TOutputs extends z.ZodType = z.ZodType> {
|
|
31
|
+
name: string
|
|
32
|
+
moduleName: string
|
|
33
|
+
config: unknown
|
|
34
|
+
imports: ModuleInstance[]
|
|
35
|
+
_outputsSchema: TOutputs
|
|
36
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
37
|
+
_definition: ModuleDefinition<any, TOutputs>
|
|
38
|
+
}
|