ffmpeg-static-electron-forge
Version:
This module is designed for fast integration with electron forge and webpack.
104 lines (103 loc) • 5 kB
JavaScript
;
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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.bins = exports.paths = void 0;
const plugin_base_1 = __importDefault(require("@electron-forge/plugin-base"));
const path_1 = __importDefault(require("path"));
const glob_1 = require("glob");
const util_1 = __importDefault(require("util"));
const os_1 = __importDefault(require("os"));
const fs_1 = __importDefault(require("fs"));
const ffmpegName = os_1.default.platform() === "win32" ? "ffmpeg.exe" : "ffmpeg";
const ffprobeName = os_1.default.platform() === "win32" ? "ffprobe.exe" : "ffprobe";
class FFmpegStatic extends plugin_base_1.default {
constructor() {
super(...arguments);
this.name = "ffmpeg-static-electron-forge";
this.resolveForgeConfig = (forgeConfig) => __awaiter(this, void 0, void 0, function* () {
if (!forgeConfig.packagerConfig) {
forgeConfig.packagerConfig = {};
}
if (!forgeConfig.packagerConfig.asar) {
throw new Error("The ffmpeg-static-electron-forge plugin requires asar to be truthy or an object");
}
if (forgeConfig.packagerConfig.asar === true) {
forgeConfig.packagerConfig.asar = {};
}
const existingUnpack = forgeConfig.packagerConfig.asar.unpack;
const newUnpack = "ffmpeg,ffmpeg.exe,ffprobe,ffprobe.exe";
if (existingUnpack) {
forgeConfig.packagerConfig.asar.unpack = `{${existingUnpack},${newUnpack}}`;
}
else {
forgeConfig.packagerConfig.asar.unpack = newUnpack;
}
return forgeConfig;
});
this.prePackage = (forgeConfig) => __awaiter(this, void 0, void 0, function* () {
if (this.config.remove)
yield this.removeFFmpegName();
const platform = this.config.platform || os_1.default.platform();
const arch = this.config.arch || os_1.default.arch();
const binaries = {
darwin: ["x64", "arm64"],
linux: ["x64", "ia32"],
win32: ["x64", "ia32"],
};
// @ts-ignore
if (platform in binaries && binaries[platform].includes(arch)) {
const ffmpegPath = path_1.default.join(__dirname, "bin", platform, arch, ffmpegName);
const ffprobePath = path_1.default.join(__dirname, "bin", platform, arch, ffprobeName);
fs_1.default.copyFileSync(ffmpegPath, path_1.default.join(this.config.path, ffmpegName));
fs_1.default.copyFileSync(ffprobePath, path_1.default.join(this.config.path, ffprobeName));
}
});
}
getHooks() {
return {
resolveForgeConfig: this.resolveForgeConfig,
prePackage: this.prePackage,
};
}
removeFFmpegName() {
return __awaiter(this, void 0, void 0, function* () {
const deleteFiles = util_1.default.promisify(fs_1.default.unlink);
const pattern = path_1.default.join(this.config.path, "**", "@(ffmpeg|ffmpeg.exe|ffprobe|ffprobe.exe)");
try {
const files = yield (0, glob_1.glob)(pattern);
try {
for (const file of files) {
yield deleteFiles(file);
console.log(`Deleted file: ${file}`);
}
}
catch (error) {
console.error(`Error to delete file: ${error}`);
}
}
catch (error) {
console.log(error);
}
});
}
}
exports.default = FFmpegStatic;
exports.paths = {
ffmpegPath: path_1.default.join(__dirname, ffmpegName),
ffprobePath: path_1.default.join(__dirname, ffprobeName),
};
exports.bins = {
ffmpegPath: path_1.default.join(os_1.default.platform(), os_1.default.arch(), ffmpegName),
ffprobePath: path_1.default.join(os_1.default.platform(), os_1.default.arch(), ffprobeName),
};