2023-03-02 19:21:45 +01:00
|
|
|
import util from "util"
|
|
|
|
const runCommand = util.promisify(require("child_process").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 = "./") {
|
2022-08-10 17:19:08 +02:00
|
|
|
const yarn = await exports.utilityInstalled("yarn")
|
|
|
|
const npm = await exports.utilityInstalled("npm")
|
|
|
|
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
|
2022-08-11 18:29:07 +02:00
|
|
|
await exports.exec(cmd, dir)
|
2022-08-10 17:19:08 +02:00
|
|
|
}
|