Skip to content

Instantly share code, notes, and snippets.

@shinriyo
Created March 30, 2014 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shinriyo/9870087 to your computer and use it in GitHub Desktop.
Save shinriyo/9870087 to your computer and use it in GitHub Desktop.
Alloy&TitaniumでCoffeeScript & Stylus & Jade https://gist.github.com/polidog/5153107 のバグ修正したものに、 http://umi-uyura.hatenablog.com/entry/2013/12/18/011407 を反映し、コードの整理しました。 詳しくはhttp://d.hatena.ne.jp/shinriyo/20140330
task("pre:compile", function (event, logger) {
var wrench = require("wrench"),
fs = require("fs"),
jade = require("jade"),
path = require("path"),
coffee = require("coffee-script"),
stylus = require("stylus"),
style_root = event.dir.styles;
event.alloyConfig.coffee = [];
event.alloyConfig.jade = [];
event.alloyConfig.stylus = [];
// jade
wrench.readdirSyncRecursive(event.dir.views).forEach(function (view) {
if (view.match(/.jade$/)) {
event.alloyConfig.jade.push(view.replace(/\.jade$/, ".xml"));
fs.writeFileSync(
path.join(event.dir.views, view.replace(/\.jade$/, ".xml")),
jade.compile(fs.readFileSync(path.join(event.dir.views, view)).toString())(event));
}
});
// coffee-script
wrench.readdirSyncRecursive(event.dir.home).forEach(function (target) {
if (target.match(/\.coffee$/)) {
event.alloyConfig.coffee.push(target.replace(/\.coffee$/, ".js"));
fs.writeFileSync(
path.join(event.dir.home, target.replace(/\.coffee$/, ".js")),
coffee.compile(fs.readFileSync(path.join(event.dir.home + "/" + target)).toString(), {
bare: true
}));
}
});
// stylus
/*wrench.readdirSyncRecursive(event.dir.styles).forEach(function(target){
if (target.match(/\.styl$/)) {
event.alloyConfig.stylus.push(target.replace(/\.styl$/, ".tss"));
fs.writeFileSync(path.join(event.dir.styles, target.replace("styl", "tss")),
compileTSS(event.dir.styles, target));
}
});*/
wrench.readdirSyncRecursive(style_root).forEach(function (view) {
if (view.match("styl$")) {
var tss = compileTSS(style_root, view);
event.alloyConfig.stylus.push(view.replace(/\.styl$/, ".tss"));
fs.writeFileSync(path.join(style_root, view.replace("styl", "tss")), tss);
}
});
function compileTSS(root, target) {
var data = fs.readFileSync(path.join(root, target), "utf8"),
tss;
stylus.render(data, function (err, css) {
css = css.replace(/;/gi, ",");
css = css.replace(/\}/gi, "},");
css = css.replace(/(.+?).?\{/gi, "\"$1\": {");
css = css.replace(/,\n\},/gi, "\n\}");
css = css.replace(/\}\n\"/gi, "\},\n\"");
css = css.replace(/['"]expr(.+?)['"]/gi, "expr$1");
css = css.replace(/['"]Ti(.+?)['"]/gi, "Ti$1");
css = css.replace(/['"]Titanium(.+?)['"]/gi, "Titanium$1");
css = css.replace(/['"]Alloy(.+?)['"]/gi, "Alloy$1");
tss = css;
});
// return tss;
var tss2 = [];
tss.split("\n").forEach(function (line) {
if (line.match(/font:/)) {
var fontObj = RegExp.rightContext.replace(/(^\s+)|(\s+$)/g, "");
var closeBrace = " }";
if (fontObj.match(/,$/)) {
closeBrace = closeBrace + ",";
fontObj = fontObj.replace(/,$/, "");
}
tss2.push(" font: {");
var fontObjArray = fontObj.split(",");
for (var i = 0; i < fontObjArray.length; i++) {
var f = fontObjArray[i].replace(/(^\s+)|(\s+$)/g, "").replace(/ /, ":");
if (1 < fontObjArray.length && i < (fontObjArray.length - 1)) {
f = f + ",";
}
tss2.push(" " + f);
}
tss2.push(closeBrace);
} else {
tss2.push(line);
}
});
return tss2.join("\n");
}
});
task("post:compile", function (event, logger) {
var fs = require("fs"),
path = require("path");
if (event.alloyConfig.deploytype != 'development') {
event.alloyConfig.jade.forEach(function (target) {
if (!target.match(/index.xml/g)) fs.unlinkSync(path.join(event.dir.views, target));
});
event.alloyConfig.coffee.forEach(function (target) {
fs.unlinkSync(event.dir.home + "/" + target);
});
event.alloyConfig.stylus.forEach(function (target) {
fs.unlinkSync(event.dir.styles + "/" + target);
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment