Created
July 11, 2014 16:18
-
-
Save rodriguezartav/d54a3d90f467a650c049 to your computer and use it in GitHub Desktop.
3VOT Transform from Angular
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _3vot = require("3vot/utils") | |
var WalkDir = require("3vot-cli/node_modules/3vot-cloud/utils/walk"); | |
var Path = require("path") | |
var fs = require("fs") | |
var rimraf = require("rimraf") | |
var $ = require('cheerio') | |
var fs = require('fs') | |
rimraf( Path.join(__dirname, "..", "assets"), function(err){ | |
if(err) return console(err) | |
copyRecursiveSync( Path.join( process.cwd() , "..","take-the-plunge","app") , Path.join(__dirname, "..", "assets") ) | |
buildFileList(); | |
copyBowerToVendor(); | |
}) | |
function buildFileList(){ | |
var apps = WalkDir( Path.join( __dirname, "..") ); | |
apps.forEach( function(file){ | |
if( file.path.indexOf("assets") > -1 && file.path.indexOf("app/assets") == -1 ){ | |
if ( file.path.indexOf(".js") > -1 || file.path.indexOf(".css") > -1 || file.path.indexOf(".htm") > -1 || file.path.indexOf(".jade") > -1){ | |
//console.log(file.name); | |
var body= fs.readFileSync(file.path, "utf-8"); | |
body = transform(body, file.name); | |
fs.writeFileSync(file.path ,body); | |
} | |
} | |
}); | |
} | |
function copyBowerToVendor(){ | |
var index = fs.readFileSync( Path.join( __dirname, "..","assets", "index.html") ).toString() | |
var parsedHTML = $.load(index) | |
var paths = [] | |
parsedHTML('link').map(function(i, foo) { | |
var path = $(foo).attr("href") | |
if(path.indexOf("bower_components") > -1) { | |
paths.push(path); | |
var nameParts = path.split("/") | |
var name = nameParts[nameParts.length-1]; | |
console.log(name); | |
$(foo).attr("href", "{3vot}/assets/scripts/vendor/" + name ) | |
} | |
if(path.indexOf("styles/") > -1) { | |
var nameParts = path.split("/") | |
var name = nameParts[nameParts.length-1]; | |
console.log(name); | |
$(foo).attr("href", "{3vot}/assets/styles/" + name ) | |
} | |
}) | |
parsedHTML('script').map(function(i, foo) { | |
var path = $(foo).attr("src") | |
if(path){ | |
if(path.indexOf("bower_components") > -1) { | |
paths.push(path); | |
var nameParts = path.split("/") | |
var name = nameParts[nameParts.length-1]; | |
console.log(name); | |
$(foo).attr("src", "{3vot}/assets/scripts/vendor/" + name ) | |
} | |
if(path.indexOf("scripts/") > -1) { | |
$(foo).attr("src", "{3vot}/assets/" + path ); | |
} | |
} | |
}) | |
for(pathIndex in paths){ | |
var path = paths[pathIndex]; | |
var filePath = Path.join( __dirname, "..", "assets" , path); | |
var body = fs.readFileSync( filePath ); | |
var parts = filePath.split("/") | |
fs.writeFileSync( Path.join( __dirname, "..", "assets","scripts","vendor", parts[parts.length-1] ), body); | |
} | |
copyRecursiveSync( Path.join( __dirname , "..","assets","bower_components","famous") , Path.join(__dirname, "..", "assets","scripts","famous") ) | |
rimraf( Path.join( __dirname, "..", "assets","bower_components" ),function(err){ if(err) console.log(err); } ) | |
var html = parsedHTML.html() | |
html = html.replace( "require.config({baseUrl: 'bower_components'});", "require.config({baseUrl: '{3vot}/assets/scripts'});") | |
fs.writeFileSync( Path.join( __dirname, ".." , "index.html"), html ) | |
} | |
function transform(body, file){ | |
if(body.indexOf("/partials") > -1){ | |
body = _3vot.replaceAll(body, "/partials", "{3vot}/assets/partials"); | |
} | |
else body = _3vot.replaceAll(body, "partials", "{3vot}/assets/partials"); | |
body = _3vot.replaceAll(body, "images", "{3vot}/assets/images"); | |
body = body.replace("../data.json","{3vot}/assets/data.json"); | |
if(file.indexOf(".css") > 0){ | |
body = _3vot.replaceAll(body, "../fonts/", "{3vot}/assets/fonts/"); | |
} | |
//if(file.name === "index.html"){ | |
// body = _3vot.replaceAll(body, "bower_components", "assets/bower_components"); | |
// body = _3vot.replaceAll(body, "scripts", "assets/scripts"); | |
// body = _3vot.replaceAll(body, "styles", "assets/styles"); | |
//} | |
return body; | |
} | |
var copyRecursiveSync = function(src, dest) { | |
var exists = fs.existsSync(src); | |
var stats = exists && fs.statSync(src); | |
var isDirectory = exists && stats.isDirectory(); | |
if (exists && isDirectory) { | |
fs.mkdirSync(dest); | |
fs.readdirSync(src).forEach(function(childItemName) { | |
copyRecursiveSync(Path.join(src, childItemName), | |
Path.join(dest, childItemName)); | |
}); | |
} else { | |
fs.writeFileSync(dest, fs.readFileSync(src)); | |
//fs.linkSync(src, dest); | |
} | |
}; | |
module.exports = transform; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment