@rohal12/twee-ts 1.7.0 → 1.9.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.
@@ -1844,7 +1844,7 @@ function modifyHead(html, modulePaths, headFile, diagnostics) {
1844
1844
  }
1845
1845
 
1846
1846
  // src/remote-formats.ts
1847
- import { mkdirSync, readFileSync as readFileSync3, writeFileSync, existsSync, readdirSync as readdirSync3, statSync as statSync4 } from "fs";
1847
+ import { mkdirSync, readFileSync as readFileSync3, writeFileSync, existsSync, readdirSync as readdirSync3, statSync as statSync4, rmSync } from "fs";
1848
1848
  import { join as join4 } from "path";
1849
1849
  import { homedir as homedir2 } from "os";
1850
1850
  var DEFAULT_SFA_INDICES = [
@@ -2075,6 +2075,75 @@ function discoverCachedFormats() {
2075
2075
  }
2076
2076
  return formats;
2077
2077
  }
2078
+ function listCachedFormats() {
2079
+ const cacheDir = getCacheDir();
2080
+ const entries = [];
2081
+ try {
2082
+ if (!existsSync(cacheDir)) return entries;
2083
+ } catch {
2084
+ return entries;
2085
+ }
2086
+ let names;
2087
+ try {
2088
+ names = readdirSync3(cacheDir);
2089
+ } catch {
2090
+ return entries;
2091
+ }
2092
+ for (const name of names) {
2093
+ const nameDir = join4(cacheDir, name);
2094
+ try {
2095
+ if (!statSync4(nameDir).isDirectory()) continue;
2096
+ } catch {
2097
+ continue;
2098
+ }
2099
+ let versions;
2100
+ try {
2101
+ versions = readdirSync3(nameDir);
2102
+ } catch {
2103
+ continue;
2104
+ }
2105
+ for (const version of versions) {
2106
+ const formatPath = join4(nameDir, version, "format.js");
2107
+ try {
2108
+ const stat = statSync4(formatPath);
2109
+ entries.push({
2110
+ name,
2111
+ version,
2112
+ sizeBytes: stat.size,
2113
+ modifiedAt: stat.mtime
2114
+ });
2115
+ } catch {
2116
+ }
2117
+ }
2118
+ }
2119
+ return entries;
2120
+ }
2121
+ function clearCachedFormats(name) {
2122
+ const cacheDir = getCacheDir();
2123
+ if (!existsSync(cacheDir)) return 0;
2124
+ if (!name) {
2125
+ const entries = listCachedFormats();
2126
+ const count2 = entries.length;
2127
+ rmSync(cacheDir, { recursive: true, force: true });
2128
+ return count2;
2129
+ }
2130
+ const nameDir = join4(cacheDir, name);
2131
+ if (!existsSync(nameDir)) return 0;
2132
+ let versions;
2133
+ try {
2134
+ versions = readdirSync3(nameDir);
2135
+ } catch {
2136
+ return 0;
2137
+ }
2138
+ const count = versions.length;
2139
+ rmSync(nameDir, { recursive: true, force: true });
2140
+ return count;
2141
+ }
2142
+ function getCacheSize() {
2143
+ const entries = listCachedFormats();
2144
+ const totalBytes = entries.reduce((sum, e) => sum + e.sizeBytes, 0);
2145
+ return { totalBytes, count: entries.length };
2146
+ }
2078
2147
 
2079
2148
  // src/compiler.ts
2080
2149
  var CREATOR_NAME3 = "Twee-ts";
@@ -2593,6 +2662,10 @@ async function main() {
2593
2662
  runInit();
2594
2663
  return;
2595
2664
  }
2665
+ if (positionals[0] === "cache") {
2666
+ runCache(positionals.slice(1));
2667
+ return;
2668
+ }
2596
2669
  let config = null;
2597
2670
  if (!values["no-config"]) {
2598
2671
  if (values.config) {
@@ -2761,6 +2834,63 @@ This is the starting passage. Edit this file to begin writing your story.
2761
2834
  console.log(" src/Start.tw");
2762
2835
  console.log("\nRun: npx @rohal12/twee-ts");
2763
2836
  }
2837
+ function formatBytes(bytes) {
2838
+ if (bytes < 1024) return `${bytes}B`;
2839
+ const kb = bytes / 1024;
2840
+ if (kb < 1024) return `${Math.round(kb)}K`;
2841
+ const mb = kb / 1024;
2842
+ return `${mb.toFixed(1)}M`;
2843
+ }
2844
+ function runCache(args) {
2845
+ const subcommand = args[0];
2846
+ switch (subcommand) {
2847
+ case "list": {
2848
+ const entries = listCachedFormats();
2849
+ if (entries.length === 0) {
2850
+ console.log("No cached formats.");
2851
+ return;
2852
+ }
2853
+ for (const e of entries) {
2854
+ const date = e.modifiedAt.toISOString().slice(0, 10);
2855
+ console.log(`${e.name.padEnd(16)} ${e.version.padEnd(10)} ${formatBytes(e.sizeBytes).padStart(6)} ${date}`);
2856
+ }
2857
+ return;
2858
+ }
2859
+ case "clear": {
2860
+ const name = args[1];
2861
+ const count = clearCachedFormats(name);
2862
+ if (count === 0) {
2863
+ console.log(name ? `No cached formats matching "${name}".` : "Cache is already empty.");
2864
+ } else {
2865
+ console.log(`Cleared ${count} cached format${count === 1 ? "" : "s"}.`);
2866
+ }
2867
+ return;
2868
+ }
2869
+ case "size": {
2870
+ const { totalBytes, count } = getCacheSize();
2871
+ if (count === 0) {
2872
+ console.log("Cache is empty.");
2873
+ } else {
2874
+ console.log(`Total: ${formatBytes(totalBytes)} (${count} format${count === 1 ? "" : "s"})`);
2875
+ }
2876
+ return;
2877
+ }
2878
+ case "path": {
2879
+ console.log(getCacheDir());
2880
+ return;
2881
+ }
2882
+ default: {
2883
+ console.error(`Usage: twee-ts cache <list|clear|size|path>
2884
+
2885
+ list List cached formats with name, version, size
2886
+ clear Delete all cached formats
2887
+ clear <name> Delete cached formats matching name
2888
+ size Show total cache size
2889
+ path Print cache directory path`);
2890
+ process.exit(subcommand ? 1 : 0);
2891
+ }
2892
+ }
2893
+ }
2764
2894
  function printUsage() {
2765
2895
  console.log(`twee-ts v${VERSION} \u2014 TypeScript Twee-to-HTML compiler
2766
2896
 
@@ -2794,7 +2924,13 @@ Options:
2794
2924
  -c, --config <file> Config file path (default: ${CONFIG_FILENAME})
2795
2925
  --no-config Skip config file loading
2796
2926
  -h, --help Show this help
2797
- -v, --version Show version`);
2927
+ -v, --version Show version
2928
+
2929
+ Subcommands:
2930
+ cache list List cached remote formats
2931
+ cache clear [name] Clear cached formats (all or by name)
2932
+ cache size Show total cache size
2933
+ cache path Print cache directory path`);
2798
2934
  }
2799
2935
  main().catch((err) => {
2800
2936
  console.error(err instanceof Error ? err.message : String(err));