Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

proper handing of scripts.start with args passed in #35

Merged
merged 1 commit into from

1 participant

@bcoe
Owner

No description provided.

@bcoe bcoe merged commit 5f855e9 into master

1 check failed

Details continuous-integration/travis-ci The Travis CI build is in progress
@bcoe bcoe deleted the arg-env-fixes branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 20, 2014
  1. @bcoe
This page is out of date. Refresh to see the latest.
View
34 lib/service.js
@@ -32,9 +32,16 @@ function Service(opts) {
// Generate a daemon wrapper from an ejs template.
Service.prototype.generateScript = function() {
if (this.runnable()) {
+ // create copy of args, so that we can
+ // non-destructively update with args from scipts.start.
+ var flatArgs = this._flattenArgs();
+
fs.writeFileSync(
this.scriptPath(),
- ejs.render(fs.readFileSync(this.template).toString(), this),
+ ejs.render(fs.readFileSync(this.template).toString(), _.merge({}, this, {
+ startScript: this._startScript(flatArgs),
+ flatArgs: flatArgs
+ })),
{
mode: 0755
}
@@ -44,6 +51,31 @@ Service.prototype.generateScript = function() {
}
};
+// convert args hash to arg list.
+Service.prototype._flattenArgs = function() {
+ var _this = this,
+ flatArgs = [];
+
+ Object.keys(this.args).forEach(function(key) {
+ flatArgs.push(key);
+ flatArgs.push(_this.args[key]);
+ });
+
+ return flatArgs;
+};
+
+// replace scripts.start with a standardized
+// bin that executes on more platforms.
+Service.prototype._startScript = function(runArgs) {
+ var startScript = this.scripts.start.replace(/^node +/, ''),
+ splitScript = startScript.split(/[ =]/);
+
+ startScript = splitScript.shift();
+ runArgs.unshift.apply(runArgs, splitScript);
+
+ return startScript;
+};
+
// is this service runnable? it could just
// provide CLI functionality.
Service.prototype.runnable = function() {
View
2  service.json
@@ -2,7 +2,7 @@
"ndm-test": {
"description": "thumbnailing service",
"scripts": {
- "start": "./test.js",
+ "start": "node ./test.js convert --foo=bar",
"foo": "./bin/foo.js"
},
"env": {
View
5 templates/launchctl.ejs
@@ -9,9 +9,8 @@
<key>ProgramArguments</key>
<array>
<string><%= nodeBin %></string>
- <string><%= scripts.start.replace(/^node +/, '') %></string><% Object.keys(args).forEach(function(key) { %>
- <string><%= key %></string>
- <string><%= args[key] %></string><% }); %>
+ <string><%= startScript %></string><% flatArgs.forEach(function(arg) { %>
+ <string><%= arg %></string><% }); %>
</array>
<key>EnvironmentVariables</key>
<dict><% Object.keys(env).forEach(function(key) { %>
View
2  templates/upstart-centos.ejs
@@ -9,5 +9,5 @@ limit nofile 1000000 1000000
script<% if (uid) { %>
su - <%= uid %><% } %>
cd <%= workingDirectory %>
- <% Object.keys(env).forEach(function(key) {%><%= key %>=<%= env[key] %> <% }); %><%= nodeBin %> <%= scripts.start.replace(/^node +/, '') %><% Object.keys(args).forEach(function(key) { %> <%= key %> <%= args[key] %><% }); %> >> <%= logFile %> 2>&1
+ <% Object.keys(env).forEach(function(key) {%><%= key %>=<%= env[key] %> <% }); %><%= nodeBin %> <%= startScript %><% flatArgs.forEach(function(arg) { %> <%= arg %><% }); %> >> <%= logFile %> 2>&1
end script
View
2  templates/upstart-ubuntu.ejs
@@ -11,7 +11,7 @@ console log
script
cd <%= workingDirectory %>
- <% Object.keys(env).forEach(function(key) {%><%= key %>=<%= env[key] %> <% }); %><%= nodeBin %> <%= scripts.start.replace(/^node +/, '') %><% Object.keys(args).forEach(function(key) { %> <%= key %> <%= args[key] %><% }); %> >> <%= logFile %> 2>&1
+ <% Object.keys(env).forEach(function(key) {%><%= key %>=<%= env[key] %> <% }); %><%= nodeBin %> <%= startScript %><% flatArgs.forEach(function(arg) { %> <%= arg %><% }); %> >> <%= logFile %> 2>&1
end script
respawn
View
34 test/service-test.js
@@ -126,9 +126,6 @@ Lab.experiment('service', function() {
Lab.experiment('generateScript', function() {
function sharedAssertions(script) {
- // it should populate the bin for the script.
- Lab.expect(script).to.match(/\.\/test.js/)
-
// local environment variables populated.
Lab.expect(script).to.match(/PORT/);
Lab.expect(script).to.match(/8000/);
@@ -163,6 +160,9 @@ Lab.experiment('service', function() {
sharedAssertions(script);
+ // it should populate the bin for the script.
+ Lab.expect(script).to.match(/>.\/test.js/)
+
done();
});
});
@@ -187,6 +187,9 @@ Lab.experiment('service', function() {
// we should not try to su.
Lab.expect(script).to.not.match(/su -/);
+ // it should populate the bin for the script.
+ Lab.expect(script).to.match(/bin\/node \.\/test.js/)
+
done();
});
@@ -231,6 +234,9 @@ Lab.experiment('service', function() {
sharedAssertions(script);
+ // it should populate the bin for the script.
+ Lab.expect(script).to.match(/bin\/node \.\/test.js/)
+
done();
});
@@ -348,4 +354,26 @@ Lab.experiment('service', function() {
});
+ Lab.experiment('_startScript', function() {
+ Lab.it('should remove node bin from the start script', function(done) {
+ var service = Service.allServices()[0],
+ startScript = service._startScript([]);
+
+ Lab.expect(startScript).to.eql('./test.js');
+
+ done();
+ });
+
+ Lab.it('should add arguments from scripts.start to flatArgs', function(done) {
+ var service = Service.allServices()[0],
+ args = ['foo'],
+ startScript = service._startScript(args);
+
+ Lab.expect(args[0]).to.eql('convert');
+ Lab.expect(args).to.contain('foo');
+
+ done();
+ });
+ });
+
});
Something went wrong with that request. Please try again.