electron-builder.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. const path = require('path')
  2. const normalizePackageData = require('normalize-package-data');
  3. const fs = require("fs/promises");
  4. const generateMetadata = require('./generate-metadata-for-builder')
  5. const macBundleDocumentTypes = require("./mac-bundle-document-types.js");
  6. // Monkey-patch to not remove things I explicitly didn't say so
  7. // See: https://github.com/electron-userland/electron-builder/issues/6957
  8. let transformer = require('app-builder-lib/out/fileTransformer')
  9. const builder_util_1 = require("builder-util");
  10. transformer.createTransformer = function(srcDir, configuration, extraMetadata, extraTransformer) {
  11. const mainPackageJson = path.join(srcDir, "package.json");
  12. const isRemovePackageScripts = configuration.removePackageScripts !== false;
  13. const isRemovePackageKeywords = configuration.removePackageKeywords !== false;
  14. const packageJson = path.sep + "package.json";
  15. return file => {
  16. if (file === mainPackageJson) {
  17. return modifyMainPackageJson(file, extraMetadata, isRemovePackageScripts, isRemovePackageKeywords);
  18. }
  19. if (extraTransformer != null) {
  20. return extraTransformer(file);
  21. }
  22. else {
  23. return null;
  24. }
  25. };
  26. }
  27. async function modifyMainPackageJson(file, extraMetadata, isRemovePackageScripts, isRemovePackageKeywords) {
  28. const mainPackageData = JSON.parse(await fs.readFile(file, "utf-8"));
  29. if (extraMetadata != null) {
  30. builder_util_1.deepAssign(mainPackageData, extraMetadata);
  31. return JSON.stringify(mainPackageData, null, 2);
  32. }
  33. return null;
  34. }
  35. /// END Monkey-Patch
  36. const builder = require("electron-builder")
  37. const pngIcon = 'resources/app-icons/beta.png'
  38. const icoIcon = 'resources/app-icons/beta.ico'
  39. const svgIcon = 'resources/app-icons/beta.svg'
  40. const icnsIcon = 'resources/app-icons/beta.icns'
  41. let options = {
  42. "appId": "dev.pulsar-edit.pulsar",
  43. "npmRebuild": false,
  44. "publish": null,
  45. files: [
  46. // --- Inclusions ---
  47. // Core Repo Inclusions
  48. "package.json",
  49. "dot-atom/**/*",
  50. "exports/**/*",
  51. "resources/**/*",
  52. "src/**/*",
  53. "static/**/*",
  54. "vendor/**/*",
  55. "node_modules/**/*",
  56. // Core Repo Test Inclusions
  57. "spec/jasmine-test-runner.js",
  58. "spec/spec-helper.js",
  59. "spec/jasmine-junit-reporter.js",
  60. "spec/spec-helper-functions.js",
  61. "spec/atom-reporter.js",
  62. "spec/jasmine-list-reporter.js",
  63. // --- Exclusions ---
  64. // Core Repo Exclusions
  65. "!docs/",
  66. "!keymaps/",
  67. "!menus/",
  68. "!script/",
  69. "!integration/",
  70. "!hooks/",
  71. // Git Related Exclusions
  72. "!**/{.git,.gitignore,.gitattributes,.git-keep,.github}",
  73. "!**/{.eslintignore,PULL_REQUEST_TEMPLATE.md,ISSUE_TEMPLATE.md,CONTRIBUTING.md,SECURITY.md}",
  74. // Development Tools Exclusions
  75. "!**/{npm-debug.log,yarn.lock,.yarn-integrity,.yarn-metadata.json,.npmignore}",
  76. "!**/npm/{doc,html,man}",
  77. "!.editorconfig",
  78. "!**/{appveyor.yml,.travis.yml,circle.yml}",
  79. "!**/{__pycache__,thumbs.db,.flowconfig,.idea,.vs,.nyc_output}",
  80. "!**/*.{iml,o,hprof,orig,pyc,pyo,rbc,swp,csproj,sln,xproj}",
  81. "!**/{.jshintrc,.pairs,.lint,.lintignore,.eslintrc,.jshintignore}",
  82. "!**/{.coffeelintignore,.editorconfig,.nycrc,.coffeelint.json,.vscode,coffeelint.json}",
  83. // Common File Exclusions
  84. "!**/{.DS_Store,.hg,.svn,CVS,RCS,SCCS}",
  85. // Build Chain Exclusions
  86. "!**/*.{cc,h}", // Ignore *.cc and *.h files from native modules
  87. "!**/*.js.map",
  88. "!**/{Makefile}",
  89. "!**/build/{binding.Makefile,config.gypi,gyp-mac-tool,Makefile}",
  90. "!**/build/Release/{obj.target,obj,.deps}",
  91. // Test Exclusions
  92. "!**/pegjs/examples",
  93. "!**/node_modules/*/{test,__tests__,tests,powered-test,example,examples}",
  94. "!**/node_modules/babel-core/lib/transformation/transforers/spec", // Ignore babel-core spec
  95. "!**/{oniguruma,dev-live-reload,deprecation-cop,one-dark-ui,incompatible-packages,git-diff,line-ending-selector}/spec",
  96. "!**/{link,grammar-selector,json-schema-traverse,exception-reporting,one-light-ui,autoflow,about,go-to-line,sylvester,apparatus}/spec",
  97. "!**/{archive-view,autocomplete-plus,autocomplete-atom-api,autocomplete-css,autosave}/spec",
  98. // Other Exclusions
  99. "!**/._*",
  100. "!**/node_modules/*.d.ts",
  101. "!**/node_modules/.bin",
  102. "!**/node_modules/native-mate",
  103. "!node_modules/fuzzy-native/node_modules", // node_modules of the fuzzy-native package are only required for building it
  104. "!**/node_modules/spellchecker/vendor/hunspell/.*",
  105. "!**/git-utils/deps",
  106. "!**/oniguruma/deps",
  107. "!**/less/dist",
  108. "!**/get-parameter-names/node_modules/testla",
  109. "!**/get-parameter-names/node_modules/.bin/testla",
  110. "!**/jasmine-reporters/ext",
  111. "!**/deps/libgit2",
  112. // These are only required in dev-mode, when pegjs grammars aren't precompiled
  113. // "!node_modules/loophole", // Note: We do need these packages. Because our PegJS files _aren't_ all pre-compiled.
  114. // "!node_modules/pegjs", // Note: if these files are excluded, 'snippets' package breaks.
  115. // "!node_modules/.bin/pegjs", // Note: https://github.com/pulsar-edit/pulsar/pull/206
  116. ],
  117. "extraResources": [
  118. {
  119. "from": "pulsar.sh",
  120. "to": "pulsar.sh"
  121. }, {
  122. "from": "ppm",
  123. "to": "app/ppm"
  124. }, {
  125. "from": pngIcon,
  126. "to": "pulsar.png"
  127. }, {
  128. "from": "LICENSE.md",
  129. "to": "LICENSE.md"
  130. },
  131. ],
  132. compression: "normal",
  133. deb: {
  134. afterInstall: "script/post-install.sh",
  135. afterRemove: "script/post-uninstall.sh",
  136. },
  137. rpm: {
  138. afterInstall: "script/post-install.sh",
  139. afterRemove: "script/post-uninstall.sh",
  140. compression: 'xz',
  141. fpm: ['--rpm-rpmbuild-define=_build_id_links none']
  142. },
  143. "linux": {
  144. // Giving a single PNG icon to electron-builder prevents the correct
  145. // construction of the icon path, so we have to specify a folder containing
  146. // multiple icons named each with its size.
  147. "icon": "resources/icons",
  148. "category": "Development",
  149. "synopsis": "A Community-led Hyper-Hackable Text Editor",
  150. "target": [
  151. { target: "appimage" },
  152. { target: "deb" },
  153. { target: "rpm" },
  154. { target: "tar.gz" }
  155. ],
  156. "extraResources": [
  157. {
  158. // Extra SVG icon included in the resources folder to give a chance to
  159. // Linux packagers to add a scalable desktop icon under
  160. // /usr/share/icons/hicolor/scalable
  161. // (used only by desktops to show it on bar/switcher and app menus).
  162. "from": svgIcon,
  163. "to": "pulsar.svg"
  164. },
  165. ],
  166. "executableArgs": ['--no-sandbox'],
  167. },
  168. "mac": {
  169. "icon": icnsIcon,
  170. "category": "public.app-category.developer-tools",
  171. "minimumSystemVersion": "10.8",
  172. "hardenedRuntime": true,
  173. "extendInfo": {
  174. // This contains extra values that will be inserted into the App's plist
  175. "CFBundleExecutable": "Pulsar",
  176. "NSAppleScriptEnabled": "YES",
  177. "NSMainNibFile": "MainMenu",
  178. "NSRequiresAquaSystemAppearance": "NO",
  179. "CFBundleDocumentTypes": macBundleDocumentTypes.create(),
  180. "CFBundleURLTypes": [
  181. { "CFBundleURLSchemes": [ "atom" ] },
  182. { "CFBundleURLName": "Atom Shared Session Protocol" }
  183. ]
  184. },
  185. },
  186. "dmg": {
  187. "sign": false
  188. },
  189. "win": {
  190. "icon": icoIcon,
  191. "extraResources": [
  192. {
  193. "from": icoIcon,
  194. "to": "pulsar.ico"
  195. },
  196. {
  197. "from": "resources/win/pulsar.cmd",
  198. "to": "pulsar.cmd"
  199. },
  200. {
  201. "from": "resources/win/pulsar.js",
  202. "to": "pulsar.js"
  203. },
  204. {
  205. "from": "resources/win/modifyWindowsPath.ps1",
  206. "to": "modifyWindowsPath.ps1"
  207. }
  208. ],
  209. "target": [
  210. { "target": "nsis" },
  211. { target: "zip" },
  212. ],
  213. },
  214. // Windows NSIS Configuration
  215. "nsis": {
  216. "oneClick": false,
  217. "allowToChangeInstallationDirectory": true,
  218. "uninstallDisplayName": "Pulsar",
  219. "runAfterFinish": true,
  220. "createDesktopShortcut": true,
  221. "createStartMenuShortcut": true,
  222. "guid": "0949b555-c22c-56b7-873a-a960bdefa81f",
  223. // The GUID is generated from Electron-Builder based on our AppID
  224. // Hardcoding it here means it will always be used as generated from
  225. // the AppID 'dev.pulsar-edit.pulsar'. If this value ever changes,
  226. // A PR to GitHub Desktop must be made with the updated value
  227. "include": "resources/win/installer.nsh"
  228. },
  229. "extraMetadata": {
  230. },
  231. "afterSign": "script/mac-notarise.js",
  232. "asarUnpack": [
  233. "node_modules/github/bin/*",
  234. "node_modules/github/lib/*", // Resolves Error in console
  235. "**/node_modules/dugite/git/**", // Include dugite postInstall output (matching glob used for Atom)
  236. "**/node_modules/spellchecker/**", // Matching Atom Glob
  237. ]
  238. }
  239. /**
  240. The below optional entitlements is needed for the following reasons:
  241. - `allow-jit` needs to be applied on silicon builds for WASM to work:
  242. https://github.com/pulsar-edit/pulsar/pull/454
  243. - But setting `allow-jit` on Intel decreases performance of `fork()` operations
  244. e.g. `require('child_process').spanw(...)`
  245. - This monkey patch will no longer be needed when we can bump Electron
  246. and get `libuv` `v1.42.0` as this issue is fixed upstream there
  247. - See: https://github.com/microsoft/vscode/issues/105446
  248. */
  249. if (process.arch === "x64") {
  250. options.mac.entitlements = "resources/mac/entitlements.intel.plist";
  251. options.mac.entitlementsInherit = "resources/mac/entitlements.intel.plist";
  252. } else {
  253. options.mac.entitlements = "resources/mac/entitlements.silicon.plist";
  254. options.mac.entitlementsInherit = "resources/mac/entitlements.silicon.plist";
  255. }
  256. function whatToBuild() {
  257. const argvStartingWith = process.argv.findIndex(e => e.match('electron-builder.js'))
  258. const what = process.argv[argvStartingWith + 1]
  259. if(what) {
  260. const filter = e => e.target === what
  261. options.linux.target = options.linux.target.filter(filter)
  262. options.win.target = options.win.target.filter(filter)
  263. // options.mac.target = options.mac.target.filter(filter)
  264. return options
  265. } else {
  266. return options
  267. }
  268. }
  269. async function main() {
  270. const package = await fs.readFile('package.json', "utf-8")
  271. let options = whatToBuild()
  272. options.extraMetadata = generateMetadata(JSON.parse(package))
  273. builder.build({
  274. config: options
  275. }).then((result) => {
  276. console.log("Built binaries")
  277. fs.mkdir('binaries').catch(() => "")
  278. Promise.all(result.map(r => fs.copyFile(r, path.join('binaries', path.basename(r)))))
  279. }).catch((error) => {
  280. console.error("Error building binaries")
  281. console.error(error)
  282. process.exit(1)
  283. })
  284. }
  285. main()