[![NPM](https://nodei.co/npm/proton-lib.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/proton-lib/) [![NPM](https://nodei.co/npm-dl/proton-lib.png?months=6&height=3)](https://nodei.co/npm/proton-lib/)

# Proton

Proton is a library designed to distribute any native desktop application using [Electron](https://github.com/electron/electron) or as a stand alone node app.

# Usage (Project in development)

It's an initial version which supports Java platform, proton requires a JSON configuration file (proton.json) in order to configure the basic app handling.

The following is an example of proton.json file:
```json
{
  "name": "App name",
  "icon": {
    "title": "App title",
    "file": "app.ico"
  },
  "platform": {
    "name": "java",
    "version": "1.8.0_51",
    "env": "prod",
    "environments": {
      "prod": {
        "serviceUrl": "http://localhost:8080/latestVersionNumber",
        "download": "http://localhost:8080/download/app.jar",
        "artifact": "app.jar",
        "jvmargs": "-Xms256m -Xmx512m"
      }
    },
    "artifacts": {
      "maxStorageSize": "1GB"
    }
  }
}
```

Proton usage example with Electron:
```javascript
var proton = require('proton-lib');
var http = require('http');
var electron = require('electron');

//electron code...

proton.checkUpdates((options,downloadApp) => {
  //your app update logic goes here - example with basic http get
  http.get(options.env.serviceUrl, (res) => {
    res.setEncoding('utf-8');
    var data = '';
    res.on('data', (chunk) => {
      data += chunk;
    });
    res.on('end', () => {
      var obj = JSON.parse(data);
      downloadApp(obj.version);
    });
    res.on('error', (e) => {
      downloadApp(); //force app download
    });
  });
};

//optional stuff goes here...

proton.start(electron);

```
Proton stand alone usage example:
```javascript
var proton = require('proton-lib');
var http = require('http');

proton.checkUpdates((options,downloadApp) => {
  //your app update logic goes here - example with basic http get
  http.get(options.env.serviceUrl, (res) => {
    res.setEncoding('utf-8');
    var data = '';
    res.on('data', (chunk) => {
      data += chunk;
    });
    res.on('end', () => {
      var obj = JSON.parse(data);
      downloadApp(obj.version);
    });
    res.on('error', (e) => {
      downloadApp(); //force app download
    });
  });
};

proton.start();
```

# Features
  - Auto update target application (see `proton.checkUpdates(callback)`)
  - Application downloading and storage into cache files
  - Designed to support any platform (currently Java only)
  - App platform configuration are segregated by environments (e.g.: dev, qa, stage, production)
  - Automatic cache files clean up (default max size is 1GB and oldest files are priority for clean up)
  - Resources directory handling

# Implemented Platforms
  - Java (`'java'`)

# Basic proton flow
  1. Check for application updates (see `proton.checkUpdates(callback)`)
  2. Download application version if not found in cache
      - check downloadAppCb in `proton.checkUpdates((options,downloadAppCb) => { /*logic here*/ })`
  3. Execute application

# Optional configurations

Proton optional process config before `proton.start()`
```javascript
proton.process.stdout.on((data) => {
  console.log(data.toString()); //standard target process output
});

proton.process.stderr.on((data) => {
  console.error(data.toString()); //standard target process error output
});

proton.common.debug = (msg) => {
  console.log("DEBUG: " + msg); //enable proton debug
};

proton.common.resources.directory.onRequest((msg) => {
  return './../app.asar.unpacked/'; //resource directory override
});
```

# 1.1.x Release roadmap
1. Test and Fix Java Platform issues

# 1.2.x Release roadmap
1. Define UI integration with Electron

# Releases
- 1.1.3
  - Improved error handling / fixed modules
- 1.1.4
  - Added resources directory (to work with packaged applications, e.g.: asar)  
  - Added optional configuration maxStorageSize for artifacts cache storage (default 1GB)
  - Fixed proton init concurrency
