Skip to content

Commit

Permalink
fix: windows file path
Browse files Browse the repository at this point in the history
  • Loading branch information
originalix committed Dec 29, 2023
1 parent 1c6835e commit 900803e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 58 deletions.
118 changes: 61 additions & 57 deletions main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ logger.debug('preloadJsUrl', preloadJsUrl);

const sdkConnectSrc = isDev
? `file://${path.join(staticPath, 'js-sdk/')}`
: '/static/js-sdk/';
: formatUrl({
pathname: path.join(__dirname, '../build/static/js-sdk/'),
protocol: 'file',
slashes: true,
});

function initChildProcess() {
return initProcess();
Expand Down Expand Up @@ -94,62 +98,62 @@ function createWindow() {
}
);

if (!isDev) {
const PROTOCOL = 'file';
session.defaultSession.protocol.interceptFileProtocol(
PROTOCOL,
(request, callback) => {
const isJsSdkFile = request.url.indexOf('/static/js-sdk') > -1;
const isIFrameHtml =
request.url.indexOf('/static/js-sdk/iframe.html') > -1;

logger.debug('=====>request: ', request.url);

// resolve iframe path
if (isJsSdkFile && isIFrameHtml) {
callback({
path: path.join(__dirname, '../build/static/js-sdk/iframe.html'),
});
return;
}

if (isJsSdkFile) {
const urlPath = parse(request.url).pathname;
if (urlPath) {
const decodedPath = decodeURI(urlPath);
// Remove leading '/' on Windows
const normalizedPath =
process.platform === 'win32'
? decodedPath.replace(/^\/+/, '')
: decodedPath;
// File path for files in js-sdk folder
const sdkFilePath = path.join(
__dirname,
`../build${normalizedPath}`
);
logger.debug('sdkfilePath: ', sdkFilePath);
callback({ path: sdkFilePath });
return;
}
}

// Otherwise, convert the file URL to a file path
const parsedUrl = parse(request.url);
let filePath = '';

if (parsedUrl.pathname) {
filePath = decodeURI(path.normalize(parsedUrl.pathname));
}

// Windows platform compatibility
if (process.platform === 'win32') {
filePath = filePath.replace(/^\/+/, '');
}

callback({ path: filePath });
}
);
}
// if (!isDev) {
// const PROTOCOL = 'file';
// session.defaultSession.protocol.interceptFileProtocol(
// PROTOCOL,
// (request, callback) => {
// const isJsSdkFile = request.url.indexOf('/static/js-sdk') > -1;
// const isIFrameHtml =
// request.url.indexOf('/static/js-sdk/iframe.html') > -1;

// logger.debug('=====>request: ', request.url);

// // resolve iframe path
// if (isJsSdkFile && isIFrameHtml) {
// callback({
// path: path.join(__dirname, '../build/static/js-sdk/iframe.html'),
// });
// return;
// }

// if (isJsSdkFile) {
// const urlPath = parse(request.url).pathname;
// if (urlPath) {
// const decodedPath = decodeURI(urlPath);
// // Remove leading '/' on Windows
// const normalizedPath =
// process.platform === 'win32'
// ? decodedPath.replace(/^\/+/, '')
// : decodedPath;
// // File path for files in js-sdk folder
// const sdkFilePath = path.join(
// __dirname,
// `../build${normalizedPath}`
// );
// logger.debug('sdkfilePath: ', sdkFilePath);
// callback({ path: sdkFilePath });
// return;
// }
// }

// // Otherwise, convert the file URL to a file path
// const parsedUrl = parse(request.url);
// let filePath = '';

// if (parsedUrl.pathname) {
// filePath = decodeURI(path.normalize(parsedUrl.pathname));
// }

// // Windows platform compatibility
// if (process.platform === 'win32') {
// filePath = filePath.replace(/^\/+/, '');
// }

// callback({ path: filePath });
// }
// );
// }

ipcMain.on('read-bin-file', (event, filePath, responseChannel) => {
const firmwarePath = isDev
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
"test": "node scripts/test.js",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"clean:build": "rimraf ./build-electron && rimraf ./build && rimraf ./web-build && rimraf ./dist",
"clean:build": "rimraf ./build-electron && rimraf ./build && rimraf ./web-build && rimraf ./dist && rimraf ./public/js-sdk",
"build:renderer": "rm -rf ./build && rm -rf ./web-build && NODE_ENV=production node scripts/build.js && mv ./web-build ./build && rsync -a public/static/ build/static",
"build:main": "rimraf ./dist && yarn copy:inject && webpack --config development/webpack.main.config.js",
"build:electron": "export CSC_IDENTITY_AUTO_DISCOVERY=false && electron-builder build -mw --config development/electron-builder.config.js",
Expand Down

0 comments on commit 900803e

Please sign in to comment.