@textadventures/squiffy-cli 6.0.1 → 7.0.0-beta.1

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.
@@ -0,0 +1,8 @@
1
+ import type { CompileSuccess } from "squiffy-compiler";
2
+ /**
3
+ * Finds the asset files referenced by a story: anything in the story text or scripts that looks like a
4
+ * relative path to an asset file (by extension), and exists in the story's folder (or below it).
5
+ * Returns a map of path (relative to the story folder, with "/" separators) to bytes.
6
+ */
7
+ export declare const findAssets: (inputFilename: string, storyText: string) => Record<string, Uint8Array>;
8
+ export declare const getStoryText: (result: CompileSuccess) => string;
package/dist/assets.js ADDED
@@ -0,0 +1,63 @@
1
+ import * as fs from "fs";
2
+ import * as path from "path";
3
+ // File types that we look for references to in a story. Anything else (.squiffy, .js, .css, .html etc.)
4
+ // is either part of the story source or something we'd be guessing about.
5
+ const assetExtensions = [
6
+ "apng", "avif", "bmp", "gif", "ico", "jpeg", "jpg", "png", "svg", "webp",
7
+ "aac", "flac", "m4a", "mp3", "oga", "ogg", "opus", "wav", "weba",
8
+ "m4v", "mp4", "ogv", "webm", "vtt",
9
+ "otf", "ttf", "woff", "woff2",
10
+ "pdf",
11
+ ];
12
+ // Matches things that look like a relative path to an asset file, e.g. "cat.png", "images/cat.png",
13
+ // "./sounds/meow.mp3" or "my%20cat.png". Paths containing spaces aren't matched, so use %20 for those.
14
+ const assetPathPattern = new RegExp(String.raw `(?<![\w%./:\\-])((?:\.\/)?[\w%.-]+(?:\/[\w%.-]+)*\.(?:${assetExtensions.join("|")}))(?![\w%/-])`, "gi");
15
+ /**
16
+ * Finds the asset files referenced by a story: anything in the story text or scripts that looks like a
17
+ * relative path to an asset file (by extension), and exists in the story's folder (or below it).
18
+ * Returns a map of path (relative to the story folder, with "/" separators) to bytes.
19
+ */
20
+ export const findAssets = (inputFilename, storyText) => {
21
+ const basePath = path.resolve(path.dirname(inputFilename));
22
+ const result = {};
23
+ for (const match of storyText.matchAll(assetPathPattern)) {
24
+ let reference = match[1].replace(/^(\.\/)+/, "");
25
+ try {
26
+ reference = decodeURI(reference);
27
+ }
28
+ catch {
29
+ // Not valid percent-encoding - use it as it is
30
+ }
31
+ if (reference in result)
32
+ continue;
33
+ const filename = path.resolve(basePath, reference);
34
+ const relative = path.relative(basePath, filename);
35
+ if (!relative || relative.startsWith("..") || path.isAbsolute(relative))
36
+ continue;
37
+ let stat;
38
+ try {
39
+ stat = fs.statSync(filename);
40
+ }
41
+ catch {
42
+ continue;
43
+ }
44
+ if (!stat.isFile())
45
+ continue;
46
+ result[relative.split(path.sep).join("/")] = fs.readFileSync(filename);
47
+ }
48
+ return result;
49
+ };
50
+ // All the text and script in a compiled story, which is where we look for asset references.
51
+ export const getStoryText = (result) => {
52
+ const texts = [];
53
+ for (const section of Object.values(result.output.story.sections)) {
54
+ texts.push(section.text ?? "");
55
+ for (const passage of Object.values(section.passages ?? {})) {
56
+ texts.push(passage.text ?? "");
57
+ }
58
+ }
59
+ for (const js of result.output.js) {
60
+ texts.push(...js);
61
+ }
62
+ return texts.join("\n");
63
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,73 @@
1
+ import { describe, test, expect, beforeEach, afterEach, vi } from "vitest";
2
+ import { unzipSync } from "fflate";
3
+ import { findAssets } from "./assets.js";
4
+ import { createPackageFiles } from "./file-packager.js";
5
+ import fs from "fs/promises";
6
+ import path from "path";
7
+ import { fileURLToPath } from "url";
8
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
9
+ const TEST_DIR = path.join(__dirname, "..", "test-fixtures-assets");
10
+ const STORY_DIR = path.join(TEST_DIR, "story");
11
+ const storyFile = path.join(STORY_DIR, "story.squiffy");
12
+ const pngBytes = new Uint8Array([0x89, 0x50, 0x4e, 0x47]);
13
+ const writeFile = async (relativePath, content = pngBytes) => {
14
+ const filename = path.join(STORY_DIR, relativePath);
15
+ await fs.mkdir(path.dirname(filename), { recursive: true });
16
+ await fs.writeFile(filename, content);
17
+ };
18
+ beforeEach(async () => {
19
+ await fs.mkdir(STORY_DIR, { recursive: true });
20
+ vi.spyOn(console, "log").mockImplementation(() => { });
21
+ });
22
+ afterEach(async () => {
23
+ vi.restoreAllMocks();
24
+ await fs.rm(TEST_DIR, { recursive: true, force: true });
25
+ });
26
+ describe("findAssets", () => {
27
+ test("finds referenced asset files in the story folder", async () => {
28
+ await writeFile("cat.png");
29
+ await writeFile("images/dog.jpg");
30
+ await writeFile("sounds/meow.mp3");
31
+ await writeFile("my cat.png");
32
+ await writeFile("unused.png");
33
+ const assets = findAssets(storyFile, `![A cat](cat.png)
34
+ <img src="./images/dog.jpg">
35
+ squiffy.set("sound", "sounds/meow.mp3");
36
+ <img src="my%20cat.png">`);
37
+ expect(Object.keys(assets).sort()).toEqual(["cat.png", "images/dog.jpg", "my cat.png", "sounds/meow.mp3"]);
38
+ expect(new Uint8Array(assets["cat.png"])).toEqual(pngBytes);
39
+ });
40
+ test("ignores missing files, absolute URLs, non-asset files and files outside the story folder", async () => {
41
+ await writeFile("cat.png");
42
+ await writeFile("notes.txt", "notes");
43
+ await fs.writeFile(path.join(TEST_DIR, "outside.png"), pngBytes);
44
+ const assets = findAssets(storyFile, `missing.png
45
+ https://example.com/cat.png
46
+ //example.com/cat.png
47
+ notes.txt
48
+ ../outside.png
49
+ story.squiffy`);
50
+ expect(assets).toEqual({});
51
+ });
52
+ });
53
+ describe("createPackageFiles with assets", () => {
54
+ const story = `@title Assets
55
+
56
+ ![A cat](cat.png)`;
57
+ test("adds referenced assets to the zip", async () => {
58
+ await writeFile("story.squiffy", story);
59
+ await writeFile("cat.png");
60
+ await writeFile("unused.png");
61
+ expect(await createPackageFiles(storyFile, STORY_DIR, { createZip: true })).toBe(true);
62
+ const zip = unzipSync(new Uint8Array(await fs.readFile(path.join(STORY_DIR, "output.zip"))));
63
+ expect(Object.keys(zip).sort()).toEqual(["cat.png", "index.html", "squiffy.runtime.global.js", "story.js", "style.css"]);
64
+ expect(zip["cat.png"]).toEqual(pngBytes);
65
+ });
66
+ test("embeds referenced assets in inline HTML", async () => {
67
+ await writeFile("story.squiffy", story);
68
+ await writeFile("cat.png");
69
+ expect(await createPackageFiles(storyFile, STORY_DIR, { inlineHtml: true })).toBe(true);
70
+ const html = await fs.readFile(path.join(STORY_DIR, "index.html"), "utf8");
71
+ expect(html).toContain('"cat.png":"data:image/png;base64,iVBORw=="');
72
+ });
73
+ });
@@ -1,7 +1,8 @@
1
1
  import * as fs from "fs";
2
2
  import * as path from "path";
3
- import { compile, findV5Syntax } from "squiffy-compiler";
3
+ import { compile, findV5Syntax, formatDiagnostic } from "squiffy-compiler";
4
4
  import { externalFiles } from "./external-files.js";
5
+ import { findAssets, getStoryText } from "./assets.js";
5
6
  import { createPackage } from "@textadventures/squiffy-packager";
6
7
  async function getCompileResult(inputFilename) {
7
8
  const inputFile = fs.readFileSync(inputFilename);
@@ -9,13 +10,21 @@ async function getCompileResult(inputFilename) {
9
10
  if (findV5Syntax(inputText).length > 0) {
10
11
  console.warn("This story looks like it uses Squiffy 5 text processor syntax. Run with --convert-v5 to convert it to Squiffy 6.");
11
12
  }
12
- return await compile({
13
+ const result = await compile({
13
14
  scriptBaseFilename: path.basename(inputFilename),
14
15
  script: inputText,
15
- onWarning: console.warn,
16
16
  externalFiles: externalFiles(inputFilename),
17
17
  globalJs: true,
18
18
  });
19
+ for (const diagnostic of result.diagnostics) {
20
+ if (diagnostic.severity == "error") {
21
+ console.error(formatDiagnostic(diagnostic));
22
+ }
23
+ else {
24
+ console.warn(formatDiagnostic(diagnostic));
25
+ }
26
+ }
27
+ return result;
19
28
  }
20
29
  export const writeScriptFile = async (inputFilename, outputPath, outputFilename) => {
21
30
  console.log("Loading " + inputFilename);
@@ -34,7 +43,14 @@ export const createPackageFiles = async (inputFilename, outputPath, options = {}
34
43
  console.log("Failed.");
35
44
  return false;
36
45
  }
37
- const pkg = await createPackage(result, options);
46
+ // Asset files (images etc.) referenced by the story. The other package files are written to the
47
+ // story's folder, alongside these, so they only need to be passed on for the zip or for inlining.
48
+ const assets = options.assets ?? findAssets(inputFilename, getStoryText(result));
49
+ const assetPaths = Object.keys(assets);
50
+ if (assetPaths.length > 0 && (options.createZip || options.inlineHtml)) {
51
+ console.log(`Including asset${assetPaths.length == 1 ? "" : "s"}: ${assetPaths.join(", ")}`);
52
+ }
53
+ const pkg = await createPackage(result, { ...options, assets });
38
54
  const files = pkg.files;
39
55
  for (const file of Object.keys(files)) {
40
56
  console.log(`Writing ${file}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@textadventures/squiffy-cli",
3
- "version": "6.0.1",
3
+ "version": "7.0.0-beta.1",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/textadventures/squiffy.git",
@@ -35,26 +35,27 @@
35
35
  "squiffy-packager": "dist/squiffy.js"
36
36
  },
37
37
  "dependencies": {
38
- "@textadventures/squiffy-packager": "^6.0.1",
38
+ "@textadventures/squiffy-packager": "^7.0.0-beta.1",
39
39
  "finalhandler": "^2.1.0",
40
- "glob": "^13.0.6",
40
+ "glob": "^14.0.2",
41
41
  "serve-static": "^2.2.0",
42
- "squiffy-compiler": "^6.0.1",
43
- "squiffy-runtime": "^6.0.1",
42
+ "squiffy-compiler": "^7.0.0-beta.1",
43
+ "squiffy-runtime": "^7.0.0-beta.1",
44
44
  "yargs": "^18.2.0"
45
45
  },
46
46
  "devDependencies": {
47
47
  "@types/finalhandler": "^1.2.4",
48
- "@types/node": "^26.6.5",
48
+ "@types/node": "^26.6.6",
49
49
  "@types/serve-static": "^2.2.0",
50
50
  "@types/yargs": "^17.0.33",
51
+ "fflate": "^0.8.3",
51
52
  "tsx": "^4.23.15",
52
53
  "typescript": "^6.0.3",
53
- "vitest": "^5.0.4"
54
+ "vitest": "^5.0.5"
54
55
  },
55
56
  "files": [
56
57
  "dist"
57
58
  ],
58
59
  "type": "module",
59
- "gitHead": "e5186d77e730d0f055091299197c13edfe08c92e"
60
+ "gitHead": "558b8c3c0a4ce8d7a2e4eb0e9a439ecfc4c53a62"
60
61
  }