Skip to content

Commit

Permalink
Intial push
Browse files Browse the repository at this point in the history
  • Loading branch information
ganeshrvel committed Jan 24, 2019
0 parents commit 070c14e
Show file tree
Hide file tree
Showing 3 changed files with 235 additions and 0 deletions.
56 changes: 56 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release
.eslintcache

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules/
app/node_modules

# OSX
.DS_Store

# flow-typed
flow-typed/npm/*
!flow-typed/npm/module_vx.x.x.js

# App packaged
release
app/main.prod.js
app/main.prod.js.map
app/renderer.prod.js
app/renderer.prod.js.map
app/style.css
app/style.css.map
dist
dll
main.js
main.js.map

.idea
npm-debug.log.*
yarn.lock
package-lock.json
todo.txt
*yarn-error.log
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2010-present Ganesh Rathinavel

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

157 changes: 157 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
# Bundling precompiled binary or native file into an electron app

- Author: [Ganesh Rathinavel](https://www.linkedin.com/in/ganeshrvel "Ganesh Rathinavel")
- License: [MIT](https://github.com/ganeshrvel/openmtp/blob/master/LICENSE "MIT")
- Website URL: [https://github.com/ganeshrvel/tutorial-electron-bundling-binaries](https://github.com/ganeshrvel/tutorial-electron-bundling-binaries/ "https://github.com/ganeshrvel/tutorial-electron-bundling-binaries")
- Repo URL: [https://github.com/ganeshrvel/tutorial-electron-bundling-binaries](https://github.com/ganeshrvel/tutorial-electron-bundling-binaries/ "https://github.com/ganeshrvel/tutorial-electron-bundling-binaries")
- Contacts: ganeshrvel@outlook.com


### Introduction

##### Distributing an electron app alone with pre-compiled native files could get tricky as asar packager tends to exclude such files while packaging the app. I had to spend a significant amount of time researching how to get this done. This was originally implemented inside [OpenMTP - Advanced Android File Transfer Application for macOS](https://github.com/ganeshrvel/openmtp "OpenMTP - Advanced Android File Transfer Application for macOS").

### Implementation

- Create the OS folder inside your electron "build" directory.

```shell
# Replace {OS} with the required OS name. eg: mac, windows, linux
$ mkdir -p ./build/{OS}/bin
```

- Place the binaries inside this folder.
- Install npm packages

```shell
$ npm install electron-root-path

or

$ yarn add electron-root-path
```

- Create the file *./app/binaries.js* and add the below code

```javascript
'use strict';

import path from 'path';
import { remote } from 'electron';
import { rootPath } from 'electron-root-path';
import getPlatform from './get-platform';

const IS_PROD = process.env.NODE_ENV === 'production';
const root = rootPath;
const { getAppPath } = remote.app;
const isPackaged =
process.mainModule.filename.indexOf('app.asar') !== -1;

const binariesPath =
IS_PROD && isPackaged
? path.join(path.dirname(getAppPath()), '..', './Resources', './bin')
: path.join(root, './resources', getPlatform(), './bin');

export const execPath = path.resolve(path.join(binariesPath, './binary-file'));
```

- Create the file *./app/get-platform.js* and paste the below code

```javascript
'use strict';

import { platform } from 'os';

export default () => {
switch (platform()) {
case 'aix':
case 'freebsd':
case 'linux':
case 'openbsd':
case 'android':
return 'linux';
case 'darwin':
case 'sunos':
return 'mac';
case 'win32':
return 'win';
}
};
```

- Add the below code to your *./package.json* file

```javascript
"build": {
"extraFiles": [
{
"from": "build/mac/bin",
"to": "Resources/bin",
"filter": [
"**/*"
]
}
],
},
```

- To distribute the code via Mac App Store add the below code to your *package.json* file

```javascript
"build": {
"mas": {
"type": "distribution",
"category": "public.app-category.productivity",
"entitlements": "build/entitlements.mas.plist",
"icon": "build/icon.icns",
"binaries": [
"dist/mas/OpenMTP.app/Contents/Resources/bin/mtp-cli"
]
},
}
```


- Import the binary file as:

```javascript
import { execPath } from './binaries';

// your program code:
var command = spawn(execPath, arg, {});
```


### Clone
```shell
$ git clone --depth 1 --single-branch --branch master https://github.com/ganeshrvel/tutorial-electron-bundling-binaries.git

$ cd tutorial-electron-bundling-binaries
```

### Contribute
- Fork the repo and create your branch from master.
- Ensure that the changes pass linting.
- Update the documentation if needed.
- Make sure your code lints.
- Issue a pull request!

When you submit code changes, your submissions are understood to be under the same [MIT License](https://github.com/ganeshrvel/tutorial-electron-bundling-binaries/blob/master/LICENSE "MIT License") that covers the project. Feel free to contact the maintainers if that's a concern.


### Buy me a coffee
Help me keep the app FREE and open for all.
Paypal me: [paypal.me/ganeshrvel](https://paypal.me/ganeshrvel "paypal.me/ganeshrvel")

### Contacts
Please feel free to contact me at ganeshrvel@outlook.com

### More repos
- [Electron-React-Redux Advanced Boilerplate](https://github.com/ganeshrvel/electron-react-redux-advanced-boilerplate "Electron React Redux advanced boilerplate")
- [OpenMTP - Advanced Android File Transfer Application for macOS](https://github.com/ganeshrvel/openmtp "OpenMTP - Advanced Android File Transfer Application for macOS")
- [electron-root-path](https://github.com/ganeshrvel/npm-electron-root-path "Get the root path of an Electron Application")

### License
tutorial-electron-bundling-binaries | Get the root path of an Electron Application is released under [MIT License](https://github.com/ganeshrvel/tutorial-electron-bundling-binaries/blob/master/LICENSE "MIT License").

Copyright © 2018 - 2019 Ganesh Rathinavel

0 comments on commit 070c14e

Please sign in to comment.