Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bundle Fuse Installer with Release #357

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ jobs:
run: yarn install --network-timeout 1000000
- name: Get daemon
run: yarn download-daemon
- name: Get daemon
if: matrix.os == 'macos-latest'
run: yarn download-fuse
- name: Linux app pack and release
if: matrix.os == 'ubuntu-latest'
run: yarn electron-pack --linux
Expand All @@ -50,7 +53,7 @@ jobs:
if: matrix.os == 'macos-latest'
run: yarn electron-pack --mac
- name: Windows app pack and release
env:
env:
CSC_LINK: ${{ secrets.WIN_CSC_LINK }}
CSC_KEY_PASSWORD: ${{ secrets.WIN_CSC_KEY_PASSWORD }}
if: matrix.os == 'windows-latest'
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"build": "node scripts/build.js",
"test": "node scripts/test.js",
"download-daemon": "rimraf resources && node scripts/download-daemon.js",
"download-fuse": "rimraf resources/FuseInstaller && node scripts/download-fuse-pkg.js",
"lint": "eslint ./ --ext .js --ext .jsx",
"electron:dev": "concurrently \"cross-env BROWSER=none yarn start\" \"wait-on http://localhost:3000 && electron .\"",
"electron-pack": "electron-builder -c.extraMetadata.main=build/electron-app.js",
Expand All @@ -23,7 +24,7 @@
"build-storybook": "build-storybook -s public"
},
"dependencies": {
"@fleekhq/space-client": "^1.1.5",
"@fleekhq/space-client": "^1.1.6",
"@fortawesome/fontawesome-svg-core": "^1.2.28",
"@fortawesome/free-brands-svg-icons": "^5.13.0",
"@fortawesome/free-solid-svg-icons": "^5.13.0",
Expand Down
22 changes: 22 additions & 0 deletions public/electron/events/askpass.osascript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env osascript -l JavaScript

/* eslint-disable */

ObjC.import('stdlib')

const app = Application.currentApplication()
app.includeStandardAdditions = true

const result = app.displayDialog('Space wants to install OSX FUSE and needs privilege access. Enter your password to allow this.', {
defaultAnswer: '',
withIcon: 'note',
buttons: ['Cancel', 'Install OSX FUSE'],
defaultButton: 'Install OSX FUSE',
hiddenAnswer: true,
})

if (result.buttonReturned === 'Install OSX FUSE') {
result.textReturned
} else {
$.exit(255)
}
133 changes: 133 additions & 0 deletions public/electron/events/fuse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const { ipcMain } = require('electron');
const isDev = require('electron-is-dev');
const { exec } = require('child_process');
const downloadFile = require('../utils/downloader');

const EVENT_PREFIX = 'fuse';

const DOWNLOAD_FUSE_EVENT = `${EVENT_PREFIX}:download`;
const DOWNLOAD_FUSE_ERROR_EVENT = `${DOWNLOAD_FUSE_EVENT}:error`;
const DOWNLOAD_FUSE_SUCCESS_EVENT = `${DOWNLOAD_FUSE_EVENT}:success`;
const UPDATE_PROGRESS_DOWNLOAD_FUSE_EVENT = `${DOWNLOAD_FUSE_EVENT}:update`;

const INSTALL_FUSE_EVENT = `${EVENT_PREFIX}:install`;
const INSTALL_FUSE_ERROR_EVENT = `${INSTALL_FUSE_EVENT}:error`;
const INSTALL_FUSE_SUCCESS_EVENT = `${INSTALL_FUSE_EVENT}:success`;

const DELETE_FUSE_INSTALLER = `${EVENT_PREFIX}:delete`;
const DELETE_FUSE_INSTALLER_ERROR = `${DELETE_FUSE_INSTALLER}:error`;
const DELETE_FUSE_INSTALLER_SUCCESS = `${DELETE_FUSE_INSTALLER}:success`;

const fusePkgUrl = 'https://space-fuse-asset.s3-us-west-2.amazonaws.com/FUSE+for+macOS+3.11.0.pkg';
const resourceName = 'FuseInstaller.pkg';

/* eslint-disable no-console */
const registerFuseEvents = (mainWindow) => {
ipcMain.on(INSTALL_FUSE_EVENT, async (event) => {
const installFuse = () => new Promise((resolve, reject) => {
const installerPath = isDev
? path.join(__dirname, '../../../resources/FuseInstaller.pkg')
: path.join(process.resourcesPath, 'FuseInstaller.pkg');

const command = `sudo -A installer -pkg ${installerPath} -target /`;
const options = {
env: {
PATH: process.env.PATH,
SUDO_ASKPASS: path.join(__dirname, 'askpass.osascript.js'),
},
};

exec(command, options, (err, stdout, stderr) => {
if (err || stderr) {
reject(err || stderr);
return;
}

resolve(stdout);
});
});

const loadFuseKernel = () => new Promise((resolve, reject) => {
const command = '/Library/Filesystems/osxfuse.fs/Contents/Resources/load_osxfuse';

exec(command, (err, stdout, stderr) => {
if (err || stderr) {
reject(err || stderr);
return;
}

resolve(stdout);
});
});

try {
console.log(chalk.yellow('installing fuse'));
const resInstall = await installFuse();
console.log(chalk.blue(resInstall));

console.log(chalk.yellow('loading fuse kernel'));
const resKernel = await loadFuseKernel();
console.log(chalk.blue(resKernel));

event.sender.send(INSTALL_FUSE_SUCCESS_EVENT);
console.log(chalk.yellow('installation completed successfully'));
} catch (error) {
console.error(chalk.red(error));
event.sender.send(INSTALL_FUSE_ERROR_EVENT);
}
});

ipcMain.on(DOWNLOAD_FUSE_EVENT, async () => {
try {
const cwd = process.cwd();
const [data, headers, writer] = await downloadFile(fusePkgUrl, cwd, resourceName);

const totalLength = headers['content-length'];

data.on('data', (chunk) => {
mainWindow.webContents.send(UPDATE_PROGRESS_DOWNLOAD_FUSE_EVENT, {
totalLength,
chunkLength: chunk.length,
});
});

data.on('error', (error) => {
data.destroy();
writer.destroy();
console.error('DOWNLOAD_FUSE_ERROR_EVENT', error);
mainWindow.webContents.send(DOWNLOAD_FUSE_ERROR_EVENT, error);
});

writer.on('error', (error) => {
writer.destroy();
console.error('DOWNLOAD_FUSE_ERROR_EVENT', error);
mainWindow.webContents.send(DOWNLOAD_FUSE_ERROR_EVENT, error);
});

writer.on('finish', async () => {
mainWindow.webContents.send(DOWNLOAD_FUSE_SUCCESS_EVENT);
});

data.pipe(writer);
} catch (error) {
console.error('DOWNLOAD_FUSE_ERROR_EVENT', error);
mainWindow.webContents.send(DOWNLOAD_FUSE_ERROR_EVENT, error);
}
});

ipcMain.on(DELETE_FUSE_INSTALLER, (event) => {
fs.unlink(path.join(process.cwd(), resourceName), (err) => {
if (err) {
event.sender.send(DELETE_FUSE_INSTALLER_ERROR, err);
return;
}

event.sender.send(DELETE_FUSE_INSTALLER_SUCCESS);
});
});
};

module.exports = registerFuseEvents;
2 changes: 2 additions & 0 deletions public/electron/events/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const registerWalletEvents = require('./wallet');
const registerWinResizeEvents = require('./win-resize');
const registerShellEvents = require('./shell');
const registerSubscriptions = require('./subscriptions');
const registerFuseEvents = require('./fuse');

const registerEvents = ({
app,
Expand All @@ -36,6 +37,7 @@ const registerEvents = ({
registerWinResizeEvents(mainWindow);
registerShellEvents(mainWindow);
registerSubscriptions(mainWindow);
registerFuseEvents(mainWindow);

if (!isDev && process.env.SKIP_AUTOUPDATE !== 'true') {
registerAppUpdate({ app, mainWindow });
Expand Down
26 changes: 26 additions & 0 deletions public/electron/utils/downloader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const fs = require('fs-extra');
const path = require('path');
const axios = require('axios');

const defaultAxiosOptions = {
method: 'GET',
responseType: 'stream',
};

async function downloadFile(url, targetPath, filename, options = {}) {
const { data, headers } = await axios({
...defaultAxiosOptions,
...options,
url,
});

if (!fs.existsSync(targetPath)) {
fs.mkdirSync(targetPath);
}

const writer = fs.createWriteStream(path.join(targetPath, filename), { mode: 0o755 });

return [data, headers, writer];
}

module.exports = downloadFile;
69 changes: 69 additions & 0 deletions scripts/download-fuse-pkg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* eslint-disable no-console */
const fs = require('fs-extra');
const path = require('path');
const axios = require('axios');
const ProgressBar = require('progress');

async function getInstaller() {
const { cwd, platform } = process;
if (platform !== 'darwin') {
return
}

const resourcesPath = path.resolve(cwd(), 'resources');

const fusePkgUrl = `https://space-fuse-asset.s3-us-west-2.amazonaws.com/FUSE+for+macOS+3.11.0.pkg`;
const { data, headers } = await axios({
method: 'GET',
responseType: 'stream',
url: fusePkgUrl,
}).catch((error) => {
console.error(`\nError when trying to download the package: ${fusePkgUrl}`);
console.error(`Error : ${error.stack || error.message}`);
process.exit(1);
});

const totalLength = headers['content-length'];

console.log(`Downloading Osx Fuse from ${fusePkgUrl}:`);
const progressBar = new ProgressBar(`File: FUSE+for+macOS+3.11.0.pkg [:bar] :percent :etas`, {
width: 40,
complete: '=',
incomplete: ' ',
renderThrottle: 1,
total: parseInt(totalLength, 10),
});

if (!fs.existsSync(resourcesPath)) {
fs.mkdirSync(resourcesPath);
}
// save to ./resources/
const writer = fs.createWriteStream(path.join(resourcesPath, `FuseInstaller.pkg`), { mode: 0o755 });

data.on('data', (chunk) => (
progressBar.tick(chunk.length)
));

data.on('error', (error) => {
data.destroy();
writer.destroy();

console.error(`\nError when downloading the Fuse installer binary: ${error.stack || error.message}`);
process.exit(1);
});

writer.on('finish', async () => {
process.exit(0);
});

writer.on('error', (error) => {
writer.destroy();

console.error(`\nError when saving the Fuse installer: ${error.stack || error.message}`);
process.exit(1);
});

data.pipe(writer);
}

getInstaller();
59 changes: 59 additions & 0 deletions src/events/fuse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { ipcRenderer } from 'electron';

const EVENT_PREFIX = 'fuse';

const DOWNLOAD_FUSE_EVENT = `${EVENT_PREFIX}:download`;
const DOWNLOAD_FUSE_ERROR_EVENT = `${DOWNLOAD_FUSE_EVENT}:error`;
const DOWNLOAD_FUSE_SUCCESS_EVENT = `${DOWNLOAD_FUSE_EVENT}:success`;
const UPDATE_PROGRESS_DOWNLOAD_FUSE_EVENT = `${DOWNLOAD_FUSE_EVENT}:update`;

const INSTALL_FUSE_EVENT = `${EVENT_PREFIX}:install`;
const INSTALL_FUSE_ERROR_EVENT = `${INSTALL_FUSE_EVENT}:error`;
const INSTALL_FUSE_SUCCESS_EVENT = `${INSTALL_FUSE_EVENT}:success`;

const DELETE_FUSE_INSTALLER = `${EVENT_PREFIX}:delete`;
const DELETE_FUSE_INSTALLER_ERROR = `${DELETE_FUSE_INSTALLER}:error`;
const DELETE_FUSE_INSTALLER_SUCCESS = `${DELETE_FUSE_INSTALLER}:success`;

/* eslint-disable no-console */
const registerFuseEvents = () => {
ipcRenderer.on(DOWNLOAD_FUSE_ERROR_EVENT, (event, error) => {
console.error('DOWNLOAD_FUSE_ERROR_EVENT', error);
});

ipcRenderer.on(DOWNLOAD_FUSE_SUCCESS_EVENT, () => {
console.log('DOWNLOAD_FUSE_SUCCESS_EVENT');
});

ipcRenderer.on(UPDATE_PROGRESS_DOWNLOAD_FUSE_EVENT, (event, payload) => {
console.log('UPDATE_PROGRESS_DOWNLOAD_FUSE_EVENT', payload);
});
};

export const installFuse = () => new Promise((resolve, reject) => {
ipcRenderer.send(INSTALL_FUSE_EVENT);

ipcRenderer.on(INSTALL_FUSE_ERROR_EVENT, (event, error) => {
reject(error);
});

ipcRenderer.on(INSTALL_FUSE_SUCCESS_EVENT, (event, result) => {
resolve(result);
});
});

export const deleteFuseInstaller = () => new Promise((resolve, reject) => {
ipcRenderer.send(DELETE_FUSE_INSTALLER);

ipcRenderer.on(DELETE_FUSE_INSTALLER_ERROR, (event, err) => {
reject(err);
});

ipcRenderer.on(DELETE_FUSE_INSTALLER_SUCCESS, () => {
resolve();
});
});

export const downloadFuse = () => ipcRenderer.send(DOWNLOAD_FUSE_EVENT);

export default registerFuseEvents;
3 changes: 3 additions & 0 deletions src/events/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import registerUsageEvents from './usage';
import registerNotificationSubscribe from './notifications-subscribe';
import walletSubscribe from './wallet';
import registerSubscriptions from './subscriptions';
import registerFuseEvents from './fuse';

const registerEvents = () => {
registerShortcuts();
Expand All @@ -35,6 +36,7 @@ const registerEvents = () => {
walletSubscribe();
registerSubscriptions();
registerSubscriptions();
registerFuseEvents();
};

export default registerEvents;
Expand All @@ -52,3 +54,4 @@ export * from './usage';
export * from './wallet';
export * from './win-resize';
export * from './subscriptions';
export * from './fuse';
Loading