balena-cli
Version:
The official balena Command Line Interface
100 lines • 3.22 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.umount = umount;
exports.isMounted = isMounted;
exports.safeUmount = safeUmount;
exports.denyMount = denyMount;
const child_process = require("child_process");
const path = require("path");
const util_1 = require("util");
const execFile = (0, util_1.promisify)(child_process.execFile);
async function umount(device) {
if (process.platform === 'win32') {
return;
}
const { sanitizePath, whichBin } = await Promise.resolve().then(() => require('./which'));
device = sanitizePath(device);
const cmd = [];
if (process.platform === 'darwin') {
cmd.push('/usr/sbin/diskutil', 'unmountDisk', 'force', device);
}
else {
const glob = (0, util_1.promisify)(await Promise.resolve().then(() => require('glob')));
const devices = await glob(`${device}?*`, { nodir: true, nonull: true });
cmd.push(await whichBin('umount'), ...devices);
}
if (cmd.length > 1) {
let stderr = '';
try {
const proc = await execFile(cmd[0], cmd.slice(1));
stderr = proc.stderr;
}
catch (err) {
const msg = [
'',
`Error executing "${cmd.join(' ')}"`,
stderr || '',
err.message || '',
];
if (process.platform === 'linux') {
if (process.env.DEBUG) {
console.error(msg.join('\n[debug] '));
}
return;
}
const { ExpectedError } = await Promise.resolve().then(() => require('../errors'));
throw new ExpectedError(msg.join('\n'));
}
}
}
async function isMounted(device) {
if (process.platform === 'win32') {
return true;
}
if (!device) {
return false;
}
const { whichBin } = await Promise.resolve().then(() => require('./which'));
const mountCmd = await whichBin('mount');
let stdout = '';
let stderr = '';
try {
const proc = await execFile(mountCmd);
stdout = proc.stdout;
stderr = proc.stderr;
}
catch (err) {
const { ExpectedError } = await Promise.resolve().then(() => require('../errors'));
throw new ExpectedError(`Error executing "${mountCmd}":\n${stderr}\n${err.message}`);
}
const result = (stdout || '')
.split('\n')
.some((line) => line.startsWith(device));
return result;
}
async function safeUmount(drive) {
if (!drive) {
return;
}
if (await isMounted(drive)) {
await umount(drive);
}
}
async function denyMount(target, handler, opts = {}) {
const denymount = (0, util_1.promisify)(await Promise.resolve().then(() => require('denymount')));
if (process.pkg) {
opts.executablePath || (opts.executablePath = path.join(path.dirname(process.execPath), 'denymount'));
}
const dmHandler = async (cb) => {
let err;
try {
await handler();
}
catch (e) {
err = e;
}
cb(err);
};
await denymount(target, dmHandler, opts);
}
//# sourceMappingURL=umount.js.map
;