Updated installx with post script fixes

This commit is contained in:
Brian Zalewski 2024-04-01 08:56:27 +00:00
parent e139dda65b
commit 34a0429cfe

View file

@ -1,13 +1,13 @@
#!/usr/bin/env zx #!/usr/bin/env zx
import osInfo from 'linux-os-info' import osInfo from 'linux-os-info'
// $.verbose = false $.verbose = false
let installOrder, osArch, osId, osType, pkgs, sysType let installOrder, osArch, osId, osType, pkgs, sysType
const cacheDir = os.homedir() + '/.cache/installx' const cacheDir = os.homedir() + '/.cache/installx'
function log(message) { function log(message) {
console.log(`${chalk.cyanBright('instx->')} ${message}`) process.env.DEBUG && console.log(`${chalk.cyanBright('instx->')} ${message}`)
} }
async function getOsInfo() { async function getOsInfo() {
@ -27,15 +27,27 @@ function execPromise(command) {
} }
async function runSilentCommand(command) { async function runSilentCommand(command) {
require('child_process').execSync(`${command}`, { stdio: 'inherit', shell: true }) try {
require('child_process').execSync(`${command}`, { stdio: 'inherit', shell: true })
} catch (e) {
console.error('Failed to run silent command', e)
}
} }
async function runScript(key, script) { async function runScript(key, script) {
log(`Running script..`)
fs.writeFileSync(`${cacheDir}/${key}`, script) fs.writeFileSync(`${cacheDir}/${key}`, script)
const file = await $`cat ${cacheDir}/${key} | ( grep "^# @file" || [ "$?" == "1" ] ) | sed 's/^# @file //'` const [ templatedScript, file, brief ] = await Promise.all([
const brief = await $`cat ${cacheDir}/${key} | ( grep "^# @brief" || [ "$?" == "1" ] ) | sed 's/^# @brief //'` $`cat ${cacheDir}/${key} | chezmoi execute-template`,
fs.writeFileSync(`${cacheDir}/${key}-glow`, '```sh\n' + (file.stdout ? (!file.stdout && !brief.stdout ? '```sh' : '') + `# ${file.stdout}\n` : '') + (brief.stdout ? `> ${brief.stdout}\n` : '') + (file.stdout || brief.stdout ? '```sh\n' : '') + script + "\n```") $`cat ${cacheDir}/${key} | ( grep "^# @file" || [ "$?" == "1" ] ) | sed 's/^# @file //'`,
runSilentCommand(`glow "${cacheDir}/${key}-glow" && bash "${cacheDir}/${key}"`) $`cat ${cacheDir}/${key} | ( grep "^# @brief" || [ "$?" == "1" ] ) | sed 's/^# @brief //'`
])
fs.writeFileSync(`${cacheDir}/${key}-glow`, (file.stdout ? `# ${file.stdout}\n\n` : '') + (brief.stdout ? `> ${brief.stdout}\n\n` : '') + '```sh\n' + templatedScript + "\n```")
try {
runSilentCommand(`glow --width 140 "${cacheDir}/${key}-glow" && bash "${cacheDir}/${key}"`)
} catch (e) {
console.error(`Failed to run script associated with ${key}`, e)
}
} }
function getPkgData(pref, pkg, installer) { function getPkgData(pref, pkg, installer) {
@ -57,7 +69,7 @@ function getPkgData(pref, pkg, installer) {
} else if (pkg[`${pref}:${osType}:${installer}`]) { } else if (pkg[`${pref}:${osType}:${installer}`]) {
return `${pref}:${osType}:${installer}` // Handles case like `_bin:darwin:pipx:` return `${pref}:${osType}:${installer}` // Handles case like `_bin:darwin:pipx:`
} else if (pkg[`${pref}:${installer}`]) { } else if (pkg[`${pref}:${installer}`]) {
return `${pref}` // Handles case like `_bin:pipx:` return `${pref}:${installer}` // Handles case like `_bin:pipx:`
} else if (pkg[`${pref}`]) { } else if (pkg[`${pref}`]) {
return `${pref}` // Handles case like `_bin:` return `${pref}` // Handles case like `_bin:`
} else { } else {
@ -217,7 +229,6 @@ async function installPackages(pkgInstructions) {
const promises = [] const promises = []
log(`Populating install order lists`) log(`Populating install order lists`)
for (const option of installOrder[sysType]) { for (const option of installOrder[sysType]) {
console.log(installOrder[sysType])
const instructions = pkgInstructions.filter(x => x.installType === option) const instructions = pkgInstructions.filter(x => x.installType === option)
if (instructions.length) { if (instructions.length) {
combined[option] = instructions combined[option] = instructions
@ -309,7 +320,7 @@ async function installPackages(pkgInstructions) {
case 'sbopkg': // TODO case 'sbopkg': // TODO
break break
case 'script': case 'script':
promises.push(...combined[key].flatMap(x => x.installList.flatMap(i => runScript(i)))) promises.push(...combined[key].flatMap(x => x.installList.flatMap(i => runScript(x.listKey, i))))
break break
case 'whalebrew': // TODO case 'whalebrew': // TODO
break break
@ -320,12 +331,12 @@ async function installPackages(pkgInstructions) {
promises.push($`yay -Sy --noconfirm --needed ${combined[key].flatMap(x => x.installList).join(' ')}`) promises.push($`yay -Sy --noconfirm --needed ${combined[key].flatMap(x => x.installList).join(' ')}`)
break break
default: default:
console.log(`Unable to find install key instructions for ${key}`) log(`Unable to find install key instructions for ${key}`)
} }
} }
const installs = await Promise.allSettled(promises) const installs = await Promise.allSettled(promises)
log(`All of the installations have finished`) log(`All of the installations have finished`)
console.log('Installs:', installs) process.env.DEBUG && console.log('Installs:', installs)
await postInstall(combined) await postInstall(combined)
} }
@ -334,7 +345,7 @@ async function postInstall(combined) {
const promises = [] const promises = []
Object.keys(combined).includes('flatpak') && promises.push(createFlatpakLinks()) Object.keys(combined).includes('flatpak') && promises.push(createFlatpakLinks())
const postInstalls = await Promise.allSettled(promises) const postInstalls = await Promise.allSettled(promises)
console.log('Post installs:', postInstalls) process.env.DEBUG && console.log('Post installs:', postInstalls)
} }
async function acquireManagerList(type, command) { async function acquireManagerList(type, command) {
@ -395,7 +406,7 @@ async function main() {
installOrder = initData[1].installerPreference installOrder = initData[1].installerPreference
log(`Populating lists of pre-installed packages`) log(`Populating lists of pre-installed packages`)
const lists = [ const lists = [
acquireManagerList('apt', `dpkg -l`), acquireManagerList('apt', `if command -v dpkg; then dpkg -l; fi`),
acquireManagerList('brew', `brew list -1`), acquireManagerList('brew', `brew list -1`),
acquireManagerList('cargo', `cargo install --list | awk '/^[[:alnum:]]/ {print $1}'`), acquireManagerList('cargo', `cargo install --list | awk '/^[[:alnum:]]/ {print $1}'`),
acquireManagerList('dnf', `rpm -qa`), acquireManagerList('dnf', `rpm -qa`),
@ -405,7 +416,7 @@ async function main() {
acquireManagerList('pacman', `pacman -Qs`), acquireManagerList('pacman', `pacman -Qs`),
acquireManagerList('pip3', `pip3 list | awk '{print $1}'`), acquireManagerList('pip3', `pip3 list | awk '{print $1}'`),
acquireManagerList('pipx', `pipx list --short | awk '{print $1}'`), acquireManagerList('pipx', `pipx list --short | awk '{print $1}'`),
acquireManagerList('snap', `snap list`), acquireManagerList('snap', `if command -v snapd; then snap list; fi`),
acquireManagerList('zap', `zap list`) acquireManagerList('zap', `zap list`)
] ]
const managerLists = { const managerLists = {
@ -473,13 +484,13 @@ async function main() {
} }
return true return true
}) })
console.log(installInstructions)
log(`Running installation routine`) log(`Running installation routine`)
await installPackages(installInstructions) await installPackages(installInstructions)
log(`Running post-install scripts`) log(`Running post-install scripts`)
const postScripts = installData const postScripts = installData
.flatMap(x => { .flatMap(x => {
const postField = getPkgData('_post', x, x.installType) const postField = getPkgData('_post', x, x.installType)
log(`Running post-install script for ${x.listKey}`)
return (postField && runScript(x.listKey, x[postField])) || Promise.resolve() return (postField && runScript(x.listKey, x[postField])) || Promise.resolve()
}) })
await Promise.all(postScripts) await Promise.all(postScripts)