index.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. import process from 'node:process';
  2. import {Buffer} from 'node:buffer';
  3. import path from 'node:path';
  4. import {fileURLToPath} from 'node:url';
  5. import childProcess from 'node:child_process';
  6. import fs, {constants as fsConstants} from 'node:fs/promises';
  7. import isWsl from 'is-wsl';
  8. import defineLazyProperty from 'define-lazy-prop';
  9. import defaultBrowser from 'default-browser';
  10. import isInsideContainer from 'is-inside-container';
  11. // Path to included `xdg-open`.
  12. const __dirname = path.dirname(fileURLToPath(import.meta.url));
  13. const localXdgOpenPath = path.join(__dirname, 'xdg-open');
  14. const {platform, arch} = process;
  15. /**
  16. Get the mount point for fixed drives in WSL.
  17. @inner
  18. @returns {string} The mount point.
  19. */
  20. const getWslDrivesMountPoint = (() => {
  21. // Default value for "root" param
  22. // according to https://docs.microsoft.com/en-us/windows/wsl/wsl-config
  23. const defaultMountPoint = '/mnt/';
  24. let mountPoint;
  25. return async function () {
  26. if (mountPoint) {
  27. // Return memoized mount point value
  28. return mountPoint;
  29. }
  30. const configFilePath = '/etc/wsl.conf';
  31. let isConfigFileExists = false;
  32. try {
  33. await fs.access(configFilePath, fsConstants.F_OK);
  34. isConfigFileExists = true;
  35. } catch {}
  36. if (!isConfigFileExists) {
  37. return defaultMountPoint;
  38. }
  39. const configContent = await fs.readFile(configFilePath, {encoding: 'utf8'});
  40. const configMountPoint = /(?<!#.*)root\s*=\s*(?<mountPoint>.*)/g.exec(configContent);
  41. if (!configMountPoint) {
  42. return defaultMountPoint;
  43. }
  44. mountPoint = configMountPoint.groups.mountPoint.trim();
  45. mountPoint = mountPoint.endsWith('/') ? mountPoint : `${mountPoint}/`;
  46. return mountPoint;
  47. };
  48. })();
  49. const pTryEach = async (array, mapper) => {
  50. let latestError;
  51. for (const item of array) {
  52. try {
  53. return await mapper(item); // eslint-disable-line no-await-in-loop
  54. } catch (error) {
  55. latestError = error;
  56. }
  57. }
  58. throw latestError;
  59. };
  60. const baseOpen = async options => {
  61. options = {
  62. wait: false,
  63. background: false,
  64. newInstance: false,
  65. allowNonzeroExitCode: false,
  66. ...options,
  67. };
  68. if (Array.isArray(options.app)) {
  69. return pTryEach(options.app, singleApp => baseOpen({
  70. ...options,
  71. app: singleApp,
  72. }));
  73. }
  74. let {name: app, arguments: appArguments = []} = options.app ?? {};
  75. appArguments = [...appArguments];
  76. if (Array.isArray(app)) {
  77. return pTryEach(app, appName => baseOpen({
  78. ...options,
  79. app: {
  80. name: appName,
  81. arguments: appArguments,
  82. },
  83. }));
  84. }
  85. if (app === 'browser' || app === 'browserPrivate') {
  86. // IDs from default-browser for macOS and windows are the same
  87. const ids = {
  88. 'com.google.chrome': 'chrome',
  89. 'google-chrome.desktop': 'chrome',
  90. 'org.mozilla.firefox': 'firefox',
  91. 'firefox.desktop': 'firefox',
  92. 'com.microsoft.msedge': 'edge',
  93. 'com.microsoft.edge': 'edge',
  94. 'microsoft-edge.desktop': 'edge',
  95. };
  96. // Incognito flags for each browser in `apps`.
  97. const flags = {
  98. chrome: '--incognito',
  99. firefox: '--private-window',
  100. edge: '--inPrivate',
  101. };
  102. const browser = await defaultBrowser();
  103. if (browser.id in ids) {
  104. const browserName = ids[browser.id];
  105. if (app === 'browserPrivate') {
  106. appArguments.push(flags[browserName]);
  107. }
  108. return baseOpen({
  109. ...options,
  110. app: {
  111. name: apps[browserName],
  112. arguments: appArguments,
  113. },
  114. });
  115. }
  116. throw new Error(`${browser.name} is not supported as a default browser`);
  117. }
  118. let command;
  119. const cliArguments = [];
  120. const childProcessOptions = {};
  121. if (platform === 'darwin') {
  122. command = 'open';
  123. if (options.wait) {
  124. cliArguments.push('--wait-apps');
  125. }
  126. if (options.background) {
  127. cliArguments.push('--background');
  128. }
  129. if (options.newInstance) {
  130. cliArguments.push('--new');
  131. }
  132. if (app) {
  133. cliArguments.push('-a', app);
  134. }
  135. } else if (platform === 'win32' || (isWsl && !isInsideContainer() && !app)) {
  136. const mountPoint = await getWslDrivesMountPoint();
  137. command = isWsl
  138. ? `${mountPoint}c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe`
  139. : `${process.env.SYSTEMROOT || process.env.windir || 'C:\\Windows'}\\System32\\WindowsPowerShell\\v1.0\\powershell`;
  140. cliArguments.push(
  141. '-NoProfile',
  142. '-NonInteractive',
  143. '-ExecutionPolicy',
  144. 'Bypass',
  145. '-EncodedCommand',
  146. );
  147. if (!isWsl) {
  148. childProcessOptions.windowsVerbatimArguments = true;
  149. }
  150. const encodedArguments = ['Start'];
  151. if (options.wait) {
  152. encodedArguments.push('-Wait');
  153. }
  154. if (app) {
  155. // Double quote with double quotes to ensure the inner quotes are passed through.
  156. // Inner quotes are delimited for PowerShell interpretation with backticks.
  157. encodedArguments.push(`"\`"${app}\`""`);
  158. if (options.target) {
  159. appArguments.push(options.target);
  160. }
  161. } else if (options.target) {
  162. encodedArguments.push(`"${options.target}"`);
  163. }
  164. if (appArguments.length > 0) {
  165. appArguments = appArguments.map(argument => `"\`"${argument}\`""`);
  166. encodedArguments.push('-ArgumentList', appArguments.join(','));
  167. }
  168. // Using Base64-encoded command, accepted by PowerShell, to allow special characters.
  169. options.target = Buffer.from(encodedArguments.join(' '), 'utf16le').toString('base64');
  170. } else {
  171. if (app) {
  172. command = app;
  173. } else {
  174. // When bundled by Webpack, there's no actual package file path and no local `xdg-open`.
  175. const isBundled = !__dirname || __dirname === '/';
  176. // Check if local `xdg-open` exists and is executable.
  177. let exeLocalXdgOpen = false;
  178. try {
  179. await fs.access(localXdgOpenPath, fsConstants.X_OK);
  180. exeLocalXdgOpen = true;
  181. } catch {}
  182. const useSystemXdgOpen = process.versions.electron
  183. ?? (platform === 'android' || isBundled || !exeLocalXdgOpen);
  184. command = useSystemXdgOpen ? 'xdg-open' : localXdgOpenPath;
  185. }
  186. if (appArguments.length > 0) {
  187. cliArguments.push(...appArguments);
  188. }
  189. if (!options.wait) {
  190. // `xdg-open` will block the process unless stdio is ignored
  191. // and it's detached from the parent even if it's unref'd.
  192. childProcessOptions.stdio = 'ignore';
  193. childProcessOptions.detached = true;
  194. }
  195. }
  196. if (platform === 'darwin' && appArguments.length > 0) {
  197. cliArguments.push('--args', ...appArguments);
  198. }
  199. // This has to come after `--args`.
  200. if (options.target) {
  201. cliArguments.push(options.target);
  202. }
  203. const subprocess = childProcess.spawn(command, cliArguments, childProcessOptions);
  204. if (options.wait) {
  205. return new Promise((resolve, reject) => {
  206. subprocess.once('error', reject);
  207. subprocess.once('close', exitCode => {
  208. if (!options.allowNonzeroExitCode && exitCode > 0) {
  209. reject(new Error(`Exited with code ${exitCode}`));
  210. return;
  211. }
  212. resolve(subprocess);
  213. });
  214. });
  215. }
  216. subprocess.unref();
  217. return subprocess;
  218. };
  219. const open = (target, options) => {
  220. if (typeof target !== 'string') {
  221. throw new TypeError('Expected a `target`');
  222. }
  223. return baseOpen({
  224. ...options,
  225. target,
  226. });
  227. };
  228. export const openApp = (name, options) => {
  229. if (typeof name !== 'string' && !Array.isArray(name)) {
  230. throw new TypeError('Expected a valid `name`');
  231. }
  232. const {arguments: appArguments = []} = options ?? {};
  233. if (appArguments !== undefined && appArguments !== null && !Array.isArray(appArguments)) {
  234. throw new TypeError('Expected `appArguments` as Array type');
  235. }
  236. return baseOpen({
  237. ...options,
  238. app: {
  239. name,
  240. arguments: appArguments,
  241. },
  242. });
  243. };
  244. function detectArchBinary(binary) {
  245. if (typeof binary === 'string' || Array.isArray(binary)) {
  246. return binary;
  247. }
  248. const {[arch]: archBinary} = binary;
  249. if (!archBinary) {
  250. throw new Error(`${arch} is not supported`);
  251. }
  252. return archBinary;
  253. }
  254. function detectPlatformBinary({[platform]: platformBinary}, {wsl}) {
  255. if (wsl && isWsl) {
  256. return detectArchBinary(wsl);
  257. }
  258. if (!platformBinary) {
  259. throw new Error(`${platform} is not supported`);
  260. }
  261. return detectArchBinary(platformBinary);
  262. }
  263. export const apps = {};
  264. defineLazyProperty(apps, 'chrome', () => detectPlatformBinary({
  265. darwin: 'google chrome',
  266. win32: 'chrome',
  267. linux: ['google-chrome', 'google-chrome-stable', 'chromium'],
  268. }, {
  269. wsl: {
  270. ia32: '/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe',
  271. x64: ['/mnt/c/Program Files/Google/Chrome/Application/chrome.exe', '/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe'],
  272. },
  273. }));
  274. defineLazyProperty(apps, 'firefox', () => detectPlatformBinary({
  275. darwin: 'firefox',
  276. win32: 'C:\\Program Files\\Mozilla Firefox\\firefox.exe',
  277. linux: 'firefox',
  278. }, {
  279. wsl: '/mnt/c/Program Files/Mozilla Firefox/firefox.exe',
  280. }));
  281. defineLazyProperty(apps, 'edge', () => detectPlatformBinary({
  282. darwin: 'microsoft edge',
  283. win32: 'msedge',
  284. linux: ['microsoft-edge', 'microsoft-edge-dev'],
  285. }, {
  286. wsl: '/mnt/c/Program Files (x86)/Microsoft/Edge/Application/msedge.exe',
  287. }));
  288. defineLazyProperty(apps, 'browser', () => 'browser');
  289. defineLazyProperty(apps, 'browserPrivate', () => 'browserPrivate');
  290. export default open;