2023-03-02 19:21:45 +01:00
|
|
|
import util from "util"
|
2023-08-07 22:31:55 +02:00
|
|
|
import childProcess from "child_process"
|
2023-11-20 21:52:29 +01:00
|
|
|
|
2023-08-07 22:31:55 +02:00
|
|
|
const runCommand = util.promisify(childProcess.exec)
|
2022-08-10 17:19:08 +02:00
|
|
|
|
2023-03-02 19:21:45 +01:00
|
|
|
export async function exec(command: string, dir = "./") {
|
|
|
|
const { stdout } = await runCommand(command, { cwd: dir })
|
2022-08-10 17:19:08 +02:00
|
|
|
return stdout
|
|
|
|
}
|
|
|
|
|
2023-03-02 19:21:45 +01:00
|
|
|
export async function utilityInstalled(utilName: string) {
|
2022-08-10 17:19:08 +02:00
|
|
|
try {
|
2023-03-02 19:21:45 +01:00
|
|
|
await exec(`${utilName} --version`)
|
2022-08-10 17:19:08 +02:00
|
|
|
return true
|
|
|
|
} catch (err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-02 19:21:45 +01:00
|
|
|
export async function runPkgCommand(command: string, dir = "./") {
|
2023-08-07 22:31:55 +02:00
|
|
|
const yarn = await utilityInstalled("yarn")
|
|
|
|
const npm = await utilityInstalled("npm")
|
2022-08-10 17:19:08 +02:00
|
|
|
if (!yarn && !npm) {
|
|
|
|
throw new Error("Must have yarn or npm installed to run build.")
|
|
|
|
}
|
2022-08-11 12:03:33 +02:00
|
|
|
const npmCmd = command === "install" ? `npm ${command}` : `npm run ${command}`
|
2022-10-04 11:18:04 +02:00
|
|
|
const cmd = yarn ? `yarn ${command} --ignore-engines` : npmCmd
|
2023-08-07 22:31:55 +02:00
|
|
|
await exec(cmd, dir)
|
2022-08-10 17:19:08 +02:00
|
|
|
}
|