mirror of
https://github.com/smogon/sprites.git
synced 2026-07-18 16:31:33 -05:00
deploy: remove old scripting functionality
This commit is contained in:
parent
e31ff207d2
commit
1a23009f34
|
|
@ -1,122 +0,0 @@
|
|||
|
||||
import pathlib from 'path';
|
||||
import fs from 'fs';
|
||||
import * as spritepath from './spritepath.js';
|
||||
import debugfn from 'debug';
|
||||
import vm from 'vm';
|
||||
|
||||
const debug = debugfn('deploy');
|
||||
|
||||
let STATE;
|
||||
|
||||
function resetState(srcDir, map) {
|
||||
STATE = {
|
||||
dstDir: ".",
|
||||
srcDir,
|
||||
transform: (dst) => dst,
|
||||
map,
|
||||
allowIgnore: false
|
||||
};
|
||||
};
|
||||
|
||||
function addPair(src, dst) {
|
||||
dst = STATE.transform(dst);
|
||||
|
||||
if (pathlib.isAbsolute(dst)) {
|
||||
throw new Error(`Absolute destination: ${dst}`);
|
||||
}
|
||||
|
||||
dst = pathlib.join(STATE.destDir, dst);
|
||||
|
||||
if (STATE.map.has(dst)) {
|
||||
if (!STATE.allowIgnore) {
|
||||
throw new Error(`Duplicate entry: ${dst}`);
|
||||
}
|
||||
} else {
|
||||
STATE.map.set(dst, src);
|
||||
}
|
||||
}
|
||||
|
||||
const ENV = {
|
||||
toPSID(dst) {
|
||||
const parsed = spritepath.parse(dst);
|
||||
parsed.name = spritepath.toPSID(parsed.data[0]);
|
||||
return pathlib.format(parsed);
|
||||
},
|
||||
|
||||
toPSSpriteID(dst) {
|
||||
const parsed = spritepath.parse(dst);
|
||||
parsed.name = spritepath.toPSSpriteID(parsed.data);
|
||||
return pathlib.format(parsed);
|
||||
},
|
||||
|
||||
toSmogonAlias(dst) {
|
||||
const parsed = spritepath.parse(dst);
|
||||
parsed.name = spritepath.toSmogonAlias(parsed.data[0]);
|
||||
return pathlib.format(parsed);
|
||||
},
|
||||
|
||||
toSmogonSpriteAlias(dst) {
|
||||
const parsed = spritepath.parse(dst);
|
||||
parsed.name = spritepath.toSmogonSpriteAlias(parsed.data);
|
||||
return pathlib.format(parsed);
|
||||
},
|
||||
|
||||
transform(f, body) {
|
||||
const oldTransform = STATE.transform;
|
||||
STATE.transform = f;
|
||||
body();
|
||||
STATE.transform = oldTransform;
|
||||
},
|
||||
|
||||
ignore(body) {
|
||||
if (STATE.allowIgnore) {
|
||||
throw new Error("Already in ignore mode");
|
||||
}
|
||||
STATE.allowIgnore = true;
|
||||
body();
|
||||
STATE.allowIgnore = false;
|
||||
},
|
||||
|
||||
dest(path) {
|
||||
STATE.destDir = path;
|
||||
},
|
||||
|
||||
sel(...srcs) {
|
||||
for (const src of srcs) {
|
||||
if (pathlib.isAbsolute(src)) {
|
||||
throw new Error(`Absolute source: ${src}`);
|
||||
}
|
||||
|
||||
const path = pathlib.join(STATE.srcDir, src);
|
||||
|
||||
if (fs.statSync(path).isDirectory()) {
|
||||
for (const name of fs.readdirSync(path)) {
|
||||
addPair(pathlib.join(path, name), name);
|
||||
}
|
||||
} else {
|
||||
addPair(path, pathlib.basename(path));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function run1(file, map) {
|
||||
const contents = fs.readFileSync(file, 'utf8');
|
||||
const curDir = pathlib.dirname(file);
|
||||
resetState(curDir, map);
|
||||
debug(`Processing ${file}`);
|
||||
vm.runInNewContext(contents, ENV);
|
||||
}
|
||||
|
||||
export function run(files) {
|
||||
const map = new Map;
|
||||
for (const file of files) {
|
||||
run1(file, map);
|
||||
}
|
||||
const results = [];
|
||||
for (const [dst, src] of map) {
|
||||
results.push({src, dst});
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
|
@ -1,79 +0,0 @@
|
|||
|
||||
import pathlib from 'path';
|
||||
|
||||
export function decodeComponent(s) {
|
||||
return s
|
||||
.replace(/([^_])_([^_])/g, "$1 $2")
|
||||
.replace(/~/g, "-")
|
||||
// Must occur last, or escaped ~/_ will be transformed
|
||||
.replace(/__(....)/g, (_, m) => String.fromCharCode(parseInt(m, 16)));
|
||||
}
|
||||
|
||||
export function decode(s) {
|
||||
return s.split("-").map(c => decodeComponent(c));
|
||||
}
|
||||
|
||||
export function parse(s) {
|
||||
const {dir, root, name, ext} = pathlib.parse(s);
|
||||
return {
|
||||
dir,
|
||||
root,
|
||||
data: decode(name),
|
||||
ext
|
||||
};
|
||||
}
|
||||
|
||||
export function encodeComponent(s) {
|
||||
return s
|
||||
.replace(/[^0-9a-zA-Z-. ]/g, c => '__' + c.charCodeAt(0).toString(16).padStart(4, "0"))
|
||||
.replace(" ", "_")
|
||||
.replace("-", "~");
|
||||
}
|
||||
|
||||
export function encode(l) {
|
||||
return l.map(c => encodeComponent(c)).join("-");
|
||||
}
|
||||
|
||||
export function format({dir, root, data, ext}) {
|
||||
return pathlib.format({
|
||||
dir,
|
||||
root,
|
||||
name: encode(data),
|
||||
ext
|
||||
});
|
||||
}
|
||||
|
||||
export function parsePokemonFilename([num, formeNum, base, forme=null]) {
|
||||
return {num: parseInt(num, 10), formeNum: parseInt(formeNum, 10), base, forme};
|
||||
}
|
||||
|
||||
export function toPSID(name) {
|
||||
return name.toLowerCase().replace(/[^a-z0-9]+/g, '');
|
||||
}
|
||||
|
||||
export function toPSSpriteID(data) {
|
||||
const info = parsePokemonFilename(data);
|
||||
let result = toPSID(info.base);
|
||||
if (info.forme !== null) {
|
||||
if (info.forme === 'Female') {
|
||||
info.forme = 'F';
|
||||
}
|
||||
result += '-' + toPSID(info.forme);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export function toSmogonAlias(name) {
|
||||
return name.toLowerCase().
|
||||
replace(/[ _]+/, "-").
|
||||
replace(/[^a-z0-9-]+/g, '');
|
||||
}
|
||||
|
||||
export function toSmogonSpriteAlias(data) {
|
||||
const info = parsePokemonFilename(data);
|
||||
let result = toSmogonAlias(info.base);
|
||||
if (info.forme !== null) {
|
||||
result += '-' + toSmogonAlias(info.forme);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user