main.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. import * as electron from 'electron/main';
  2. import * as fs from 'node:fs';
  3. import { Module } from 'node:module';
  4. import * as path from 'node:path';
  5. import * as url from 'node:url';
  6. const { app, dialog } = electron;
  7. type DefaultAppOptions = {
  8. file: null | string;
  9. noHelp: boolean;
  10. version: boolean;
  11. webdriver: boolean;
  12. interactive: boolean;
  13. abi: boolean;
  14. modules: string[];
  15. }
  16. // Parse command line options.
  17. const argv = process.argv.slice(1);
  18. const option: DefaultAppOptions = {
  19. file: null,
  20. noHelp: Boolean(process.env.ELECTRON_NO_HELP),
  21. version: false,
  22. webdriver: false,
  23. interactive: false,
  24. abi: false,
  25. modules: []
  26. };
  27. let nextArgIsRequire = false;
  28. for (const arg of argv) {
  29. if (nextArgIsRequire) {
  30. option.modules.push(arg);
  31. nextArgIsRequire = false;
  32. continue;
  33. } else if (arg === '--version' || arg === '-v') {
  34. option.version = true;
  35. break;
  36. } else if (arg.match(/^--app=/)) {
  37. option.file = arg.split('=')[1];
  38. break;
  39. } else if (arg === '--interactive' || arg === '-i' || arg === '-repl') {
  40. option.interactive = true;
  41. } else if (arg === '--test-type=webdriver') {
  42. option.webdriver = true;
  43. } else if (arg === '--require' || arg === '-r') {
  44. nextArgIsRequire = true;
  45. continue;
  46. } else if (arg === '--abi' || arg === '-a') {
  47. option.abi = true;
  48. continue;
  49. } else if (arg === '--no-help') {
  50. option.noHelp = true;
  51. continue;
  52. } else if (arg[0] === '-') {
  53. continue;
  54. } else {
  55. option.file = arg;
  56. break;
  57. }
  58. }
  59. if (nextArgIsRequire) {
  60. console.error('Invalid Usage: --require [file]\n\n"file" is required');
  61. process.exit(1);
  62. }
  63. // Set up preload modules
  64. if (option.modules.length > 0) {
  65. (Module as any)._preloadModules(option.modules);
  66. }
  67. async function loadApplicationPackage (packagePath: string) {
  68. // Add a flag indicating app is started from default app.
  69. Object.defineProperty(process, 'defaultApp', {
  70. configurable: false,
  71. enumerable: true,
  72. value: true
  73. });
  74. try {
  75. // Override app's package.json data.
  76. packagePath = path.resolve(packagePath);
  77. const packageJsonPath = path.join(packagePath, 'package.json');
  78. let appPath;
  79. if (fs.existsSync(packageJsonPath)) {
  80. let packageJson;
  81. const emitWarning = process.emitWarning;
  82. try {
  83. process.emitWarning = () => {};
  84. packageJson = (await import(url.pathToFileURL(packageJsonPath).toString(), {
  85. with: { type: 'json' }
  86. })).default;
  87. } catch (e) {
  88. showErrorMessage(`Unable to parse ${packageJsonPath}\n\n${(e as Error).message}`);
  89. return;
  90. } finally {
  91. process.emitWarning = emitWarning;
  92. }
  93. if (packageJson.version) {
  94. app.setVersion(packageJson.version);
  95. }
  96. if (packageJson.productName) {
  97. app.name = packageJson.productName;
  98. } else if (packageJson.name) {
  99. app.name = packageJson.name;
  100. }
  101. if (packageJson.desktopName) {
  102. app.setDesktopName(packageJson.desktopName);
  103. } else {
  104. app.setDesktopName(`${app.name}.desktop`);
  105. }
  106. // Set v8 flags, deliberately lazy load so that apps that do not use this
  107. // feature do not pay the price
  108. if (packageJson.v8Flags) {
  109. (await import('node:v8')).setFlagsFromString(packageJson.v8Flags);
  110. }
  111. appPath = packagePath;
  112. }
  113. let filePath: string;
  114. try {
  115. filePath = (Module as any)._resolveFilename(packagePath, null, true);
  116. app.setAppPath(appPath || path.dirname(filePath));
  117. } catch (e) {
  118. showErrorMessage(`Unable to find Electron app at ${packagePath}\n\n${(e as Error).message}`);
  119. return;
  120. }
  121. // Run the app.
  122. await import(url.pathToFileURL(filePath).toString());
  123. } catch (e) {
  124. console.error('App threw an error during load');
  125. console.error((e as Error).stack || e);
  126. throw e;
  127. }
  128. }
  129. function showErrorMessage (message: string) {
  130. app.focus();
  131. dialog.showErrorBox('Error launching app', message);
  132. process.exit(1);
  133. }
  134. async function loadApplicationByURL (appUrl: string) {
  135. const { loadURL } = await import('./default_app.js');
  136. loadURL(appUrl);
  137. }
  138. async function loadApplicationByFile (appPath: string) {
  139. const { loadFile } = await import('./default_app.js');
  140. loadFile(appPath);
  141. }
  142. async function startRepl () {
  143. if (process.platform === 'win32') {
  144. console.error('Electron REPL not currently supported on Windows');
  145. process.exit(1);
  146. }
  147. // Prevent quitting.
  148. app.on('window-all-closed', () => {});
  149. const GREEN = '32';
  150. const colorize = (color: string, s: string) => `\x1b[${color}m${s}\x1b[0m`;
  151. const electronVersion = colorize(GREEN, `v${process.versions.electron}`);
  152. const nodeVersion = colorize(GREEN, `v${process.versions.node}`);
  153. console.info(`
  154. Welcome to the Electron.js REPL \\[._.]/
  155. You can access all Electron.js modules here as well as Node.js modules.
  156. Using: Node.js ${nodeVersion} and Electron.js ${electronVersion}
  157. `);
  158. const { start } = await import('node:repl');
  159. const repl = start({
  160. prompt: '> '
  161. }).on('exit', () => {
  162. process.exit(0);
  163. });
  164. function defineBuiltin (context: any, name: string, getter: Function) {
  165. const setReal = (val: any) => {
  166. // Deleting the property before re-assigning it disables the
  167. // getter/setter mechanism.
  168. delete context[name];
  169. context[name] = val;
  170. };
  171. Object.defineProperty(context, name, {
  172. get: () => {
  173. const lib = getter();
  174. delete context[name];
  175. Object.defineProperty(context, name, {
  176. get: () => lib,
  177. set: setReal,
  178. configurable: true,
  179. enumerable: false
  180. });
  181. return lib;
  182. },
  183. set: setReal,
  184. configurable: true,
  185. enumerable: false
  186. });
  187. }
  188. defineBuiltin(repl.context, 'electron', () => electron);
  189. for (const api of Object.keys(electron) as (keyof typeof electron)[]) {
  190. defineBuiltin(repl.context, api, () => electron[api]);
  191. }
  192. // Copied from node/lib/repl.js. For better DX, we don't want to
  193. // show e.g 'contentTracing' at a higher priority than 'const', so
  194. // we only trigger custom tab-completion when no common words are
  195. // potentially matches.
  196. const commonWords = [
  197. 'async', 'await', 'break', 'case', 'catch', 'const', 'continue',
  198. 'debugger', 'default', 'delete', 'do', 'else', 'export', 'false',
  199. 'finally', 'for', 'function', 'if', 'import', 'in', 'instanceof', 'let',
  200. 'new', 'null', 'return', 'switch', 'this', 'throw', 'true', 'try',
  201. 'typeof', 'var', 'void', 'while', 'with', 'yield'
  202. ];
  203. const electronBuiltins = [...Object.keys(electron), 'original-fs', 'electron'];
  204. const defaultComplete: Function = repl.completer;
  205. (repl as any).completer = (line: string, callback: Function) => {
  206. const lastSpace = line.lastIndexOf(' ');
  207. const currentSymbol = line.substring(lastSpace + 1, repl.cursor);
  208. const filterFn = (c: string) => c.startsWith(currentSymbol);
  209. const ignores = commonWords.filter(filterFn);
  210. const hits = electronBuiltins.filter(filterFn);
  211. if (!ignores.length && hits.length) {
  212. callback(null, [hits, currentSymbol]);
  213. } else {
  214. defaultComplete.apply(repl, [line, callback]);
  215. }
  216. };
  217. }
  218. // Start the specified app if there is one specified in command line, otherwise
  219. // start the default app.
  220. if (option.file && !option.webdriver) {
  221. const file = option.file;
  222. // eslint-disable-next-line n/no-deprecated-api
  223. const protocol = url.parse(file).protocol;
  224. const extension = path.extname(file);
  225. if (protocol === 'http:' || protocol === 'https:' || protocol === 'file:' || protocol === 'chrome:') {
  226. await loadApplicationByURL(file);
  227. } else if (extension === '.html' || extension === '.htm') {
  228. await loadApplicationByFile(path.resolve(file));
  229. } else {
  230. await loadApplicationPackage(file);
  231. }
  232. } else if (option.version) {
  233. console.log('v' + process.versions.electron);
  234. process.exit(0);
  235. } else if (option.abi) {
  236. console.log(process.versions.modules);
  237. process.exit(0);
  238. } else if (option.interactive) {
  239. await startRepl();
  240. } else {
  241. if (!option.noHelp) {
  242. const welcomeMessage = `
  243. Electron ${process.versions.electron} - Build cross platform desktop apps with JavaScript, HTML, and CSS
  244. Usage: electron [options] [path]
  245. A path to an Electron app may be specified. It must be one of the following:
  246. - index.js file.
  247. - Folder containing a package.json file.
  248. - Folder containing an index.js file.
  249. - .html/.htm file.
  250. - http://, https://, or file:// URL.
  251. Options:
  252. -i, --interactive Open a REPL to the main process.
  253. -r, --require Module to preload (option can be repeated).
  254. -v, --version Print the version.
  255. -a, --abi Print the Node ABI version.`;
  256. console.log(welcomeMessage);
  257. }
  258. await loadApplicationByFile('index.html');
  259. }