@textadventures/squiffy-cli 7.0.0-beta.3 → 7.0.0-beta.4
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/convert-v6.d.ts +1 -0
- package/dist/convert-v6.js +74 -0
- package/dist/convert-v6.test.d.ts +1 -0
- package/dist/convert-v6.test.js +67 -0
- package/dist/file-packager.js +6 -2
- package/dist/squiffy.js +12 -0
- package/package.json +5 -5
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const convertV6Story: (inputFilename: string) => Promise<boolean>;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import * as fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { analyse, convertSetters, findV5Syntax, formatProjectFile, PROJECT_FILENAME } from "squiffy-compiler";
|
|
4
|
+
import { listSquiffyFiles } from "squiffy-compiler/node";
|
|
5
|
+
import { externalFiles } from "./external-files.js";
|
|
6
|
+
// Converts a Squiffy 6 story to Squiffy 7's rules for setters (#373): text in @set and link
|
|
7
|
+
// setters is quoted, and @name becomes name, in the main file and every .squiffy file it
|
|
8
|
+
// @imports. Then adds a squiffy.json, which is what turns the new rules on, leaving out any other
|
|
9
|
+
// .squiffy files in the folder so the story is made of the same files as before. Each changed
|
|
10
|
+
// file is kept with a .bak extension. Returns false if the story couldn't be converted.
|
|
11
|
+
export const convertV6Story = async (inputFilename) => {
|
|
12
|
+
const mainFile = path.resolve(inputFilename);
|
|
13
|
+
const root = path.dirname(mainFile);
|
|
14
|
+
console.log("Loading " + mainFile);
|
|
15
|
+
for (let dir = root;; dir = path.dirname(dir)) {
|
|
16
|
+
const projectFile = path.join(dir, PROJECT_FILENAME);
|
|
17
|
+
if (fs.existsSync(projectFile)) {
|
|
18
|
+
console.error(`${projectFile} already exists, so this story already uses Squiffy 7's rules for setters.`);
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
if (path.dirname(dir) == dir)
|
|
22
|
+
break;
|
|
23
|
+
}
|
|
24
|
+
const script = fs.readFileSync(mainFile).toString();
|
|
25
|
+
if (findV5Syntax(script).length > 0) {
|
|
26
|
+
console.error("This story looks like it uses Squiffy 5 text processor syntax. Run with --convert-v5 first.");
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
// The story's files: the main file and the .squiffy files it @imports
|
|
30
|
+
const analysis = await analyse({ script, scriptBaseFilename: path.basename(mainFile), externalFiles: externalFiles(mainFile) });
|
|
31
|
+
if (!analysis.success) {
|
|
32
|
+
console.error("The story has errors, so it can't be converted. Fix them, then try again.");
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
const storyFiles = analysis.files.map((file, i) => i == 0 ? mainFile : path.resolve(root, file.filename ?? ""));
|
|
36
|
+
// Convert everything first, so nothing is written if a .bak is in the way
|
|
37
|
+
const changes = [];
|
|
38
|
+
let problems = 0;
|
|
39
|
+
for (const filename of storyFiles) {
|
|
40
|
+
const result = convertSetters(fs.readFileSync(filename).toString());
|
|
41
|
+
if (result.converted > 0) {
|
|
42
|
+
if (fs.existsSync(filename + ".bak")) {
|
|
43
|
+
console.error(`${filename}.bak already exists. Move or delete it, then try again.`);
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
changes.push({ filename, script: result.script, converted: result.converted });
|
|
47
|
+
}
|
|
48
|
+
if (result.problems.length > 0) {
|
|
49
|
+
if (problems == 0)
|
|
50
|
+
console.warn("These couldn't be converted automatically, so have been left as they are:");
|
|
51
|
+
for (const problem of result.problems) {
|
|
52
|
+
console.warn(` ${path.relative(root, filename)} line ${problem.line}: ${problem.text}`);
|
|
53
|
+
console.warn(` ${problem.reason}`);
|
|
54
|
+
}
|
|
55
|
+
problems += result.problems.length;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
for (const change of changes) {
|
|
59
|
+
fs.copyFileSync(change.filename, change.filename + ".bak");
|
|
60
|
+
fs.writeFileSync(change.filename, change.script);
|
|
61
|
+
console.log(`Converted ${change.converted} setter${change.converted == 1 ? "" : "s"} in ${path.relative(root, change.filename)}. The original file has been saved as ${path.basename(change.filename)}.bak`);
|
|
62
|
+
}
|
|
63
|
+
if (changes.length == 0) {
|
|
64
|
+
console.log("No setters needed converting.");
|
|
65
|
+
}
|
|
66
|
+
// squiffy.json makes every .squiffy file in the folder part of the story, so leave out the
|
|
67
|
+
// ones that weren't
|
|
68
|
+
const inStory = new Set(storyFiles);
|
|
69
|
+
const exclude = (await listSquiffyFiles(root)).filter(file => !inStory.has(path.resolve(root, file)));
|
|
70
|
+
fs.writeFileSync(path.join(root, PROJECT_FILENAME), formatProjectFile({ main: path.basename(mainFile), exclude }));
|
|
71
|
+
console.log(`Added ${PROJECT_FILENAME}, so the story now uses Squiffy 7's rules for setters.`
|
|
72
|
+
+ (exclude.length ? ` It leaves out the other .squiffy files in the folder: ${exclude.join(", ")}.` : ""));
|
|
73
|
+
return true;
|
|
74
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { describe, test, expect, beforeEach, afterEach, vi } from "vitest";
|
|
2
|
+
import { convertV6Story } from "./convert-v6.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-v6");
|
|
8
|
+
const file = (name) => path.join(TEST_DIR, name);
|
|
9
|
+
const read = (name) => fs.readFile(file(name), "utf8");
|
|
10
|
+
const exists = (name) => fs.access(file(name)).then(() => true, () => false);
|
|
11
|
+
beforeEach(async () => {
|
|
12
|
+
await fs.mkdir(path.join(TEST_DIR, "parts"), { recursive: true });
|
|
13
|
+
vi.spyOn(console, "log").mockImplementation(() => { });
|
|
14
|
+
vi.spyOn(console, "warn").mockImplementation(() => { });
|
|
15
|
+
vi.spyOn(console, "error").mockImplementation(() => { });
|
|
16
|
+
});
|
|
17
|
+
afterEach(async () => {
|
|
18
|
+
vi.restoreAllMocks();
|
|
19
|
+
await fs.rm(TEST_DIR, { recursive: true, force: true });
|
|
20
|
+
});
|
|
21
|
+
describe("convertV6Story", () => {
|
|
22
|
+
test("converts the main file and its imports, keeps backups, and adds a squiffy.json", async () => {
|
|
23
|
+
const main = "@import parts/shop.squiffy\n\n[[start]]:\n@set weapon = sword\n[[Shop]](shop, gold = @start_gold)";
|
|
24
|
+
const shop = "[[shop]]:\n@set mood = grumpy";
|
|
25
|
+
const unchanged = "[[other]]:\n@set gold = 10";
|
|
26
|
+
await fs.writeFile(file("story.squiffy"), main);
|
|
27
|
+
await fs.writeFile(file("parts/shop.squiffy"), shop);
|
|
28
|
+
await fs.writeFile(file("parts/other.squiffy"), "@import ../unused.squiffy");
|
|
29
|
+
await fs.writeFile(file("unused.squiffy"), unchanged);
|
|
30
|
+
await fs.writeFile(file("story2.squiffy"), "[[x]]:\n@set a = b");
|
|
31
|
+
expect(await convertV6Story(file("story.squiffy"))).toBe(true);
|
|
32
|
+
expect(await read("story.squiffy")).toBe("@import parts/shop.squiffy\n\n[[start]]:\n@set weapon = \"sword\"\n[[Shop]](shop, gold = start_gold)");
|
|
33
|
+
expect(await read("parts/shop.squiffy")).toBe("[[shop]]:\n@set mood = \"grumpy\"");
|
|
34
|
+
expect(await read("story.squiffy.bak")).toBe(main);
|
|
35
|
+
expect(await read("parts/shop.squiffy.bak")).toBe(shop);
|
|
36
|
+
// Files that aren't part of the story are left alone, and left out of it
|
|
37
|
+
expect(await read("story2.squiffy")).toBe("[[x]]:\n@set a = b");
|
|
38
|
+
expect(JSON.parse(await read("squiffy.json"))).toMatchObject({
|
|
39
|
+
main: "story.squiffy",
|
|
40
|
+
exclude: ["parts/other.squiffy", "story2.squiffy", "unused.squiffy"],
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
test("adds a squiffy.json even if nothing needs converting", async () => {
|
|
44
|
+
await fs.writeFile(file("story.squiffy"), "[[start]]:\n@set gold = 10");
|
|
45
|
+
expect(await convertV6Story(file("story.squiffy"))).toBe(true);
|
|
46
|
+
expect(await exists("story.squiffy.bak")).toBe(false);
|
|
47
|
+
expect(JSON.parse(await read("squiffy.json")).main).toBe("story.squiffy");
|
|
48
|
+
});
|
|
49
|
+
test("refuses a story that already has a squiffy.json", async () => {
|
|
50
|
+
await fs.writeFile(file("story.squiffy"), "[[start]]:\n@set a = b");
|
|
51
|
+
await fs.writeFile(file("squiffy.json"), "{ \"version\": 1, \"main\": \"story.squiffy\" }");
|
|
52
|
+
expect(await convertV6Story(file("story.squiffy"))).toBe(false);
|
|
53
|
+
expect(await read("story.squiffy")).toBe("[[start]]:\n@set a = b");
|
|
54
|
+
});
|
|
55
|
+
test("refuses a story with Squiffy 5 syntax", async () => {
|
|
56
|
+
await fs.writeFile(file("story.squiffy"), "[[start]]:\n{if x:yes}\n@set a = b");
|
|
57
|
+
expect(await convertV6Story(file("story.squiffy"))).toBe(false);
|
|
58
|
+
expect(await exists("squiffy.json")).toBe(false);
|
|
59
|
+
});
|
|
60
|
+
test("changes nothing if a backup is in the way", async () => {
|
|
61
|
+
await fs.writeFile(file("story.squiffy"), "[[start]]:\n@set a = b");
|
|
62
|
+
await fs.writeFile(file("story.squiffy.bak"), "old");
|
|
63
|
+
expect(await convertV6Story(file("story.squiffy"))).toBe(false);
|
|
64
|
+
expect(await read("story.squiffy")).toBe("[[start]]:\n@set a = b");
|
|
65
|
+
expect(await exists("squiffy.json")).toBe(false);
|
|
66
|
+
});
|
|
67
|
+
});
|
package/dist/file-packager.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import * as fs from "fs";
|
|
2
2
|
import * as path from "path";
|
|
3
|
-
import { compile, findV5Syntax, formatDiagnostic } from "squiffy-compiler";
|
|
3
|
+
import { compile, convertSetters, 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
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;
|
|
8
|
+
const toStory = (input) => typeof input == "string" ? { mainFile: input, expressions: false } : input;
|
|
9
9
|
async function getCompileResult(story) {
|
|
10
10
|
const inputFilename = story.mainFile;
|
|
11
11
|
const inputFile = fs.readFileSync(inputFilename);
|
|
@@ -13,11 +13,15 @@ async function getCompileResult(story) {
|
|
|
13
13
|
if (findV5Syntax(inputText).length > 0) {
|
|
14
14
|
console.warn("This story looks like it uses Squiffy 5 text processor syntax. Run with --convert-v5 to convert it to Squiffy 6.");
|
|
15
15
|
}
|
|
16
|
+
else if (!story.expressions && convertSetters(inputText).converted > 0) {
|
|
17
|
+
console.log("This story uses Squiffy 6's rules for @set and link setters. Run with --convert-v6 to convert it to Squiffy 7's.");
|
|
18
|
+
}
|
|
16
19
|
const result = await compile({
|
|
17
20
|
scriptBaseFilename: path.basename(inputFilename),
|
|
18
21
|
script: inputText,
|
|
19
22
|
externalFiles: externalFiles(inputFilename),
|
|
20
23
|
storyFiles: story.storyFiles,
|
|
24
|
+
expressions: story.expressions,
|
|
21
25
|
globalJs: true,
|
|
22
26
|
});
|
|
23
27
|
for (const diagnostic of result.diagnostics) {
|
package/dist/squiffy.js
CHANGED
|
@@ -4,6 +4,7 @@ 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 { convertV6Story } from "./convert-v6.js";
|
|
7
8
|
import { findStory, StoryNotFoundError } from "squiffy-compiler/node";
|
|
8
9
|
import pkg from "../package.json" with { type: "json" };
|
|
9
10
|
import path from "path";
|
|
@@ -20,6 +21,8 @@ const argv = yargs(hideBin(process.argv))
|
|
|
20
21
|
.describe("inline", "Create single HTML file with everything inlined")
|
|
21
22
|
.describe("convert-v5", "Convert Squiffy 5 text processor syntax to Squiffy 6, keeping a backup of the original file")
|
|
22
23
|
.boolean("convert-v5")
|
|
24
|
+
.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
|
+
.boolean("convert-v6")
|
|
23
26
|
.version(version)
|
|
24
27
|
.parseSync();
|
|
25
28
|
console.log("Squiffy " + version);
|
|
@@ -50,6 +53,15 @@ if (argv.convertV5) {
|
|
|
50
53
|
process.exitCode = 1;
|
|
51
54
|
}
|
|
52
55
|
}
|
|
56
|
+
else if (argv.convertV6) {
|
|
57
|
+
if (argv._[0] === undefined) {
|
|
58
|
+
console.error("Give the story's main .squiffy file to convert.");
|
|
59
|
+
process.exitCode = 1;
|
|
60
|
+
}
|
|
61
|
+
else if (!await convertV6Story(input)) {
|
|
62
|
+
process.exitCode = 1;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
53
65
|
else if (options.scriptOnly) {
|
|
54
66
|
const story = await findStoryOrExit();
|
|
55
67
|
const outputFilename = typeof options.scriptOnly === "string" ? options.scriptOnly : "story.js";
|
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.4",
|
|
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.4",
|
|
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.4",
|
|
43
|
+
"squiffy-runtime": "^7.0.0-beta.4",
|
|
44
44
|
"yargs": "^18.2.0"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
@@ -57,5 +57,5 @@
|
|
|
57
57
|
"dist"
|
|
58
58
|
],
|
|
59
59
|
"type": "module",
|
|
60
|
-
"gitHead": "
|
|
60
|
+
"gitHead": "751f5f19aebb41942dd45f0de61f60a3ed5f9b8e"
|
|
61
61
|
}
|