main.ts 8.3 KB

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