Skip to content

Commit

Permalink
Detect architecture (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
Richienb committed Feb 28, 2021
1 parent 8c00bd8 commit 851bc07
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 17 deletions.
49 changes: 33 additions & 16 deletions index.js
Expand Up @@ -8,7 +8,7 @@ const defineLazyProperty = require('define-lazy-prop');
// Path to included `xdg-open`.
const localXdgOpenPath = path.join(__dirname, 'xdg-open');

const {platform} = process;
const {platform, arch} = process;

/**
Get the mount point for fixed drives in WSL.
Expand Down Expand Up @@ -215,33 +215,50 @@ const open = async (target, options) => {
return subprocess;
};

function detectPlatformBinary(platformMap, {wsl}) {
function detectArchBinary(binary) {
if (typeof binary === 'string') {
return binary;
}

const {[arch]: archBinary} = binary;

if (!archBinary) {
throw new Error(`${arch} is not supported`);
}

return archBinary;
}

function detectPlatformBinary({[platform]: platformBinary}, {wsl}) {
if (wsl && isWsl) {
return wsl;
return detectArchBinary(wsl);
}

if (!platformMap.has(platform)) {
if (!platformBinary) {
throw new Error(`${platform} is not supported`);
}

return platformMap.get(platform);
return detectArchBinary(platformBinary);
}

const apps = {};

defineLazyProperty(apps, 'chrome', () => detectPlatformBinary(new Map([
['darwin', 'google chrome canary'],
['win32', 'Chrome'],
['linux', ['google-chrome', 'google-chrome-stable']]
]), {
wsl: '/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe'
defineLazyProperty(apps, 'chrome', () => detectPlatformBinary({
darwin: 'google chrome canary',
win32: 'chrome',
linux: ['google-chrome', 'google-chrome-stable']
}, {
wsl: {
ia32: '/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe',
x64: ['/mnt/c/Program Files/Google/Chrome/Application/chrome.exe', '/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe']
}
}));

defineLazyProperty(apps, 'firefox', () => detectPlatformBinary(new Map([
['darwin', 'firefox'],
['win32', 'C:\\Program Files\\Mozilla Firefox\\firefox.exe'],
['linux', 'firefox']
]), {
defineLazyProperty(apps, 'firefox', () => detectPlatformBinary({
darwin: 'firefox',
win32: 'C:\\Program Files\\Mozilla Firefox\\firefox.exe',
linux: 'firefox'
}, {
wsl: '/mnt/c/Program Files/Mozilla Firefox/firefox.exe'
}));

Expand Down
12 changes: 11 additions & 1 deletion package.json
Expand Up @@ -11,7 +11,7 @@
"url": "https://sindresorhus.com"
},
"engines": {
"node": "^10.17"
"node": ">=10.17"
},
"scripts": {
"test": "xo && tsd"
Expand Down Expand Up @@ -57,5 +57,15 @@
"ava": "^3.15.0",
"tsd": "^0.14.0",
"xo": "^0.37.1"
},
"xo": {
"rules": {
"node/no-unsupported-features/node-builtins": [
"error",
{
"ignores": ["fs.promises"]
}
]
}
}
}

0 comments on commit 851bc07

Please sign in to comment.