@rohal12/twee-ts 1.6.0 → 1.8.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/dist/index.cjs CHANGED
@@ -24,15 +24,20 @@ __export(src_exports, {
24
24
  TweeLexer: () => TweeLexer,
25
25
  TweeTsError: () => TweeTsError,
26
26
  applyTagAliases: () => applyTagAliases,
27
+ clearCachedFormats: () => clearCachedFormats,
27
28
  compile: () => compile,
28
29
  compileToFile: () => compileToFile,
29
30
  discoverCachedFormats: () => discoverCachedFormats,
30
31
  discoverFormats: () => discoverFormats,
31
32
  fetchAndCacheFormat: () => fetchAndCacheFormat,
32
33
  fetchDirectFormat: () => fetchDirectFormat,
34
+ formatLintReport: () => formatLintReport,
33
35
  generateIFID: () => generateIFID,
34
36
  getCacheDir: () => getCacheDir,
37
+ getCacheSize: () => getCacheSize,
35
38
  getFormatSearchDirs: () => getFormatSearchDirs,
39
+ lint: () => lint,
40
+ listCachedFormats: () => listCachedFormats,
36
41
  loadConfig: () => loadConfig,
37
42
  loadConfigFile: () => loadConfigFile,
38
43
  parseFormatJSON: () => parseFormatJSON,
@@ -2135,6 +2140,75 @@ function discoverCachedFormats() {
2135
2140
  }
2136
2141
  return formats;
2137
2142
  }
2143
+ function listCachedFormats() {
2144
+ const cacheDir = getCacheDir();
2145
+ const entries = [];
2146
+ try {
2147
+ if (!(0, import_node_fs6.existsSync)(cacheDir)) return entries;
2148
+ } catch {
2149
+ return entries;
2150
+ }
2151
+ let names;
2152
+ try {
2153
+ names = (0, import_node_fs6.readdirSync)(cacheDir);
2154
+ } catch {
2155
+ return entries;
2156
+ }
2157
+ for (const name of names) {
2158
+ const nameDir = (0, import_node_path7.join)(cacheDir, name);
2159
+ try {
2160
+ if (!(0, import_node_fs6.statSync)(nameDir).isDirectory()) continue;
2161
+ } catch {
2162
+ continue;
2163
+ }
2164
+ let versions;
2165
+ try {
2166
+ versions = (0, import_node_fs6.readdirSync)(nameDir);
2167
+ } catch {
2168
+ continue;
2169
+ }
2170
+ for (const version of versions) {
2171
+ const formatPath = (0, import_node_path7.join)(nameDir, version, "format.js");
2172
+ try {
2173
+ const stat = (0, import_node_fs6.statSync)(formatPath);
2174
+ entries.push({
2175
+ name,
2176
+ version,
2177
+ sizeBytes: stat.size,
2178
+ modifiedAt: stat.mtime
2179
+ });
2180
+ } catch {
2181
+ }
2182
+ }
2183
+ }
2184
+ return entries;
2185
+ }
2186
+ function clearCachedFormats(name) {
2187
+ const cacheDir = getCacheDir();
2188
+ if (!(0, import_node_fs6.existsSync)(cacheDir)) return 0;
2189
+ if (!name) {
2190
+ const entries = listCachedFormats();
2191
+ const count2 = entries.length;
2192
+ (0, import_node_fs6.rmSync)(cacheDir, { recursive: true, force: true });
2193
+ return count2;
2194
+ }
2195
+ const nameDir = (0, import_node_path7.join)(cacheDir, name);
2196
+ if (!(0, import_node_fs6.existsSync)(nameDir)) return 0;
2197
+ let versions;
2198
+ try {
2199
+ versions = (0, import_node_fs6.readdirSync)(nameDir);
2200
+ } catch {
2201
+ return 0;
2202
+ }
2203
+ const count = versions.length;
2204
+ (0, import_node_fs6.rmSync)(nameDir, { recursive: true, force: true });
2205
+ return count;
2206
+ }
2207
+ function getCacheSize() {
2208
+ const entries = listCachedFormats();
2209
+ const totalBytes = entries.reduce((sum, e) => sum + e.sizeBytes, 0);
2210
+ return { totalBytes, count: entries.length };
2211
+ }
2138
2212
 
2139
2213
  // src/compiler.ts
2140
2214
  var CREATOR_NAME3 = "Twee-ts";
@@ -2437,6 +2511,69 @@ function storyInspect(story) {
2437
2511
  };
2438
2512
  }
2439
2513
 
2514
+ // src/lint.ts
2515
+ async function lint(options) {
2516
+ const result = await compile({ ...options, outputMode: "json" });
2517
+ const map = storyInspect(result.story);
2518
+ return {
2519
+ diagnostics: result.diagnostics,
2520
+ stats: result.stats,
2521
+ formatName: result.story.twine2.format,
2522
+ formatVersion: result.story.twine2.formatVersion,
2523
+ start: map.start,
2524
+ passages: map.passages.length,
2525
+ storyPassages: map.storyPassages.length,
2526
+ infoPassages: map.infoPassages.length,
2527
+ brokenLinks: map.brokenLinks,
2528
+ deadEnds: map.deadEnds,
2529
+ orphans: map.orphans
2530
+ };
2531
+ }
2532
+ function formatLintReport(result) {
2533
+ const lines = [];
2534
+ const formatStr = result.formatName ? `${result.formatName}${result.formatVersion ? " " + result.formatVersion : ""}` : "unknown";
2535
+ lines.push(`Format: ${formatStr}`);
2536
+ lines.push(
2537
+ `Passages: ${result.passages} total (${result.storyPassages} story, ${result.infoPassages} info), ${result.stats.words.toLocaleString()} words, ${result.stats.files.length} files`
2538
+ );
2539
+ lines.push(`Start: ${result.start}`);
2540
+ if (result.brokenLinks.length > 0) {
2541
+ lines.push("");
2542
+ lines.push(`Broken links (${result.brokenLinks.length}):`);
2543
+ for (const link of result.brokenLinks) {
2544
+ lines.push(` ${link.from} -> ${link.to} (passage "${link.to}" does not exist)`);
2545
+ }
2546
+ }
2547
+ if (result.deadEnds.length > 0) {
2548
+ lines.push("");
2549
+ lines.push(`Dead ends (${result.deadEnds.length}): ${result.deadEnds.join(", ")}`);
2550
+ }
2551
+ if (result.orphans.length > 0) {
2552
+ lines.push("");
2553
+ lines.push(`Orphans (${result.orphans.length}): ${result.orphans.join(", ")}`);
2554
+ }
2555
+ const errors = result.diagnostics.filter((d) => d.level === "error");
2556
+ const warnings = result.diagnostics.filter((d) => d.level === "warning");
2557
+ if (errors.length > 0 || warnings.length > 0) {
2558
+ lines.push("");
2559
+ lines.push(`Diagnostics: ${errors.length} error(s), ${warnings.length} warning(s)`);
2560
+ for (const d of errors) {
2561
+ lines.push(` error: ${d.message}`);
2562
+ }
2563
+ for (const d of warnings) {
2564
+ lines.push(` warning: ${d.message}`);
2565
+ }
2566
+ }
2567
+ lines.push("");
2568
+ const hasErrors = errors.length > 0 || result.brokenLinks.length > 0;
2569
+ if (hasErrors) {
2570
+ lines.push("Lint failed.");
2571
+ } else {
2572
+ lines.push("Lint passed.");
2573
+ }
2574
+ return lines.join("\n");
2575
+ }
2576
+
2440
2577
  // src/config.ts
2441
2578
  var import_node_fs8 = require("fs");
2442
2579
  var import_node_path8 = require("path");
@@ -2543,15 +2680,20 @@ function scaffoldConfig() {
2543
2680
  TweeLexer,
2544
2681
  TweeTsError,
2545
2682
  applyTagAliases,
2683
+ clearCachedFormats,
2546
2684
  compile,
2547
2685
  compileToFile,
2548
2686
  discoverCachedFormats,
2549
2687
  discoverFormats,
2550
2688
  fetchAndCacheFormat,
2551
2689
  fetchDirectFormat,
2690
+ formatLintReport,
2552
2691
  generateIFID,
2553
2692
  getCacheDir,
2693
+ getCacheSize,
2554
2694
  getFormatSearchDirs,
2695
+ lint,
2696
+ listCachedFormats,
2555
2697
  loadConfig,
2556
2698
  loadConfigFile,
2557
2699
  parseFormatJSON,