@textadventures/squiffy-cli 7.0.0-beta.4 → 7.0.0-beta.5
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/assets.d.ts +5 -0
- package/dist/assets.js +31 -14
- package/dist/assets.test.js +27 -0
- package/dist/convert-twine.d.ts +2 -0
- package/dist/convert-twine.js +44 -0
- package/dist/convert-twine.test.d.ts +1 -0
- package/dist/convert-twine.test.js +62 -0
- package/dist/file-packager.js +8 -4
- package/dist/squiffy.js +16 -1
- package/package.json +5 -6
- package/LICENSE +0 -22
package/dist/assets.d.ts
CHANGED
|
@@ -5,4 +5,9 @@ import type { CompileSuccess } from "squiffy-compiler";
|
|
|
5
5
|
* Returns a map of path (relative to the story folder, with "/" separators) to bytes.
|
|
6
6
|
*/
|
|
7
7
|
export declare const findAssets: (inputFilename: string, storyText: string) => Record<string, Uint8Array>;
|
|
8
|
+
/**
|
|
9
|
+
* Reads the story's own stylesheets and scripts (@import of .css and .js files), which the compiler
|
|
10
|
+
* names relative to the story folder, so they can go in the zip or be included in a single HTML file.
|
|
11
|
+
*/
|
|
12
|
+
export declare const findImportedFiles: (inputFilename: string, result: CompileSuccess) => Record<string, Uint8Array>;
|
|
8
13
|
export declare const getStoryText: (result: CompileSuccess) => string;
|
package/dist/assets.js
CHANGED
|
@@ -30,23 +30,40 @@ export const findAssets = (inputFilename, storyText) => {
|
|
|
30
30
|
}
|
|
31
31
|
if (reference in result)
|
|
32
32
|
continue;
|
|
33
|
-
|
|
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);
|
|
33
|
+
readFileInFolder(basePath, reference, result);
|
|
47
34
|
}
|
|
48
35
|
return result;
|
|
49
36
|
};
|
|
37
|
+
// Adds a file to result, by its path relative to basePath, if it's a file in that folder (or below it)
|
|
38
|
+
const readFileInFolder = (basePath, reference, result) => {
|
|
39
|
+
const filename = path.resolve(basePath, reference);
|
|
40
|
+
const relative = path.relative(basePath, filename);
|
|
41
|
+
if (!relative || relative.startsWith("..") || path.isAbsolute(relative))
|
|
42
|
+
return;
|
|
43
|
+
let stat;
|
|
44
|
+
try {
|
|
45
|
+
stat = fs.statSync(filename);
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
if (!stat.isFile())
|
|
51
|
+
return;
|
|
52
|
+
result[relative.split(path.sep).join("/")] = fs.readFileSync(filename);
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Reads the story's own stylesheets and scripts (@import of .css and .js files), which the compiler
|
|
56
|
+
* names relative to the story folder, so they can go in the zip or be included in a single HTML file.
|
|
57
|
+
*/
|
|
58
|
+
export const findImportedFiles = (inputFilename, result) => {
|
|
59
|
+
const basePath = path.resolve(path.dirname(inputFilename));
|
|
60
|
+
const { externalStylesheets, externalScripts } = result.getUiInfo();
|
|
61
|
+
const files = {};
|
|
62
|
+
for (const reference of [...externalStylesheets, ...externalScripts]) {
|
|
63
|
+
readFileInFolder(basePath, reference, files);
|
|
64
|
+
}
|
|
65
|
+
return files;
|
|
66
|
+
};
|
|
50
67
|
// All the text and script in a compiled story, which is where we look for asset references.
|
|
51
68
|
export const getStoryText = (result) => {
|
|
52
69
|
const texts = [];
|
package/dist/assets.test.js
CHANGED
|
@@ -70,4 +70,31 @@ describe("createPackageFiles with assets", () => {
|
|
|
70
70
|
const html = await fs.readFile(path.join(STORY_DIR, "index.html"), "utf8");
|
|
71
71
|
expect(html).toContain('"cat.png":"data:image/png;base64,iVBORw=="');
|
|
72
72
|
});
|
|
73
|
+
const importingStory = `@title Imports
|
|
74
|
+
@import my_style.css
|
|
75
|
+
@import my_script.js
|
|
76
|
+
|
|
77
|
+
`;
|
|
78
|
+
test("includes the story's own stylesheets and scripts in inline HTML", async () => {
|
|
79
|
+
await writeFile("story.squiffy", importingStory);
|
|
80
|
+
await writeFile("cat.png");
|
|
81
|
+
await writeFile("my_style.css", "#squiffy { color: rebeccapurple; }");
|
|
82
|
+
await writeFile("my_script.js", "window.storyScriptRan = true;");
|
|
83
|
+
expect(await createPackageFiles(storyFile, STORY_DIR, { inlineHtml: true })).toBe(true);
|
|
84
|
+
const html = await fs.readFile(path.join(STORY_DIR, "index.html"), "utf8");
|
|
85
|
+
expect(html).toContain("#squiffy { color: rebeccapurple; }");
|
|
86
|
+
expect(html).toContain("window.storyScriptRan = true;");
|
|
87
|
+
expect(html).not.toContain('href="my_style.css"');
|
|
88
|
+
expect(html).not.toContain('src="my_script.js"');
|
|
89
|
+
expect(html).toContain('"cat.png":"data:image/png;base64,iVBORw=="');
|
|
90
|
+
});
|
|
91
|
+
test("adds the story's own stylesheets and scripts to the zip", async () => {
|
|
92
|
+
await writeFile("story.squiffy", importingStory);
|
|
93
|
+
await writeFile("cat.png");
|
|
94
|
+
await writeFile("my_style.css", "#squiffy { color: rebeccapurple; }");
|
|
95
|
+
await writeFile("my_script.js", "window.storyScriptRan = true;");
|
|
96
|
+
expect(await createPackageFiles(storyFile, STORY_DIR, { createZip: true })).toBe(true);
|
|
97
|
+
const zip = unzipSync(new Uint8Array(await fs.readFile(path.join(STORY_DIR, "output.zip"))));
|
|
98
|
+
expect(Object.keys(zip).sort()).toEqual(["cat.png", "index.html", "my_script.js", "my_style.css", "squiffy.runtime.global.js", "story.js", "style.css"]);
|
|
99
|
+
});
|
|
73
100
|
});
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import * as fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { convertTwine, formatTwineProblems, TwineParseError } from "squiffy-compiler";
|
|
4
|
+
export const PROBLEMS_FILENAME = "twine-import.txt";
|
|
5
|
+
// Converts a Twine story (#378), in Twee or Twine's HTML, into a new folder with a Squiffy story
|
|
6
|
+
// in it: the story file, a squiffy.json, the story's CSS and JavaScript, and twine-import.txt,
|
|
7
|
+
// which lists what couldn't be converted exactly. The folder is next to the Twine file, with the
|
|
8
|
+
// same name, unless another is given. Returns false if the story couldn't be converted.
|
|
9
|
+
export const convertTwineFile = (inputFilename, outputFolder) => {
|
|
10
|
+
console.log("Loading " + inputFilename);
|
|
11
|
+
if (!fs.existsSync(inputFilename)) {
|
|
12
|
+
console.error(`${inputFilename} doesn't exist.`);
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
const parsed = path.parse(path.resolve(inputFilename));
|
|
16
|
+
const folder = path.resolve(outputFolder ?? path.join(parsed.dir, parsed.name));
|
|
17
|
+
if (fs.existsSync(folder) && (!fs.statSync(folder).isDirectory() || fs.readdirSync(folder).length > 0)) {
|
|
18
|
+
console.error(`${folder} already exists. Choose another folder with --out, or move it out of the way.`);
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
let result;
|
|
22
|
+
try {
|
|
23
|
+
result = convertTwine(fs.readFileSync(inputFilename, "utf8"));
|
|
24
|
+
}
|
|
25
|
+
catch (e) {
|
|
26
|
+
if (e instanceof TwineParseError) {
|
|
27
|
+
console.error(e.message);
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
throw e;
|
|
31
|
+
}
|
|
32
|
+
fs.mkdirSync(folder, { recursive: true });
|
|
33
|
+
for (const [name, text] of Object.entries(result.files)) {
|
|
34
|
+
fs.writeFileSync(path.join(folder, name), text);
|
|
35
|
+
}
|
|
36
|
+
fs.writeFileSync(path.join(folder, PROBLEMS_FILENAME), formatTwineProblems(result, parsed.base));
|
|
37
|
+
const relative = path.relative(process.cwd(), folder) || ".";
|
|
38
|
+
console.log(`Converted ${result.passages} passage${result.passages == 1 ? "" : "s"} from ${result.format || "Twine"} into ${path.join(relative, result.main)}.`);
|
|
39
|
+
if (result.problems.length > 0) {
|
|
40
|
+
console.warn(`${result.problems.length} thing${result.problems.length == 1 ? "" : "s"} couldn't be converted exactly. They're listed in ${path.join(relative, PROBLEMS_FILENAME)}.`);
|
|
41
|
+
}
|
|
42
|
+
console.log(`To try it: npx @textadventures/squiffy-cli ${relative} -s`);
|
|
43
|
+
return true;
|
|
44
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { describe, test, expect, beforeEach, afterEach, vi } from "vitest";
|
|
2
|
+
import { convertTwineFile } from "./convert-twine.js";
|
|
3
|
+
import fs from "fs/promises";
|
|
4
|
+
import path from "path";
|
|
5
|
+
import { fileURLToPath } from "url";
|
|
6
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
7
|
+
const TEST_DIR = path.join(__dirname, "..", "test-fixtures-convert-twine");
|
|
8
|
+
const tweeFile = path.join(TEST_DIR, "cave.twee");
|
|
9
|
+
const outputFolder = path.join(TEST_DIR, "cave");
|
|
10
|
+
const twee = `:: StoryTitle
|
|
11
|
+
The Cave
|
|
12
|
+
|
|
13
|
+
:: StoryData
|
|
14
|
+
{ "format": "Harlowe", "format-version": "3.3.9", "start": "Start" }
|
|
15
|
+
|
|
16
|
+
:: Start
|
|
17
|
+
(set: $torch to false)You're at the mouth of a cave. [[Go in->Cave]]
|
|
18
|
+
|
|
19
|
+
:: Cave
|
|
20
|
+
(if: $torch)[You can see.](else:)[It's dark.] (click: ?page)[Bats!]
|
|
21
|
+
`;
|
|
22
|
+
beforeEach(async () => {
|
|
23
|
+
await fs.mkdir(TEST_DIR, { recursive: true });
|
|
24
|
+
vi.spyOn(console, "log").mockImplementation(() => { });
|
|
25
|
+
vi.spyOn(console, "warn").mockImplementation(() => { });
|
|
26
|
+
vi.spyOn(console, "error").mockImplementation(() => { });
|
|
27
|
+
});
|
|
28
|
+
afterEach(async () => {
|
|
29
|
+
vi.restoreAllMocks();
|
|
30
|
+
await fs.rm(TEST_DIR, { recursive: true, force: true });
|
|
31
|
+
});
|
|
32
|
+
describe("convertTwineFile", () => {
|
|
33
|
+
test("converts the story into a folder named after the file", async () => {
|
|
34
|
+
await fs.writeFile(tweeFile, twee);
|
|
35
|
+
expect(convertTwineFile(tweeFile)).toBe(true);
|
|
36
|
+
const story = await fs.readFile(path.join(outputFolder, "story.squiffy"), "utf8");
|
|
37
|
+
expect(story).toContain("@title The Cave");
|
|
38
|
+
expect(story).toContain("[[Start]]:\n@set torch = false\nYou're at the mouth of a cave. [[Go in]](Cave)");
|
|
39
|
+
expect(story).toContain("{{#if torch}}You can see.{{else}}It's dark.{{/if}}");
|
|
40
|
+
expect(JSON.parse(await fs.readFile(path.join(outputFolder, "squiffy.json"), "utf8")).main).toBe("story.squiffy");
|
|
41
|
+
expect(await fs.readFile(path.join(outputFolder, "twine-import.txt"), "utf8")).toContain("Clicking anywhere on the page has been converted to Continue links.");
|
|
42
|
+
});
|
|
43
|
+
test("writes to the folder given", async () => {
|
|
44
|
+
await fs.writeFile(tweeFile, twee);
|
|
45
|
+
const other = path.join(TEST_DIR, "other");
|
|
46
|
+
expect(convertTwineFile(tweeFile, other)).toBe(true);
|
|
47
|
+
await expect(fs.access(path.join(other, "story.squiffy"))).resolves.toBeUndefined();
|
|
48
|
+
});
|
|
49
|
+
test("doesn't write into a folder that has files in it", async () => {
|
|
50
|
+
await fs.writeFile(tweeFile, twee);
|
|
51
|
+
await fs.mkdir(outputFolder);
|
|
52
|
+
await fs.writeFile(path.join(outputFolder, "notes.txt"), "mine");
|
|
53
|
+
expect(convertTwineFile(tweeFile)).toBe(false);
|
|
54
|
+
await expect(fs.access(path.join(outputFolder, "story.squiffy"))).rejects.toThrow();
|
|
55
|
+
});
|
|
56
|
+
test("says if the file isn't a Twine story", async () => {
|
|
57
|
+
const htmlFile = path.join(TEST_DIR, "page.html");
|
|
58
|
+
await fs.writeFile(htmlFile, "<html><body>Hello</body></html>");
|
|
59
|
+
expect(convertTwineFile(htmlFile)).toBe(false);
|
|
60
|
+
expect(console.error).toHaveBeenCalledWith("This doesn't look like a Twine story: there's no story data in it.");
|
|
61
|
+
});
|
|
62
|
+
});
|
package/dist/file-packager.js
CHANGED
|
@@ -2,7 +2,7 @@ import * as fs from "fs";
|
|
|
2
2
|
import * as path from "path";
|
|
3
3
|
import { compile, convertSetters, findV5Syntax, formatDiagnostic } from "squiffy-compiler";
|
|
4
4
|
import { externalFiles } from "./external-files.js";
|
|
5
|
-
import { findAssets, getStoryText } from "./assets.js";
|
|
5
|
+
import { findAssets, findImportedFiles, getStoryText } from "./assets.js";
|
|
6
6
|
import { createPackage } from "@textadventures/squiffy-packager";
|
|
7
7
|
// A story given as a filename is just that file and its @imports, as with no squiffy.json
|
|
8
8
|
const toStory = (input) => typeof input == "string" ? { mainFile: input, expressions: false } : input;
|
|
@@ -59,9 +59,13 @@ export const createPackageFiles = async (input, outputPath, options = {}) => {
|
|
|
59
59
|
console.log("Failed.");
|
|
60
60
|
return false;
|
|
61
61
|
}
|
|
62
|
-
// Asset files (images etc.) referenced by the story
|
|
63
|
-
// story's folder, alongside these, so they only need to be passed
|
|
64
|
-
|
|
62
|
+
// Asset files (images etc.) referenced by the story, and its own stylesheets and scripts. The other
|
|
63
|
+
// package files are written to the story's folder, alongside these, so they only need to be passed
|
|
64
|
+
// on for the zip or for inlining.
|
|
65
|
+
const assets = options.assets ?? {
|
|
66
|
+
...findAssets(inputFilename, getStoryText(result)),
|
|
67
|
+
...findImportedFiles(inputFilename, result),
|
|
68
|
+
};
|
|
65
69
|
const assetPaths = Object.keys(assets);
|
|
66
70
|
if (assetPaths.length > 0 && (options.createZip || options.inlineHtml)) {
|
|
67
71
|
console.log(`Including asset${assetPaths.length == 1 ? "" : "s"}: ${assetPaths.join(", ")}`);
|
package/dist/squiffy.js
CHANGED
|
@@ -5,6 +5,7 @@ import { serve } from "./server.js";
|
|
|
5
5
|
import { createPackageFiles, writeScriptFile } from "./file-packager.js";
|
|
6
6
|
import { convertV5File } from "./convert-v5.js";
|
|
7
7
|
import { convertV6Story } from "./convert-v6.js";
|
|
8
|
+
import { convertTwineFile } from "./convert-twine.js";
|
|
8
9
|
import { findStory, StoryNotFoundError } from "squiffy-compiler/node";
|
|
9
10
|
import pkg from "../package.json" with { type: "json" };
|
|
10
11
|
import path from "path";
|
|
@@ -23,6 +24,10 @@ const argv = yargs(hideBin(process.argv))
|
|
|
23
24
|
.boolean("convert-v5")
|
|
24
25
|
.describe("convert-v6", "Convert a Squiffy 6 story's @set and link setters to Squiffy 7's rules and add a squiffy.json, keeping a backup of each file changed")
|
|
25
26
|
.boolean("convert-v6")
|
|
27
|
+
.describe("convert-twine", "Convert a Twine story (a .twee file, or Twine's published or archive .html) to Squiffy, in a new folder next to it")
|
|
28
|
+
.boolean("convert-twine")
|
|
29
|
+
.describe("out", "With --convert-twine, the folder to put the Squiffy story in")
|
|
30
|
+
.string("out")
|
|
26
31
|
.version(version)
|
|
27
32
|
.parseSync();
|
|
28
33
|
console.log("Squiffy " + version);
|
|
@@ -62,6 +67,15 @@ else if (argv.convertV6) {
|
|
|
62
67
|
process.exitCode = 1;
|
|
63
68
|
}
|
|
64
69
|
}
|
|
70
|
+
else if (argv.convertTwine) {
|
|
71
|
+
if (argv._[0] === undefined) {
|
|
72
|
+
console.error("Give the Twine file to convert: a .twee file, or Twine's published or archive .html.");
|
|
73
|
+
process.exitCode = 1;
|
|
74
|
+
}
|
|
75
|
+
else if (!convertTwineFile(input, argv.out)) {
|
|
76
|
+
process.exitCode = 1;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
65
79
|
else if (options.scriptOnly) {
|
|
66
80
|
const story = await findStoryOrExit();
|
|
67
81
|
const outputFilename = typeof options.scriptOnly === "string" ? options.scriptOnly : "story.js";
|
|
@@ -72,7 +86,8 @@ else {
|
|
|
72
86
|
const outputPath = path.dirname(story.mainFile);
|
|
73
87
|
const result = await createPackageFiles(story, outputPath, {
|
|
74
88
|
createZip: !!options.zip,
|
|
75
|
-
inlineHtml: !!options.inline
|
|
89
|
+
inlineHtml: !!options.inline,
|
|
90
|
+
backButton: story.backButton,
|
|
76
91
|
});
|
|
77
92
|
if (result && options.serve) {
|
|
78
93
|
const port = argv.p || 8282;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@textadventures/squiffy-cli",
|
|
3
|
-
"version": "7.0.0-beta.
|
|
3
|
+
"version": "7.0.0-beta.5",
|
|
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.
|
|
38
|
+
"@textadventures/squiffy-packager": "^7.0.0-beta.5",
|
|
39
39
|
"finalhandler": "^2.1.0",
|
|
40
40
|
"glob": "^14.0.6",
|
|
41
41
|
"serve-static": "^2.2.0",
|
|
42
|
-
"squiffy-compiler": "^7.0.0-beta.
|
|
43
|
-
"squiffy-runtime": "^7.0.0-beta.
|
|
42
|
+
"squiffy-compiler": "^7.0.0-beta.5",
|
|
43
|
+
"squiffy-runtime": "^7.0.0-beta.5",
|
|
44
44
|
"yargs": "^18.2.0"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
@@ -56,6 +56,5 @@
|
|
|
56
56
|
"files": [
|
|
57
57
|
"dist"
|
|
58
58
|
],
|
|
59
|
-
"type": "module"
|
|
60
|
-
"gitHead": "751f5f19aebb41942dd45f0de61f60a3ed5f9b8e"
|
|
59
|
+
"type": "module"
|
|
61
60
|
}
|
package/LICENSE
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
The MIT License (MIT)
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2014-2026 Alex Warren, textadventures.co.uk and contributors
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
22
|
-
|