@jasonscheirer/electron-forge-maker-msix
Version:
An `electron-forge` maker for MSIX that supports `electron-forge` v6 and can be used as a replacement for `electron-builder`. Supports code-signing.
122 lines • 3.81 kB
JavaScript
// Borrowed with modifications from https://github.com/felixrieseberg/electron-updater-yaml
// MIT Licensed
import crypto from 'crypto';
import fs from 'fs';
import path from 'path';
/**
* Returns the contents of the app-update.yml file, which you should place in
* your app package's "resources" folder. This file is used by the auto-updater
* to determine where to download updates from.
*/
export async function getAppUpdateYml(options) {
const { name, url, publisherName } = options;
const channel = options.channel || 'latest';
const updaterCacheDirName = options.updaterCacheDirName || `${name.toLowerCase()}-updater`;
let ymlContents = `provider: generic
url: '${url}'
channel: ${channel}
updaterCacheDirName: ${updaterCacheDirName}\n`;
if (publisherName) {
ymlContents += `publisherName:
- ${publisherName}\n`;
}
return ymlContents;
}
/**
* Returns the contents of the channel.yml file, which you should place in your
*/
export async function getChannelYml(options) {
const { installerPath, version } = options;
const platform = options.platform || process.platform;
const releaseDate = options.releaseDate || new Date().toISOString();
const files = await getFiles(installerPath, platform);
let filesText = '';
for (const file of files) {
filesText += `
- url: ${file.name}
sha512: ${file.hash}
size: ${file.size}`;
}
const ymlContents = `version: ${version}
files:${filesText}
path: ${files[0].name}
sha512: ${files[0].hash}
releaseDate: '${releaseDate}'\n`;
return ymlContents;
}
/**
* Returns files we should use to auto-update
*/
async function getFiles(installerPath, platform) {
const files = [];
const result = [];
if (platform === 'win32') {
const exeFile = getFileInFolder(/\.exe$/, installerPath);
const msixFile = getFileInFolder(/\.msix$/, installerPath);
if (!(exeFile || msixFile)) {
throw new Error(`Could not find .exe or .msix file in ${installerPath}`);
}
if (exeFile) {
files.push(exeFile);
}
if (msixFile) {
files.push(msixFile);
}
}
else if (platform === 'darwin') {
const zipFile = getFileInFolder(/\.zip$/, installerPath);
const dmgFile = getFileInFolder(/\.dmg$/, installerPath);
if (!zipFile) {
throw new Error(`Could not find .zip file in ${installerPath}`);
}
if (!dmgFile) {
throw new Error(`Could not find .dmg file in ${installerPath}`);
}
files.push(zipFile, dmgFile);
}
else {
throw new Error(`Unsupported platform: ${platform}`);
}
for (const file of files) {
const filePath = path.join(installerPath, file);
result.push({
name: file,
path: filePath,
size: getFileSize(filePath),
hash: await getFileHash(filePath),
});
}
return result;
}
/**
* Returns an sha512 hash of the file at the given path
*/
function getFileHash(filePath) {
const hash = crypto.createHash('sha512');
const stream = fs.createReadStream(filePath);
hash.setEncoding('base64');
return new Promise((resolve, reject) => {
stream.on('end', () => {
hash.end();
resolve(hash.read());
});
stream.on('error', (error) => {
hash.end();
reject(error);
});
stream.pipe(hash);
});
}
/**
* Returns file size in bytes
*/
function getFileSize(filePath) {
return fs.statSync(filePath).size;
}
/**
* Find files in a folder that match a certain extension
*/
function getFileInFolder(test, dir) {
return fs.readdirSync(dir).find((file) => test.test(file));
}
//# sourceMappingURL=yaml.js.map