main.ts 7.8 KB

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