Skip to content

Instantly share code, notes, and snippets.

@lis186
Created June 27, 2013 08:42
Show Gist options
  • Save lis186/5874957 to your computer and use it in GitHub Desktop.
Save lis186/5874957 to your computer and use it in GitHub Desktop.
Grunt script that generate view (app/views/*.xml) and style(app/styles/*.tss) from jade(app/views/.jade) in Titanium Alloy project.
module.exports = function(grunt) {
function getJadeFiles(srcdir, destdir, wildcard) {
var path = require('path');
var files = {};
grunt.file.expand({cwd: srcdir}, wildcard).forEach(function(relpath) {
files[path.join(destdir, relpath.replace('.jade','.xml'))] = path.join(srcdir, relpath);
});
return files;
}
function generateXml2tssCommand(srcdir, destdir, wildcard) {
var path = require('path');
var commands = [];
grunt.file.expand({cwd: srcdir}, wildcard).forEach(function(relpath) {
var xmlFile = path.join(srcdir, relpath);
var tssFile = path.join(destdir, relpath.replace('.xml','.tss'));
if(grunt.file.exists(tssFile)){
grunt.log.writeln(tssFile + ' exists, ignored.');
}else{
grunt.log.writeln(tssFile + ' generated.');
commands.push('xml2tss '+ xmlFile + ' > ' + tssFile);
}
});
commands = commands.toString().replace(/,/gi, ' && ');
return commands;
}
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
watch: {
jade2xml: {
files: ["app/views/*.jade"],
tasks: ["jade", 'shell']
}
},
jade: {
compile: {
options: {
data: {
debug: false
}
},
files: getJadeFiles('app/views', 'app/views', '**/*.jade')
}
},
shell: {
xml2tss: {
command: generateXml2tssCommand('app/views', 'app/styles', '**/*.xml'),
options: {
stdout: true
}
}
}
});
grunt.loadNpmTasks('grunt-contrib-jade');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-shell');
grunt.registerTask('default', ['jade', 'shell']);
grunt.registerTask('xml2tss', ['shell']);
};
{
"name": "alloy-jade2tss",
"version": "0.1.0",
"devDependencies": {
"grunt": "~0.4.1",
"grunt-contrib-jade": "~0.7.0",
"grunt-contrib-watch": "*",
"grunt-shell": "*",
"xml2tss": "*"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment