slatebox 0.0.0-stage → 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/LICENSE.md +21 -0
- package/README.md +151 -2
- package/bin/slatebox.mjs +9 -0
- package/package.json +41 -4
- package/src/agent-prompt.mjs +64 -0
- package/src/ai/settings-adapter.mjs +198 -0
- package/src/answers.mjs +489 -0
- package/src/cli.mjs +477 -0
- package/src/errors.mjs +23 -0
- package/src/image.mjs +138 -0
- package/src/interview.mjs +170 -0
- package/src/output.mjs +51 -0
- package/src/plan.mjs +51 -0
- package/src/prompt.mjs +87 -0
- package/src/record.mjs +46 -0
- package/src/render/compose.mjs +47 -0
- package/src/render/helm.mjs +78 -0
- package/src/render/k8s.mjs +204 -0
- package/src/render/settings.mjs +59 -0
- package/src/runner.mjs +50 -0
- package/src/secret-values.mjs +40 -0
- package/src/secrets.mjs +119 -0
- package/src/targets/docker.mjs +685 -0
- package/src/targets/export.mjs +73 -0
- package/src/targets/hosts.mjs +202 -0
- package/src/targets/k8s.mjs +366 -0
- package/src/yaml.mjs +293 -0
package/src/yaml.mjs
ADDED
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
// The YAML the installer reads and writes: answers files, install records,
|
|
2
|
+
// compose files and Kubernetes manifests. It is a strict subset (block maps
|
|
3
|
+
// and lists, flow maps and lists, plain and quoted scalars, comments), so the
|
|
4
|
+
// package needs no dependency. Anything outside the subset is refused with
|
|
5
|
+
// its line number rather than guessed at. JSON is accepted as is.
|
|
6
|
+
|
|
7
|
+
class YamlError extends Error {
|
|
8
|
+
constructor(message, line) {
|
|
9
|
+
super(`YAML line ${line}: ${message}`)
|
|
10
|
+
this.line = line
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// ---------------------------------------------------------------- reading
|
|
15
|
+
|
|
16
|
+
function stripComment(text) {
|
|
17
|
+
let quote = null
|
|
18
|
+
for (let i = 0; i < text.length; i++) {
|
|
19
|
+
const c = text[i]
|
|
20
|
+
if (quote) {
|
|
21
|
+
if (quote === '"' && c === '\\') { i++; continue }
|
|
22
|
+
if (c === quote) {
|
|
23
|
+
if (quote === "'" && text[i + 1] === "'") { i++; continue }
|
|
24
|
+
quote = null
|
|
25
|
+
}
|
|
26
|
+
} else if (c === '"' || c === "'") {
|
|
27
|
+
if (i === 0 || /[\s,[{:]/.test(text[i - 1])) quote = c
|
|
28
|
+
} else if (c === '#' && (i === 0 || /\s/.test(text[i - 1]))) {
|
|
29
|
+
return text.slice(0, i)
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return text
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function scalar(raw, line) {
|
|
36
|
+
const text = raw.trim()
|
|
37
|
+
if (text === '' || text === '~' || text === 'null' || text === 'Null' || text === 'NULL') return null
|
|
38
|
+
if (text === 'true' || text === 'True' || text === 'TRUE') return true
|
|
39
|
+
if (text === 'false' || text === 'False' || text === 'FALSE') return false
|
|
40
|
+
if (/^[-+]?(0|[1-9][0-9]*)$/.test(text)) return Number(text)
|
|
41
|
+
if (/^[-+]?(0|[1-9][0-9]*)?\.[0-9]+([eE][-+]?[0-9]+)?$/.test(text) || /^[-+]?(0|[1-9][0-9]*)[eE][-+]?[0-9]+$/.test(text)) return Number(text)
|
|
42
|
+
if (text.startsWith('"')) {
|
|
43
|
+
try { return JSON.parse(text) } catch { throw new YamlError(`bad double-quoted string ${text}`, line) }
|
|
44
|
+
}
|
|
45
|
+
if (text.startsWith("'")) {
|
|
46
|
+
if (!text.endsWith("'") || text.length < 2) throw new YamlError(`unterminated single-quoted string ${text}`, line)
|
|
47
|
+
const inner = text.slice(1, -1)
|
|
48
|
+
if (/(^|[^'])'([^']|$)/.test(inner.replace(/''/g, ''))) throw new YamlError(`bad single-quoted string ${text}`, line)
|
|
49
|
+
return inner.replace(/''/g, "'")
|
|
50
|
+
}
|
|
51
|
+
if (/^[|>]/.test(text)) throw new YamlError('block scalars (| and >) are not supported; use a quoted string', line)
|
|
52
|
+
if (/^[&*!%@`]/.test(text)) throw new YamlError(`anchors, aliases, tags and reserved characters are not supported (${text})`, line)
|
|
53
|
+
return text
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Flow collections: { a: b, c: [1, 2] } and [a, "b"].
|
|
57
|
+
function flow(text, line) {
|
|
58
|
+
let i = 0
|
|
59
|
+
const ws = () => { while (i < text.length && /\s/.test(text[i])) i++ }
|
|
60
|
+
const fail = message => { throw new YamlError(message, line) }
|
|
61
|
+
function quoted() {
|
|
62
|
+
const q = text[i]
|
|
63
|
+
let j = i + 1
|
|
64
|
+
while (j < text.length) {
|
|
65
|
+
if (q === '"' && text[j] === '\\') { j += 2; continue }
|
|
66
|
+
if (text[j] === q) {
|
|
67
|
+
if (q === "'" && text[j + 1] === "'") { j += 2; continue }
|
|
68
|
+
break
|
|
69
|
+
}
|
|
70
|
+
j++
|
|
71
|
+
}
|
|
72
|
+
if (j >= text.length) fail('unterminated string')
|
|
73
|
+
const raw = text.slice(i, j + 1)
|
|
74
|
+
i = j + 1
|
|
75
|
+
return scalar(raw, line)
|
|
76
|
+
}
|
|
77
|
+
function plain(stops) {
|
|
78
|
+
const start = i
|
|
79
|
+
while (i < text.length && !stops.includes(text[i])) {
|
|
80
|
+
if (text[i] === ':' && stops.includes(':') && (i + 1 >= text.length || /[\s,}\]]/.test(text[i + 1]))) break
|
|
81
|
+
i++
|
|
82
|
+
}
|
|
83
|
+
return text.slice(start, i)
|
|
84
|
+
}
|
|
85
|
+
function value(stops) {
|
|
86
|
+
ws()
|
|
87
|
+
if (text[i] === '{') return map()
|
|
88
|
+
if (text[i] === '[') return list()
|
|
89
|
+
if (text[i] === '"' || text[i] === "'") return quoted()
|
|
90
|
+
return scalar(plain(stops), line)
|
|
91
|
+
}
|
|
92
|
+
function map() {
|
|
93
|
+
i++
|
|
94
|
+
const out = {}
|
|
95
|
+
ws()
|
|
96
|
+
if (text[i] === '}') { i++; return out }
|
|
97
|
+
for (;;) {
|
|
98
|
+
ws()
|
|
99
|
+
const key = text[i] === '"' || text[i] === "'" ? String(quoted()) : plain([':', ',', '}']).trim()
|
|
100
|
+
ws()
|
|
101
|
+
if (text[i] !== ':') fail(`expected ":" after ${key} in a flow map`)
|
|
102
|
+
i++
|
|
103
|
+
out[key] = value([',', '}'])
|
|
104
|
+
ws()
|
|
105
|
+
if (text[i] === ',') { i++; continue }
|
|
106
|
+
if (text[i] === '}') { i++; return out }
|
|
107
|
+
fail('unterminated flow map')
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
function list() {
|
|
111
|
+
i++
|
|
112
|
+
const out = []
|
|
113
|
+
ws()
|
|
114
|
+
if (text[i] === ']') { i++; return out }
|
|
115
|
+
for (;;) {
|
|
116
|
+
out.push(value([',', ']']))
|
|
117
|
+
ws()
|
|
118
|
+
if (text[i] === ',') { i++; continue }
|
|
119
|
+
if (text[i] === ']') { i++; return out }
|
|
120
|
+
fail('unterminated flow list')
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
const result = value([])
|
|
124
|
+
ws()
|
|
125
|
+
if (i < text.length) fail(`unexpected text after a flow value: ${text.slice(i)}`)
|
|
126
|
+
return result
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function inlineValue(text, line) {
|
|
130
|
+
const t = text.trim()
|
|
131
|
+
if (t.startsWith('{') || t.startsWith('[')) return flow(t, line)
|
|
132
|
+
return scalar(t, line)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// "key: value" (the colon must be followed by a space or the end).
|
|
136
|
+
function splitKey(text, line) {
|
|
137
|
+
let key, rest
|
|
138
|
+
if (text.startsWith('"') || text.startsWith("'")) {
|
|
139
|
+
const q = text[0]
|
|
140
|
+
let j = 1
|
|
141
|
+
while (j < text.length && text[j] !== q) j += q === '"' && text[j] === '\\' ? 2 : 1
|
|
142
|
+
key = String(scalar(text.slice(0, j + 1), line))
|
|
143
|
+
rest = text.slice(j + 1)
|
|
144
|
+
if (!rest.startsWith(':')) return null
|
|
145
|
+
rest = rest.slice(1)
|
|
146
|
+
} else {
|
|
147
|
+
const match = /^([^\s:#{}[\],][^:]*?|[^\s:#{}[\],]):(\s|$)/.exec(text)
|
|
148
|
+
if (!match) return null
|
|
149
|
+
key = match[1].trim()
|
|
150
|
+
rest = text.slice(match[0].length - match[2].length)
|
|
151
|
+
}
|
|
152
|
+
if (rest && !/^\s/.test(rest)) return null
|
|
153
|
+
return { key, rest: rest.trim() }
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export function parse(source) {
|
|
157
|
+
const text = String(source).replace(/^/, '')
|
|
158
|
+
const trimmed = text.trim()
|
|
159
|
+
if (trimmed.startsWith('{') || trimmed.startsWith('[')) {
|
|
160
|
+
try { return JSON.parse(trimmed) } catch { /* flow YAML on one line: handled below */ }
|
|
161
|
+
}
|
|
162
|
+
const lines = []
|
|
163
|
+
text.split(/\r?\n/).forEach((raw, index) => {
|
|
164
|
+
if (/^\t/.test(raw)) throw new YamlError('tabs are not allowed for indentation', index + 1)
|
|
165
|
+
const content = stripComment(raw).replace(/\s+$/, '')
|
|
166
|
+
if (!content.trim() || content.trim() === '---') return
|
|
167
|
+
lines.push({ indent: content.length - content.trimStart().length, text: content.trim(), line: index + 1 })
|
|
168
|
+
})
|
|
169
|
+
if (!lines.length) return null
|
|
170
|
+
let pos = 0
|
|
171
|
+
|
|
172
|
+
function block(indent) {
|
|
173
|
+
const first = lines[pos]
|
|
174
|
+
if (first.text === '-' || first.text.startsWith('- ')) return sequence(indent)
|
|
175
|
+
return mapping(indent)
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function nested(parentIndent, line) {
|
|
179
|
+
const next = lines[pos]
|
|
180
|
+
if (!next) return null
|
|
181
|
+
if (next.indent > parentIndent) return block(next.indent)
|
|
182
|
+
if (next.indent === parentIndent && (next.text === '-' || next.text.startsWith('- '))) return sequence(parentIndent)
|
|
183
|
+
return null
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function mapping(indent) {
|
|
187
|
+
const out = {}
|
|
188
|
+
while (pos < lines.length) {
|
|
189
|
+
const entry = lines[pos]
|
|
190
|
+
if (entry.indent < indent) break
|
|
191
|
+
if (entry.indent > indent) throw new YamlError('unexpected indentation', entry.line)
|
|
192
|
+
if (entry.text === '-' || entry.text.startsWith('- ')) break
|
|
193
|
+
const kv = splitKey(entry.text, entry.line)
|
|
194
|
+
if (!kv) throw new YamlError(`expected "key: value", found ${entry.text}`, entry.line)
|
|
195
|
+
if (Object.hasOwn(out, kv.key)) throw new YamlError(`duplicate key ${kv.key}`, entry.line)
|
|
196
|
+
pos++
|
|
197
|
+
out[kv.key] = kv.rest === '' ? nested(indent, entry.line) : inlineValue(kv.rest, entry.line)
|
|
198
|
+
}
|
|
199
|
+
return out
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function sequence(indent) {
|
|
203
|
+
const out = []
|
|
204
|
+
while (pos < lines.length) {
|
|
205
|
+
const entry = lines[pos]
|
|
206
|
+
if (entry.indent < indent) break
|
|
207
|
+
if (entry.indent > indent) throw new YamlError('unexpected indentation', entry.line)
|
|
208
|
+
if (!(entry.text === '-' || entry.text.startsWith('- '))) break
|
|
209
|
+
const rest = entry.text === '-' ? '' : entry.text.slice(2).trim()
|
|
210
|
+
const itemIndent = indent + 2 + (entry.text.slice(2).length - entry.text.slice(2).trimStart().length)
|
|
211
|
+
if (rest === '') {
|
|
212
|
+
pos++
|
|
213
|
+
out.push(nested(indent, entry.line))
|
|
214
|
+
} else if (!rest.startsWith('{') && !rest.startsWith('[') && splitKey(rest, entry.line)) {
|
|
215
|
+
// "- key: value" opens a map whose further keys sit at the item's indent.
|
|
216
|
+
lines[pos] = { indent: itemIndent, text: rest, line: entry.line }
|
|
217
|
+
out.push(mapping(itemIndent))
|
|
218
|
+
} else {
|
|
219
|
+
pos++
|
|
220
|
+
out.push(inlineValue(rest, entry.line))
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
return out
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const result = block(lines[0].indent)
|
|
227
|
+
if (pos < lines.length) throw new YamlError('unexpected indentation', lines[pos].line)
|
|
228
|
+
return result
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// ---------------------------------------------------------------- writing
|
|
232
|
+
|
|
233
|
+
const PLAIN = /^[A-Za-z0-9_./][A-Za-z0-9_./:@+=,() -]*$/
|
|
234
|
+
const RESERVED = /^(~|null|true|false|yes|no|on|off|y|n)$/i
|
|
235
|
+
|
|
236
|
+
function needsQuotes(value) {
|
|
237
|
+
if (value === '') return true
|
|
238
|
+
if (!PLAIN.test(value)) return true
|
|
239
|
+
if (RESERVED.test(value)) return true
|
|
240
|
+
if (/^[-+]?(\.[0-9]|[0-9])/.test(value) && !Number.isNaN(Number(value.replace(/_/g, '')))) return true
|
|
241
|
+
if (/^[-+]?[0-9]/.test(value) && /^[-+]?[0-9.eE+-]+$/.test(value)) return true
|
|
242
|
+
if (/^0[0-9]/.test(value)) return true
|
|
243
|
+
if (/: |:$| #|\s$/.test(value)) return true
|
|
244
|
+
return false
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function scalarText(value) {
|
|
248
|
+
if (value === null || value === undefined) return 'null'
|
|
249
|
+
if (typeof value === 'boolean') return String(value)
|
|
250
|
+
if (typeof value === 'number') {
|
|
251
|
+
if (!Number.isFinite(value)) throw new Error('cannot write a non-finite number')
|
|
252
|
+
return String(value)
|
|
253
|
+
}
|
|
254
|
+
const text = String(value)
|
|
255
|
+
return needsQuotes(text) ? JSON.stringify(text) : text
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function keyText(key) {
|
|
259
|
+
return /^[A-Za-z0-9_./-]+$/.test(key) && !RESERVED.test(key) && !/^[0-9]/.test(key) ? key : JSON.stringify(key)
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const isObject = value => value && typeof value === 'object' && !Array.isArray(value)
|
|
263
|
+
const isEmpty = value => (Array.isArray(value) && !value.length) || (isObject(value) && !Object.keys(value).length)
|
|
264
|
+
|
|
265
|
+
function write(value, indent) {
|
|
266
|
+
const pad = ' '.repeat(indent)
|
|
267
|
+
if (Array.isArray(value)) {
|
|
268
|
+
return value.map(item => {
|
|
269
|
+
if (isObject(item) && !isEmpty(item)) {
|
|
270
|
+
const body = write(item, indent + 2)
|
|
271
|
+
return `${pad}- ${body.slice(indent + 2)}`
|
|
272
|
+
}
|
|
273
|
+
if (Array.isArray(item) && item.length) return `${pad}-\n${write(item, indent + 2)}`
|
|
274
|
+
return `${pad}- ${isEmpty(item) ? (Array.isArray(item) ? '[]' : '{}') : scalarText(item)}`
|
|
275
|
+
}).join('\n')
|
|
276
|
+
}
|
|
277
|
+
return Object.entries(value).filter(([, item]) => item !== undefined).map(([key, item]) => {
|
|
278
|
+
if (isEmpty(item)) return `${pad}${keyText(key)}: ${Array.isArray(item) ? '[]' : '{}'}`
|
|
279
|
+
if (isObject(item)) return `${pad}${keyText(key)}:\n${write(item, indent + 2)}`
|
|
280
|
+
if (Array.isArray(item)) return `${pad}${keyText(key)}:\n${write(item, indent)}`
|
|
281
|
+
return `${pad}${keyText(key)}: ${scalarText(item)}`
|
|
282
|
+
}).join('\n')
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
export function stringify(value) {
|
|
286
|
+
if (!isObject(value) && !Array.isArray(value)) return `${scalarText(value)}\n`
|
|
287
|
+
if (isEmpty(value)) return Array.isArray(value) ? '[]\n' : '{}\n'
|
|
288
|
+
return `${write(value, 0)}\n`
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
export function stringifyAll(documents) {
|
|
292
|
+
return documents.map(stringify).join('---\n')
|
|
293
|
+
}
|