Skip to content

Instantly share code, notes, and snippets.

@bsparks
Created July 10, 2012 22:43
Show Gist options
  • Save bsparks/3086678 to your computer and use it in GitHub Desktop.
Save bsparks/3086678 to your computer and use it in GitHub Desktop.
start of my node.js multi svn tool
#!/usr/bin/env node
var _ = require("lodash"),
util = require('util'),
spawn = require('child_process').spawn,
fs = require("fs"),
p = require("path"),
ansi = require("ansi"),
cursor = ansi(process.stdout),
program = require("commander"),
projectConfig;
program
.version('0.0.1')
.option('-c, --config [file]', 'optional custom config file', "projects.json")
.option('-v, --verbose', 'display verbose information');
function readConfig(file) {
var files = _.map(
[ process.cwd(), process.env[(process.platform === 'win32') ? 'USERPROFILE' : 'HOME'] ],
function (path) {
return p.join(path, file);
}),
data;
for (var i = 0, ln = files.length; i < ln; i += 1) {
try {
data = fs.readFileSync(files[i]);
break;
} catch (err) { }
}
if (data) {
return JSON.parse(data);
} else {
throw new Error('Unable to find configuration file in ' + files.join(', '));
}
}
function checkout(repo) {
var ck = spawn('svn', ['checkout', repo.url, repo.name]);
if(program.verbose) {
ck.stdout.on('data', function(data) {
console.log(repo + ": " + data);
});
}
}
function update(repo, workingDir) {
var up = spawn('svn', ['update'], {cwd: workingDir}),
success = true;
if(program.verbose) {
up.stdout.on('data', function (data) {
console.log(repo + ": " + data);
});
}
up.stderr.on('data', function (data) {
cursor.red();
console.error(repo + ': ' + data);
cursor.reset();
success = false;
});
up.on('exit', function (code) {
if(success) {
cursor.green();
console.log(repo + ": updated successfully.");
} else {
cursor.red();
console.log(repo + ": failed to update!");
}
cursor.reset();
});
}
function bulkUpdate() {
_.each(projectConfig, function(repo, name) {
update(name, p.join(process.cwd(), name));
});
}
function bulkCheckout() {
_.each(projectConfig, function(repo, name) {
repo.name = name;
checkout(repo);
});
}
function loadConfig() {
projectConfig = readConfig(program.config);
}
program
.command("list")
.description("list projects")
.action(loadConfig)
.action(function() {
_.each(projectConfig, function(repo, name) {
console.log("+ " + name);
});
});
program
.command("svn")
.description("update repositories")
.action(loadConfig)
.action(bulkUpdate);
program
.command("init")
.description("initialize projects (svn checkout)")
.action(loadConfig)
.action(bulkCheckout);
program.parse(process.argv);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment