@textadventures/squiffy-cli 6.0.0-beta.4 → 6.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/convert-v5.d.ts +1 -0
- package/dist/convert-v5.js +30 -0
- package/dist/convert-v5.test.d.ts +1 -0
- package/dist/convert-v5.test.js +47 -0
- package/dist/file-packager.js +4 -1
- package/dist/squiffy.js +9 -1
- package/package.json +5 -5
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const convertV5File: (inputFilename: string) => boolean;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import * as fs from "fs";
|
|
2
|
+
import { convertV5Syntax } from "squiffy-compiler";
|
|
3
|
+
// Converts Squiffy 5 text processor syntax in a .squiffy file to Squiffy 6 Handlebars syntax.
|
|
4
|
+
// The original file is kept with a .bak extension. Returns false if the file couldn't be written.
|
|
5
|
+
export const convertV5File = (inputFilename) => {
|
|
6
|
+
console.log("Loading " + inputFilename);
|
|
7
|
+
const result = convertV5Syntax(fs.readFileSync(inputFilename).toString());
|
|
8
|
+
if (result.converted == 0 && result.problems.length == 0) {
|
|
9
|
+
console.log("No Squiffy 5 text processor syntax found.");
|
|
10
|
+
return true;
|
|
11
|
+
}
|
|
12
|
+
if (result.converted > 0) {
|
|
13
|
+
const backupFilename = inputFilename + ".bak";
|
|
14
|
+
if (fs.existsSync(backupFilename)) {
|
|
15
|
+
console.error(`${backupFilename} already exists. Move or delete it, then try again.`);
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
fs.copyFileSync(inputFilename, backupFilename);
|
|
19
|
+
fs.writeFileSync(inputFilename, result.script);
|
|
20
|
+
console.log(`Converted ${result.converted} Squiffy 5 text processor command${result.converted == 1 ? "" : "s"}. The original file has been saved as ${backupFilename}`);
|
|
21
|
+
}
|
|
22
|
+
if (result.problems.length > 0) {
|
|
23
|
+
console.warn("These couldn't be converted automatically, so have been left as they are:");
|
|
24
|
+
for (const problem of result.problems) {
|
|
25
|
+
console.warn(` line ${problem.line}: ${problem.text}`);
|
|
26
|
+
console.warn(` ${problem.reason}`);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return true;
|
|
30
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { describe, test, expect, beforeEach, afterEach, vi } from "vitest";
|
|
2
|
+
import { convertV5File } from "./convert-v5.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-v5");
|
|
8
|
+
const storyFile = path.join(TEST_DIR, "story.squiffy");
|
|
9
|
+
const backupFile = storyFile + ".bak";
|
|
10
|
+
beforeEach(async () => {
|
|
11
|
+
await fs.mkdir(TEST_DIR, { recursive: true });
|
|
12
|
+
vi.spyOn(console, "log").mockImplementation(() => { });
|
|
13
|
+
vi.spyOn(console, "warn").mockImplementation(() => { });
|
|
14
|
+
vi.spyOn(console, "error").mockImplementation(() => { });
|
|
15
|
+
});
|
|
16
|
+
afterEach(async () => {
|
|
17
|
+
vi.restoreAllMocks();
|
|
18
|
+
await fs.rm(TEST_DIR, { recursive: true, force: true });
|
|
19
|
+
});
|
|
20
|
+
describe("convertV5File", () => {
|
|
21
|
+
test("converts the file and keeps a backup", async () => {
|
|
22
|
+
const original = "[[a]]:\n{if x:yes}{else:no} {x}";
|
|
23
|
+
await fs.writeFile(storyFile, original);
|
|
24
|
+
expect(convertV5File(storyFile)).toBe(true);
|
|
25
|
+
expect(await fs.readFile(storyFile, "utf8")).toBe("[[a]]:\n{{#if x}}yes{{else}}no{{/if}} {{x}}");
|
|
26
|
+
expect(await fs.readFile(backupFile, "utf8")).toBe(original);
|
|
27
|
+
});
|
|
28
|
+
test("reports what it couldn't convert", async () => {
|
|
29
|
+
await fs.writeFile(storyFile, "[[a]]:\n{x}\n{@x*=2}");
|
|
30
|
+
expect(convertV5File(storyFile)).toBe(true);
|
|
31
|
+
expect(await fs.readFile(storyFile, "utf8")).toBe("[[a]]:\n{{x}}\n{@x*=2}");
|
|
32
|
+
expect(console.warn).toHaveBeenCalledWith(" line 3: {@x*=2}");
|
|
33
|
+
});
|
|
34
|
+
test("doesn't write anything when there's nothing to convert", async () => {
|
|
35
|
+
await fs.writeFile(storyFile, "[[a]]:\n{{x}}");
|
|
36
|
+
expect(convertV5File(storyFile)).toBe(true);
|
|
37
|
+
await expect(fs.access(backupFile)).rejects.toThrow();
|
|
38
|
+
expect(console.log).toHaveBeenCalledWith("No Squiffy 5 text processor syntax found.");
|
|
39
|
+
});
|
|
40
|
+
test("doesn't overwrite an existing backup", async () => {
|
|
41
|
+
await fs.writeFile(storyFile, "[[a]]:\n{x}");
|
|
42
|
+
await fs.writeFile(backupFile, "old backup");
|
|
43
|
+
expect(convertV5File(storyFile)).toBe(false);
|
|
44
|
+
expect(await fs.readFile(storyFile, "utf8")).toBe("[[a]]:\n{x}");
|
|
45
|
+
expect(await fs.readFile(backupFile, "utf8")).toBe("old backup");
|
|
46
|
+
});
|
|
47
|
+
});
|
package/dist/file-packager.js
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import * as fs from "fs";
|
|
2
2
|
import * as path from "path";
|
|
3
|
-
import { compile } from "squiffy-compiler";
|
|
3
|
+
import { compile, findV5Syntax } from "squiffy-compiler";
|
|
4
4
|
import { externalFiles } from "./external-files.js";
|
|
5
5
|
import { createPackage } from "@textadventures/squiffy-packager";
|
|
6
6
|
async function getCompileResult(inputFilename) {
|
|
7
7
|
const inputFile = fs.readFileSync(inputFilename);
|
|
8
8
|
const inputText = inputFile.toString();
|
|
9
|
+
if (findV5Syntax(inputText).length > 0) {
|
|
10
|
+
console.warn("This story looks like it uses Squiffy 5 text processor syntax. Run with --convert-v5 to convert it to Squiffy 6.");
|
|
11
|
+
}
|
|
9
12
|
return await compile({
|
|
10
13
|
scriptBaseFilename: path.basename(inputFilename),
|
|
11
14
|
script: inputText,
|
package/dist/squiffy.js
CHANGED
|
@@ -3,6 +3,7 @@ import yargs from "yargs";
|
|
|
3
3
|
import { hideBin } from "yargs/helpers";
|
|
4
4
|
import { serve } from "./server.js";
|
|
5
5
|
import { createPackageFiles, writeScriptFile } from "./file-packager.js";
|
|
6
|
+
import { convertV5File } from "./convert-v5.js";
|
|
6
7
|
import pkg from "../package.json" with { type: "json" };
|
|
7
8
|
import path from "path";
|
|
8
9
|
const version = pkg.version;
|
|
@@ -16,6 +17,8 @@ const argv = yargs(hideBin(process.argv))
|
|
|
16
17
|
.describe("scriptonly", "Only generate JavaScript file (and optionally specify a name)")
|
|
17
18
|
.describe("zip", "Create zip file")
|
|
18
19
|
.describe("inline", "Create single HTML file with everything inlined")
|
|
20
|
+
.describe("convert-v5", "Convert Squiffy 5 text processor syntax to Squiffy 6, keeping a backup of the original file")
|
|
21
|
+
.boolean("convert-v5")
|
|
19
22
|
.version(version)
|
|
20
23
|
.parseSync();
|
|
21
24
|
console.log("Squiffy " + version);
|
|
@@ -27,7 +30,12 @@ const options = {
|
|
|
27
30
|
};
|
|
28
31
|
const inputFilename = argv._[0];
|
|
29
32
|
const outputPath = path.resolve(path.dirname(inputFilename));
|
|
30
|
-
if (
|
|
33
|
+
if (argv.convertV5) {
|
|
34
|
+
if (!convertV5File(inputFilename)) {
|
|
35
|
+
process.exitCode = 1;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
else if (options.scriptOnly) {
|
|
31
39
|
const outputFilename = typeof options.scriptOnly === "string" ? options.scriptOnly : "story.js";
|
|
32
40
|
await writeScriptFile(inputFilename, outputPath, outputFilename);
|
|
33
41
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@textadventures/squiffy-cli",
|
|
3
|
-
"version": "6.0.0-beta.
|
|
3
|
+
"version": "6.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": "^6.0.0-beta.
|
|
38
|
+
"@textadventures/squiffy-packager": "^6.0.0-beta.5",
|
|
39
39
|
"finalhandler": "^2.1.0",
|
|
40
40
|
"glob": "^13.0.6",
|
|
41
41
|
"serve-static": "^2.2.0",
|
|
42
|
-
"squiffy-compiler": "^6.0.0-beta.
|
|
43
|
-
"squiffy-runtime": "^6.0.0-beta.
|
|
42
|
+
"squiffy-compiler": "^6.0.0-beta.5",
|
|
43
|
+
"squiffy-runtime": "^6.0.0-beta.5",
|
|
44
44
|
"yargs": "^18.1.0"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
@@ -56,5 +56,5 @@
|
|
|
56
56
|
"dist"
|
|
57
57
|
],
|
|
58
58
|
"type": "module",
|
|
59
|
-
"gitHead": "
|
|
59
|
+
"gitHead": "55e706ccb3aee2527b5629984642b6681b47a00d"
|
|
60
60
|
}
|