@joycostudio/chopper 0.5.4 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -1
- package/codecs/manifest.json +1 -1
- package/dist/browser/{chunk-G4APMK7P.js → chunk-DH5FHQ76.js} +3 -3
- package/dist/browser/{chunk-G4APMK7P.js.map → chunk-DH5FHQ76.js.map} +1 -1
- package/dist/browser/{in-process-5DUPLJNY.js → in-process-GBWSF46M.js} +3 -3
- package/dist/browser/{in-process-5DUPLJNY.js.map → in-process-GBWSF46M.js.map} +1 -1
- package/dist/browser/index.js +2 -2
- package/dist/browser/worker.js +4 -2
- package/dist/browser/worker.js.map +1 -1
- package/dist/cli/index.js +167 -33
- package/dist/cli/index.js.map +1 -1
- package/dist/config/index.d.ts +37 -3
- package/dist/config/index.js +379 -2
- package/dist/config/index.js.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/node/index.js +169 -33
- package/dist/node/index.js.map +1 -1
- package/package.json +4 -2
package/dist/cli/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { readFile, readdir, mkdir, copyFile, mkdtemp, writeFile, rename, rm } from 'fs/promises';
|
|
3
|
-
import { extname, parse, join, dirname, resolve, relative, isAbsolute, sep
|
|
3
|
+
import { extname, parse, join, dirname, resolve, relative, isAbsolute, sep } from 'path';
|
|
4
4
|
import { fileURLToPath } from 'url';
|
|
5
5
|
import { PropertyType, NodeIO } from '@gltf-transform/core';
|
|
6
6
|
import { ALL_EXTENSIONS, EXTTextureWebP, EXTTextureAVIF, KHRTextureBasisu } from '@gltf-transform/extensions';
|
|
@@ -21,6 +21,7 @@ import webpEncoderFactory from '@jsquash/webp/codec/enc/webp_enc.js';
|
|
|
21
21
|
import { defaultOptions as defaultOptions$2 } from '@jsquash/webp/meta.js';
|
|
22
22
|
import { encodeToKTX2 } from 'ktx2-encoder';
|
|
23
23
|
import { glob } from 'tinyglobby';
|
|
24
|
+
import picomatch from 'picomatch';
|
|
24
25
|
import { createJiti } from 'jiti';
|
|
25
26
|
|
|
26
27
|
// src/core/recipes.ts
|
|
@@ -225,7 +226,7 @@ function latin1(bytes) {
|
|
|
225
226
|
|
|
226
227
|
// package.json
|
|
227
228
|
var package_default = {
|
|
228
|
-
version: "0.
|
|
229
|
+
version: "0.6.0"};
|
|
229
230
|
|
|
230
231
|
// src/codecs/manifest.ts
|
|
231
232
|
var CODEC_MANIFEST_HASH = "2046d60deb0f9f224743f4404f3c6e8ba5399f81b2811fa50466e93fce3bd322";
|
|
@@ -1360,6 +1361,8 @@ function createNodeChopper(options = {}) {
|
|
|
1360
1361
|
if (!options.codecDirectory) chopper = created;
|
|
1361
1362
|
return created;
|
|
1362
1363
|
}
|
|
1364
|
+
|
|
1365
|
+
// src/config/validation.ts
|
|
1363
1366
|
function validateConfig(config) {
|
|
1364
1367
|
if (!config || typeof config !== "object" || Array.isArray(config)) {
|
|
1365
1368
|
return failure("invalid_recipe", "Config must be an object");
|
|
@@ -1367,6 +1370,12 @@ function validateConfig(config) {
|
|
|
1367
1370
|
const candidate = config;
|
|
1368
1371
|
const unknownField = unknownFieldIssue(candidate, ["output", "rules", "concurrency"], "config");
|
|
1369
1372
|
if (unknownField) return failure("invalid_recipe", unknownField);
|
|
1373
|
+
if (typeof candidate.output !== "string" || !candidate.output) {
|
|
1374
|
+
return failure("invalid_recipe", "Config output directory is required");
|
|
1375
|
+
}
|
|
1376
|
+
if (!isProjectRelativePath(candidate.output, true)) {
|
|
1377
|
+
return failure("invalid_recipe", `Output directory must be a project-relative POSIX path: ${candidate.output}`);
|
|
1378
|
+
}
|
|
1370
1379
|
if (!Array.isArray(candidate.rules)) {
|
|
1371
1380
|
return failure("invalid_recipe", "Config rules must be an array");
|
|
1372
1381
|
}
|
|
@@ -1380,11 +1389,153 @@ function validateConfig(config) {
|
|
|
1380
1389
|
const entry = rule;
|
|
1381
1390
|
const ruleUnknownField = unknownFieldIssue(entry, ["include", "exclude", "recipe", "path"], `config rule ${index}`);
|
|
1382
1391
|
if (ruleUnknownField) return failure("invalid_recipe", ruleUnknownField);
|
|
1392
|
+
if (!isStringArray(entry.include)) {
|
|
1393
|
+
return failure("invalid_recipe", `Config rule ${index} include must be an array of glob patterns`);
|
|
1394
|
+
}
|
|
1395
|
+
if (entry.exclude !== void 0 && !isStringArray(entry.exclude)) {
|
|
1396
|
+
return failure("invalid_recipe", `Config rule ${index} exclude must be an array of glob patterns`);
|
|
1397
|
+
}
|
|
1398
|
+
const invalidGlob = [...entry.include, ...entry.exclude ?? []].find(
|
|
1399
|
+
(pattern) => !isProjectRelativeGlob(pattern)
|
|
1400
|
+
);
|
|
1401
|
+
if (invalidGlob) {
|
|
1402
|
+
return failure("invalid_recipe", `Config rule ${index} glob must be project-relative: ${invalidGlob}`);
|
|
1403
|
+
}
|
|
1404
|
+
if (entry.path !== void 0 && (typeof entry.path !== "string" || !entry.path)) {
|
|
1405
|
+
return failure("invalid_recipe", `Config rule ${index} path must be a non-empty string`);
|
|
1406
|
+
}
|
|
1383
1407
|
const recipe = validateRecipe(entry.recipe);
|
|
1384
1408
|
if (!recipe.ok) return recipe;
|
|
1385
1409
|
}
|
|
1386
1410
|
return success(config);
|
|
1387
1411
|
}
|
|
1412
|
+
function normalizeProjectPath(path) {
|
|
1413
|
+
if (typeof path !== "string") return void 0;
|
|
1414
|
+
if (!isProjectRelativePath(path, false)) return void 0;
|
|
1415
|
+
const parts = [];
|
|
1416
|
+
for (const part of path.split("/")) {
|
|
1417
|
+
if (!part || part === ".") continue;
|
|
1418
|
+
if (part === "..") {
|
|
1419
|
+
if (!parts.length) return void 0;
|
|
1420
|
+
parts.pop();
|
|
1421
|
+
} else {
|
|
1422
|
+
parts.push(part);
|
|
1423
|
+
}
|
|
1424
|
+
}
|
|
1425
|
+
return parts.join("/") || void 0;
|
|
1426
|
+
}
|
|
1427
|
+
function isStringArray(value) {
|
|
1428
|
+
return Array.isArray(value) && value.every((item) => typeof item === "string" && item.length > 0);
|
|
1429
|
+
}
|
|
1430
|
+
function isProjectRelativeGlob(pattern) {
|
|
1431
|
+
const path = pattern.startsWith("!") && !pattern.startsWith("!(") ? pattern.slice(1) : pattern;
|
|
1432
|
+
return path.length > 0 && !path.includes("\0") && !path.startsWith("/") && !/^[A-Za-z]:\//.test(path) && !path.split("/").includes("..");
|
|
1433
|
+
}
|
|
1434
|
+
function isProjectRelativePath(path, allowCurrentDirectory) {
|
|
1435
|
+
if (!path || path.includes("\\") || path.includes("\0") || path.startsWith("/") || /^[A-Za-z]:\//.test(path)) {
|
|
1436
|
+
return false;
|
|
1437
|
+
}
|
|
1438
|
+
const normalized = normalizeSegments(path);
|
|
1439
|
+
return normalized !== void 0 && (allowCurrentDirectory || normalized !== "");
|
|
1440
|
+
}
|
|
1441
|
+
function normalizeSegments(path) {
|
|
1442
|
+
const parts = [];
|
|
1443
|
+
for (const part of path.split("/")) {
|
|
1444
|
+
if (!part || part === ".") continue;
|
|
1445
|
+
if (part === "..") {
|
|
1446
|
+
if (!parts.length) return void 0;
|
|
1447
|
+
parts.pop();
|
|
1448
|
+
} else {
|
|
1449
|
+
parts.push(part);
|
|
1450
|
+
}
|
|
1451
|
+
}
|
|
1452
|
+
return parts.join("/");
|
|
1453
|
+
}
|
|
1454
|
+
|
|
1455
|
+
// src/config/planner.ts
|
|
1456
|
+
var GLOB_OPTIONS = {
|
|
1457
|
+
dot: false,
|
|
1458
|
+
nobrace: false,
|
|
1459
|
+
nocase: false,
|
|
1460
|
+
noextglob: false,
|
|
1461
|
+
noglobstar: false,
|
|
1462
|
+
posix: true
|
|
1463
|
+
};
|
|
1464
|
+
function createConfigAssetPlanner(config) {
|
|
1465
|
+
let prepared;
|
|
1466
|
+
return (input) => {
|
|
1467
|
+
prepared ??= prepareRules(config);
|
|
1468
|
+
if (!prepared.ok) return prepared;
|
|
1469
|
+
const normalizedInput = normalizeProjectPath(input);
|
|
1470
|
+
if (!normalizedInput) {
|
|
1471
|
+
return failure("invalid_recipe", `Asset input must be a project-relative POSIX path: ${input}`);
|
|
1472
|
+
}
|
|
1473
|
+
const planned = /* @__PURE__ */ new Map();
|
|
1474
|
+
for (const { rule, matches } of prepared.data) {
|
|
1475
|
+
if (!matches(normalizedInput)) continue;
|
|
1476
|
+
const output = resolveOutput(rule, normalizedInput);
|
|
1477
|
+
if (!output.ok) return output;
|
|
1478
|
+
planned.set(output.data, { input: normalizedInput, output: output.data, rule });
|
|
1479
|
+
}
|
|
1480
|
+
return success([...planned.values()]);
|
|
1481
|
+
};
|
|
1482
|
+
}
|
|
1483
|
+
function prepareRules(config) {
|
|
1484
|
+
const validated = validateConfig(config);
|
|
1485
|
+
if (!validated.ok) return validated;
|
|
1486
|
+
try {
|
|
1487
|
+
return success(validated.data.rules.map((rule) => ({ rule, matches: compileRuleMatcher(rule) })));
|
|
1488
|
+
} catch (cause) {
|
|
1489
|
+
return failure("invalid_recipe", "Config contains an invalid glob pattern", cause);
|
|
1490
|
+
}
|
|
1491
|
+
}
|
|
1492
|
+
function compileRuleMatcher(rule) {
|
|
1493
|
+
const includePatterns = [];
|
|
1494
|
+
const ignorePatterns = (rule.exclude ?? []).filter((pattern) => !pattern.startsWith("!") || pattern.startsWith("!("));
|
|
1495
|
+
for (const pattern of rule.include) {
|
|
1496
|
+
if (pattern.startsWith("!") && !pattern.startsWith("!(")) {
|
|
1497
|
+
if (!pattern.startsWith("!!") || pattern.startsWith("!!(")) ignorePatterns.push(pattern.slice(1));
|
|
1498
|
+
} else {
|
|
1499
|
+
includePatterns.push(pattern);
|
|
1500
|
+
}
|
|
1501
|
+
}
|
|
1502
|
+
const include = picomatch(includePatterns.map(expandDirectoryPattern), GLOB_OPTIONS);
|
|
1503
|
+
const ignore = picomatch(ignorePatterns.map(expandDirectoryPattern), GLOB_OPTIONS);
|
|
1504
|
+
return (input) => include(input) && !ignore(input);
|
|
1505
|
+
}
|
|
1506
|
+
function expandDirectoryPattern(pattern) {
|
|
1507
|
+
const normalized = pattern.split("/").filter((segment) => segment && segment !== ".").join("/") || "**";
|
|
1508
|
+
const withoutTrailingSlash = normalized.endsWith("/") ? normalized.slice(0, -1) : normalized;
|
|
1509
|
+
return withoutTrailingSlash.endsWith("*") ? withoutTrailingSlash : `${withoutTrailingSlash}/**`;
|
|
1510
|
+
}
|
|
1511
|
+
function resolveOutput(rule, input) {
|
|
1512
|
+
const template = rule.path ?? "{dir}/{name}.{ext}";
|
|
1513
|
+
if (template.includes("\\") || template.startsWith("/") || /^[A-Za-z]:\//.test(template)) {
|
|
1514
|
+
return failure("invalid_recipe", `Output path must stay inside the configured output directory: ${template}`);
|
|
1515
|
+
}
|
|
1516
|
+
const slash = input.lastIndexOf("/");
|
|
1517
|
+
const directory = slash < 0 ? "" : input.slice(0, slash);
|
|
1518
|
+
const base = slash < 0 ? input : input.slice(slash + 1);
|
|
1519
|
+
const dot = base.lastIndexOf(".");
|
|
1520
|
+
const name = dot <= 0 ? base : base.slice(0, dot);
|
|
1521
|
+
const extension = rule.recipe.kind === "glb" ? ".glb" : outputExtension(rule.recipe.encode);
|
|
1522
|
+
const rendered = template.replaceAll("{dir}/", directory ? `${directory}/` : "").replaceAll("{dir}", directory).replaceAll("{name}", name).replaceAll("{ext}", extension.slice(1));
|
|
1523
|
+
if (/\{[^{}]+\}/.test(rendered)) {
|
|
1524
|
+
return failure("invalid_recipe", `Output path contains an unsupported placeholder: ${template}`);
|
|
1525
|
+
}
|
|
1526
|
+
const output = normalizeProjectPath(rendered);
|
|
1527
|
+
return output ? success(output) : failure("invalid_recipe", `Output path must stay inside the configured output directory: ${template}`);
|
|
1528
|
+
}
|
|
1529
|
+
|
|
1530
|
+
// src/config/index.ts
|
|
1531
|
+
function createChopper2(config) {
|
|
1532
|
+
const defined = { ...config };
|
|
1533
|
+
Object.defineProperty(defined, "resolve", {
|
|
1534
|
+
enumerable: false,
|
|
1535
|
+
value: createConfigAssetPlanner(defined)
|
|
1536
|
+
});
|
|
1537
|
+
return defined;
|
|
1538
|
+
}
|
|
1388
1539
|
async function loadConfig(path = "chopper.config.ts") {
|
|
1389
1540
|
const absolutePath = resolve(path);
|
|
1390
1541
|
try {
|
|
@@ -1424,34 +1575,26 @@ async function planAssets(config, cwd) {
|
|
|
1424
1575
|
if (!validated.ok) return validated;
|
|
1425
1576
|
config = validated.data;
|
|
1426
1577
|
const planned = [];
|
|
1427
|
-
if (typeof config.output !== "string" || !config.output) {
|
|
1428
|
-
return failure("invalid_recipe", "Config output directory is required");
|
|
1429
|
-
}
|
|
1430
1578
|
const outputRoot = resolve(cwd, config.output);
|
|
1431
1579
|
const relativeRoot = relative(cwd, outputRoot);
|
|
1432
1580
|
if (isAbsolute(relativeRoot) || relativeRoot === ".." || relativeRoot.startsWith(`..${sep}`)) {
|
|
1433
1581
|
return failure("invalid_recipe", `Output directory must be inside the project: ${config.output}`);
|
|
1434
1582
|
}
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
return failure("invalid_recipe", `Output path must stay inside ${config.output}: ${rule.path}`);
|
|
1448
|
-
}
|
|
1449
|
-
planned.push({
|
|
1450
|
-
input: resolve(cwd, match),
|
|
1451
|
-
output,
|
|
1583
|
+
const planner = createChopper2(config);
|
|
1584
|
+
const discoveryPatterns = config.rules.flatMap(
|
|
1585
|
+
({ include }) => include.filter((pattern) => !pattern.startsWith("!") || pattern.startsWith("!("))
|
|
1586
|
+
);
|
|
1587
|
+
const matches = await glob(discoveryPatterns, { cwd, onlyFiles: true });
|
|
1588
|
+
for (const match of matches.sort()) {
|
|
1589
|
+
const result = planner.resolve(match);
|
|
1590
|
+
if (!result.ok) return result;
|
|
1591
|
+
planned.push(
|
|
1592
|
+
...result.data.map(({ input, output, rule }) => ({
|
|
1593
|
+
input: resolve(cwd, input),
|
|
1594
|
+
output: resolve(outputRoot, output),
|
|
1452
1595
|
rule
|
|
1453
|
-
})
|
|
1454
|
-
|
|
1596
|
+
}))
|
|
1597
|
+
);
|
|
1455
1598
|
}
|
|
1456
1599
|
const resolved = /* @__PURE__ */ new Map();
|
|
1457
1600
|
const inputs = new Set(planned.map(({ input }) => input));
|
|
@@ -1512,15 +1655,6 @@ async function executeAsset(asset, mode, cwd, chopper2) {
|
|
|
1512
1655
|
};
|
|
1513
1656
|
}
|
|
1514
1657
|
}
|
|
1515
|
-
function resolveOutput(rule, input, outputRoot) {
|
|
1516
|
-
const extension = rule.recipe.kind === "glb" ? ".glb" : outputExtension(rule.recipe.encode);
|
|
1517
|
-
const inputDirectory = dirname(input) === "." ? "" : dirname(input);
|
|
1518
|
-
const name = basename(input, extname(input));
|
|
1519
|
-
return resolve(
|
|
1520
|
-
outputRoot,
|
|
1521
|
-
(rule.path ?? "{dir}/{name}.{ext}").replaceAll("{dir}", inputDirectory).replaceAll("{name}", name).replaceAll("{ext}", extension.slice(1))
|
|
1522
|
-
);
|
|
1523
|
-
}
|
|
1524
1658
|
async function writeAtomic(path, bytes) {
|
|
1525
1659
|
await mkdir(dirname(path), { recursive: true });
|
|
1526
1660
|
const temporaryDirectory = await mkdtemp(join(dirname(path), ".chopper-"));
|