ts deflopt

This commit is contained in:
Christopher Monsanto 2020-07-17 08:03:24 -04:00
parent f85b216972
commit 6f27c6689a
6 changed files with 34 additions and 9 deletions

View File

@ -8,7 +8,13 @@ importers:
'@types/node': ^14.0.23
typescript: ^3.9.7
tools/deflopt:
specifiers: {}
dependencies:
'@types/node': 14.0.23
devDependencies:
typescript: 3.9.7
specifiers:
'@types/node': ^14.0.23
typescript: ^3.9.7
tools/deploy:
dependencies:
'@smogon/sprite-data': 'link:../../data/lib'

View File

@ -20,7 +20,7 @@ Total number of bytes saved: 0
const PROCESSED_RE = /Number of files processed : *([0-9]+)/;
const REWRITTEN_RE = /Number of files rewritten : *([0-9]+)/;
function parse(re, output) {
function parse(re : RegExp, output : string) {
const m = output.match(re);
if (!m) {
throw new Error(`Can't match output: ` + output);
@ -29,7 +29,7 @@ function parse(re, output) {
return num;
}
export function deflopt(exe, file) {
export function deflopt(exe : string, file : string) {
// DeflOpt doesn't like absolute paths, it thinks they are Windows-style
// command line switches.
file = file.replace(/\//g, "\\");

View File

@ -11,10 +11,10 @@ if (src === undefined || exe === undefined) {
throw new Error('tools/deflopt <path to DeflOpt.exe> <file>');
}
asTmp("deflopt", src, dst => {
asTmp("deflopt", src, (dst : string) => {
const {processed, rewritten} = deflopt(exe, dst);
if (processed !== 1) {
throw new Error(`Didn't process any files.`);
}
return {changed: rewritten > 0};
return {changed: rewritten > 0, ret : null};
});

View File

@ -1 +1,13 @@
{"type":"module","dependencies":{}}
{
"type": "module",
"dependencies": {
"@types/node": "^14.0.23"
},
"main": "./dist/index.js",
"scripts": {
"prepare": "tsc"
},
"devDependencies": {
"typescript": "^3.9.7"
}
}

View File

@ -4,16 +4,16 @@ import fs from 'fs';
import crypto from 'crypto';
import os from 'os';
function join(...paths) {
function join(...paths : string[]) {
return pathlib.join(os.tmpdir(), ...paths);
}
// Adapted from https://stackoverflow.com/a/61312694
function mktemp(prefix) {
function mktemp(prefix : string) {
return join(`${prefix}.${crypto.randomBytes(6).readUIntLE(0,6).toString(36)}`);
}
export function asTmp(prefix, src, f) {
export function asTmp<T>(prefix : string, src : string, f : (tmpName: string) => {changed : boolean, ret : T}) {
const parsed = pathlib.parse(src);
const dst = mktemp(prefix + "." + parsed.name) + parsed.ext;
// This has an astronomically low chance of throwing, but if it does, you

View File

@ -0,0 +1,7 @@
{
"extends": "../../tsconfig-base",
"compilerOptions": {
"outDir": "dist",
"allowJs": true
}
}