index.d.ts (4906B)
1 import {ChildProcess} from 'child_process'; 2 3 declare namespace open { 4 interface Options { 5 /** 6 Wait for the opened app to exit before fulfilling the promise. If `false` it's fulfilled immediately when opening the app. 7 8 Note that it waits for the app to exit, not just for the window to close. 9 10 On Windows, you have to explicitly specify an app for it to be able to wait. 11 12 @default false 13 */ 14 readonly wait?: boolean; 15 16 /** 17 __macOS only__ 18 19 Do not bring the app to the foreground. 20 21 @default false 22 */ 23 readonly background?: boolean; 24 25 /** 26 __macOS only__ 27 28 Open a new instance of the app even it's already running. 29 30 A new instance is always opened on other platforms. 31 32 @default false 33 */ 34 readonly newInstance?: boolean; 35 36 /** 37 Specify the `name` of the app to open the `target` with, and optionally, app `arguments`. `app` can be an array of apps to try to open and `name` can be an array of app names to try. If each app fails, the last error will be thrown. 38 39 The app name is platform dependent. Don't hard code it in reusable modules. For example, Chrome is `google chrome` on macOS, `google-chrome` on Linux and `chrome` on Windows. If possible, use [`open.apps`](#openapps) which auto-detects the correct binary to use. 40 41 You may also pass in the app's full path. For example on WSL, this can be `/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe` for the Windows installation of Chrome. 42 43 The app `arguments` are app dependent. Check the app's documentation for what arguments it accepts. 44 */ 45 readonly app?: App | readonly App[]; 46 47 /** 48 Allow the opened app to exit with nonzero exit code when the `wait` option is `true`. 49 50 We do not recommend setting this option. The convention for success is exit code zero. 51 52 @default false 53 */ 54 readonly allowNonzeroExitCode?: boolean; 55 } 56 57 interface OpenAppOptions extends Omit<Options, 'app'> { 58 /** 59 Arguments passed to the app. 60 61 These arguments are app dependent. Check the app's documentation for what arguments it accepts. 62 */ 63 readonly arguments?: readonly string[]; 64 } 65 66 type AppName = 67 | 'chrome' 68 | 'firefox' 69 | 'edge'; 70 71 type App = { 72 name: string | readonly string[]; 73 arguments?: readonly string[]; 74 }; 75 } 76 77 // eslint-disable-next-line no-redeclare 78 declare const open: { 79 /** 80 Open stuff like URLs, files, executables. Cross-platform. 81 82 Uses the command `open` on macOS, `start` on Windows and `xdg-open` on other platforms. 83 84 There is a caveat for [double-quotes on Windows](https://github.com/sindresorhus/open#double-quotes-on-windows) where all double-quotes are stripped from the `target`. 85 86 @param target - The thing you want to open. Can be a URL, file, or executable. Opens in the default app for the file type. For example, URLs open in your default browser. 87 @returns The [spawned child process](https://nodejs.org/api/child_process.html#child_process_class_childprocess). You would normally not need to use this for anything, but it can be useful if you'd like to attach custom event listeners or perform other operations directly on the spawned process. 88 89 @example 90 ``` 91 import open = require('open'); 92 93 // Opens the image in the default image viewer 94 await open('unicorn.png', {wait: true}); 95 console.log('The image viewer app closed'); 96 97 // Opens the url in the default browser 98 await open('https://sindresorhus.com'); 99 100 // Opens the URL in a specified browser. 101 await open('https://sindresorhus.com', {app: {name: 'firefox'}}); 102 103 // Specify app arguments. 104 await open('https://sindresorhus.com', {app: {name: 'google chrome', arguments: ['--incognito']}}); 105 ``` 106 */ 107 ( 108 target: string, 109 options?: open.Options 110 ): Promise<ChildProcess>; 111 112 /** 113 An object containing auto-detected binary names for common apps. Useful to work around cross-platform differences. 114 115 @example 116 ``` 117 import open = require('open'); 118 119 await open('https://google.com', { 120 app: { 121 name: open.apps.chrome 122 } 123 }); 124 ``` 125 */ 126 apps: Record<open.AppName, string | readonly string[]>; 127 128 /** 129 Open an app. Cross-platform. 130 131 Uses the command `open` on macOS, `start` on Windows and `xdg-open` on other platforms. 132 133 @param name - The app you want to open. Can be either builtin supported `open.apps` names or other name supported in platform. 134 @returns The [spawned child process](https://nodejs.org/api/child_process.html#child_process_class_childprocess). You would normally not need to use this for anything, but it can be useful if you'd like to attach custom event listeners or perform other operations directly on the spawned process. 135 136 @example 137 ``` 138 const {apps, openApp} = require('open'); 139 140 // Open Firefox 141 await openApp(apps.firefox); 142 143 // Open Chrome incognito mode 144 await openApp(apps.chrome, {arguments: ['--incognito']}); 145 146 // Open Xcode 147 await openApp('xcode'); 148 ``` 149 */ 150 openApp: (name: open.App['name'], options?: open.OpenAppOptions) => Promise<ChildProcess>; 151 }; 152 153 export = open;