Skip to content

Commit

Permalink
Update handle-file-urls.js
Browse files Browse the repository at this point in the history
  • Loading branch information
RobbieTheWagner committed Jan 26, 2021
1 parent 9f871fe commit d99245e
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions electron-app/src/handle-file-urls.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { protocol } = require('electron');
const fs = require('fs');
const path = require('path');
const { fileURLToPath } = require('url');
const { promisify } = require('util');

const access = promisify(fs.access);

//
Expand All @@ -14,15 +14,29 @@ const access = promisify(fs.access);
// interpreted as being relative to the root of the Ember app. If so, we return
// that path, and if not we leave them as-is, as their absolute path.
//
async function getAssetPath(emberAppDir, url) {
let urlPath = fileURLToPath(url);
// Get the root of the path -- should be '/' on MacOS or something like
// 'C:\' on Windows
let { root } = path.parse(urlPath);
// Get the relative path from the root to the full path
let relPath = path.relative(root, urlPath);
// Join the relative path with the Ember app directory
let appPath = path.join(emberAppDir, relPath);
try {
await access(appPath);
return appPath;
} catch (e) {
return urlPath;
}
}

module.exports = function handleFileURLs(emberAppDir) {
const { protocol } = require('electron');

protocol.interceptFileProtocol('file', async ({ url }, callback) => {
let urlPath = fileURLToPath(url);
let appPath = path.join(emberAppDir, urlPath);
try {
await access(appPath);
callback(appPath);
} catch (e) {
callback(urlPath);
}
callback(await getAssetPath(emberAppDir, url));
});
};

module.exports.getAssetPath = getAssetPath;

0 comments on commit d99245e

Please sign in to comment.