From bfc877bba5ca31d01550df643f20920e7e582dd1 Mon Sep 17 00:00:00 2001 From: oleg Date: Mon, 18 May 2015 23:27:02 +0300 Subject: [PATCH] add yaml configs support --- app.js | 22 ++++++++++++------ data/config.json | 6 ----- data/config.yaml | 4 ++++ data/projects/project1/config.json | 16 ------------- data/projects/project1/config.yaml | 26 +++++++++++++++++++++ gulpfile.js | 2 +- lib/reader/yaml.js | 37 ++++++++++++++++++++++++++++++ package.json | 1 + 8 files changed, 84 insertions(+), 30 deletions(-) delete mode 100644 data/config.json create mode 100644 data/config.yaml delete mode 100644 data/projects/project1/config.json create mode 100644 data/projects/project1/config.yaml create mode 100644 lib/reader/yaml.js diff --git a/app.js b/app.js index b3bdd31..74fbe7f 100644 --- a/app.js +++ b/app.js @@ -32,6 +32,8 @@ var app = { dataio: dataio }; +app.lib = {}; +app.lib.reader = reader; Steppy( function() { @@ -46,20 +48,26 @@ Steppy( fs.exists(app.config.paths.builds, function(isExists) { stepCallback(null, isExists); }); + }, + function(err, isBuildsDirExists) { + if (!isBuildsDirExists) { + fs.mkdir(app.config.paths.builds, this.slot()); + } else { + this.pass(null); + } + + // register plugins + require('./lib/reader/yaml').register(app); reader.load(app.config.paths.data, 'config', this.slot()); }, - function(err, isBuildsDirExists, config) { - if (!isBuildsDirExists) { - fs.mkdir(app.config.paths.builds, this.slot()); - } - + function(err, mkdirResult, config) { _(app.config).defaults(config); - console.log('Server config:', JSON.stringify(app.config.nodes, null, 4)); + console.log('Server config:', JSON.stringify(app.config, null, 4)); + // init resources require('./resources')(app); - }, function(err) { if (err) throw err; diff --git a/data/config.json b/data/config.json deleted file mode 100644 index d387631..0000000 --- a/data/config.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "nodes": [{ - "type": "local", - "maxExecutorsCount": 1 - }] -} \ No newline at end of file diff --git a/data/config.yaml b/data/config.yaml new file mode 100644 index 0000000..c3ae012 --- /dev/null +++ b/data/config.yaml @@ -0,0 +1,4 @@ + +nodes: + - type: local + maxExecutorsCount: 1 diff --git a/data/projects/project1/config.json b/data/projects/project1/config.json deleted file mode 100644 index aadae04..0000000 --- a/data/projects/project1/config.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "name": "project1", - "scm": { - "type": "mercurial", - "repository": "./test/repos/mercurial", - "rev": "default" - }, - "steps": [ - {"type": "shell", "cmd": "sleep 2 && echo \"hello, cur dir is `pwd`\""}, - {"type": "shell", "name": "sleep", "cmd": "sleep 4"}, - {"type": "shell", "cmd": "echo 1 > 1.txt"}, - {"type": "shell", "cmd": "sleep 4"}, - {"type": "shell", "cmd": "echo 2 > 2.txt"}, - {"type": "shell", "cmd": "cat 1.txt 2.txt"} - ] -} \ No newline at end of file diff --git a/data/projects/project1/config.yaml b/data/projects/project1/config.yaml new file mode 100644 index 0000000..0ce3f1e --- /dev/null +++ b/data/projects/project1/config.yaml @@ -0,0 +1,26 @@ + +name: project1 + +scm: + type: mercurial + repository: ./test/repos/mercurial + rev: default + +steps: + - type: shell + cmd: > + echo "long multiline string" && + sleep 2 && + echo "is not a problem when you're using yaml" && + echo "cur dir is `pwd`" + - type: shell + name: sleep + cmd: sleep 4 + - type: shell + cmd: echo 1 > 1.txt + - type: shell + cmd: sleep 4 + - type: shell + cmd: echo 2 > 2.txt + - type: shell + cmd: cat 1.txt 2.txt diff --git a/gulpfile.js b/gulpfile.js index 9c82a86..01ee8ca 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -30,7 +30,7 @@ gulp.task('develop', function() { return nodemon({ ignore: ['static/**/*.js', 'app/**/*.js', 'node_modules/**'], script: 'app.js', - ext: 'js,json' + ext: 'js,json,yaml' }); }); diff --git a/lib/reader/yaml.js b/lib/reader/yaml.js new file mode 100644 index 0000000..3d89b31 --- /dev/null +++ b/lib/reader/yaml.js @@ -0,0 +1,37 @@ +'use strict'; + +var Steppy = require('twostep').Steppy, + inherits = require('util').inherits, + ParentReader = require('./base').Reader, + fs = require('fs'), + path = require('path'), + yaml = require('js-yaml'); + + +function Reader() { + ParentReader.call(this); +} + +inherits(Reader, ParentReader); + +Reader.prototype.ext = 'yaml'; + +exports.register = function(app) { + app.lib.reader.register(Reader.prototype.ext, Reader); +}; + +Reader.prototype._load = function(dir, name, callback) { + var self = this; + Steppy( + function() { + var filePath = path.join(dir, name + '.' + self.ext); + fs.readFile(filePath, 'utf8', this.slot()); + }, + function(err, text) { + var content = yaml.load(text); + + this.pass(content); + }, + callback + ); +}; diff --git a/package.json b/package.json index e637c09..420cd3b 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "dependencies": { "data.io": "0.3.0", "jade": "1.9.2", + "js-yaml": "3.3.1", "nlevel": "1.0.2", "node-static": "0.7.6", "socket.io": "1.3.5",