From dcfbe0cfe0628a117ab4a56e2844eaae1d3837bb Mon Sep 17 00:00:00 2001 From: Christopher Monsanto Date: Wed, 29 Apr 2020 07:31:25 -0400 Subject: [PATCH] Add gendeploy script --- tools/gendeploy.js | 70 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 tools/gendeploy.js diff --git a/tools/gendeploy.js b/tools/gendeploy.js new file mode 100644 index 00000000..208ff679 --- /dev/null +++ b/tools/gendeploy.js @@ -0,0 +1,70 @@ + +const fs = require('fs'); +const pathlib = require('path'); + +process.argv.splice(0, 2); + +if (process.argv.length < 3) { + throw new Error("Must provide at least one directory/file to generate a deployment script for, a destination directory, and a deploy json file."); +} + +const deployFile = process.argv.pop(); +const baseDir = pathlib.dirname(deployFile); +const dstDir = process.argv.pop(); +const srcs = process.argv; + +function decode(s) { + return s.replace(/__(....)/g, (_, m) => String.fromCharCode(parseInt(m, 16))).replace("_", " "); +} + +function decomposeName(name) { + const [base,forme=null] = name.split("--"); + return {base, forme}; +} + +function toPSID(name) { + return name.toLowerCase().replace(/[^a-z0-9]+/g, ''); +} + +function toPSSpriteID(name) { + const info = decomposeName(name); + let result = toPSID(info.base); + if (info.forme !== null) { + if (info.forme === 'Female') { + info.forme = 'F'; + } + result += '-' + toPSID(info.forme); + } + return result; +} + +const deployMap = new Map; + +function addFile(path) { + const parsed = pathlib.parse(path); + const newName = toPSSpriteID(decode(parsed.name)) + parsed.ext; + const srcPath = pathlib.relative(baseDir, path); + const dstPath = pathlib.join(dstDir, newName); + if (deployMap.has(dstPath)) { + throw new Error(`Already an entry for ${dstPath}`); + } + deployMap.set(dstPath, srcPath); +} + +for (const src of srcs) { + if (fs.statSync(src).isDirectory()) { + for (const name of fs.readdirSync(src)) { + addFile(pathlib.join(src, name)); + } + } else { + addFile(src); + } +} + +const output = []; +for (const [dst, src] of deployMap) { + output.push([src, dst]); +} + +fs.mkdirSync(baseDir, {recursive: true}); +fs.writeFileSync(deployFile, JSON.stringify(output, null, ' '));