Run TS sources directly: ESM sprite-root, .ts imports, node:test

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Christopher Monsanto
2026-08-12 17:44:51 -04:00
parent ac0ef09084
commit 75580a962a
9 changed files with 15 additions and 71 deletions

View File

@@ -1,7 +1,7 @@
import path from 'path';
import fs from 'fs';
import root from '@smogon/sprite-root';
import root from '@smogon/sprite-root/index.ts';
const libdir = path.join(root, "data");
@@ -70,7 +70,7 @@ export function parseFilename(s : string) : SpriteFilename {
if (s.length < 2)
throw new Error(`Filename ${s} needs to be at least 2 characters'`);
const prefix = s[0];
const prefix = s[0]!;
if (!prefix.match(/[a-z]/))
throw new Error(`Filename ${s} must start with alpha character`);
@@ -79,14 +79,14 @@ export function parseFilename(s : string) : SpriteFilename {
for (const part of parts.slice(1)) {
if (part.length === 0)
throw new Error(`Can't parse ${s}`);
extra.set(part[0], part.slice(1));
extra.set(part[0]!, part.slice(1));
}
if (prefix === 'x') {
const name = parts[0].slice(1);
const name = parts[0]!.slice(1);
return {extension: true, name, extra};
} else {
const id = parts[0];
const id = parts[0]!;
return {extension: false, id, extra};
}
}

View File

@@ -1,3 +1,4 @@
import path = require('path');
export = path.resolve(__dirname, "../../../" /* extra path component is because we will be in dist/ */);
import path from 'path';
export default path.resolve(import.meta.dirname, "../../");

View File

@@ -1,6 +1,6 @@
import program from 'commander';
import * as script from './script.js';
import * as script from './script.ts';
import nodePath from 'path';
function collect(value : string, previous : string[]) {

View File

@@ -2,8 +2,8 @@
import fs from 'fs';
import nodePath from 'path';
import vm from 'vm';
import * as pathlib from './path.js';
import * as spritedata from '@smogon/sprite-data';
import * as pathlib from './path.ts';
import * as spritedata from '@smogon/sprite-data/index.ts';
import tar from 'tar-stream';
import crypto from 'crypto';
import b32encode from 'base32-encode';

View File

@@ -1,57 +0,0 @@
import * as script from '../dist/script.js';
import expect from 'expect';
import path from 'path';
import {fileURLToPath} from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const spriteSrc = path.join(__dirname, "src");
it('aq', () => {
const aq = new script.ActionQueue();
aq.copy("foo", "bar");
aq.debug("test");
aq.copy("baz", "./bar");
aq.copy("baz", "/bar")
expect(aq.log).toEqual(expect.arrayContaining([
{type: 'Op', op: {type: 'Copy', src: 'foo'}, dst: "bar", valid: 'Multiple', debugObjs: []},
{type: 'Op', op: {type: 'Copy', src: 'baz'}, dst: "bar", valid: 'Multiple', debugObjs: ["test"]},
{type: 'Op', op: {type: 'Copy', src: 'baz'}, dst: "/bar", valid: 'Absolute', debugObjs: []},
]));
});
it('runOnFile', () => {
const scr = new script.Script('name === "rhydont" ? skip() : {name: "25"}', 'expr');
const aq = new script.ActionQueue();
script.runOnFile(scr, '/foo/bar/pikachu.png', aq);
script.runOnFile(scr, '/foo/bar/rhydont.png', aq);
expect(aq.log.length).toBe(1);
expect(aq.log).toEqual(expect.arrayContaining([
{type: 'Op', op: {type: 'Copy', src: '/foo/bar/pikachu.png'}, dst: "25.png", valid: 'Success', debugObjs: []},
]));
});
it('run identity', () => {
const aq = new script.ActionQueue();
const scr = new script.Script(` list(".").forEach(p => copy(p, p))`, 'expr');
script.run(scr, spriteSrc, aq);
expect(aq.log).toEqual(expect.arrayContaining([
{type: 'Op', op: {type: 'Copy', src: path.join(spriteSrc, '32.png')}, dst: "32.png", valid: 'Success', debugObjs: []},
{type: 'Op', op: {type: 'Copy', src: path.join(spriteSrc, '192-g-vsmogon.png')}, dst: "192-g-vsmogon.png", valid: 'Success', debugObjs: []},
]));
});
it('run delta', () => {
const aq = new script.ActionQueue();
const scr = new script.Script(` list(".").forEach(p => copy(p, {dir: "dest"}))`, 'expr');
script.run(scr, spriteSrc, aq);
expect(aq.log).toEqual(expect.arrayContaining([
{type: 'Op', op: {type: 'Copy', src: path.join(spriteSrc, '32.png')}, dst: "dest/32.png", valid: 'Success', debugObjs: []},
{type: 'Op', op: {type: 'Copy', src: path.join(spriteSrc, '192-g-vsmogon.png')}, dst: "dest/192-g-vsmogon.png", valid: 'Success', debugObjs: []},
]));
});

View File

@@ -4,7 +4,7 @@ import spritesmith from 'spritesmith'
import path from 'path'
import fs from 'fs'
import util from 'util';
import * as spritedata from '@smogon/sprite-data';
import * as spritedata from '@smogon/sprite-data/index.ts';
program
.option('--image <file>', 'where to put image')

View File

@@ -6,7 +6,7 @@ export function getDims(input : string) {
{encoding:'utf8'});
const [imageWidth, imageHeight, width, height, left, top] =
info.split(/x|\+/g).map(dim => parseInt(dim));
info.split(/x|\+/g).map(dim => parseInt(dim)) as [number, number, number, number, number, number];
const right = imageWidth - (left + width);
const bottom = imageHeight - (top + height);

View File

@@ -1,6 +1,6 @@
import program from 'commander';
import * as image from './image.js';
import * as image from './image.ts';
program
.option('-c, --check', 'Check only')

View File

@@ -31,7 +31,7 @@ const TUP_DB_GENERATED = 4;
const TUP_DB_GENERATED_DIR = 7;
// ^ and $ are virtual directories for environment variables
const traversalQuery = db.prepare(`
const traversalQuery = db.prepare<[number], {id: number, type: number, ctime: number, name: string}>(`
SELECT id, type, mtime as ctime, name
FROM node
WHERE dir = ?