add basic build and project

This commit is contained in:
oleg 2014-12-03 00:21:32 +03:00
parent 1387d80e10
commit da60e92520
2 changed files with 130 additions and 0 deletions

43
lib/build.js Normal file
View File

@ -0,0 +1,43 @@
'use strict';
var createScm = require('../lib/scm').createScm,
createCommnd = require('../lib/command').createCommnd;
function Build(params) {
this.config = params.config;
this.cwd = params.cwd;
}
exports.Build = Build;
Build.prototype.run = function(state, callback) {
var self = this;
state.step = state.step || 'getSources';
this[state.step](state, function(err) {
if (err) {
state.err = err;
self.onFailure(state, callback);
return;
}
if (state.step === 'getSources') {
state.step = 'steps';
state.stepIndex = 0;
} else if (state.step === 'steps') {
if (state.stepIndex + 1 < self.config.steps.length) {
state.stepIndex++;
} else {
delete state.stepIndex;
}
}
});
};
Build.prototype.getSources = function(state, callback) {
};
Build.prototype.steps = function(state, callback) {
var cmdParams = this.config.steps[state.stepIndex];
var cmd = createCommand({type: cmdParams.type});
cmd.run(cmdParams, callback);
};

87
lib/project.js Normal file
View File

@ -0,0 +1,87 @@
'use strict';
var Steppy = require('Steppy'),
fs = require('fs'),
path = require('path');
function Project(config) {
this.config = config;
}
/**
* Validates and returns given `config` to the `callback`(err, config)
*/
exports.validateConfig = function(config, callback) {
callback(null, config);
};
/**
* Loads and returns project instance
*/
exports.load = function(baseDir, name, callback) {
var dir = path.join(baseDir, name);
Steppy(
function() {
fs.readdir(dir, this.slot());
},
function(err, dirContent) {
if (dirContent.indexOf('config.json') === -1) throw new Error(
'config.json is not found at project dir ' + dir
);
exports.loadConfig(dir, this.slot());
},
function(err, config) {
exports.validateConfig(config, this.slot());
},
function(err, config) {
this.pass(new Project(confg));
},
callback
);
};
exports.loadConfig = function(dir, callback) {
var configPath = path.join(dir, 'config.json');
Steppy(
function() {
fs.readFile(configPath, 'utf8', this.slot());
},
function(err, configText) {
try {
this.pass(JSON.parse(configText));
} catch(error) {
error.message = (
'Error while parsing json from config ' +
configPath + ': ' + error.message
);
throw error;
}
},
callback
);
};
exports.saveConfig = function(config, dir, callback) {
fs.writeFile(
path.join(dir, 'config.json'),
JSON.stringify(config, null, 4),
callback
);
};
exports.create = function(baseDir, config, callback) {
var dir;
Steppy(
function() {
dir = path.join(baseDir, config.name);
fs.mkdir(dir, this.slot());
},
function(err) {
exports.saveConfig(config, baseDir, this.slot());
},
function(err) {
exports.load(dir, this.slot());
},
callback
);
};