balena-cli
Version:
The official balena Command Line Interface
105 lines (103 loc) • 4.22 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@oclif/core");
const errors_1 = require("../../errors");
const cf = require("../../utils/common-flags");
const lazy_1 = require("../../utils/lazy");
class LocalFlashCmd extends core_1.Command {
async run() {
const { args: params, flags: options } = await this.parse(LocalFlashCmd);
if (process.platform === 'linux') {
const { promisify } = await Promise.resolve().then(() => require('util'));
const { exec } = await Promise.resolve().then(() => require('child_process'));
const execAsync = promisify(exec);
let distroVersion = '';
try {
const info = await execAsync('cat /proc/version');
distroVersion = info.stdout.toLowerCase();
}
catch (_a) {
}
if (distroVersion.includes('microsoft')) {
throw new errors_1.ExpectedError((0, lazy_1.stripIndent) `
This command is known not to work on WSL. Please use a CLI release
for Windows (not WSL), or balenaEtcher.`);
}
}
const drive = await this.getDrive(options);
const { confirm } = await Promise.resolve().then(() => require('../../utils/patterns'));
await confirm(options.yes, 'This will erase the selected drive. Are you sure?');
const { sourceDestination, multiWrite } = await Promise.resolve().then(() => require('etcher-sdk'));
const file = new sourceDestination.File({
path: params.image,
});
const source = await file.getInnerSource();
const visuals = (0, lazy_1.getVisuals)();
const progressBars = {
flashing: new visuals.Progress('Flashing'),
verifying: new visuals.Progress('Validating'),
};
await multiWrite.pipeSourceToDestinations({
source,
destinations: [drive],
onFail: (_, error) => {
console.error((0, lazy_1.getChalk)().red.bold(error.message));
if (error.message.includes('EACCES')) {
console.error((0, lazy_1.getChalk)().red.bold('Try running this command with elevated privileges, with sudo or in a shell running with admininstrator privileges.'));
}
},
onProgress: (progress) => {
progressBars[progress.type].update(progress);
},
verify: true,
});
}
async getDrive(options) {
const drive = options.drive || (await (0, lazy_1.getVisuals)().drive('Select a drive'));
const sdk = await Promise.resolve().then(() => require('etcher-sdk'));
const adapter = new sdk.scanner.adapters.BlockDeviceAdapter({
includeSystemDrives: () => false,
unmountOnSuccess: false,
write: true,
direct: true,
});
const scanner = new sdk.scanner.Scanner([adapter]);
await scanner.start();
try {
const d = scanner.getBy('device', drive);
if (d === undefined ||
!(d instanceof sdk.sourceDestination.BlockDevice)) {
throw new errors_1.ExpectedError(`Drive not found: ${options.drive}`);
}
return d;
}
finally {
scanner.stop();
}
}
}
LocalFlashCmd.description = (0, lazy_1.stripIndent) `
Flash an image to a drive.
Flash a balenaOS image to a drive.
Image file may be one of: .img|.zip|.gz|.bz2|.xz
If --drive is not specified, then it will interactively
show a list of available drives for selection.
`;
LocalFlashCmd.examples = [
'$ balena local flash path/to/balenaos.img',
'$ balena local flash path/to/balenaos.img --drive /dev/disk2',
'$ balena local flash path/to/balenaos.img --drive /dev/disk2 --yes',
];
LocalFlashCmd.args = {
image: core_1.Args.string({
description: 'path to OS image',
required: true,
}),
};
LocalFlashCmd.flags = {
drive: cf.drive,
yes: cf.yes,
};
LocalFlashCmd.offlineCompatible = true;
exports.default = LocalFlashCmd;
//# sourceMappingURL=flash.js.map
;