-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
165 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { WebpackPlugin } from '@electron-forge/plugin-webpack'; | ||
|
||
import { PluginBase } from '@electron-forge/plugin-base'; | ||
import { ForgeHookMap, ResolvedForgeConfig } from '@electron-forge/shared-types'; | ||
import { DefinePlugin } from 'webpack'; | ||
|
||
import { execSync } from 'child_process'; | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
|
||
import * as d from 'debug'; | ||
|
||
const debug = d('sidecar'); | ||
|
||
function addWebpackDefine(config: ResolvedForgeConfig, name: string, outPath: string): ResolvedForgeConfig { | ||
config.plugins.forEach((plugin) => { | ||
if (plugin.name == 'webpack' && plugin instanceof WebpackPlugin) { | ||
const { mainConfig } = plugin.config as any; | ||
if (mainConfig.plugins == null) { | ||
mainConfig.plugins = []; | ||
} | ||
mainConfig.plugins.push(new DefinePlugin({ | ||
// expose path to helper via this webpack define | ||
[name]: JSON.stringify( | ||
process.argv[1].includes('electron-forge-start') | ||
// on `npm run start`, point directly to the build dir | ||
? outPath | ||
// otherwise point relative to the resources folder of the bundled app | ||
: path.basename(outPath) | ||
), | ||
})); | ||
} | ||
}); | ||
return config; | ||
} | ||
|
||
function build(baseDir: string, arch: string, outPath: string) { | ||
const commands = [ | ||
//'npm rebuild mountutils', | ||
'tsc --project tsconfig.sidecar.json', | ||
`pkg ${baseDir}/util/api.js -c pkg-sidecar.json` + | ||
// so that we can cross-compile for arm64 on x64 | ||
' --no-bytecode --public --public-packages "*"' + | ||
` --target ${arch} --output ${outPath}`, | ||
]; | ||
commands.forEach((cmd) => execSync(cmd, { shell: 'bash', stdio: 'inherit' })); | ||
} | ||
|
||
function copyArtifact(filePath: string, outPath: string) { | ||
const dest = path.resolve(outPath, path.basename(filePath)); | ||
debug(`copying '${filePath}' to '${dest}'`); | ||
fs.copyFileSync(filePath, dest); | ||
} | ||
|
||
// loosely based on https://github.com/cwellsx/electron-forge-resource-plugin | ||
export class SidecarPlugin extends PluginBase<void> { | ||
name = 'sidecar'; | ||
|
||
constructor() { | ||
super(); | ||
this.getHooks = this.getHooks.bind(this); | ||
} | ||
|
||
getHooks(): ForgeHookMap { | ||
const DEFINE_NAME = 'ETCHER_UTIL_BIN_PATH'; | ||
const BASE_DIR = 'out/sidecar'; | ||
const BIN_DIR = `${BASE_DIR}/bin`; | ||
const OUT_PATH = `${BIN_DIR}/etcher-util${process.platform === 'win32' ? '.exe' : ''}`; | ||
|
||
return { | ||
resolveForgeConfig: async (currentConfig) => { | ||
debug('resolveForgeConfig'); | ||
return addWebpackDefine(currentConfig, DEFINE_NAME, OUT_PATH); | ||
}, | ||
generateAssets: async (_config, platform, arch) => { | ||
debug('generateAssets', { platform, arch }); | ||
build(BASE_DIR, arch, OUT_PATH); | ||
}, | ||
packageAfterCopy: async (_config, buildPath, electronVersion, platform, arch) => { | ||
debug('packageAfterCopy', { buildPath, electronVersion, platform, arch }); | ||
// buildPath points to appPath, which is inside resources dir, which is what we want | ||
copyArtifact(OUT_PATH, path.dirname(buildPath)); | ||
}, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,12 @@ | ||
// See the Electron documentation for details on how to use preload scripts: | ||
// https://www.electronjs.org/docs/latest/tutorial/process-model#preload-scripts | ||
|
||
import * as webapi from '../webapi'; | ||
|
||
declare global { | ||
interface Window { | ||
etcher: typeof webapi; | ||
} | ||
} | ||
|
||
window['etcher'] = webapi; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// | ||
// Anything exported from this module will become available to the | ||
// renderer process via preload. They're accessible as `window.etcher.foo()`. | ||
// | ||
|
||
import { ipcRenderer } from 'electron'; | ||
|
||
// FIXME: this is a workaround for the renderer to be able to find the etcher-util | ||
// binary. We should instead export a function that asks the main process to launch | ||
// the binary itself. | ||
export async function getEtcherUtilPath(): Promise<string> { | ||
return await ipcRenderer.invoke('get-util-path'); | ||
} |
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,8 +14,6 @@ | |
"url": "[email protected]:balena-io/etcher.git" | ||
}, | ||
"scripts": { | ||
"build:rebuild-mountutils": "cd node_modules/mountutils && npm rebuild", | ||
"build:sidecar": "npm run build:rebuild-mountutils && tsc --project tsconfig.sidecar.json && pkg build/util/api.js -c pkg-sidecar.json --target node18 --output generated/etcher-util", | ||
"lint-css": "prettier --write lib/**/*.css", | ||
"lint-ts": "balena-lint --fix --typescript typings lib tests webpack.config.ts", | ||
"lint": "npm run lint-ts && npm run lint-css", | ||
|
@@ -88,6 +86,7 @@ | |
"@svgr/webpack": "5.5.0", | ||
"@types/chai": "4.3.4", | ||
"@types/copy-webpack-plugin": "6.4.3", | ||
"@types/debug": "^4.1.12", | ||
"@types/mime-types": "2.1.1", | ||
"@types/mini-css-extract-plugin": "1.4.3", | ||
"@types/mocha": "^9.1.1", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters