Skip to content

Commit

Permalink
Update elevate.js
Browse files Browse the repository at this point in the history
  • Loading branch information
xhyrom authored May 14, 2022
1 parent 4905f4d commit 059586d
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions new-injectors/elevate.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
const { spawnSync } = require('child_process');

// It seems `sudo npm ...` no longer give the script sudo perms in npm v7, so here we are.
// It seems `sudo npm ...` no longer gives the script sudo perms in npm v7, so here we are.
if (process.platform === 'linux' && process.getuid() !== 0) {
// todo: decide whether this is a viable solution for the long term. people may use doas or other tools for elevating themselves.
spawnSync('sudo', process.argv, { stdio: 'inherit' })
tryToElevate('sudo')
tryToElevate('doas')

console.warn('Neither doas nor sudo were found. Falling back to su.')
console.log('Please enter your root password')
spawnSync('su', ['-c', process.argv.join(' ')], { stdio: 'inherit'})
process.exit(0)
}

function tryToElevate(command) {
const { error } = spawnSync(command, process.argv, { stdio: 'inherit'})
if (!error) {
process.exit(0)
} else if (error.code !== 'ENOENT') {
console.error(error)
process.exit(0)
}
}

0 comments on commit 059586d

Please sign in to comment.