Skip to content

Commit

Permalink
Merge pull request #37 from edjafarov/custom-names
Browse files Browse the repository at this point in the history
Custom app names, exec paths, etc.
  • Loading branch information
adam-lynch committed Aug 27, 2014
2 parents 107517e + 0d5f7a2 commit ec9eaf2
Show file tree
Hide file tree
Showing 7 changed files with 335 additions and 153 deletions.
226 changes: 145 additions & 81 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,79 +1,92 @@
node-webkit-updater [![NPM version][npm-image]][npm-url]
=======
This is [node-webkit](https://github.com/rogerwang/node-webkit)-updater.

```
npm install node-webkit-updater
```

It gives you low-level API to:

1. Check the manifest for version (from your running "old" app).
2. If the version is different from the running one, download new package to a temp directory.
3. Unpack the package in temp.
4. Run new app from temp and kill the old one (i.e. still all from the running app).
5. The new app (in temp) will copy itself to the original folder, overriding the old app.
6. The new app will run itself from original folder and exit the process.

You should build this logic by yourself though. As a reference you can use [example](app/index.html).

Covered by tests and works for [linux](http://screencast.com/t/Je2ptbHhP), [windows](http://screencast.com/t/MSTKqVS3) and [mac](http://screencast.com/t/OXyC5xoA).
### How to run the tests
```
git clone [email protected]:edjafarov/updater.git
npm run deps
npm test
```

## Quick Start
```javascript
var gui = require('nw.gui');
var pkg = require('../package.json'); // Insert your app's manifest here
var updater = require('node-webkit-updater');
var upd = new updater(pkg);

/* Checks the remote manifest for latest available version and calls the autoupgrading function */
upd.checkNewVersion(function(error, newVersionExists, manifest) {
if (!error && newVersionExists) {
upgradeNow(manifest);
}
});

/* Downloads the new version, unpacks it, replaces the existing files, runs the new version, and exits the old app */
function upgradeNow(newManifest) {
var newVersion = upd.download(function(error, filename) {
if (!error) {
upd.unpack(filename, function(error, newAppPath) {
if (!error) {
upd.runInstaller(newAppPath, [upd.getAppPath(), upd.getAppExec()],{});
gui.App.quit();
}
});
}
}, newManifest);
}
```


## API

As a reference you can use the [example](https://github.com/edjafarov/updater/blob/master/app/index.html).

node-webkit-updater [![NPM version][npm-image]][npm-url]
=======
This is [node-webkit](https://github.com/rogerwang/node-webkit)-updater.

```
npm install node-webkit-updater
```

It gives you low-level API to:

1. Check the manifest for version (from your running "old" app).
2. If the version is different from the running one, download new package to a temp directory.
3. Unpack the package in temp.
4. Run new app from temp and kill the old one (i.e. still all from the running app).
5. The new app (in temp) will copy itself to the original folder, overriding the old app.
6. The new app will run itself from original folder and exit the process.

You should build this logic by yourself though. As a reference you can use [example](app/index.html).

Covered by tests and works for [linux](http://screencast.com/t/Je2ptbHhP), [windows](http://screencast.com/t/MSTKqVS3) and [mac](http://screencast.com/t/OXyC5xoA).
### How to run the tests
```
git clone [email protected]:edjafarov/updater.git
npm run deps
npm test
```

## Quick Start
```javascript
var gui = require('nw.gui');
var pkg = require('../package.json'); // Insert your app's manifest here
var updater = require('node-webkit-updater');
var upd = new updater(pkg);

/* Checks the remote manifest for latest available version and calls the autoupgrading function */
upd.checkNewVersion(function(error, newVersionExists, manifest) {
if (!error && newVersionExists) {
upgradeNow(manifest);
}
});

/* Downloads the new version, unpacks it, replaces the existing files, runs the new version, and exits the old app */
function upgradeNow(newManifest) {
var newVersion = upd.download(function(error, filename) {
if (!error) {
upd.unpack(filename, function(error, newAppPath) {
if (!error) {
upd.runInstaller(newAppPath, [upd.getAppPath(), upd.getAppExec()],{});
gui.App.quit();
}
}, newManifest);
}
}, newManifest);
}
```


## API

As a reference you can use the [example](https://github.com/edjafarov/updater/blob/master/app/index.html).

<a name="new_updater"></a>
###new updater(manifest)
Creates new instance of updater. Manifest could be a `package.json` of project.```json{ "name": "updapp", "version": "0.0.2", "author": "Eldar Djafarov <[email protected]>", "manifestUrl": "http://localhost:3000/package.json", "packages": { "mac": "http://localhost:3000/releases/updapp/mac/updapp.zip", "win": "http://localhost:3000/releases/updapp/win/updapp.zip", "linux32": "http://localhost:3000/releases/updapp/linux32/updapp.tar.gz" }}```Inside the app manifest, you need to specify where to download packages from for all supported OS'es, a manifest url where this manifest can be found and the current version of the app.Note that compressed apps are assumed to be downloaded in the format produced by [node-webkit-builder](https://github.com/mllrsohn/node-webkit-builder) (or [grunt-node-webkit-builder](https://github.com/mllrsohn/grunt-node-webkit-builder)).
Creates new instance of updater. Manifest could be a `package.json` of project.

Note that compressed apps are assumed to be downloaded in the format produced by [node-webkit-builder](https://github.com/mllrsohn/node-webkit-builder) (or [grunt-node-webkit-builder](https://github.com/mllrsohn/grunt-node-webkit-builder)).

**Params**

- manifest `object`
- manifest `object` - See the [manifest schema](#manifest-schema) below.

<a name="updater#checkNewVersion"></a>
###updater.checkNewVersion(cb)
Will check the latest available version of the application by requesting the manifest specified in `manufestUrl`.The callback will always be called; the second paramter indicates whether or not there's a newer version.This function assumes you use [Semantic Versioning](http://semver.org) and enforces it; if your local version is `0.2.0` and the remote one is `0.1.23456` then the callback will be called with `false` as the second paramter. If on the off chance you don't use semantic versioning, you could manually download the remote manifest and call `download` if you're happy that the remote version is newer.
Will check the latest available version of the application by requesting the manifest specified in `manifestUrl`.

The callback will always be called; the second parameter indicates whether or not there's a newer version.
This function assumes you use [Semantic Versioning](http://semver.org) and enforces it; if your local version is `0.2.0` and the remote one is `0.1.23456` then the callback will be called with `false` as the second paramter. If on the off chance you don't use semantic versioning, you could manually download the remote manifest and call `download` if you're happy that the remote version is newer.

**Params**

- cb `function` - Callback arguments: error, newerVersionExists (`Boolean`), remoteManfiest
- cb `function` - Callback arguments: error, newerVersionExists (`Boolean`), remoteManifest

<a name="updater#clean"></a>
###updater.clean(cb)
Cleans previous app from temp folder. Do it before downloading stuff.

**Params**

- cb `function` - called when download completes. Callback arguments: error, downloaded filepath

<a name="updater#download"></a>
###updater.download(cb, newManifest)
Expand All @@ -82,7 +95,7 @@ Downloads the new app to a template folder
**Params**

- cb `function` - called when download completes. Callback arguments: error, downloaded filepath
- newManifest `Object` - package.json manifest where are defined remote url
- newManifest `Object` - see [manifest schema](#manifest-schema) below

**Returns**: `Request` - Request - stream, the stream contains `manifest` property with new manifest
<a name="updater#getAppPath"></a>
Expand All @@ -96,13 +109,15 @@ Returns current application executable

**Returns**: `string`
<a name="updater#unpack"></a>
###updater.unpack(filename, cb)
Will unpack the `filename` in temporary folder.For Windows, [unzip](https://www.mkssoftware.com/docs/man1/unzip.1.asp) is used.
###updater.unpack(filename, cb, manifest)
Will unpack the `filename` in temporary folder.
For Windows, [unzip](https://www.mkssoftware.com/docs/man1/unzip.1.asp) is used.

**Params**

- filename `string`
- cb `function` - Callback arguments: error, unpacked directory
- manifest `object`

<a name="updater#runInstaller"></a>
###updater.runInstaller(appPath, args, options)
Expand Down Expand Up @@ -131,18 +146,67 @@ Runs the app from original path.
**Params**

- execPath `string`
- args `array` - Arguments based to the app being ran
- options `object` - Optional
- args `array` - Arguments based to the app being ran. Ignored on Windows & Mac
- options `object` - Optional. Ignored on Windows & Mac

---

## Manifest Schema

An example manifest:

```json
{
"name": "updapp",
"version": "0.0.2",
"author": "Eldar Djafarov <[email protected]>",
"manifestUrl": "http://localhost:3000/package.json",
"packages": {
"mac": {
"url": "http://localhost:3000/releases/updapp/mac/updapp.zip"
},
"win": {
"url": "http://localhost:3000/releases/updapp/win/updapp.zip"
},
"linux32": {
"url": "http://localhost:3000/releases/updapp/linux32/updapp.tar.gz"
}
}
}
```

The manifest could be a `package.json` of project, but doesn't have to be.

### manifest.name

The name of your app. From time, it is assumed your Mac app is called `<manifest.name>.app`, your Windows executable is `<manifest.name>.exe`, etc.

### manifest.version
[semver](http://semver.org) version of your app.

### manifest.manifestUrl
The URL where your latest manifest is hosted; where node-webkit-updater looks to check if there is a newer version of your app available.

### manifest.packages
An "object" containing an object for each OS your app (at least this version of your app) supports; `mac`, `win`, `linux32`, `linux64`.

### manifest.packages.{mac, win, linux32, linux64}.url
Each package has to contain a `url` property pointing to where the app (for the version & OS in question) can be downloaded.

### manifest.packages.{mac, win, linux32, linux64}.execPath (Optional)
It's assumed your app is stored at the root of your package, use this to override that and specify a path (relative to the root of your package).

This can also be used to override `manifest.name`; e.g. if your `manifest.name` is `helloWorld` (therefore `helloWorld.app` on Mac) but your Windows executable is named `nw.exe`. Then you'd set `execPath` to `nw.exe`

---

## Troubleshooting

If you get an error on Mac about too many files being open, run `ulimit -n 10240`

## Contributing

See [CONTRIBUTING.md](CONTRIBUTING.md)

---

## Troubleshooting

If you get an error on Mac about too many files being open, run `ulimit -n 10240`

## Contributing

See [CONTRIBUTING.md](CONTRIBUTING.md)

[npm-url]: https://npmjs.org/package/node-webkit-updater
[npm-url]: https://npmjs.org/package/node-webkit-updater
[npm-image]: https://badge.fury.io/js/node-webkit-updater.png
14 changes: 8 additions & 6 deletions app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
console.log(err);
return;
}
upd.run(execPath, null, {});
upd.run(execPath, null);
gui.App.quit();
}
}
Expand All @@ -73,29 +73,31 @@
d = true;
clearInterval(newVersionCheckIntervalId);
var loaded = 0;
var newVersion = upd.download(newVersionDownloaded, manifest);
var newVersion = upd.download(function(error, filename){
newVersionDownloaded(error, filename, manifest);
}, manifest);
newVersion.on('data', function(chunk){
loaded+=chunk.length;
document.getElementById('loaded').innerHTML = "New version loading " + Math.floor(loaded / 1024) + 'kb';
})
}

function newVersionDownloaded(err, filename){
function newVersionDownloaded(err, filename, manifest){
if(err){
console.log(err)
return Error(err);
}
document.getElementById('loaded').innerHTML = "unpacking: " + filename;
upd.unpack(filename, newVersionUnpacked);
upd.unpack(filename, newVersionUnpacked, manifest);
}

function newVersionUnpacked(err, newAppPath){
if(err){
console.log(err)
return Error(err);
}
console.log(newAppPath, upd.getAppPath(), upd.getAppExec());
var runner = upd.runInstaller(newAppPath, [upd.getAppPath(), upd.getAppExec()],{});
console.log(newAppPath, [upd.getAppPath(), upd.getAppExec()]);
var runner = upd.runInstaller(newAppPath, [upd.getAppPath(), upd.getAppExec()]);
runner.stdout.on('data',
function(data){
console.log(data)
Expand Down
5 changes: 4 additions & 1 deletion app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"license": "MIT",
"manifestUrl": "https://raw.githubusercontent.com/openconf/updater/master/package.json",
"packages": {
"mac": "https://github.com/openconf/updater/blob/master/deploy/releases/updapp/mac/updapp.dmg?raw=true"
"mac": {
"url": "https://github.com/openconf/updater/blob/master/deploy/releases/updapp/mac/updapp.dmg?raw=true"
}
},
"window": {
"toolbar": true
Expand All @@ -15,6 +17,7 @@
"dependencies": {
"ncp": "^0.5.1",
"request": "^2.36.0",
"del": "~0.1.2",
"semver": "^3.0.1"
}
}
Loading

0 comments on commit ec9eaf2

Please sign in to comment.