main.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. const {app, dialog, shell, Menu} = require('electron')
  2. const fs = require('fs')
  3. const Module = require('module')
  4. const path = require('path')
  5. const url = require('url')
  6. // Parse command line options.
  7. const argv = process.argv.slice(1)
  8. const option = {
  9. file: null,
  10. help: null,
  11. default: null,
  12. version: null,
  13. webdriver: null,
  14. modules: []
  15. }
  16. for (let i = 0; i < argv.length; i++) {
  17. if (argv[i] === '--version' || argv[i] === '-v') {
  18. option.version = true
  19. break
  20. } else if (argv[i].match(/^--app=/)) {
  21. option.file = argv[i].split('=')[1]
  22. break
  23. } else if (argv[i] === '--default' || argv[i] === '-d') {
  24. option.default = true
  25. break
  26. } else if (argv[i] === '--interactive' || argv[i] === '-i' || argv[i] === '-repl') {
  27. option.interactive = true
  28. } else if (argv[i] === '--test-type=webdriver') {
  29. option.webdriver = true
  30. } else if (argv[i] === '--require' || argv[i] === '-r') {
  31. option.modules.push(argv[++i])
  32. continue
  33. } else if (argv[i] === '--abi' || argv[i] === '-a') {
  34. option.abi = true
  35. continue
  36. } else if (argv[i][0] === '-') {
  37. continue
  38. } else {
  39. option.file = argv[i]
  40. break
  41. }
  42. }
  43. // Quit when all windows are closed and no other one is listening to this.
  44. app.on('window-all-closed', () => {
  45. if (app.listeners('window-all-closed').length === 1 && !option.interactive) {
  46. app.quit()
  47. }
  48. })
  49. // Create default menu.
  50. app.once('ready', () => {
  51. if (Menu.getApplicationMenu()) return
  52. const template = [
  53. {
  54. label: 'Edit',
  55. submenu: [
  56. {
  57. role: 'undo'
  58. },
  59. {
  60. role: 'redo'
  61. },
  62. {
  63. type: 'separator'
  64. },
  65. {
  66. role: 'cut'
  67. },
  68. {
  69. role: 'copy'
  70. },
  71. {
  72. role: 'paste'
  73. },
  74. {
  75. role: 'pasteandmatchstyle'
  76. },
  77. {
  78. role: 'delete'
  79. },
  80. {
  81. role: 'selectall'
  82. }
  83. ]
  84. },
  85. {
  86. label: 'View',
  87. submenu: [
  88. {
  89. role: 'reload'
  90. },
  91. {
  92. role: 'forcereload'
  93. },
  94. {
  95. role: 'toggledevtools'
  96. },
  97. {
  98. type: 'separator'
  99. },
  100. {
  101. role: 'resetzoom'
  102. },
  103. {
  104. role: 'zoomin'
  105. },
  106. {
  107. role: 'zoomout'
  108. },
  109. {
  110. type: 'separator'
  111. },
  112. {
  113. role: 'togglefullscreen'
  114. }
  115. ]
  116. },
  117. {
  118. role: 'window',
  119. submenu: [
  120. {
  121. role: 'minimize'
  122. },
  123. {
  124. role: 'close'
  125. }
  126. ]
  127. },
  128. {
  129. role: 'help',
  130. submenu: [
  131. {
  132. label: 'Learn More',
  133. click () {
  134. shell.openExternal('https://electronjs.org')
  135. }
  136. },
  137. {
  138. label: 'Documentation',
  139. click () {
  140. shell.openExternal(
  141. `https://github.com/electron/electron/tree/v${process.versions.electron}/docs#readme`
  142. )
  143. }
  144. },
  145. {
  146. label: 'Community Discussions',
  147. click () {
  148. shell.openExternal('https://discuss.atom.io/c/electron')
  149. }
  150. },
  151. {
  152. label: 'Search Issues',
  153. click () {
  154. shell.openExternal('https://github.com/electron/electron/issues')
  155. }
  156. }
  157. ]
  158. }
  159. ]
  160. if (process.platform === 'darwin') {
  161. template.unshift({
  162. label: 'Electron',
  163. submenu: [
  164. {
  165. role: 'about'
  166. },
  167. {
  168. type: 'separator'
  169. },
  170. {
  171. role: 'services',
  172. submenu: []
  173. },
  174. {
  175. type: 'separator'
  176. },
  177. {
  178. role: 'hide'
  179. },
  180. {
  181. role: 'hideothers'
  182. },
  183. {
  184. role: 'unhide'
  185. },
  186. {
  187. type: 'separator'
  188. },
  189. {
  190. role: 'quit'
  191. }
  192. ]
  193. })
  194. template[1].submenu.push({
  195. type: 'separator'
  196. }, {
  197. label: 'Speech',
  198. submenu: [
  199. {
  200. role: 'startspeaking'
  201. },
  202. {
  203. role: 'stopspeaking'
  204. }
  205. ]
  206. })
  207. template[3].submenu = [
  208. {
  209. role: 'close'
  210. },
  211. {
  212. role: 'minimize'
  213. },
  214. {
  215. role: 'zoom'
  216. },
  217. {
  218. type: 'separator'
  219. },
  220. {
  221. role: 'front'
  222. }
  223. ]
  224. } else {
  225. template.unshift({
  226. label: 'File',
  227. submenu: [{
  228. role: 'quit'
  229. }]
  230. })
  231. }
  232. const menu = Menu.buildFromTemplate(template)
  233. Menu.setApplicationMenu(menu)
  234. })
  235. if (option.modules.length > 0) {
  236. Module._preloadModules(option.modules)
  237. }
  238. function loadApplicationPackage (packagePath) {
  239. // Add a flag indicating app is started from default app.
  240. process.defaultApp = true
  241. try {
  242. // Override app name and version.
  243. packagePath = path.resolve(packagePath)
  244. const packageJsonPath = path.join(packagePath, 'package.json')
  245. if (fs.existsSync(packageJsonPath)) {
  246. let packageJson
  247. try {
  248. packageJson = require(packageJsonPath)
  249. } catch (e) {
  250. showErrorMessage(`Unable to parse ${packageJsonPath}\n\n${e.message}`)
  251. return
  252. }
  253. if (packageJson.version) {
  254. app.setVersion(packageJson.version)
  255. }
  256. if (packageJson.productName) {
  257. app.setName(packageJson.productName)
  258. } else if (packageJson.name) {
  259. app.setName(packageJson.name)
  260. }
  261. app.setPath('userData', path.join(app.getPath('appData'), app.getName()))
  262. app.setPath('userCache', path.join(app.getPath('cache'), app.getName()))
  263. app.setAppPath(packagePath)
  264. }
  265. try {
  266. Module._resolveFilename(packagePath, module, true)
  267. } catch (e) {
  268. showErrorMessage(`Unable to find Electron app at ${packagePath}\n\n${e.message}`)
  269. return
  270. }
  271. // Run the app.
  272. Module._load(packagePath, module, true)
  273. } catch (e) {
  274. console.error('App threw an error during load')
  275. console.error(e.stack || e)
  276. throw e
  277. }
  278. }
  279. function showErrorMessage (message) {
  280. app.focus()
  281. dialog.showErrorBox('Error launching app', message)
  282. process.exit(1)
  283. }
  284. function loadApplicationByUrl (appUrl) {
  285. require('./default_app').load(appUrl)
  286. }
  287. function startRepl () {
  288. if (process.platform === 'win32') {
  289. console.error('Electron REPL not currently supported on Windows')
  290. process.exit(1)
  291. }
  292. const repl = require('repl')
  293. repl.start('> ').on('exit', () => {
  294. process.exit(0)
  295. })
  296. }
  297. // Start the specified app if there is one specified in command line, otherwise
  298. // start the default app.
  299. if (option.file && !option.webdriver) {
  300. const file = option.file
  301. const protocol = url.parse(file).protocol
  302. const extension = path.extname(file)
  303. if (protocol === 'http:' || protocol === 'https:' || protocol === 'file:') {
  304. loadApplicationByUrl(file)
  305. } else if (extension === '.html' || extension === '.htm') {
  306. loadApplicationByUrl('file://' + path.resolve(file))
  307. } else {
  308. loadApplicationPackage(file)
  309. }
  310. } else if (option.version) {
  311. console.log('v' + process.versions.electron)
  312. process.exit(0)
  313. } else if (option.abi) {
  314. console.log(process.versions.modules)
  315. process.exit(0)
  316. } else if (option.default) {
  317. const indexPath = path.join(__dirname, '/index.html')
  318. loadApplicationByUrl(`file://${indexPath}`)
  319. } else if (option.interactive) {
  320. startRepl()
  321. } else {
  322. const welcomeMessage = `
  323. Electron ${process.versions.electron} - Build cross platform desktop apps with JavaScript, HTML, and CSS
  324. Usage: electron [options] [path]
  325. A path to an Electron app may be specified. It must be one of the following:
  326. - index.js file.
  327. - Folder containing a package.json file.
  328. - Folder containing an index.js file.
  329. - .html/.htm file.
  330. - http://, https://, or file:// URL.
  331. Options:
  332. -d, --default Run the default bundled Electron app.
  333. -i, --interactive Open a REPL to the main process.
  334. -r, --require Module to preload (option can be repeated).
  335. -v, --version Print the version.
  336. -a, --abi Print the Node ABI version.`
  337. console.log(welcomeMessage)
  338. const indexPath = path.join(__dirname, '/index.html')
  339. loadApplicationByUrl(`file://${indexPath}`)
  340. }