2022-08-26 18:09:19 +08:00
|
|
|
import { isSudo, execRootSync } from "admina"
|
2022-11-21 15:02:23 +08:00
|
|
|
import { statSync } from "fs"
|
2022-08-08 12:03:16 +08:00
|
|
|
|
|
|
|
/**
|
2022-11-21 15:02:23 +08:00
|
|
|
* Give the user access to the given path (and its sub-directories if a directory). It changes the owner to the
|
|
|
|
* SUDO_USER. This allows the user to use the folder without sudo
|
2022-08-08 12:03:16 +08:00
|
|
|
*
|
|
|
|
* @param path The path to give the user access to
|
|
|
|
*/
|
|
|
|
export function giveUserAccess(path: string) {
|
|
|
|
if (
|
|
|
|
(process.platform === "linux" || process.platform === "darwin") &&
|
|
|
|
isSudo() &&
|
|
|
|
process.env.SUDO_USER !== undefined
|
|
|
|
) {
|
2022-11-21 15:02:23 +08:00
|
|
|
const isDirectory = statSync(path).isDirectory()
|
|
|
|
execRootSync("chown", [...(isDirectory ? ["-R"] : []), process.env.SUDO_USER, path], {
|
|
|
|
cwd: path,
|
|
|
|
stdio: "inherit",
|
|
|
|
shell: true,
|
|
|
|
})
|
2022-08-08 12:03:16 +08:00
|
|
|
}
|
|
|
|
}
|