message: patch: handle ext2fs with webpack
diff: */
  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';
  @@ -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);
  @@ -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
  @@ -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'),
  }),
  @@ -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',
  },
  {
  @@ -394,6 +416,8 @@ const guiConfigCopyPatterns = [
  },
  ];
  +console.log(guiConfigCopyPatterns);
  +
  if (os.platform() === 'win32') {
  // liblzma.dll is required on Windows for lzma-native
  guiConfigCopyPatterns.push({
  