diff --git a/LICENSE b/LICENSE index ce133cd..0602c44 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (C) 2014 Zapty Inc, Arvind Agarwal & the Contributors +Copyright (C) 2014 Zapty Inc, Arvind Agarwal Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index cd34453..0a22d6d 100644 --- a/README.md +++ b/README.md @@ -175,7 +175,7 @@ This command will stop service if running, clean up all provisioned files and se $ sudo forever list ``` -Run Non nodejs Scripts as Service +Run non nodejs scripts as service --------------------------------- forever allows to use -c command line parameter to point to alternate command line for execution, using that one can easily launch non-node apps also as service diff --git a/bin/forever-service b/bin/forever-service index ad6d968..5db7107 100755 --- a/bin/forever-service +++ b/bin/forever-service @@ -76,6 +76,10 @@ platforms.get(function(err, platform){ if(options.envVars) ctx.envVars = options.envVars; + if(ctx.envVars){ + //Split at space, but ignore space inside quotes.. + ctx.envVarsArray = ctx.envVars.match(/(?:[^\s"']+|["'][^"']*["'])+/g); + } if(options.scriptOptions) ctx.scriptOptions = options.scriptOptions; if(options.minUptime) ctx.minUptime = options.minUptime; if(options.spinSleepTime) ctx.spinSleepTime = options.spinSleepTime; diff --git a/lib/installer.js b/lib/installer.js index c75de54..4c6c8b3 100644 --- a/lib/installer.js +++ b/lib/installer.js @@ -27,3 +27,9 @@ exports.delete = function(ctx, callback){ ctx.installer.delete(ctx, scripts, callback); }); } + + +exports.splitEnvVariables=function(envVars){ + //Split at space, but ignore space inside quotes.. + return envVars.match(/(?:[^\s"']+|["'][^"']*["'])+/g); +} \ No newline at end of file diff --git a/package.json b/package.json index 299cc0f..e5cb862 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "forever-service", - "version": "0.4.3", + "version": "0.4.4", "preferGlobal": "true", "description": "Provision node script as a service via forever, allowing it to automatically start on boot, working across various Linux distros and OS", "main": "lib/api.js", diff --git a/templates/upstart/upstart.template b/templates/upstart/upstart.template index 6e66345..2f04e47 100644 --- a/templates/upstart/upstart.template +++ b/templates/upstart/upstart.template @@ -41,8 +41,8 @@ env KILLWAITTIME={{forceKillWaitTime|default('5000')}} chdir {{cwd}} -{%- if envVars|default(false) %} -{%- for v in envVars.split(" ") %} +{%- if envVarsArray|default(false) %} +{%- for v in envVarsArray %} env {{ v }} {%- endfor %} {%- endif %} diff --git a/test/scriptBuilderTest.js b/test/scriptBuilderTest.js index 844e6be..a076832 100644 --- a/test/scriptBuilderTest.js +++ b/test/scriptBuilderTest.js @@ -1,5 +1,6 @@ var should = require('should'); var scriptBuilder = require('../lib/scriptBuilder'); +var installer = require('../lib/installer'); var fs = require('fs-extra'); describe('Generate sysvinit script', function(){ @@ -51,3 +52,47 @@ describe('Generate sysvinit script', function(){ }); }); + +describe("Check Environment Variable splitting", function(){ + + it("Should work with single env variable", function(){ + var e = installer.splitEnvVariables('HOME=/xyz/'); + e.should.be.an.Array; + e.should.have.length(1); + e.should.containEql('HOME=/xyz/'); + }); + + + it("Should split env variables by space", function(){ + var e = installer.splitEnvVariables('z=10 x=test HOME=/xyz/'); + e.should.be.an.Array; + e.should.have.length(3); + e.should.containEql('z=10'); + e.should.containEql('x=test'); + e.should.containEql('HOME=/xyz/'); + }); + + + it("Should split env variables by space but avoid space splitting inside quote", function(){ + var e = installer.splitEnvVariables('z=10 x="test testing" HOME=/xyz/'); + e.should.be.an.Array; + e.should.have.length(3); + e.should.containEql('z=10'); + e.should.containEql('x="test testing"'); + e.should.containEql('HOME=/xyz/'); + }); + + + it("Should split env variables by space but avoid space splitting inside single quote", function(){ + var e = installer.splitEnvVariables("z=10 x='test testing' HOME=/xyz/"); + e.should.be.an.Array; + e.should.have.length(3); + e.should.containEql('z=10'); + e.should.containEql("x='test testing'"); + e.should.containEql('HOME=/xyz/'); + }); + + +}); + + diff --git a/test/server/normalserver.js b/test/server/normalserver.js index b5ca082..c15269d 100644 --- a/test/server/normalserver.js +++ b/test/server/normalserver.js @@ -22,4 +22,6 @@ process.once('SIGTERM', function(){ server.listen(8088); console.log("Server is listening"); - +if(process.env.TEST){ + console.log("Env variable TEST "+process.env.TEST); +}