Skip to content

Instantly share code, notes, and snippets.

@Unitech
Last active August 29, 2015 14:07
Show Gist options
  • Save Unitech/ca7438f39764f7424932 to your computer and use it in GitHub Desktop.
Save Unitech/ca7438f39764f7424932 to your computer and use it in GitHub Desktop.
vizion.js

Vizion.js

Executing multiple asynchronous commands

To avoid callback hell and weird tricks, async module is recommended.

Example:

var exec = require("child_process").exec;

async.parallel([
  async.apply(exec, 'git rev-parse HEAD'),
  async.apply(exec, 'git symbolic-ref --short HEAD')
],
function (err, results) {
  console.log(results);
});

From: http://syskall.com/executing-multiple-shell-commands-cleanly-in-node-dot-js-with-async/

Async module: https://github.com/caolan/async

OR

shelljs can be used to execute ordered commands

// Run external tool synchronously
if (exec('git commit -am "Auto-commit"').code !== 0) {
  echo('Error: Git commit failed');
  exit(1);
}

https://github.com/arturadib/shelljs

Module architecture

Each supported versionning system must be splitted into different files (git.js, hg.js, svn.js)

var vizion = require('vizion');
/**
* Grab metadata for svn/git/hg repositories
*/
vizion.analyze({
folder : '/tmp/folder'
}, function(err, meta) {
if (err) throw new Error(err);
/**
*
* meta = {
* type : 'git',
* branch : 'development',
* commment : 'This is a comment',
* update_time : 'DATE',
* url : 'http://an.url',
* revision : 'f0a1d45936cf7a3c969e4caba96546fd23255796',
* next_rev : null, // null if its latest in the branch
* prev_rev : 'aaaaasdaadas7a3c969e4caba96546fd23255796'
* }
*
*/
});
/**
* Know if a path is up to date
*/
vizion.isUpToDate({
folder : '/tmp/folder'
}, function(err, meta) {
if (err) throw new Error(err);
/**
*
* meta = {
* is_up_to_date : false,
* new_revision : 'ZZZZZzzzza3c969e4caba96546fd23255796'
* }
*
*/
});
/**
* Update the repository to latest
* - on fail it should rollback to the latest commit (maybe it's already an automatic thing)
*/
vizion.update({
folder : '/tmp/folder'
}, function(err, meta) {
if (err) throw new Error(err);
/**
*
* meta = {
* success : true,
* current_revision : 'ZZZZZzzzza3c969e4caba96546fd23255796'
* }
*
*/
});
/**
* Revert to a specified commit
* - Eg: this do a git reset --hard <commit_revision>
*/
vizion.revertTo({
revision : 'f0a1d45936cf7a3c969e4caba96546fd23255796',
branch : 'master', // Do we need the branch name when we pass a commit id ?
folder : '/tmp/folder'
}, function(err, data) {
if (err) throw new Error(err);
/**
*
* data = {
* success : true,
* current_revision : 'f0a1d45936cf7a3c969e4caba96546fd23255796'
* }
*
*/
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment