Created
December 10, 2015 17:32
-
-
Save kolombet/4c0e5d54e216dbc3ae33 to your computer and use it in GitHub Desktop.
generate embeds
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
/** | |
* Составляет embed из выбранной директории | |
*/ | |
if (process.argv.length <= 2) { | |
console.log("Usage: " + __filename + " path/to/directory"); | |
process.exit(-1); | |
} | |
var fs = require("fs"); | |
var path = require("path"); | |
var workingDir = process.argv[2] | |
if (fs.lstatSync(workingDir).isDirectory() == false) | |
{ | |
console.log("Error: " + __filename + " is not directory"); | |
process.exit(-1); | |
} | |
var images = fs.readdirSync(workingDir).filter(isImage).map(getBaseName); | |
var classes = images.map(getClassName); | |
var embeds = []; | |
for (var i = 0; i < images.length; i++) | |
{ | |
embeds.push(generateEmbed(images[i], classes[i])); | |
} | |
var embedsConcat = embeds.join("\n"); | |
console.log(embedsConcat); | |
//console.log("images" + images.toString()); | |
//console.log("classes " + classes.toString()); | |
var template = fs.readFileSync("classTemplate.txt", {encoding:"UTF-8"}); | |
//template = template.replace("{{embeds}}", embedTemplate); | |
//console.log(template); | |
function generateEmbed(fileName, className) | |
{ | |
var embedTemplate = "[Embed(source = \"{{name}}\")] public static const {{className}}:Class;"; | |
embedTemplate = embedTemplate.replace("{{name}}", fileName).replace("{{className}}", className); | |
return embedTemplate; | |
} | |
function getClassName(str) | |
{ | |
var extRemoved = str.split(".")[0]; | |
var upper = toUpperCaseEachWord(extRemoved, "_"); | |
console.log(upper); | |
return upper; | |
} | |
function getBaseName(str) | |
{ | |
return path.basename(str); | |
} | |
function isImage(str) | |
{ | |
var ext = extension(str); | |
return ext == "jpg" || ext == "png" | |
} | |
function extension(str) | |
{ | |
var arr = str.split("."); | |
return arr[arr.length-1]; | |
} | |
function toUpperCaseEachWord( str, delim ) { | |
delim = delim ? delim : ' '; | |
return str.split( delim ).map( function(v) { return toUpperCaseFirstChar(v) } ).join(""); | |
} | |
function toUpperCaseFirstChar( str ) { | |
return str.substr( 0, 1 ).toUpperCase() + str.substr( 1 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment