Skip to content

Instantly share code, notes, and snippets.

@trosck
Created April 10, 2023 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trosck/28bb3032690a61ea83d33dbe228d1c9f to your computer and use it in GitHub Desktop.
Save trosck/28bb3032690a61ea83d33dbe228d1c9f to your computer and use it in GitHub Desktop.
modinfo nodejs prettier
import { exec } from "node:child_process";
const mods = await new Promise((resolve, reject) => {
exec("lsmod", (error, stdout, stderr) => {
if (error) return reject(error)
const mods_array = stdout.split("\n")
resolve(
mods_array.slice(1, mods_array.length - 1).map(
mod => mod.split(" ")[0]
)
)
})
})
const promises = [];
for (const mod of mods) {
promises.push(
new Promise((resolve, reject) => {
exec('modinfo ' + mod, (error, stdout, stderr) => {
if (error) return reject(error)
resolve(stdout)
})
})
)
}
const format_output = (output) => {
return Object.entries(output).map(
([name, value]) => `${name}: ${value || "---"}`
).join("\n")
}
const result = await Promise.all(promises)
console.log(result.map(
v => v.split("\n").reduce(
(store, line) => {
const name = line.slice(0, 16).split(":")[0].trim()
const value = line.slice(16).trim()
if (name) {
store.current = name
store.output[name] = ""
}
store.output[store.current] += value
return store
}, {
output: {},
current: null
}
)
).map(item => format_output({
name: item.output.name,
version: item.output.version,
author: item.output.author,
description: item.output.description
})).join("\n\n"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment