rpi_setup/scripts/image.ts

67 lines
1.5 KiB
TypeScript
Raw Normal View History

2024-03-13 14:46:29 +01:00
import { ensureFile } from '@std/fs'
import { ZipReaderStream } from '@zip-js/zip-js'
import config from '../config.json' with { type: 'json' }
const imageName = './dist/rpi_os.img'
await ensureFile(imageName)
const image = await Deno.open(imageName, { create: true, write: true })
console.log(
`%c[setup:image]%c getting img from %c${config.image.url}`,
'color: royalblue; font-weight: bold',
'',
'text-decoration: underline',
)
const archive = await fetch(config.image.url)
if (archive.ok && archive.body) {
console.log(
`%c[setup:image]%c img found`,
'color: green; font-weight: bold',
'',
)
} else {
console.error(
`%c[setup:image]%c no img found or server doesn't respond with 200`,
'color: red; font-weight: bold',
'',
)
Deno.exit(1)
}
console.log(
`%c[setup:image]%c decompressing archive`,
'color: royalblue; font-weight: bold',
'',
)
let imgInArchive = false
for await (const file of archive.body.pipeThrough(new ZipReaderStream())) {
if (file.filename.endsWith('.img')) {
imgInArchive = true
console.log(
`%c[setup:image]%c found img file %c${file.filename}`,
'color: green; font-weight: bold',
'',
'text-decoration: underline',
)
console.log(
`%c[setup:image]%c writing file to %c${imageName}`,
'color: royalblue; font-weight: bold',
'',
'text-decoration: underline',
)
await file.readable?.pipeTo(image.writable)
break
}
}
if (!imgInArchive) {
console.error(
`%c[setup:image]%c no img found in archive`,
'color: red; font-weight: bold',
'',
)
Deno.exit(2)
}