Created
July 4, 2018 08:14
-
-
Save vigoritodcx/05963f6b82fe19c6e7e8d147aaa5439b to your computer and use it in GitHub Desktop.
grunt cambe functions
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
grunt.registerTask ( "cambe", "cambe routines handler", function ( action, type, name, variation ) { | |
function getFileList(type, name, variation) { | |
var cleanType, cleanName, cleanVariation, folder, file, | |
fileList = [], | |
cambe_valids = ["context","area","module","block","element"], | |
cambe_ext = ["php", "js", "scss"], | |
regex = "/[`~!@#£$%^&*()_|+\\-=÷¿?;:'\",.<>\\{\\}\\[\\]\\\\\\/]+/gi"; | |
cleanName = ( typeof name === "string" ) ? name.replace ( regex, "_" ) : null; | |
cleanType = ( typeof type === "string" ) ? type.replace ( regex, "_" ) : null; | |
if (cleanType.substring(cleanType.length-1) === "s") {cleanType = cleanType.substring(0, type.length - 1);} | |
cleanVariation = ( typeof variation === "string" ) ? variation.replace ( regex, "_" ) : null; | |
if ( (cleanType == null) || (cleanName == null) || (!cambe_valids.includes(cleanType)) ) { | |
grunt.log.error(this + ": no valid type or name provided [" + type + " | " + name + "]" ); | |
return; | |
} | |
folder = cleanType + "s"; | |
cambe_ext.forEach(function(ext) { | |
switch ( ext ) { | |
case "php": | |
file = cleanType + "-" + cleanName + ((cleanVariation != null) ? "-" + cleanVariation : ''); | |
fileList[ext] = proj.paths.app + "" + folder + "/" + file + "." + ext; | |
break; | |
case "js": | |
file = "app" + "." + cleanType + "." + cleanName; | |
fileList[ext] = proj.paths.source + proj['paths'][ext] + folder + "/" + file+ "." + ext; | |
break; | |
case "scss": | |
file = cleanType + "-" + cleanName; | |
fileList[ext] = proj.paths.source + proj['paths'][ext] + folder + "/" + file+ "." + ext; | |
break; | |
default: | |
break; | |
} | |
}); | |
return fileList; | |
} | |
var fileList = getFileList(type, name, variation); | |
grunt.task.clearQueue(); | |
grunt.log.writeln(this.description); | |
switch ( action ) { | |
case "compose": | |
Object.values(fileList).forEach(function ( filepath ) { | |
if (!grunt.file.exists(filepath)) { | |
grunt.file.write(filepath); | |
grunt.log.ok(filepath + ': created'); | |
} else { | |
grunt.log.error ( filepath + ": skipped"); | |
} | |
}); | |
break; | |
case "delete": | |
Object.values(fileList).forEach(function ( filepath ) { | |
if (grunt.file.exists(filepath)) { | |
grunt.file.delete(filepath); | |
grunt.log.error(filepath + ': deleted'); | |
} | |
}); | |
break; | |
default: | |
grunt.log.error(this.name + ": no action found [" + action + "]" ); | |
break; | |
} | |
grunt.task.clearQueue(); | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment