handbrake-ts
Version:
Handbrake with TypeScript and support for electron
248 lines • 10.5 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Installator = void 0;
const rxjs_1 = require("rxjs");
const Errors_1 = require("./Errors");
const Node_1 = require("./Node");
class Installator {
constructor(config) {
this.baseDownloadLink = 'http://github.com/HandBrake/HandBrake/releases/download';
this.veresion = '1.3.3';
this.config = config;
this.stateChange = new rxjs_1.BehaviorSubject({ status: 'idle' });
}
get state() {
return this.stateChange.getValue();
}
updateState(change) {
const newState = Object.assign(this.state, change);
this.stateChange.next(newState);
}
// install
isInstalled(dist) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const platform = dist !== null && dist !== void 0 ? dist : this.config.detectDist();
const map = {
'darwin': this.isInstaledMac.bind(this),
'linux': this.isInstaledLinux.bind(this),
'win32': this.isInstaledWindows.bind(this),
'invalid': () => { throw new Errors_1.ErrorHB("Invalid platform", "PLATFORM_NOT_SUPPORTED"); }
};
const method = (_a = map[platform]) !== null && _a !== void 0 ? _a : map['invalid'];
return yield method();
});
}
install(dist) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
console.log(this.config, JSON.stringify(this.config));
if (yield this.isInstalled(dist)) {
this.updateState({ status: 'installed' });
return;
}
this.updateState({ status: 'instaling' });
const platform = dist !== null && dist !== void 0 ? dist : this.config.detectDist();
const map = {
'darwin': this.installMac.bind(this),
'linux': this.installLinux.bind(this),
'win32': this.installWin.bind(this),
'invalid': () => { throw new Errors_1.ErrorHB("Invalid platform", "PLATFORM_NOT_SUPPORTED"); }
};
try {
const method = (_a = map[platform]) !== null && _a !== void 0 ? _a : map['invalid'];
const result = yield method();
this.updateState({ status: 'installed' });
return result;
}
catch (e) {
this.updateState({ status: 'failure', error: e.message });
}
});
}
uninstall(dist) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
if (!(yield this.isInstalled(dist))) {
this.updateState({ status: 'uinstalled' });
return;
}
this.updateState({ status: 'uninstalling' });
const platform = dist !== null && dist !== void 0 ? dist : this.config.detectDist();
const map = {
'darwin': this.uninstallMac.bind(this),
'linux': this.uninstallLinux.bind(this),
'win32': this.uninstalllWin.bind(this),
'invalid': () => { throw new Errors_1.ErrorHB("Invalid platform", "PLATFORM_NOT_SUPPORTED"); }
};
try {
const method = (_a = map[platform]) !== null && _a !== void 0 ? _a : map['invalid'];
const result = yield method();
this.updateState({ status: 'uinstalled' });
return result;
}
catch (e) {
this.updateState({ status: 'failure', error: e.message });
}
});
}
// win
installWin() {
return __awaiter(this, void 0, void 0, function* () {
const link = this.getDownloadLink('HandBrakeCLI-1.3.3-win-x86_64.zip');
const file = yield this.downloadFile(link, 'zip');
const unzipDir = Node_1.path.join(this.config.mainDirectory, 'unzip');
this.updateState({ status: 'extractiong' });
Node_1.fs
.createReadStream(file)
.pipe(Node_1.unzipper.Extract({ path: unzipDir }));
yield this.wait(500);
console.log(Node_1.path.join(unzipDir, 'HandBrakeCLI.exe'), Node_1.fs.existsSync(Node_1.path.join(unzipDir, 'HandBrakeCLI.exe')));
console.log(this.config.getCliPath('win32'), Node_1.fs.existsSync(this.config.getCliPath('win32')));
Node_1.fs.copyFileSync(Node_1.path.join(unzipDir, 'HandBrakeCLI.exe'), this.config.getCliPath('win32'));
yield this.wait(500);
this.removeDir(unzipDir);
this.removeFile(file);
});
}
uninstalllWin() {
return __awaiter(this, void 0, void 0, function* () {
this.removeFile(this.config.getCliPath('win32'));
});
}
// mac
installMac() {
return __awaiter(this, void 0, void 0, function* () {
const link = this.getDownloadLink('HandBrakeCLI-1.3.3.dmg');
const file = yield this.downloadFile(link, 'dmg');
console.log(file);
throw new Errors_1.ErrorHB('Not supported', 'PLATFORM_INSTALATION_NOT_SUPPORTED');
});
}
uninstallMac() {
return __awaiter(this, void 0, void 0, function* () {
this.removeFile(this.config.getCliPath('darwin'));
});
}
// linux
installLinux() {
return __awaiter(this, void 0, void 0, function* () {
const commandFull = [
'add-apt-repository ppa:stebbins/handbrake-releases -y',
'apt-get update',
'apt-get install handbrake-cli -y '
];
console.log('command install: ', 'sudo ' + commandFull.join(" && sudo "));
return yield this.runCommandSudo(commandFull.join(" && "));
});
}
uninstallLinux() {
return __awaiter(this, void 0, void 0, function* () {
const commandFull = [
'apt remove handbrake-cli -y',
'add-apt-repository -r ppa:stebbins/handbrake-releases -y',
];
console.log('command uninstalll: ', 'sudo ' + commandFull.join(" && sudo "));
return yield this.runCommandSudo(commandFull.join(" && "));
});
}
// checks state
isInstaledLinux() {
return __awaiter(this, void 0, void 0, function* () {
try {
yield this.runCommand('HandBrakeCLI --version');
return true;
}
catch (e) {
return false;
}
});
}
isInstaledWindows() {
return __awaiter(this, void 0, void 0, function* () {
return Node_1.fs.existsSync(this.config.getCliPath('win32'));
});
}
isInstaledMac() {
return __awaiter(this, void 0, void 0, function* () {
return false;
});
}
// helpers
getDownloadLink(fileName) {
return `${this.baseDownloadLink}/${this.veresion}/${fileName}`;
}
downloadFile(link, extension) {
return __awaiter(this, void 0, void 0, function* () {
this.updateState({ status: 'downloading' });
const fileName = Node_1.path.join(this.config.mainDirectory, `HandBrakeInstallation.${extension}`);
this.removeFile(fileName);
console.log('downloading ... ', link, 'to: ', fileName);
const res = yield Node_1.fetch(link, { redirect: 'follow' });
if (res.ok) {
const file = Node_1.fs.createWriteStream(fileName);
res.body.pipe(file);
return new Promise((res, rej) => {
file.on('finish', () => __awaiter(this, void 0, void 0, function* () {
// file.close();
console.log('finish download');
yield this.wait(500);
res(fileName);
}));
});
}
else {
this.removeFile(fileName);
throw new Errors_1.ErrorHB(`Failed to donwload: ${link}`, 'DOWNLOAD_FAILED');
}
});
}
removeFile(fileName) {
if (Node_1.fs.existsSync(fileName)) {
Node_1.fs.unlinkSync(fileName);
}
}
removeDir(dir) {
console.log('remove dir: ', dir);
try {
Node_1.fs.rmdirSync(dir, { recursive: true, maxRetries: 2, retryDelay: 500 });
}
catch (e) {
console.warn(e.message);
}
}
runCommandSudo(command) {
return __awaiter(this, void 0, void 0, function* () {
try {
return yield new Promise((resolve, reject) => {
const options = { name: 'Hand Break' };
Node_1.sudoPrompt.exec(command, options, (err, stdout, stderr) => err ? reject(err) : resolve(stdout));
});
}
catch (e) {
throw new Errors_1.ErrorHB(e.message, 'LINUX_INSTALL_ERROR');
}
});
}
runCommand(command) {
return new Promise((resolve, reject) => {
Node_1.childProcess.exec(command, (err, stdout, stderr) => err ? reject(err) : resolve(stdout));
});
}
wait(time) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise(res => setTimeout(res, time));
});
}
}
exports.Installator = Installator;
//# sourceMappingURL=Installator.js.map