Skip to content

Commit

Permalink
patch: handle ext2fs with webpack
Browse files Browse the repository at this point in the history
  • Loading branch information
mcraa committed Dec 23, 2022
1 parent 2872658 commit b59b171
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions webpack.config.ts
Expand Up @@ -15,7 +15,7 @@
*/

import * as CopyPlugin from 'copy-webpack-plugin';
import { readdirSync } from 'fs';
import { readdirSync, existsSync } from 'fs';
import * as _ from 'lodash';
import * as os from 'os';
import outdent from 'outdent';
Expand Down Expand Up @@ -77,6 +77,27 @@ function renameNodeModules(resourcePath: string) {
);
}

function findExt2fsFolder(): string {
const ext2fs = 'node_modules/ext2fs';
const biFsExt2fs = 'node_modules/balena-image-fs/node_modules/ext2fs';

if (existsSync(ext2fs)) {
return ext2fs;
} else if (existsSync(biFsExt2fs)) {
return biFsExt2fs;
} else {
throw Error('ext2fs not found');
}
}

function makeExt2FsRegex(): RegExp {
const folder = findExt2fsFolder();
const libpath = '/lib/libext2fs\\.js&';

const regex = folder.concat(libpath).split('/').join('/');
return new RegExp(regex);
}

function findUsbPrebuild(): string[] {
const usbPrebuildsFolder = path.join('node_modules', 'usb', 'prebuilds');
const prebuildFolders = readdirSync(usbPrebuildsFolder);
Expand Down Expand Up @@ -152,6 +173,7 @@ function slashOrAntislash(pattern: RegExp): RegExp {
}

function replace(test: RegExp, ...replacements: ReplacementRule[]) {
console.log(test.source);
return {
loader: 'string-replace-loader',
// Handle windows path separators
Expand Down Expand Up @@ -322,7 +344,7 @@ const commonConfig = {
// Use the libext2fs.wasm file in the generated folder
// The way to find the app directory depends on whether we run in the renderer or in the child-writer
// We use __dirname in the child-writer and electron.remote.app.getAppPath() in the renderer
replace(/node_modules\/ext2fs\/lib\/libext2fs\.js$/, {
replace(makeExt2FsRegex(), {
search: 'scriptDirectory = __dirname + "/";',
replace: fetchWasm('ext2fs', 'lib'),
}),
Expand Down Expand Up @@ -385,7 +407,7 @@ const guiConfigCopyPatterns = [
to: 'modules/node-raspberrypi-usbboot/blobs',
},
{
from: 'node_modules/ext2fs/lib/libext2fs.wasm',
from: `${findExt2fsFolder()}/lib/libext2fs.wasm`,
to: 'modules/ext2fs/lib/libext2fs.wasm',
},
{
Expand All @@ -394,6 +416,8 @@ const guiConfigCopyPatterns = [
},
];

console.log(guiConfigCopyPatterns);

if (os.platform() === 'win32') {
// liblzma.dll is required on Windows for lzma-native
guiConfigCopyPatterns.push({
Expand Down

0 comments on commit b59b171

Please sign in to comment.