@rohal12/twee-ts 1.14.0 → 1.15.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
@@ -60,7 +60,7 @@ __export(src_exports, {
60
60
  module.exports = __toCommonJS(src_exports);
61
61
 
62
62
  // src/compiler.ts
63
- var import_node_fs7 = require("fs");
63
+ var import_node_fs8 = require("fs");
64
64
 
65
65
  // src/ifid.ts
66
66
  var import_node_crypto = require("crypto");
@@ -4094,17 +4094,35 @@ function loadInlineSources(story, sources, opts, diagnostics) {
4094
4094
  }
4095
4095
  const content = typeof source.content === "string" ? source.content : source.content.toString("utf-8");
4096
4096
  const ext = normalizedFileExt(source.filename);
4097
- if (ext === "tw" || ext === "twee" || ext === "tw2" || ext === "twee2" || !ext) {
4098
- const twee2 = ext === "tw2" || ext === "twee2" || opts.twee2Compat;
4099
- const result = parseTwee(content, {
4100
- filename: source.filename,
4101
- trim: opts.trim ?? true,
4102
- twee2Compat: twee2
4103
- });
4104
- diagnostics.push(...result.diagnostics);
4105
- for (const p of result.passages) {
4106
- storyAdd(story, p, diagnostics);
4097
+ switch (ext) {
4098
+ case "":
4099
+ case "tw":
4100
+ case "twee":
4101
+ case "tw2":
4102
+ case "twee2": {
4103
+ const twee2 = ext === "tw2" || ext === "twee2" || opts.twee2Compat;
4104
+ const result = parseTwee(content, {
4105
+ filename: source.filename,
4106
+ trim: opts.trim ?? true,
4107
+ twee2Compat: twee2
4108
+ });
4109
+ diagnostics.push(...result.diagnostics);
4110
+ for (const p of result.passages) {
4111
+ storyAdd(story, p, diagnostics);
4112
+ }
4113
+ break;
4107
4114
  }
4115
+ case "css":
4116
+ storyAdd(story, { name: (0, import_node_path5.basename)(source.filename), tags: ["stylesheet"], text: content }, diagnostics);
4117
+ break;
4118
+ case "js":
4119
+ storyAdd(story, { name: (0, import_node_path5.basename)(source.filename), tags: ["script"], text: content }, diagnostics);
4120
+ break;
4121
+ default:
4122
+ diagnostics.push({
4123
+ level: "warning",
4124
+ message: `load ${source.filename}: in-memory sources of type .${ext} are not supported; skipped.`
4125
+ });
4108
4126
  }
4109
4127
  }
4110
4128
  }
@@ -4302,7 +4320,28 @@ function loadSourcesCached(story, filenames, opts, diagnostics, processedFiles,
4302
4320
  }
4303
4321
 
4304
4322
  // src/version.ts
4305
- var VERSION = "1.2.0";
4323
+ var import_node_fs5 = require("fs");
4324
+ var import_node_path6 = require("path");
4325
+ var import_node_url = require("url");
4326
+ var import_meta = {};
4327
+ var PACKAGE_NAME = "@rohal12/twee-ts";
4328
+ function findPackageVersion(startDir) {
4329
+ let dir = startDir;
4330
+ for (; ; ) {
4331
+ try {
4332
+ const pkg = JSON.parse((0, import_node_fs5.readFileSync)((0, import_node_path6.join)(dir, "package.json"), "utf-8"));
4333
+ if (pkg.name === PACKAGE_NAME && typeof pkg.version === "string") return pkg.version;
4334
+ } catch {
4335
+ }
4336
+ const parent = (0, import_node_path6.dirname)(dir);
4337
+ if (parent === dir) return void 0;
4338
+ dir = parent;
4339
+ }
4340
+ }
4341
+ function moduleDir() {
4342
+ return typeof __dirname === "string" ? __dirname : (0, import_node_path6.dirname)((0, import_node_url.fileURLToPath)(import_meta.url));
4343
+ }
4344
+ var VERSION = findPackageVersion(moduleDir()) ?? "0.0.0";
4306
4345
 
4307
4346
  // src/output-twine2.ts
4308
4347
  var CREATOR_NAME = "Twee-ts";
@@ -4392,8 +4431,8 @@ function getTwine2DataChunk(story, startName, options) {
4392
4431
  }
4393
4432
 
4394
4433
  // src/output-twine1.ts
4395
- var import_node_fs5 = require("fs");
4396
- var import_node_path6 = require("path");
4434
+ var import_node_fs6 = require("fs");
4435
+ var import_node_path7 = require("path");
4397
4436
  var CREATOR_NAME2 = "twee-ts";
4398
4437
  function toTwine1Archive(story, _startName) {
4399
4438
  const { data, count } = getTwine1PassageChunk(story);
@@ -4401,19 +4440,19 @@ function toTwine1Archive(story, _startName) {
4401
4440
  `;
4402
4441
  }
4403
4442
  function toTwine1HTML(story, format, startName) {
4404
- const formatDir = (0, import_node_path6.dirname)(format.filename);
4405
- const parentDir = (0, import_node_path6.dirname)(formatDir);
4443
+ const formatDir = (0, import_node_path7.dirname)(format.filename);
4444
+ const parentDir = (0, import_node_path7.dirname)(formatDir);
4406
4445
  let template = readFormatSource(format);
4407
4446
  const { data, count } = getTwine1PassageChunk(story);
4408
- template = tryReplaceComponent(template, '"USER_LIB"', (0, import_node_path6.join)(formatDir, "userlib.js"), false);
4409
- template = tryReplaceComponent(template, '"ENGINE"', (0, import_node_path6.join)(parentDir, "engine.js"), true);
4410
- template = tryReplaceComponent(template, '"SUGARCANE"', (0, import_node_path6.join)(formatDir, "code.js"), true);
4411
- template = tryReplaceComponent(template, '"JONAH"', (0, import_node_path6.join)(formatDir, "code.js"), true);
4447
+ template = tryReplaceComponent(template, '"USER_LIB"', (0, import_node_path7.join)(formatDir, "userlib.js"), false);
4448
+ template = tryReplaceComponent(template, '"ENGINE"', (0, import_node_path7.join)(parentDir, "engine.js"), true);
4449
+ template = tryReplaceComponent(template, '"SUGARCANE"', (0, import_node_path7.join)(formatDir, "code.js"), true);
4450
+ template = tryReplaceComponent(template, '"JONAH"', (0, import_node_path7.join)(formatDir, "code.js"), true);
4412
4451
  if (story.twine1.settings.get("jquery") === "on") {
4413
- template = tryReplaceComponent(template, '"JQUERY"', (0, import_node_path6.join)(parentDir, "jquery.js"), true);
4452
+ template = tryReplaceComponent(template, '"JQUERY"', (0, import_node_path7.join)(parentDir, "jquery.js"), true);
4414
4453
  }
4415
4454
  if (story.twine1.settings.get("modernizr") === "on") {
4416
- template = tryReplaceComponent(template, '"MODERNIZR"', (0, import_node_path6.join)(parentDir, "modernizr.js"), true);
4455
+ template = tryReplaceComponent(template, '"MODERNIZR"', (0, import_node_path7.join)(parentDir, "modernizr.js"), true);
4417
4456
  }
4418
4457
  const displayStart = startName === "Start" ? "" : startName;
4419
4458
  template = template.replace('"VERSION"', `Compiled with ${CREATOR_NAME2}, ${VERSION}`);
@@ -4426,7 +4465,7 @@ function toTwine1HTML(story, format, startName) {
4426
4465
  } else {
4427
4466
  let footer;
4428
4467
  try {
4429
- footer = (0, import_node_fs5.readFileSync)((0, import_node_path6.join)(formatDir, "footer.html"), "utf-8");
4468
+ footer = (0, import_node_fs6.readFileSync)((0, import_node_path7.join)(formatDir, "footer.html"), "utf-8");
4430
4469
  } catch {
4431
4470
  footer = "</div>\n</body>\n</html>\n";
4432
4471
  }
@@ -4456,7 +4495,7 @@ function getTwine1PassageChunk(story) {
4456
4495
  function tryReplaceComponent(template, placeholder, componentPath, required) {
4457
4496
  if (!template.includes(placeholder)) return template;
4458
4497
  try {
4459
- let content = (0, import_node_fs5.readFileSync)(componentPath, "utf-8");
4498
+ let content = (0, import_node_fs6.readFileSync)(componentPath, "utf-8");
4460
4499
  if (content.charCodeAt(0) === 65279) content = content.slice(1);
4461
4500
  return template.replace(placeholder, () => content);
4462
4501
  } catch (e) {
@@ -4553,8 +4592,8 @@ function modifyHead(html, modulePaths, headFile, diagnostics) {
4553
4592
  }
4554
4593
 
4555
4594
  // src/remote-formats.ts
4556
- var import_node_fs6 = require("fs");
4557
- var import_node_path7 = require("path");
4595
+ var import_node_fs7 = require("fs");
4596
+ var import_node_path8 = require("path");
4558
4597
  var import_node_os2 = require("os");
4559
4598
  var DEFAULT_SFA_INDICES = [
4560
4599
  "https://videlais.github.io/story-formats-archive/official/index.json",
@@ -4562,8 +4601,8 @@ var DEFAULT_SFA_INDICES = [
4562
4601
  ];
4563
4602
  function getCacheDir() {
4564
4603
  const xdg = process.env["XDG_CACHE_HOME"];
4565
- const base = xdg || (0, import_node_path7.join)((0, import_node_os2.homedir)(), ".cache");
4566
- return (0, import_node_path7.join)(base, "twee-ts", "storyformats");
4604
+ const base = xdg || (0, import_node_path8.join)((0, import_node_os2.homedir)(), ".cache");
4605
+ return (0, import_node_path8.join)(base, "twee-ts", "storyformats");
4567
4606
  }
4568
4607
  var indexCache = /* @__PURE__ */ new Map();
4569
4608
  function clearIndexCache() {
@@ -4653,10 +4692,10 @@ async function fetchAndCacheFormat(entry, downloadUrl) {
4653
4692
  throw new Error(`Failed to parse format JSON from ${downloadUrl}`);
4654
4693
  }
4655
4694
  const cacheDir = getCacheDir();
4656
- const formatDir = (0, import_node_path7.join)(cacheDir, entry.name, entry.version);
4657
- (0, import_node_fs6.mkdirSync)(formatDir, { recursive: true });
4658
- const formatPath = (0, import_node_path7.join)(formatDir, "format.js");
4659
- (0, import_node_fs6.writeFileSync)(formatPath, text, "utf-8");
4695
+ const formatDir = (0, import_node_path8.join)(cacheDir, entry.name, entry.version);
4696
+ (0, import_node_fs7.mkdirSync)(formatDir, { recursive: true });
4697
+ const formatPath = (0, import_node_path8.join)(formatDir, "format.js");
4698
+ (0, import_node_fs7.writeFileSync)(formatPath, text, "utf-8");
4660
4699
  return {
4661
4700
  id,
4662
4701
  filename: formatPath,
@@ -4679,10 +4718,10 @@ async function fetchDirectFormat(url) {
4679
4718
  }
4680
4719
  const id = makeFormatId(data.name, data.version);
4681
4720
  const cacheDir = getCacheDir();
4682
- const formatDir = (0, import_node_path7.join)(cacheDir, data.name, data.version);
4683
- (0, import_node_fs6.mkdirSync)(formatDir, { recursive: true });
4684
- const formatPath = (0, import_node_path7.join)(formatDir, "format.js");
4685
- (0, import_node_fs6.writeFileSync)(formatPath, text, "utf-8");
4721
+ const formatDir = (0, import_node_path8.join)(cacheDir, data.name, data.version);
4722
+ (0, import_node_fs7.mkdirSync)(formatDir, { recursive: true });
4723
+ const formatPath = (0, import_node_path8.join)(formatDir, "format.js");
4724
+ (0, import_node_fs7.writeFileSync)(formatPath, text, "utf-8");
4686
4725
  return {
4687
4726
  id,
4688
4727
  filename: formatPath,
@@ -4729,10 +4768,10 @@ async function resolveRemoteFormat(name, version, indices, urls) {
4729
4768
  return void 0;
4730
4769
  }
4731
4770
  function getCachedFormat(name, version) {
4732
- const formatPath = (0, import_node_path7.join)(getCacheDir(), name, version, "format.js");
4771
+ const formatPath = (0, import_node_path8.join)(getCacheDir(), name, version, "format.js");
4733
4772
  try {
4734
- if (!(0, import_node_fs6.existsSync)(formatPath)) return void 0;
4735
- const source = (0, import_node_fs6.readFileSync)(formatPath, "utf-8");
4773
+ if (!(0, import_node_fs7.existsSync)(formatPath)) return void 0;
4774
+ const source = (0, import_node_fs7.readFileSync)(formatPath, "utf-8");
4736
4775
  const id = makeFormatId(name, version);
4737
4776
  const data = parseFormatJSON(source, id);
4738
4777
  if (!data) return void 0;
@@ -4752,26 +4791,26 @@ function discoverCachedFormats() {
4752
4791
  const formats = /* @__PURE__ */ new Map();
4753
4792
  const cacheDir = getCacheDir();
4754
4793
  try {
4755
- if (!(0, import_node_fs6.existsSync)(cacheDir)) return formats;
4794
+ if (!(0, import_node_fs7.existsSync)(cacheDir)) return formats;
4756
4795
  } catch {
4757
4796
  return formats;
4758
4797
  }
4759
4798
  let names;
4760
4799
  try {
4761
- names = (0, import_node_fs6.readdirSync)(cacheDir);
4800
+ names = (0, import_node_fs7.readdirSync)(cacheDir);
4762
4801
  } catch {
4763
4802
  return formats;
4764
4803
  }
4765
4804
  for (const name of names) {
4766
- const nameDir = (0, import_node_path7.join)(cacheDir, name);
4805
+ const nameDir = (0, import_node_path8.join)(cacheDir, name);
4767
4806
  try {
4768
- if (!(0, import_node_fs6.statSync)(nameDir).isDirectory()) continue;
4807
+ if (!(0, import_node_fs7.statSync)(nameDir).isDirectory()) continue;
4769
4808
  } catch {
4770
4809
  continue;
4771
4810
  }
4772
4811
  let versions;
4773
4812
  try {
4774
- versions = (0, import_node_fs6.readdirSync)(nameDir);
4813
+ versions = (0, import_node_fs7.readdirSync)(nameDir);
4775
4814
  } catch {
4776
4815
  continue;
4777
4816
  }
@@ -4788,33 +4827,33 @@ function listCachedFormats() {
4788
4827
  const cacheDir = getCacheDir();
4789
4828
  const entries = [];
4790
4829
  try {
4791
- if (!(0, import_node_fs6.existsSync)(cacheDir)) return entries;
4830
+ if (!(0, import_node_fs7.existsSync)(cacheDir)) return entries;
4792
4831
  } catch {
4793
4832
  return entries;
4794
4833
  }
4795
4834
  let names;
4796
4835
  try {
4797
- names = (0, import_node_fs6.readdirSync)(cacheDir);
4836
+ names = (0, import_node_fs7.readdirSync)(cacheDir);
4798
4837
  } catch {
4799
4838
  return entries;
4800
4839
  }
4801
4840
  for (const name of names) {
4802
- const nameDir = (0, import_node_path7.join)(cacheDir, name);
4841
+ const nameDir = (0, import_node_path8.join)(cacheDir, name);
4803
4842
  try {
4804
- if (!(0, import_node_fs6.statSync)(nameDir).isDirectory()) continue;
4843
+ if (!(0, import_node_fs7.statSync)(nameDir).isDirectory()) continue;
4805
4844
  } catch {
4806
4845
  continue;
4807
4846
  }
4808
4847
  let versions;
4809
4848
  try {
4810
- versions = (0, import_node_fs6.readdirSync)(nameDir);
4849
+ versions = (0, import_node_fs7.readdirSync)(nameDir);
4811
4850
  } catch {
4812
4851
  continue;
4813
4852
  }
4814
4853
  for (const version of versions) {
4815
- const formatPath = (0, import_node_path7.join)(nameDir, version, "format.js");
4854
+ const formatPath = (0, import_node_path8.join)(nameDir, version, "format.js");
4816
4855
  try {
4817
- const stat = (0, import_node_fs6.statSync)(formatPath);
4856
+ const stat = (0, import_node_fs7.statSync)(formatPath);
4818
4857
  entries.push({
4819
4858
  name,
4820
4859
  version,
@@ -4829,23 +4868,23 @@ function listCachedFormats() {
4829
4868
  }
4830
4869
  function clearCachedFormats(name) {
4831
4870
  const cacheDir = getCacheDir();
4832
- if (!(0, import_node_fs6.existsSync)(cacheDir)) return 0;
4871
+ if (!(0, import_node_fs7.existsSync)(cacheDir)) return 0;
4833
4872
  if (!name) {
4834
4873
  const entries = listCachedFormats();
4835
4874
  const count2 = entries.length;
4836
- (0, import_node_fs6.rmSync)(cacheDir, { recursive: true, force: true });
4875
+ (0, import_node_fs7.rmSync)(cacheDir, { recursive: true, force: true });
4837
4876
  return count2;
4838
4877
  }
4839
- const nameDir = (0, import_node_path7.join)(cacheDir, name);
4840
- if (!(0, import_node_fs6.existsSync)(nameDir)) return 0;
4878
+ const nameDir = (0, import_node_path8.join)(cacheDir, name);
4879
+ if (!(0, import_node_fs7.existsSync)(nameDir)) return 0;
4841
4880
  let versions;
4842
4881
  try {
4843
- versions = (0, import_node_fs6.readdirSync)(nameDir);
4882
+ versions = (0, import_node_fs7.readdirSync)(nameDir);
4844
4883
  } catch {
4845
4884
  return 0;
4846
4885
  }
4847
4886
  const count = versions.length;
4848
- (0, import_node_fs6.rmSync)(nameDir, { recursive: true, force: true });
4887
+ (0, import_node_fs7.rmSync)(nameDir, { recursive: true, force: true });
4849
4888
  return count;
4850
4889
  }
4851
4890
  function getCacheSize() {
@@ -4870,7 +4909,7 @@ async function compile(options) {
4870
4909
  }
4871
4910
  async function compileToFile(options) {
4872
4911
  const result = await compile(options);
4873
- (0, import_node_fs7.writeFileSync)(options.outFile, result.output, "utf-8");
4912
+ (0, import_node_fs8.writeFileSync)(options.outFile, result.output, "utf-8");
4874
4913
  return result;
4875
4914
  }
4876
4915
  async function compileIncremental(options, cache, changedFiles) {
@@ -4884,7 +4923,7 @@ async function watch(options) {
4884
4923
  const allPaths = [...filePaths, ...modulePaths];
4885
4924
  const handle = watchFilesystem(allPaths, options.outFile, (changedFiles) => {
4886
4925
  buildOutput(options, cache, changedFiles).then((result) => {
4887
- (0, import_node_fs7.writeFileSync)(options.outFile, result.output, "utf-8");
4926
+ (0, import_node_fs8.writeFileSync)(options.outFile, result.output, "utf-8");
4888
4927
  options.onBuild?.(result);
4889
4928
  }).catch((e) => {
4890
4929
  options.onError?.(e instanceof Error ? e : new Error(String(e)));
@@ -5240,18 +5279,18 @@ function formatLintReport(result) {
5240
5279
  }
5241
5280
 
5242
5281
  // src/config.ts
5243
- var import_node_fs8 = require("fs");
5244
- var import_node_path8 = require("path");
5282
+ var import_node_fs9 = require("fs");
5283
+ var import_node_path9 = require("path");
5245
5284
  var CONFIG_FILENAME = "twee-ts.config.json";
5246
5285
  var VALID_OUTPUT_MODES = ["html", "twee3", "twee1", "twine2-archive", "twine1-archive", "json"];
5247
5286
  var VALID_WORD_COUNT_METHODS = ["tweego", "whitespace"];
5248
5287
  function loadConfig(dir) {
5249
5288
  const base = dir ?? process.cwd();
5250
- const configPath = (0, import_node_path8.join)(base, CONFIG_FILENAME);
5251
- if (!(0, import_node_fs8.existsSync)(configPath)) return null;
5289
+ const configPath = (0, import_node_path9.join)(base, CONFIG_FILENAME);
5290
+ if (!(0, import_node_fs9.existsSync)(configPath)) return null;
5252
5291
  let raw;
5253
5292
  try {
5254
- raw = (0, import_node_fs8.readFileSync)(configPath, "utf-8");
5293
+ raw = (0, import_node_fs9.readFileSync)(configPath, "utf-8");
5255
5294
  } catch {
5256
5295
  return null;
5257
5296
  }
@@ -5271,7 +5310,7 @@ function loadConfig(dir) {
5271
5310
  function loadConfigFile(filePath) {
5272
5311
  let raw;
5273
5312
  try {
5274
- raw = (0, import_node_fs8.readFileSync)(filePath, "utf-8");
5313
+ raw = (0, import_node_fs9.readFileSync)(filePath, "utf-8");
5275
5314
  } catch (e) {
5276
5315
  throw new Error(`Cannot read config file ${filePath}: ${e instanceof Error ? e.message : String(e)}`);
5277
5316
  }