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

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.
@@ -1,3 +1,4 @@
1
+ export declare const sortByRelativePath: (basePath: string, filenames: string[]) => string[];
1
2
  export declare const externalFiles: (inputFilename: string) => {
2
3
  getMatchingFilenames: (pattern: string) => Promise<string[]>;
3
4
  getContent: (filename: string) => Promise<string>;
@@ -1,6 +1,16 @@
1
1
  import { glob } from "glob";
2
2
  import path from "path";
3
3
  import fs from "fs/promises";
4
+ // glob doesn't return matches in a guaranteed order, but the order decides which
5
+ // same-named section wins and how continue sections are numbered. So sort them,
6
+ // like the editor does, by their path relative to the story's folder.
7
+ export const sortByRelativePath = (basePath, filenames) => {
8
+ const relative = (filename) => path.relative(basePath, filename).split(path.sep).join("/");
9
+ return filenames
10
+ .map(filename => ({ filename, key: relative(filename) }))
11
+ .sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0)
12
+ .map(f => f.filename);
13
+ };
4
14
  export const externalFiles = (inputFilename) => {
5
15
  const includedFiles = [path.resolve(inputFilename)];
6
16
  const basePath = path.resolve(path.dirname(inputFilename));
@@ -8,7 +18,7 @@ export const externalFiles = (inputFilename) => {
8
18
  getMatchingFilenames: async (pattern) => {
9
19
  const filenames = path.join(basePath, pattern);
10
20
  const result = await glob(filenames);
11
- return result.filter((filename) => !includedFiles.includes(filename));
21
+ return sortByRelativePath(basePath, result.filter((filename) => !includedFiles.includes(filename)));
12
22
  },
13
23
  getContent: async (filename) => {
14
24
  // Resolve relative paths against the base path
@@ -1,3 +1,4 @@
1
+ import type { StoryOnDisk } from "squiffy-compiler/node";
1
2
  import { PackageOptions } from "@textadventures/squiffy-packager";
2
- export declare const writeScriptFile: (inputFilename: string, outputPath: string, outputFilename: string) => Promise<false | undefined>;
3
- export declare const createPackageFiles: (inputFilename: string, outputPath: string, options?: PackageOptions) => Promise<boolean>;
3
+ export declare const writeScriptFile: (input: string | StoryOnDisk, outputPath: string, outputFilename: string) => Promise<false | undefined>;
4
+ export declare const createPackageFiles: (input: string | StoryOnDisk, outputPath: string, options?: PackageOptions) => Promise<boolean>;
@@ -4,7 +4,10 @@ import { compile, findV5Syntax, formatDiagnostic } from "squiffy-compiler";
4
4
  import { externalFiles } from "./external-files.js";
5
5
  import { findAssets, getStoryText } from "./assets.js";
6
6
  import { createPackage } from "@textadventures/squiffy-packager";
7
- async function getCompileResult(inputFilename) {
7
+ // A story given as a filename is just that file and its @imports, as with no squiffy.json
8
+ const toStory = (input) => typeof input == "string" ? { mainFile: input } : input;
9
+ async function getCompileResult(story) {
10
+ const inputFilename = story.mainFile;
8
11
  const inputFile = fs.readFileSync(inputFilename);
9
12
  const inputText = inputFile.toString();
10
13
  if (findV5Syntax(inputText).length > 0) {
@@ -14,6 +17,7 @@ async function getCompileResult(inputFilename) {
14
17
  scriptBaseFilename: path.basename(inputFilename),
15
18
  script: inputText,
16
19
  externalFiles: externalFiles(inputFilename),
20
+ storyFiles: story.storyFiles,
17
21
  globalJs: true,
18
22
  });
19
23
  for (const diagnostic of result.diagnostics) {
@@ -26,9 +30,15 @@ async function getCompileResult(inputFilename) {
26
30
  }
27
31
  return result;
28
32
  }
29
- export const writeScriptFile = async (inputFilename, outputPath, outputFilename) => {
30
- console.log("Loading " + inputFilename);
31
- const result = await getCompileResult(inputFilename);
33
+ const logLoading = (story) => {
34
+ if (story.note)
35
+ console.log(story.note);
36
+ console.log("Loading " + (story.projectFile ?? story.mainFile));
37
+ };
38
+ export const writeScriptFile = async (input, outputPath, outputFilename) => {
39
+ const story = toStory(input);
40
+ logLoading(story);
41
+ const result = await getCompileResult(story);
32
42
  if (!result.success) {
33
43
  console.log("Failed.");
34
44
  return false;
@@ -36,9 +46,11 @@ export const writeScriptFile = async (inputFilename, outputPath, outputFilename)
36
46
  console.log(`Writing ${outputFilename}`);
37
47
  fs.writeFileSync(path.join(outputPath, outputFilename), await result.getJs());
38
48
  };
39
- export const createPackageFiles = async (inputFilename, outputPath, options = {}) => {
40
- console.log("Loading " + inputFilename);
41
- const result = await getCompileResult(inputFilename);
49
+ export const createPackageFiles = async (input, outputPath, options = {}) => {
50
+ const story = toStory(input);
51
+ const inputFilename = story.mainFile;
52
+ logLoading(story);
53
+ const result = await getCompileResult(story);
42
54
  if (!result.success) {
43
55
  console.log("Failed.");
44
56
  return false;
@@ -1,6 +1,7 @@
1
1
  import { describe, test, expect, beforeEach, afterEach } from "vitest";
2
2
  import { writeScriptFile, createPackageFiles } from "./file-packager.js";
3
3
  import { externalFiles } from "./external-files.js";
4
+ import { findStory } from "squiffy-compiler/node";
4
5
  import fs from "fs/promises";
5
6
  import path from "path";
6
7
  import { fileURLToPath } from "url";
@@ -147,6 +148,17 @@ describe("external-files", () => {
147
148
  expect(matches.some(f => f.includes("test2.txt"))).toBe(true);
148
149
  expect(matches.some(f => f.includes("other.md"))).toBe(false);
149
150
  });
151
+ test("getMatchingFilenames returns matches sorted by relative path", async () => {
152
+ await fs.mkdir(path.join(TEST_DIR, "b"), { recursive: true });
153
+ for (const name of ["c.txt", "a.txt", "B.txt", "b/a.txt", "a-b.txt"]) {
154
+ await fs.writeFile(path.join(TEST_DIR, name), name);
155
+ }
156
+ const ext = externalFiles(path.join(TEST_DIR, "main.squiffy"));
157
+ const matches = await ext.getMatchingFilenames("**/*.txt");
158
+ // The same plain string sort on "/"-separated relative paths as the editor
159
+ expect(matches.map(f => ext.getLocalFilename(f).split(path.sep).join("/")))
160
+ .toEqual(["B.txt", "a-b.txt", "a.txt", "b/a.txt", "c.txt"]);
161
+ });
150
162
  test("getContent reads file content", async () => {
151
163
  const contentFile = path.join(TEST_DIR, "content.txt");
152
164
  await fs.writeFile(contentFile, "Hello from external file!");
@@ -176,4 +188,17 @@ describe("external-files", () => {
176
188
  expect(matches.length).toBe(1);
177
189
  expect(matches[0]).toContain("notincluded.txt");
178
190
  });
191
+ test("a folder with a squiffy.json builds every .squiffy file in it", async () => {
192
+ await fs.writeFile(path.join(TEST_DIR, "squiffy.json"), "{ \"version\": 1, \"main\": \"story.squiffy\", \"exclude\": [\"drafts/**\"] }");
193
+ await fs.writeFile(path.join(TEST_DIR, "story.squiffy"), "@title Folder\n[[start]]:\nGo to [[chapter]].\n");
194
+ await fs.mkdir(path.join(TEST_DIR, "chapters"), { recursive: true });
195
+ await fs.writeFile(path.join(TEST_DIR, "chapters", "one.squiffy"), "[[chapter]]:\nFrom another file.\n");
196
+ await fs.mkdir(path.join(TEST_DIR, "drafts"), { recursive: true });
197
+ await fs.writeFile(path.join(TEST_DIR, "drafts", "draft.squiffy"), "[[draft]]:\nNot in the story.\n");
198
+ const story = await findStory(TEST_DIR);
199
+ expect(await writeScriptFile(story, OUTPUT_DIR, "story.js")).not.toBe(false);
200
+ const storyContent = await fs.readFile(path.join(OUTPUT_DIR, "story.js"), "utf8");
201
+ expect(storyContent).toContain("From another file.");
202
+ expect(storyContent).not.toContain("Not in the story.");
203
+ });
179
204
  });
package/dist/squiffy.js CHANGED
@@ -4,12 +4,13 @@ import { hideBin } from "yargs/helpers";
4
4
  import { serve } from "./server.js";
5
5
  import { createPackageFiles, writeScriptFile } from "./file-packager.js";
6
6
  import { convertV5File } from "./convert-v5.js";
7
+ import { findStory, StoryNotFoundError } from "squiffy-compiler/node";
7
8
  import pkg from "../package.json" with { type: "json" };
8
9
  import path from "path";
9
10
  const version = pkg.version;
10
11
  const argv = yargs(hideBin(process.argv))
11
- .usage("Usage: npx @textadventures/squiffy-cli filename.squiffy [options]")
12
- .demand(1)
12
+ .usage("Usage: npx @textadventures/squiffy-cli [filename.squiffy | folder] [options]\n\n" +
13
+ "With a folder (or nothing, for the current folder), builds the story in its squiffy.json.")
13
14
  .alias("s", "serve")
14
15
  .alias("p", "port")
15
16
  .describe("s", "Start HTTP server after compiling")
@@ -28,19 +29,36 @@ const options = {
28
29
  zip: argv.zip,
29
30
  inline: argv.inline
30
31
  };
31
- const inputFilename = argv._[0];
32
- const outputPath = path.resolve(path.dirname(inputFilename));
32
+ const input = argv._[0] === undefined ? "." : String(argv._[0]);
33
+ const findStoryOrExit = async () => {
34
+ try {
35
+ return await findStory(input);
36
+ }
37
+ catch (e) {
38
+ if (!(e instanceof StoryNotFoundError))
39
+ throw e;
40
+ console.error(e.message);
41
+ process.exit(1);
42
+ }
43
+ };
33
44
  if (argv.convertV5) {
34
- if (!convertV5File(inputFilename)) {
45
+ if (argv._[0] === undefined) {
46
+ console.error("Give the .squiffy file to convert.");
47
+ process.exitCode = 1;
48
+ }
49
+ else if (!convertV5File(input)) {
35
50
  process.exitCode = 1;
36
51
  }
37
52
  }
38
53
  else if (options.scriptOnly) {
54
+ const story = await findStoryOrExit();
39
55
  const outputFilename = typeof options.scriptOnly === "string" ? options.scriptOnly : "story.js";
40
- await writeScriptFile(inputFilename, outputPath, outputFilename);
56
+ await writeScriptFile(story, path.dirname(story.mainFile), outputFilename);
41
57
  }
42
58
  else {
43
- const result = await createPackageFiles(inputFilename, outputPath, {
59
+ const story = await findStoryOrExit();
60
+ const outputPath = path.dirname(story.mainFile);
61
+ const result = await createPackageFiles(story, outputPath, {
44
62
  createZip: !!options.zip,
45
63
  inlineHtml: !!options.inline
46
64
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@textadventures/squiffy-cli",
3
- "version": "7.0.0-beta.1",
3
+ "version": "7.0.0-beta.3",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/textadventures/squiffy.git",
@@ -35,12 +35,12 @@
35
35
  "squiffy-packager": "dist/squiffy.js"
36
36
  },
37
37
  "dependencies": {
38
- "@textadventures/squiffy-packager": "^7.0.0-beta.1",
38
+ "@textadventures/squiffy-packager": "^7.0.0-beta.3",
39
39
  "finalhandler": "^2.1.0",
40
- "glob": "^14.0.2",
40
+ "glob": "^14.0.6",
41
41
  "serve-static": "^2.2.0",
42
- "squiffy-compiler": "^7.0.0-beta.1",
43
- "squiffy-runtime": "^7.0.0-beta.1",
42
+ "squiffy-compiler": "^7.0.0-beta.3",
43
+ "squiffy-runtime": "^7.0.0-beta.3",
44
44
  "yargs": "^18.2.0"
45
45
  },
46
46
  "devDependencies": {
@@ -51,11 +51,11 @@
51
51
  "fflate": "^0.8.3",
52
52
  "tsx": "^4.23.15",
53
53
  "typescript": "^6.0.3",
54
- "vitest": "^5.0.5"
54
+ "vitest": "^5.1.0"
55
55
  },
56
56
  "files": [
57
57
  "dist"
58
58
  ],
59
59
  "type": "module",
60
- "gitHead": "558b8c3c0a4ce8d7a2e4eb0e9a439ecfc4c53a62"
60
+ "gitHead": "016f53cc52dc2cf504787ef8329740ca2cf8d481"
61
61
  }