From 900803ee3691324b4054b9c6b7a09b72e52681dd Mon Sep 17 00:00:00 2001 From: Leon Date: Fri, 29 Dec 2023 21:19:34 +0800 Subject: [PATCH] fix: windows file path --- main/main.ts | 118 ++++++++++++++++++++++++++------------------------- package.json | 2 +- 2 files changed, 62 insertions(+), 58 deletions(-) diff --git a/main/main.ts b/main/main.ts index 3fff087..d9118a5 100644 --- a/main/main.ts +++ b/main/main.ts @@ -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(); @@ -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 diff --git a/package.json b/package.json index 6c5668d..298580c 100644 --- a/package.json +++ b/package.json @@ -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",