Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect architecture #227

Merged
merged 1 commit into from Feb 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"]
}
]
}
}
}