Skip to content

Instantly share code, notes, and snippets.

@rioki
Created Sep 18, 2012
Embed
What would you like to do?
Automatically "build" when files change.
var fs = require("fs-extra");
var path = require("path");
var cp = require("child_process");
console.log("------------------------------------------");
console.log("autobuild");
console.log("------------------------------------------");
if (process.argv.length == 3) {
var configFile = process.argv[2];
}
else {
console.log("Usage: node autosync.js <config.json>");
}
fs.readFile(configFile, "utf-8", function (err, data) {
if (err) {
console.log("Failed to load configuration '" + configFile + "'.");
return;
}
var config = JSON.parse(data);
console.log("name: " + config.name);
console.log("source: " + config.sources);
console.log("build: " + config.build);
console.log("target: " + config.target);
console.log("------------------------------------------");
config.sourcePaternsRegEx = [];
config.sourcePaterns.forEach(function (pattern) {
config.sourcePaternsRegEx.push(new RegExp(pattern, "i"));
});
// TODO build project for good mesure somewhare
autobuild(config);
});
function watchSource(config) {
config.sources.forEach(function (source) {
fs.readdir(source, function(err, files) {
if (err) {
console.log(err);
return;
}
files.forEach(function (file) {
config.sourcePaternsRegEx.forEach(function (pattern) {
if (file.match(pattern)) {
var filePath = path.join(source, file);
fs.watchFile(filePath, function () {
build(config);
});
}
});
});
});
});
}
function watchBuild(config) {
fs.readdir(config.build, function (err, files) {
if (err) {
console.log(err);
return;
}
files.forEach(function (file) {
config.buildResults.forEach(function (result) {
if (file == result) {
var filePath = path.join(config.build, file);
var targetPath = path.join(config.target, file);
fs.watchFile(filePath, function () {
fs.copy(filePath, targetPath, function (err) {
if (err) {
console.log("failed to copy " + file);
}
else {
console.log("copied " + file);
}
});
});
}
});
});
});
}
function build(config, callback) {
console.log("building " + config.name);
cp.exec(config.buildCommand, { cwd: config.sources[0] }, function (error, stdout, stderr) {
console.log('stdout: \n' + stdout);
console.log('stderr: \n' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
console.log("finished building " + config.name);
});
}
function autobuild(config) {
watchSource(config);
watchBuild(config);
}
// maybe some time later
// exports.autobuild = autobuild;
{
"name": "My Project",
"sources": ["D:\\Devel\\OneProject", "D:\\Devel\\Includes"],
"sourcePaterns": [".cpp", ".cc", ".h"],
"build": "D:\\Devel\\Build\Release",
"buildCommand": "\"C:\\Program Files\\Microsoft Visual Studio\\COMMON\\MSDev98\\Bin\\msdev.exe\" mypoj.dsw /MAKE \"ALL - RELEASE\"",
"buildResults": ["myproj.dll", "OneProject.pdb"],
"target": "C:\\Program Files\\My Cool Software"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment