Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upd: Remove deps.ts barrel file (#10) #11

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/deno.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ jobs:
mkdir -p ./dist
bun build ./app/index.tsx > ./dist/bundle.js
- run: bunx tailwindcss -i ./app/assets/style.css -o ./dist/style.css

- name: Update deno.json
run: |
jq '.imports.imagescript = "https://deno.land/x/imagescript@1.3.0/mod.ts"' deno.json > deno.json.tmp
mv deno.json.tmp deno.json
- name: Setup Deno
uses: denoland/setup-deno@v1
with:
Expand Down
5 changes: 2 additions & 3 deletions .github/workflows/jsr.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
name: Publish
name: JSR Publish
on:
push:
branches:
- main
branches: ["main"]

jobs:
publish:
Expand Down
3 changes: 2 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"imports": {
"@hono/hono": "jsr:@hono/hono@^4.4.11",
"imagescript": "https://deno.land/x/imagescript@1.3.0/mod.ts",
"imagescript": "npm:imagescript@1.3.0",
"jszip": "npm:jszip@3.10.1",
"nanoid": "npm:nanoid@5.0.7",
"nbtify": "npm:nbtify@1.90.1",
Expand All @@ -42,6 +42,7 @@
"./deno.json"
],
"include": [
"./README.md",
"./src/**/*",
"./deps.ts",
"./mod.ts"
Expand Down
7 changes: 0 additions & 7 deletions deps.ts

This file was deleted.

5 changes: 5 additions & 0 deletions src/_constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import process from "node:process";
*/
export const BLOCK_VERSION = 18153475;

/**
* Minecraft behavior block format version.
*/
export const BLOCK_FORMAT_VERSION = "1.20.80";

/**
* Default block to use when no color is matched.
*/
Expand Down
30 changes: 17 additions & 13 deletions src/_decode.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { GIF, Image } from "imagescript";
import { imagescript, readFile } from "../deps.ts";
import { GIF, Image, type Frame } from "imagescript";
import { readFile } from "node:fs/promises";
import { MAX_HEIGHT, MAX_WIDTH } from "./_constants.ts";

type DecodedFrames =
| imagescript.GIF
| Array<imagescript.Image | imagescript.Frame>;
| GIF
| Array<Image | Frame>;

/**
* Decode an image from a URL
Expand All @@ -18,8 +18,8 @@ async function decodeUrl(
const data = new Uint8Array(await res.arrayBuffer());

return !href.endsWith(".gif")
? [await imagescript.Image.decode(data)]
: [...(await imagescript.GIF.decode(data, false))] as imagescript.GIF;
? [await Image.decode(data)]
: [...(await GIF.decode(data, false))] as GIF;
}

/**
Expand All @@ -32,8 +32,8 @@ async function decodeImageFile(
data: Uint8Array
): Promise<DecodedFrames> {
return !path.endsWith(".gif")
? [await imagescript.Image.decode(data)]
: [...(await imagescript.GIF.decode(data, false))] as imagescript.GIF;
? [await Image.decode(data)]
: [...(await GIF.decode(data, false))] as GIF;
}

/**
Expand All @@ -52,8 +52,8 @@ async function decodeBase64(
);

return !base64.startsWith("data:image/gif")
? [await imagescript.Image.decode(data)]
: [...(await imagescript.GIF.decode(data, false))] as imagescript.GIF;
? [await Image.decode(data)]
: [...(await GIF.decode(data, false))] as GIF;
}

/**
Expand All @@ -75,12 +75,16 @@ export default async function decode(
img = await decodeBase64(path);
}

if (!img) {
img = await decodeImageFile(path, await readFile(path));
}

// Resize every frame above the max width/height
const frames = img?.map((i: imagescript.Image | imagescript.Frame) =>
const frames = img?.map((i: Image | Frame) =>
i.height > MAX_HEIGHT
? i.resize(imagescript.Image.RESIZE_AUTO, MAX_HEIGHT)
? i.resize(Image.RESIZE_AUTO, MAX_HEIGHT)
: i.width > MAX_WIDTH
? i.resize(MAX_WIDTH, imagescript.Image.RESIZE_AUTO)
? i.resize(MAX_WIDTH, Image.RESIZE_AUTO)
: i
) ?? [];

Expand Down
11 changes: 5 additions & 6 deletions src/_lib.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { IBlock, RGB } from "./types.ts";
import { PaletteSource } from "./types.ts";
import { readFile } from "../deps.ts";
import type { IBlock, RGB, PaletteSource } from "./types.ts";
import { readFile } from "node:fs/promises";
export function compareStates(
a: Record<string, unknown>,
b: Record<string, unknown>,
Expand All @@ -18,8 +17,8 @@ export function compareStates(
*/
export function colorDistance(color1: RGB, color2: RGB) {
return Math.sqrt(
Math.pow(color1[0] - color2[0], 2) + Math.pow(color1[1] - color2[1], 2) +
Math.pow(color1[2] - color2[2], 2),
(color1[0] - color2[0]) ** 2 + (color1[1] - color2[1]) ** 2 +
(color1[2] - color2[2]) ** 2,
);
}

Expand Down Expand Up @@ -59,5 +58,5 @@ export async function parseDbInput(
}

export function hex2rgb(hex: string): RGB {
return hex.match(/[^#]{1,2}/g)!.map((x) => parseInt(x, 16)) as RGB;
return hex.match(/[^#]{1,2}/g)?.map((x) => Number.parseInt(x, 16)) as RGB;
}
2 changes: 1 addition & 1 deletion src/_palette.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IBlock, PaletteSource, RGB, RGBA } from "./types.ts";
import type { IBlock, PaletteSource, RGBA } from "./types.ts";
import { BLOCK_VERSION } from "./_constants.ts";
import { hex2rgb } from "./_lib.ts";

Expand Down
2 changes: 1 addition & 1 deletion src/mcaddon/cli.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import img2mcaddon from "./mod.ts";
import { basename } from "node:path";
import { writeFile} from "node:fs/promises";
import { parseArgs } from "node:util";
import img2mcaddon from "./mod.ts";

async function createAddon(
src: string | URL,
Expand Down
4 changes: 1 addition & 3 deletions src/mcaddon/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import { readFile } from "node:fs/promises";
import { basename, extname } from "node:path";
import img2mcstructure, { createPalette } from "../mcstructure/mod.ts";
import type { PaletteSource, RGB } from "../types.ts";
import { BLOCK_VERSION } from "../_constants.ts";

const BLOCK_FORMAT_VERSION = "1.20.80";
import { BLOCK_VERSION, BLOCK_FORMAT_VERSION } from "../_constants.ts";

function getAverageColor(image: imagescript.Image): string {
const rgb = imagescript.Image.colorToRGB(image.averageColor());
Expand Down
2 changes: 1 addition & 1 deletion src/mcfunction/mod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { PaletteSource } from "../types.ts";
import imagescript from "imagescript";
import * as imagescript from "imagescript";
import { getNearestColor } from "../_lib.ts";
import decode from "../_decode.ts";
import createPalette from "../_palette.ts";
Expand Down
7 changes: 4 additions & 3 deletions src/mcstructure/cli.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { Axis, PaletteSource } from "../types.ts";
import { basename, extname, join, parseArgs, writeFile } from "../../deps.ts";
import { basename, extname, join } from "node:path";
import { watch, writeFile} from "node:fs/promises";
import process from "node:process";
import { parseArgs } from "node:util";
import img2mcstructure, { createPalette } from "../mcstructure/mod.ts";
import { parseDbInput } from "../_lib.ts";
import process from "node:process";
import { watch } from "node:fs/promises";

export default async function main(
src: string,
Expand Down
7 changes: 5 additions & 2 deletions src/mcstructure/mod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Axis, IBlock, IMcStructure } from "../types.ts";
import { nbt, imagescript } from "../../deps.ts";
import * as nbt from "nbtify"
import * as imagescript from "imagescript"
import decode from "../_decode.ts";
import createPalette from "../_palette.ts";
import {
Expand Down Expand Up @@ -111,7 +112,9 @@ export function constructDecoded(
const layer = Array.from({ length: width * height * depth }, () => -1);
const waterLayer = layer.slice();

for (let z = 0; z < depth; z++) {
const loopDepth = Math.min(MAX_DEPTH, depth);

for (let z = 0; z < loopDepth; z++) {
const img = frames[z];

for (const [y, x, c] of img.iterateWithColors()) {
Expand Down
9 changes: 5 additions & 4 deletions src/nbt/cli.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { Axis, PaletteSource } from "../types.ts";
import { basename, extname, join } from "node:path";
import process from "node:process";
import { watch, writeFile } from "node:fs/promises";
import { parseArgs } from "node:util";
import img2nbt from "./mod.ts";
import createPalette from "../_palette.ts";
import { parseDbInput } from "../_lib.ts";
import { basename, extname, join, parseArgs, writeFile } from "../../deps.ts";
import type { Axis, PaletteSource } from "../types.ts";
import process from "node:process";
import { watch } from "node:fs/promises";

export default async function main(
src: string,
Expand Down
3 changes: 2 additions & 1 deletion src/nbt/mod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Axis, IBlock } from "../types.ts";
import { imagescript, nbt } from "../../deps.ts";
import * as nbt from "nbtify";
import * as imagescript from "imagescript";
import { DEFAULT_BLOCK, MASK_BLOCK, MAX_DEPTH } from "../_constants.ts";
import { compareStates, getNearestColor } from "../_lib.ts";
import decode from "../_decode.ts";
Expand Down
9 changes: 5 additions & 4 deletions src/schematic/cli.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { Axis, PaletteSource } from "../types.ts";
import { basename, extname, join } from "node:path";
import { watch, writeFile } from "node:fs/promises";
import process from "node:process";
import { parseArgs } from "node:util";
import img2schematic from "./mod.ts";
import createPalette from "../_palette.ts";
import { parseDbInput } from "../_lib.ts";
import { basename, extname, join, parseArgs, writeFile } from "../../deps.ts";
import type { Axis, PaletteSource } from "../types.ts";
import { watch } from "node:fs/promises";
import process from "node:process";

export default async function main(
src: string,
Expand Down
3 changes: 2 additions & 1 deletion src/schematic/mod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Axis, IBlock } from "../types.ts";
import { imagescript, nbt } from "../../deps.ts";
import * as nbt from "nbtify";
import * as imagescript from "imagescript";
import { DEFAULT_BLOCK, MASK_BLOCK } from "../_constants.ts";
import { compareStates, getNearestColor } from "../_lib.ts";
import decode from "../_decode.ts";
Expand Down
13 changes: 5 additions & 8 deletions src/vox/cli.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import vox2mcstructure, { vox2gif } from "./mod.ts";
import { parseDbInput } from "../_lib.ts";
import {
basename,
extname,
imagescript,
join,
parseArgs,
writeFile,
} from "../../deps.ts";
import * as nbt from "nbtify";
import * as imagescript from "imagescript";
import { basename, extname, join } from "node:path";
import { parseArgs } from "node:util";
import { writeFile } from "node:fs/promises";
import createPalette from "../_palette.ts";
import process from "node:process";

Expand Down
6 changes: 4 additions & 2 deletions src/vox/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { BLOCK_VERSION, DEFAULT_BLOCK, MASK_BLOCK } from "../_constants.ts";
import type { IBlock, IMcStructure } from "../types.ts";
import readVox from "vox-reader";
import { compareStates, getNearestColor } from "../_lib.ts";
import { imagescript, nbt, readFile } from "../../deps.ts";
import * as nbt from "nbtify";
import * as imagescript from "imagescript";
import { readFile } from "node:fs/promises";

interface VoxData {
pack: {
Expand Down Expand Up @@ -188,7 +190,7 @@ export async function vox2gif(
for (const value of vox.xyzi.values) {
const [x, y, z, i] = [value.x, value.y, value.z, value.i];

if (z === z) {
if (z === size[0] - z) {
layer[(size[1] - y) * size[2] + (size[2] - x)] = i;
}
}
Expand Down
Loading