Created
April 10, 2023 15:10
-
-
Save trosck/28bb3032690a61ea83d33dbe228d1c9f to your computer and use it in GitHub Desktop.
modinfo nodejs prettier
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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