Skip to content

Commit

Permalink
Merge pull request #9 from brzpegasus/package-cmd
Browse files Browse the repository at this point in the history
nw:package command
  • Loading branch information
brzpegasus committed Mar 22, 2015
2 parents 317fe50 + 15f49ba commit ae2b1de
Show file tree
Hide file tree
Showing 13 changed files with 471 additions and 94 deletions.
91 changes: 80 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

An [ember-cli](http://www.ember-cli.com/) addon for developing Ember.js applications with [NW.js](http://nwjs.io/) (formerly node-webkit).

This addon updates your Ember app with the necessary configuration and scripts to make it run in a NW.js environment.
It also provides a convenient command (`ember nw`) to both build the app and launch it in a NW.js window.
* Get started quickly with an application blueprint configured for NW.js.
* Build, watch, and run the app in NW.js with a convenient command: `ember nw`.
* Build and package your app for production in one step with the `ember nw:package` command.

## Installation

Expand All @@ -22,25 +23,33 @@ This will do the following:
* Install the addon NPM package (`npm install --save-dev ember-cli-node-webkit`)
* Run the addon blueprint (`ember generate node-webkit`)
* Add the blueprint [files](https://github.com/brzpegasus/ember-cli-node-webkit/tree/master/blueprints/node-webkit/files) to your project
* Install the [`nw`](https://www.npmjs.com/package/nw) NPM package
* Install the [`nw`](https://www.npmjs.com/package/nw) NPM package locally

## Build, Watch, and Run NW.js

### Command

You can execute `ember build --watch` then start up NW.js with a single command:

```
ember nw
```

To specify a specific target environment (e.g. `development` or `production`):
As the app gets rebuilt during development, the NW.js window will automatically reload the current page, so you can see the changes that you made without having to stop and restart the entire process.

```
ember nw --environment=<ENV_NAME>
```
### Options

As the app gets rebuilt during development, the NW.js window will automatically reload the current page, so you can see the changes that you made without having to stop and restart the entire process.
The following command line options let you specify a target environment or change the directory where the built assets are stored.

**`--environment`** _(String)_ (Default: 'development')
* Target environment for the Ember app build
* Alias: `-e <value>, -dev (--environment=development), -prod (--environment=production)`

## NW.js Binary
**`--output-path`** _(String)_ (Default: 'dist/')
* Output directory for the Ember app build
* Aliases: `-o <value>`

### NW.js Binary

This addon is configured to install NW.js from [NPM](https://www.npmjs.com/package/nw) and add it to your project as a local dependency.

Expand All @@ -52,7 +61,63 @@ To use a different NW.js:

## Packaging

See https://github.com/brzpegasus/ember-cli-node-webkit/issues/6.
### Command

```
ember nw:package
```

This command builds your Ember app, assembles all the assets necessary for NW.js, then generates the final executable using [`node-webkit-builder`](https://github.com/mllrsohn/node-webkit-builder).

### Options

You can pass the following command line options:

**`--environment`** _(String)_ (Default: 'production')
* Target environment for the Ember app build
* Alias: `-e <value>, -dev (--environment=development), -prod (--environment=production)`

**`--output-path`** _(String)_ (Default: 'dist/')
* Output directory for the Ember app build
* Aliases: `-o <value>`

**`--config-file`** _(String)_ (Default: 'config/nw-package.js')
* Configuration file for `node-webkit-builder`
* Aliases: `-f <value>`

### Configuring node-webkit-builder

`node-webkit-builder` itself comes with a lot of build [options](https://github.com/mllrsohn/node-webkit-builder#options). You can customize any of those settings by supplying a configuration file named `config/nw-package.js` in your project, or call `ember nw:package` with the `--config-file` option set to the desired file.

#### Configuration File

The configuration file must be a node module that exports a plain object with the names of the options you wish to override as keys:

```javascript
// config/nw-package.js

module.exports = {
appName: 'my-nw-app',
platforms: ['osx64', 'win64'],
buildType: function() {
return this.appVersion;
}
};
```

#### Default Settings

`ember-cli-node-webkit` sets the following options by default:

* **files**
* Value: `['package.json', 'dist/**', 'node_modules/<name>/**']`
* `'node_modules/<name>/**'` is listed for every non-dev npm dependency declared in your project.
* **platforms**
* Value: `[<current_platform>]`
* **buildDir**
* Value: `build/app`
* **cacheDir**
* Value: `build/cache`

## Contribution

Expand All @@ -68,7 +133,7 @@ npm link

Then, in your Ember CLI project:

* Add `ember-cli-node-webkit` to your `package.json`'s dev dependencies. The version doesn't really matter. The package just needs to be listed so that Ember CLI can discover and register your addon:
* Add `ember-cli-node-webkit` to your `package.json`'s dev dependencies so that Ember CLI can discover and register the addon:

```json
{
Expand Down Expand Up @@ -98,3 +163,7 @@ npm test
### Want to Help?

This addon was created to help Ember.js developers build applications in NW.js. If you find patterns that work well for you, or would like to suggest ideas to make this addon even better, feel free to open new issues or submit pull requests. I'd love to hear your feedback!

## License

[Licensed under the MIT license](http://opensource.org/licenses/mit-license.php)
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ module.exports = {

includedCommands: function() {
return {
nw: require('./lib/commands/nw')
'nw': require('./lib/commands/nw'),
'nw:package': require('./lib/commands/nw-package')
}
}
};
93 changes: 93 additions & 0 deletions lib/commands/nw-package.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
'use strict';

var fs = require('fs');
var path = require('path');
var chalk = require('chalk');
var NwBuilder = require('node-webkit-builder');

module.exports = {
name: 'nw:package',
description: 'Packages your NW.js app',

availableOptions: [
{ name: 'environment', type: String, default: 'production', aliases: ['e', { 'dev' : 'development' }, { 'prod' : 'production' }] },
{ name: 'output-path', type: String, default: 'dist/', aliases: ['o'] },
{ name: 'config-file', type: String, default: 'config/nw-package.js', aliases: ['f'] }
],

buildApp: function(options) {
var buildTask = new this.tasks.Build({
ui: this.ui,
analytics: this.analytics,
project: this.project
});

return buildTask.run(options);
},

packageApp: function(options) {
var ui = this.ui;

ui.writeLine(chalk.green('Packaging...'));

var config = this.nwConfig(options);
var nw = new NwBuilder(config);
nw.on('log', console.log);

return nw.build().then(function() {
ui.writeLine(chalk.green('Packaged project successfully.'));
});
},

nwConfig: function(options) {
var config = {};

var configFile = path.resolve(this.project.root, options.configFile);
if (fs.existsSync(configFile)) {
config = require(configFile);
}

if (!config.buildDir) { config.buildDir = 'build/app'; }
if (!config.cacheDir) { config.cacheDir = 'build/cache'; }

if (!config.files) {
config.files = this.nwFiles(options);
}

if (!config.platforms) {
var detectPlatform = require('../helpers/detect-platform');
var currentPlatform = detectPlatform();
if (currentPlatform) {
config.platforms = [currentPlatform];
}
}

return config;
},

nwFiles: function(options) {
var nwFiles = ['package.json'];

// Contents of the build output directory
nwFiles.push(options.outputPath.replace(/\/?$/, '/**'));

// Non-dev NPM modules
var npmModulesRoot = 'node_modules';
var npmDependencies = this.project.pkg.dependencies || {};

Object.keys(npmDependencies).forEach(function(dependency) {
nwFiles.push(npmModulesRoot + '/' + dependency + '/**');
});

return nwFiles;
},

run: function(options) {
var _this = this;

return this.buildApp(options)
.then(function() {
return _this.packageApp(options);
});
}
};
3 changes: 1 addition & 2 deletions lib/commands/nw.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

var path = require('path');
var spawn = require('child_process').spawn;
var chalk = require('chalk');
var RSVP = require('rsvp');
Expand All @@ -13,7 +12,7 @@ module.exports = {

availableOptions: [
{ name: 'environment', type: String, default: 'development', aliases: ['e', { 'dev': 'development' }, { 'prod': 'production' }] },
{ name: 'output-path', type: path, default: 'dist/', aliases: ['o'] }
{ name: 'output-path', type: String, default: 'dist/', aliases: ['o'] }
],

buildWatch: function(options) {
Expand Down
14 changes: 14 additions & 0 deletions lib/helpers/detect-platform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

module.exports = function() {
switch (process.platform) {
case 'darwin':
return process.arch === 'x64' ? 'osx64' : 'osx32';

case 'win32':
return (process.arch === 'x64' || process.env.hasOwnProperty('PROCESSOR_ARCHITEW6432')) ? 'win64' : 'win32';

case 'linux':
return process.arch === 'x64' ? 'linux64' : 'linux32';
}
};
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"license": "MIT",
"dependencies": {
"chalk": "^1.0.0",
"node-webkit-builder": "^1.0.11",
"rsvp": "^3.0.17"
},
"devDependencies": {
Expand All @@ -23,7 +24,8 @@
"mocha": "^2.2.1",
"mocha-jshint": "^1.0.0",
"mock-spawn": "^0.2.4",
"mockery": "^1.4.0"
"mockery": "^1.4.0",
"sinon": "^1.14.1"
},
"keywords": [
"ember-addon",
Expand Down
8 changes: 8 additions & 0 deletions tests/fixtures/project-with-config/config/nw-package.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
buildDir: 'build',
cacheDir: 'cache',
files: ['package.json'],
platforms: ['osx64', 'win64'],
appName: 'foo',
appVersion: '0.0.1'
};
6 changes: 6 additions & 0 deletions tests/fixtures/project-with-config/custom-nw-package.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
buildDir: 'build/release',
platforms: ['win64'],
appName: 'bar',
appVersion: '0.0.1'
};
16 changes: 16 additions & 0 deletions tests/helpers/mocks/node-webkit-builder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

var RSVP = require('rsvp');

function MockNodeWebkitBuilder(options) {
this.options = options;
}

module.exports = MockNodeWebkitBuilder;

MockNodeWebkitBuilder.prototype.on = function() {
};

MockNodeWebkitBuilder.prototype.build = function() {
return RSVP.resolve(this.options);
};
15 changes: 15 additions & 0 deletions tests/helpers/mocks/project.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

var path = require('path');

function MockProject(name, pkg) {
this.name = name;
this.pkg = pkg || {};
this.root = path.resolve(__dirname, '..', '..', 'fixtures', name);
}

module.exports = MockProject;

MockProject.prototype.isEmberCLIProject = function() {
return true;
};
Loading

0 comments on commit ae2b1de

Please sign in to comment.