Skip to content

Instantly share code, notes, and snippets.

@StanAngeloff
Created February 23, 2011 15:50
Show Gist options
  • Save StanAngeloff/840581 to your computer and use it in GitHub Desktop.
Save StanAngeloff/840581 to your computer and use it in GitHub Desktop.
A drop-in, quick and dirty alternative to Sprockets
#!/usr/bin/env node
var fs = require('fs'),
path = require('path'),
options = {
verbose: false,
directories: ['.']
};
var includeFollowing = false;
process.argv.slice(2).forEach(function(option) {
if (option === '-v' || option === '--verbose') {
options.verbose = true;
} else if (option === '-I' || option === '--include') {
includeFollowing = true;
} else if (option.indexOf('-I') === 0) {
options.directories.push(option.substring(2));
} else if (includeFollowing) {
options.directories.push(option);
includeFollowing = false;
}
});
function findOne(file, directories) {
var resolved = null;
directories.forEach(function(directory) {
if (resolved) {
return;
}
var target = path.join(directory, file.replace(/\.js$/, '') + '.js');
if (fs.existsSync(target)) {
resolved = target;
}
});
return resolved;
}
function pad(level) {
return Array(level * 2 + 1).join(' ');
}
function processOne(file, level) {
var lines = fs.readFileSync(file, 'utf8');
level || (level = 0);
if (options.verbose) {
console.error(pad(level) + '> ' + file + '\n');
}
lines = lines.replace(/^\s*\/\/=\s*require\s+(["<])([^">]+).$/mg, function(match, type, location) {
var resolved = findOne(location, (type === '<' ? options.directories : [path.dirname(file)]));
if (resolved) {
return processOne(resolved, level + 1);
}
throw new Error('Cannot resolve require:\n\n ' + match + '\n\n in file ' + file);
});
return lines;
}
process.stdout.write(processOne(process.argv.pop()));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment