55 lines
1.6 KiB
TypeScript
55 lines
1.6 KiB
TypeScript
|
import { JsonValue } from '@std/json'
|
||
|
import { ensureFile } from '@std/fs'
|
||
|
import piConfig from '../config.json' with { type: 'json' }
|
||
|
|
||
|
const { config, service, script, network, local } = injectEnv(piConfig)
|
||
|
|
||
|
const sh = `
|
||
|
#!/bin/sh
|
||
|
|
||
|
# configure id
|
||
|
sudo raspi-config nonint do_hostname ${config.host}
|
||
|
sudo useradd -m ${config.user}
|
||
|
echo -e "${config.password}\\n${config.password}" | passwd ${config.user}
|
||
|
|
||
|
# configure localization
|
||
|
sudo raspi-config nonint do_change_timezone ${local.timezone}
|
||
|
sudo raspi-config nonint do_configure_keyboard ${local.keyboard}
|
||
|
sudo raspi-config nonint do_wifi_country ${local.country}
|
||
|
|
||
|
# configure network
|
||
|
sudo raspi-config nonint do_wifi_ssid_passphrase ${network.ssid} ${network.password}
|
||
|
|
||
|
# configure services
|
||
|
sudo raspi-config nonint do_ssh 0 #(enable = 0, disable = 1)
|
||
|
echo -e "${service.ssh_key}" >> /home/${config.user}/.ssh/authorized_keys
|
||
|
|
||
|
# custom scripts
|
||
|
${script.join('\n')}
|
||
|
`
|
||
|
|
||
|
const fileName = './dist/prepare.sh'
|
||
|
await ensureFile(fileName)
|
||
|
await Deno.writeTextFile(fileName, sh)
|
||
|
|
||
|
function injectEnv<T extends Record<string, JsonValue>>(config: T): T {
|
||
|
for (const key in config) {
|
||
|
const value = config[key]
|
||
|
if (typeof value === 'object') {
|
||
|
config[key] = injectEnv(
|
||
|
value as Record<string, JsonValue>,
|
||
|
) as typeof value
|
||
|
} else if (typeof value === 'string') {
|
||
|
const variables = value.match(/{{\w+}}/g) ?? []
|
||
|
for (const variable of variables) {
|
||
|
const env = Deno.env.get(variable.slice(2, -2))
|
||
|
if (env === undefined) {
|
||
|
throw new Error(`env ${variable.slice(2, -2)} is not set`)
|
||
|
}
|
||
|
config[key] = value.replaceAll(variable, env) as typeof value
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return config
|
||
|
}
|