Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
next-gen-js
  • Loading branch information
Unitech committed May 1, 2015
1 parent 2216df6 commit 911ae4e
Show file tree
Hide file tree
Showing 15 changed files with 360 additions and 2 deletions.
1 change: 1 addition & 0 deletions bin/pm2
Expand Up @@ -25,6 +25,7 @@ commander.version(pkg.version)
.option('-m --mini-list', 'display a compacted list without formatting')
.option('-f --force', 'force actions')
.option('-n --name <name>', 'set a <name> for script')
.option('--next-gen-js', 'enable es6/es7 compatibility (cluster / fork)')
.option('-i --instances <number>', 'launch [number] instances (for networked app)(load balanced)')
.option('-l --log [path]', 'specify entire log file (error and out are both included)')
.option('-o --output <path>', 'specify out log file')
Expand Down
2 changes: 2 additions & 0 deletions constants.js
Expand Up @@ -26,6 +26,8 @@ var csts = {
PM2_CONF_FILE : p.join(PM2_ROOT_PATH, 'conf.js'),
PM2_MODULE_CONF_FILE : p.join(PM2_ROOT_PATH, 'module_conf.json'),

BABEL_EXEC_PATH : p.join(__dirname, 'node_modules', 'babel', 'bin', 'babel-node'),

CODE_UNCAUGHTEXCEPTION : 100,
PREFIX_MSG : chalk.green('[PM2] '),
PREFIX_MSG_ERR : chalk.red('[PM2][ERROR] '),
Expand Down
10 changes: 8 additions & 2 deletions lib/CLI.js
Expand Up @@ -2086,14 +2086,20 @@ function prepareAppName(conf){
*/
function prepareInterpreter(conf){
var betterInterpreter = extItps[path.extname(conf.script)];

if (p.extname(conf.script).indexOf('.es') > -1) {
conf.exec_interpreter = cst.BABEL_EXEC_PATH;
}

if (conf.exec_interpreter && betterInterpreter){
if(betterInterpreter != conf.exec_interpreter){
if (betterInterpreter != conf.exec_interpreter){
warn('We\'ve notice that you are running the ' + chalk.blue(betterInterpreter) + ' script, but currently using a ' +
chalk.blue(conf.exec_interpreter) + ' interpreter, may be you need inspect the ' + chalk.blue('--interpreter') + ' option.');
}
}else if(!conf.exec_interpreter){
} else if (!conf.exec_interpreter) {
conf.exec_interpreter = betterInterpreter || 'none';
}

}

/**
Expand Down
25 changes: 25 additions & 0 deletions lib/Common.js
Expand Up @@ -16,6 +16,8 @@ var p = path;
var Stringify = require('json-stringify-safe');
var Satan = require('./Satan.js');

var iojs = require('is-iojs');

var InteractorDaemonizer = require('./Interactor/InteractorDaemonizer.js');

/**
Expand All @@ -33,6 +35,29 @@ var Common = module.exports;
* @return app
*/
Common.prepareAppConf = function(app, cwd, outputter) {
/**
* Check if PM2 is runned with iojs to handle next-gen-js
*/
if (app.next_gen_js) {
if (!iojs) {
return new Error('To run next generation Javascript you need to run PM2 with io.js');
}

// If fork mode set the right interpreter
if ((app.exec_mode == 'fork_mode' ||
app.exec_mode == 'fork' ||
!app.exec_mode) &&
app.exec_interpreter === 'node') {
app.exec_interpreter = cst.BABEL_EXEC_PATH;
}

// Allow interpreter overridde
if (app.exec_interpreter) {
Common.printOut(cst.PREFIX_MSG + 'Overridding next-gen-js interpreter');
}

Common.printOut(cst.PREFIX_MSG + 'Next gen JS enabled');
}

if (app.cron_restart) {
try {
Expand Down
8 changes: 8 additions & 0 deletions lib/ProcessContainer.js
Expand Up @@ -88,6 +88,14 @@ function exec(script, stds) {
require('coffee-script/register');
}

if (p.extname(script).indexOf('.es') > -1 ||
pm2_env.next_gen_js) {
require('babel/register')({
ignore: /node_modules/,
optional: ['es7.objectRestSpread']

This comment has been minimized.

Copy link
@deepsweet

deepsweet May 2, 2015

will it be possible to set a custom babel config? for example, I need to pass stage: 0 here for some of my projects.

This comment has been minimized.

Copy link
@deepsweet

deepsweet May 2, 2015

oh, probably it will handle .babelrc automatically, need to try. sorry :)

This comment has been minimized.

Copy link
@Unitech

Unitech May 4, 2015

Author Owner

good to know, does the .babelrc is read when require('babel/register') ?

This comment has been minimized.

Copy link
@soyuka

soyuka May 4, 2015

Collaborator

.es6 extension would be nice too indexOf('.es') is doing the job
There was a fix in 5.1.12 so yes, I think it's beeing loaded.

This comment has been minimized.

Copy link
@Unitech

Unitech May 4, 2015

Author Owner

yay awesome! @soyuka what do you think about this PR? Anything to enhance?

This comment has been minimized.

Copy link
@soyuka

soyuka May 4, 2015

Collaborator

Looking at it right now great job so far <3.

This comment has been minimized.

Copy link
@Unitech

Unitech May 4, 2015

Author Owner

ES6 is sexy!

This comment has been minimized.

Copy link
@deepsweet

deepsweet May 5, 2015

good to know, does the .babelrc is read when require('babel/register') ?

yes:

Babel 5.0.0 has support for .babelrc out of the box across its entire range of integrations. This means that it will work across babel/register, babel-node as well as across the entire range of build system plugins and module loaders such as babel-loader, babelify, and others.

http://babeljs.io/blog/2015/03/31/5.0.0/#babelrc

This comment has been minimized.

Copy link
@deepsweet

deepsweet May 5, 2015

about .es extension – currently it's not supported by github (parsed as Erlang Script), but you can drop you opinion can drop your opinion here ;)

});
}

process.on('message', function (msg) {
if (msg.type === 'log:reload') {
for(var k in stds){
Expand Down
3 changes: 3 additions & 0 deletions lib/schema.json
Expand Up @@ -27,6 +27,9 @@
"ext_type": "sbyte",
"desc": "it should be a NUMBER - byte, \"[NUMBER]G\"(Gigabyte), \"[NUMBER]M\"(Megabyte) or \"[NUMBER]K\"(Kilobyte)"
},
"next_gen_js" : {
"type" : "boolean"
},
"instances": {
"type": "number"
},
Expand Down
3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -164,6 +164,9 @@
"pm2-deploy" : "latest",
"pm2-multimeter" : "0.1.2",

"babel" : "^5.x",
"is-iojs" : "1.1.0",

"shelljs" : "0.3.0",
"isbinaryfile" : "^2.0.3",
"semver" : "4.3.3"
Expand Down
68 changes: 68 additions & 0 deletions test/bash/es6.sh
@@ -0,0 +1,68 @@
#!/usr/bin/env bash

#
# Test if iojs
#
node -e "process.exit(require('is-iojs') ? 0 : 1)"
if [ $? -eq 0 ]
then
echo "io.js engine"
else
echo "Node.js engine"
exit
fi

SRC=$(cd $(dirname "$0"); pwd)
source "${SRC}/include.sh"
cd $file_path

echo -e "\033[1mRunning tests:\033[0m"


$pm2 start es6/main.es6
sleep 1

should 'process should have not been restarted' 'restart_time: 0' 1



$pm2 delete all

$pm2 start es6/main.js
sleep 1

shouldnot 'process should have been restarted' 'restart_time: 0' 1



$pm2 delete all

$pm2 start es6/main.js --next-gen-js
sleep 1

should 'process should have not been restarted' 'restart_time: 0' 1


$pm2 delete all

$pm2 start es6/main.js --next-gen-js -i 4
sleep 1

should '(CLUSTER MODE) process should have not been restarted' 'restart_time: 0' 4


$pm2 delete all

$pm2 start es6/main.es6 -i 4
sleep 1

should '(CLUSTER MODE) process should have not been restarted' 'restart_time: 0' 4



$pm2 delete all

$pm2 start es6/main.js -i 4
sleep 1

shouldnot '(CLUSTER MODE WITHOUT ES6) process should have been restarted' 'restart_time: 0' 4
8 changes: 8 additions & 0 deletions test/bash/include.sh
Expand Up @@ -64,3 +64,11 @@ function should {
[ $OUT -eq $3 ] || fail "$1"
success "$1"
}

function shouldnot {
sleep 0.5
$pm2 prettylist > /tmp/tmp_out.txt
OUT=`cat /tmp/tmp_out.txt | grep -o "$2" | wc -l`
[ $OUT -ne $3 ] || fail "$1"
success "$1"
}
15 changes: 15 additions & 0 deletions test/fixtures/es6/example-class.es6
@@ -0,0 +1,15 @@

export class Person {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

get name() {
return this.firstName + ' ' + this.lastName;
}

toString() {
return this.name;
}
}
26 changes: 26 additions & 0 deletions test/fixtures/es6/inheritance.es6
@@ -0,0 +1,26 @@

export class Shape {
constructor (id, x, y) {
this.id = id
this.move(x, y)
}
move (x, y) {
this.x = x
this.y = y
}
}

export class Rectangle extends Shape {
constructor (id, x, y, width, height) {
super(id, x, y)
this.width = width
this.height = height
}
}

export class Circle extends Shape {
constructor (id, x, y, radius) {
super(id, x, y)
this.radius = radius
}
}
10 changes: 10 additions & 0 deletions test/fixtures/es6/lib.es6
@@ -0,0 +1,10 @@

export const sqrt = Math.sqrt;

export function square(x) {
return x * x;
}

export function diag(x, y) {
return sqrt(square(x) + square(y));
}
90 changes: 90 additions & 0 deletions test/fixtures/es6/main.es6
@@ -0,0 +1,90 @@

var assert = require('assert');

/**
* Simple import
*/
import { square, diag } from './lib';

console.log('---- simple export');
console.log(square(11));
console.log(diag(4, 3));


/**
* Class
*/
import { Person } from './example-class';

var alex = new Person('Alexandre', 'Strzelewicz');

console.log('---- get attribute');

assert.equal(alex.name, 'Alexandre Strzelewicz');

console.log(alex.name);

/**
* const, let
*/

const dure = 'constant';

// String interpolation
let msg = `Hey ${dure}`;

assert.equal(msg, 'Hey constant');

console.log(msg);

// Multiline

let msg2 = `Hey my name is
${alex.name} and
I eat potatoes`;

console.log(msg2);


// Spread operator

var params = [ "hello", true, 7 ];
var other = [ 1, 2, ...params ]; // [ 1, 2, "hello", true, 7 ]
console.log(other);

var str = "foo";
var chars = [...str ]; // [ "f", "o", "o" ]

console.log(chars);

assert.deepEqual(chars, ['f', 'o', 'o']);

// Extended parameter handling

function f (x, y, ...a) {
return (x + y) * a.length
}

assert.equal(f(1, 2, "hello", true, 7), 9);

// Destructuring arguments

var list = [ 7, 42 ]
var [ a = 1, b = 2, c = 3, d ] = list
assert.equal(a, 7)
assert.equal(b, 42)
assert.equal(c, 3)
assert.equal(d, undefined);

// Inheritance test

import { Circle } from './inheritance';

var c = new Circle('noun', 10, 20, 30);



setInterval(function() {
}, 1000)
// From
// http://es6-features.org/

0 comments on commit 911ae4e

Please sign in to comment.