squiffy-compiler 6.0.0-alpha.0 → 6.0.0-alpha.2

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.
Files changed (53) hide show
  1. package/dist/compiler.d.ts +2 -1
  2. package/dist/compiler.js +11 -9
  3. package/package.json +7 -17
  4. package/dist/compiler.test.d.ts +0 -1
  5. package/dist/compiler.test.js +0 -73
  6. package/dist/external-files.d.ts +0 -5
  7. package/dist/external-files.js +0 -21
  8. package/dist/index.template.html +0 -39
  9. package/dist/packager.d.ts +0 -1
  10. package/dist/packager.js +0 -78
  11. package/dist/server.d.ts +0 -1
  12. package/dist/server.js +0 -15
  13. package/dist/squiffy.d.ts +0 -1
  14. package/dist/squiffy.js +0 -29
  15. package/dist/squiffy.runtime.d.ts +0 -34
  16. package/dist/squiffy.template.d.ts +0 -29
  17. package/dist/squiffy.template.js +0 -598
  18. package/dist/style.template.css +0 -52
  19. package/dist/version.d.ts +0 -1
  20. package/dist/version.js +0 -1
  21. package/examples/attributes/attributes.squiffy +0 -81
  22. package/examples/clearscreen/clearscreen.squiffy +0 -15
  23. package/examples/continue/continue.squiffy +0 -18
  24. package/examples/helloworld/helloworld.squiffy +0 -1
  25. package/examples/import/file2.squiffy +0 -8
  26. package/examples/import/test.js +0 -3
  27. package/examples/import/test.squiffy +0 -5
  28. package/examples/input/input.squiffy +0 -22
  29. package/examples/last/last.squiffy +0 -32
  30. package/examples/master/master.squiffy +0 -35
  31. package/examples/replace/replace.squiffy +0 -27
  32. package/examples/rotate/rotate.squiffy +0 -25
  33. package/examples/sectiontrack/sectiontrack.squiffy +0 -16
  34. package/examples/start/start.squiffy +0 -7
  35. package/examples/test/example.squiffy +0 -52
  36. package/examples/textprocessor/textprocessor.squiffy +0 -21
  37. package/examples/transitions/transitions.squiffy +0 -53
  38. package/examples/turncount/turncount.squiffy +0 -41
  39. package/examples/warnings/warnings.squiffy +0 -23
  40. package/examples/warnings/warnings2.squiffy +0 -3
  41. package/src/__snapshots__/compiler.test.ts.snap +0 -716
  42. package/src/compiler.test.ts +0 -86
  43. package/src/compiler.ts +0 -546
  44. package/src/external-files.ts +0 -22
  45. package/src/index.template.html +0 -39
  46. package/src/packager.ts +0 -97
  47. package/src/server.ts +0 -19
  48. package/src/squiffy.runtime.ts +0 -670
  49. package/src/squiffy.ts +0 -36
  50. package/src/style.template.css +0 -52
  51. package/src/version.ts +0 -1
  52. package/tsconfig.json +0 -22
  53. package/tsconfig.runtime.json +0 -12
@@ -1,3 +1,4 @@
1
+ export declare const SQUIFFY_VERSION = "6.0.0-alpha.2";
1
2
  export interface Output {
2
3
  story: OutputStory;
3
4
  js: string[][];
@@ -40,7 +41,7 @@ interface UiInfo {
40
41
  export interface CompileSuccess {
41
42
  success: true;
42
43
  output: Output;
43
- getJs: () => Promise<string>;
44
+ getJs: (excludeHeader?: boolean) => Promise<string>;
44
45
  getUiInfo: () => UiInfo;
45
46
  }
46
47
  export interface CompileError {
package/dist/compiler.js CHANGED
@@ -1,12 +1,14 @@
1
1
  import * as marked from 'marked';
2
- import { SQUIFFY_VERSION } from './version.js';
2
+ export const SQUIFFY_VERSION = '6.0.0-alpha.2';
3
3
  export async function compile(settings) {
4
4
  const story = new Story(settings.scriptBaseFilename);
5
5
  const errors = [];
6
- async function getJs(storyData) {
6
+ async function getJs(storyData, excludeHeader) {
7
7
  const outputJs = [];
8
- outputJs.push(`// Created with Squiffy ${SQUIFFY_VERSION}`);
9
- outputJs.push('// https://github.com/textadventures/squiffy');
8
+ if (!excludeHeader) {
9
+ outputJs.push(`// Created with Squiffy ${SQUIFFY_VERSION}`);
10
+ outputJs.push('// https://github.com/textadventures/squiffy');
11
+ }
10
12
  outputJs.push('export const story = {};');
11
13
  outputJs.push(`story.id = ${JSON.stringify(storyData.story.id, null, 4)};`);
12
14
  outputJs.push(`story.start = ${JSON.stringify(storyData.story.start, null, 4)};`);
@@ -334,11 +336,11 @@ export async function compile(settings) {
334
336
  ;
335
337
  function writeJs(outputJsFile, tabCount, js) {
336
338
  var tabs = new Array(tabCount + 1).join('\t');
337
- outputJsFile.push(`${tabs}function() {\n`);
339
+ outputJsFile.push(`${tabs}(squiffy, get, set) => {`);
338
340
  for (const jsLine of js) {
339
- outputJsFile.push(`${tabs}\t${jsLine}\n`);
341
+ outputJsFile.push(`${tabs}\t${jsLine}`);
340
342
  }
341
- outputJsFile.push(`${tabs}},\n`);
343
+ outputJsFile.push(`${tabs}},`);
342
344
  }
343
345
  ;
344
346
  const success = await processFileText(settings.script, settings.scriptBaseFilename, true);
@@ -347,8 +349,8 @@ export async function compile(settings) {
347
349
  return {
348
350
  success: true,
349
351
  output: storyData,
350
- getJs: () => {
351
- return getJs(storyData);
352
+ getJs: (excludeHeader) => {
353
+ return getJs(storyData, excludeHeader || false);
352
354
  },
353
355
  getUiInfo: () => {
354
356
  return {
package/package.json CHANGED
@@ -1,14 +1,9 @@
1
1
  {
2
2
  "name": "squiffy-compiler",
3
- "version": "6.0.0-alpha.0",
3
+ "version": "6.0.0-alpha.2",
4
4
  "description": "A tool for creating multiple-choice interactive stories",
5
5
  "dependencies": {
6
- "finalhandler": "^1.3.1",
7
- "glob": "^11.0.0",
8
- "jszip": "^3.9.1",
9
- "marked": "^13.0.2",
10
- "serve-static": "^1.16.2",
11
- "yargs": "^17.7.2"
6
+ "marked": "^13.0.2"
12
7
  },
13
8
  "author": "Alex Warren",
14
9
  "contributors": [
@@ -21,26 +16,21 @@
21
16
  "url": "git+https://github.com/textadventures/squiffy.git"
22
17
  },
23
18
  "license": "MIT",
24
- "bin": {
25
- "squiffy": "squiffy.js"
26
- },
27
19
  "preferGlobal": true,
28
20
  "devDependencies": {
29
- "@types/finalhandler": "^1.2.3",
30
21
  "@types/node": "^22.5.5",
31
- "@types/serve-static": "^1.15.7",
32
- "@types/yargs": "^17.0.33",
33
- "shx": "^0.3.4",
34
- "tsx": "^4.19.1",
22
+ "glob": "^11.0.0",
35
23
  "typescript": "^5.6.2",
36
24
  "vitest": "^2.1.1"
37
25
  },
38
26
  "scripts": {
39
27
  "test": "vitest",
40
- "build": "tsc && shx cp src/index.template.html ./dist/ && shx cp src/squiffy.runtime.js ./dist/ && shx cp src/style.template.css ./dist/",
41
- "dev": "tsc --project tsconfig.runtime.json && tsx src/squiffy.ts"
28
+ "build": "tsc"
42
29
  },
43
30
  "main": "dist/compiler.js",
44
31
  "types": "dist/compiler.d.ts",
32
+ "files": [
33
+ "dist"
34
+ ],
45
35
  "type": "module"
46
36
  }
@@ -1 +0,0 @@
1
- export {};
@@ -1,73 +0,0 @@
1
- import { expect, test } from 'vitest';
2
- import * as fs from 'fs';
3
- import path from 'path';
4
- import { compile } from './compiler.js';
5
- import { externalFiles } from './external-files.js';
6
- function assertSuccess(obj) {
7
- if (!obj || typeof obj !== 'object' || !('success' in obj) || !obj.success) {
8
- throw new Error('Expected success');
9
- }
10
- }
11
- test('"Hello world" should compile', async () => {
12
- const result = await compile({
13
- scriptBaseFilename: "filename.squiffy",
14
- script: "hello world",
15
- });
16
- assertSuccess(result);
17
- expect(result.output.story.start).toBe("_default");
18
- expect(Object.keys(result.output.story.sections).length).toBe(1);
19
- expect(result.output.story.sections._default.text).toBe("<p>hello world</p>");
20
- });
21
- const examples = [
22
- "attributes/attributes.squiffy",
23
- "clearscreen/clearscreen.squiffy",
24
- "continue/continue.squiffy",
25
- "helloworld/helloworld.squiffy",
26
- "import/test.squiffy",
27
- "last/last.squiffy",
28
- "master/master.squiffy",
29
- "replace/replace.squiffy",
30
- "rotate/rotate.squiffy",
31
- "sectiontrack/sectiontrack.squiffy",
32
- "start/start.squiffy",
33
- "test/example.squiffy",
34
- "textprocessor/textprocessor.squiffy",
35
- "transitions/transitions.squiffy",
36
- "turncount/turncount.squiffy",
37
- ];
38
- for (const example of examples) {
39
- test(example, async () => {
40
- const script = fs.readFileSync(`examples/${example}`, 'utf8');
41
- const filename = path.basename(example);
42
- const warnings = [];
43
- const result = await compile({
44
- scriptBaseFilename: filename,
45
- script: script,
46
- onWarning: (message) => {
47
- console.warn(message);
48
- warnings.push(message);
49
- },
50
- externalFiles: externalFiles(`examples/${example}`)
51
- });
52
- assertSuccess(result);
53
- expect(result.output).toMatchSnapshot();
54
- expect(warnings.length).toBe(0);
55
- });
56
- }
57
- const warningExamples = [
58
- "warnings/warnings.squiffy",
59
- "warnings/warnings2.squiffy",
60
- ];
61
- for (const example of warningExamples) {
62
- test(example, async () => {
63
- const script = fs.readFileSync(`examples/${example}`, 'utf8');
64
- const filename = path.basename(example);
65
- const warnings = [];
66
- await compile({
67
- scriptBaseFilename: filename,
68
- script: script,
69
- onWarning: (message) => warnings.push(message)
70
- });
71
- expect(warnings).toMatchSnapshot();
72
- });
73
- }
@@ -1,5 +0,0 @@
1
- export declare const externalFiles: (inputFilename: string) => {
2
- getMatchingFilenames: (pattern: string) => Promise<string[]>;
3
- getContent: (filename: string) => Promise<string>;
4
- getLocalFilename(filename: string): string;
5
- };
@@ -1,21 +0,0 @@
1
- import { glob } from "glob";
2
- import path from "path";
3
- import fs from "fs/promises";
4
- export const externalFiles = (inputFilename) => {
5
- const includedFiles = [path.resolve(inputFilename)];
6
- const basePath = path.resolve(path.dirname(inputFilename));
7
- return {
8
- getMatchingFilenames: async (pattern) => {
9
- const filenames = path.join(basePath, pattern);
10
- const result = await glob(filenames);
11
- return result.filter((filename) => !includedFiles.includes(filename));
12
- },
13
- getContent: async (filename) => {
14
- includedFiles.push(filename);
15
- return (await fs.readFile(filename)).toString();
16
- },
17
- getLocalFilename(filename) {
18
- return path.relative(basePath, filename);
19
- }
20
- };
21
- };
@@ -1,39 +0,0 @@
1
- <!DOCTYPE html>
2
- <html>
3
- <!-- INFO -->
4
- <head>
5
- <title><!-- TITLE --></title>
6
- <meta name="viewport" content="width=device-width, initial-scale=1">
7
- <!-- SCRIPTS -->
8
- <script type="module">
9
- import { init } from './squiffy.runtime.js';
10
- import { story } from './story.js';
11
-
12
- document.addEventListener('DOMContentLoaded', function() {
13
- const squiffyApi = init({
14
- element: document.getElementById('squiffy'),
15
- story: story,
16
- persist: true,
17
- });
18
-
19
- const restartButton = document.getElementById('restart');
20
- restartButton.addEventListener('click', function() {
21
- if (confirm('Are you sure you want to restart?')) {
22
- squiffyApi.restart();
23
- }
24
- });
25
- });
26
- </script>
27
- <link rel="stylesheet" href="style.css"/>
28
- <!-- STYLESHEETS -->
29
- </head>
30
- <body>
31
- <div id="squiffy-container">
32
- <div id="squiffy-header">
33
- <button class="squiffy-header-button" id="restart" tabindex="0">Restart</button>
34
- </div>
35
- <div id="squiffy">
36
- </div>
37
- </div>
38
- </body>
39
- </html>
@@ -1 +0,0 @@
1
- export declare const createPackage: (inputFilename: string) => Promise<string | undefined>;
package/dist/packager.js DELETED
@@ -1,78 +0,0 @@
1
- import * as fs from 'fs';
2
- import * as path from 'path';
3
- import { compile } from './compiler.js';
4
- import { SQUIFFY_VERSION } from './version.js';
5
- import { externalFiles } from './external-files.js';
6
- export const createPackage = async (inputFilename) => {
7
- return await generate(inputFilename);
8
- };
9
- async function generate(inputFilename) {
10
- console.log('Loading ' + inputFilename);
11
- var inputFile = fs.readFileSync(inputFilename);
12
- var inputText = inputFile.toString();
13
- const result = await compile({
14
- scriptBaseFilename: path.basename(inputFilename),
15
- script: inputText,
16
- onWarning: console.warn,
17
- externalFiles: externalFiles(inputFilename)
18
- });
19
- if (!result.success) {
20
- console.log('Failed.');
21
- return;
22
- }
23
- var storyJsName = /* typeof options.scriptOnly === 'string' ? options.scriptOnly : */ 'story.js';
24
- console.log('Writing ' + storyJsName);
25
- var storyJs = await result.getJs();
26
- var outputPath = path.resolve(path.dirname(inputFilename));
27
- fs.writeFileSync(path.join(outputPath, storyJsName), storyJs);
28
- const uiInfo = result.getUiInfo();
29
- console.log('Writing squiffy.runtime.js');
30
- fs.copyFileSync(path.join(import.meta.dirname, 'squiffy.runtime.js'), path.join(outputPath, 'squiffy.runtime.js'));
31
- var cssTemplateFile = fs.readFileSync(findFile('style.template.css', outputPath /*, sourcePath */));
32
- var cssData = cssTemplateFile.toString();
33
- fs.writeFileSync(path.join(outputPath, 'style.css'), cssData);
34
- console.log('Writing index.html');
35
- var htmlTemplateFile = fs.readFileSync(findFile('index.template.html', outputPath /*, sourcePath */));
36
- var htmlData = htmlTemplateFile.toString();
37
- htmlData = htmlData.replace('<!-- INFO -->', `<!--\n\nCreated with Squiffy ${SQUIFFY_VERSION}\n\n\nhttps://github.com/textadventures/squiffy\n\n-->`);
38
- htmlData = htmlData.replace('<!-- TITLE -->', uiInfo.title);
39
- var scriptData = uiInfo.externalScripts.map(script => `<script src="${script}"></script>`).join('\n');
40
- htmlData = htmlData.replace('<!-- SCRIPTS -->', scriptData);
41
- var stylesheetData = uiInfo.externalStylesheets.map(sheet => `<link rel="stylesheet" href="${sheet}"/>`).join('\n');
42
- htmlData = htmlData.replace('<!-- STYLESHEETS -->', stylesheetData);
43
- fs.writeFileSync(path.join(outputPath, 'index.html'), htmlData);
44
- console.log('Writing style.css');
45
- var cssTemplateFile = fs.readFileSync(findFile('style.template.css', outputPath /*, sourcePath */));
46
- var cssData = cssTemplateFile.toString();
47
- fs.writeFileSync(path.join(outputPath, 'style.css'), cssData);
48
- // if (options.zip) {
49
- // console.log('Creating zip file');
50
- // var JSZip = require('jszip');
51
- // var zip = new JSZip();
52
- // zip.file(storyJsName, storyJs);
53
- // zip.file('index.html', htmlData);
54
- // zip.file('style.css', cssData);
55
- // var buffer = zip.generate({
56
- // type: 'nodebuffer'
57
- // });
58
- // if (options.write) {
59
- // fs.writeFileSync(path.join(outputPath, 'output.zip'), buffer);
60
- // }
61
- // else {
62
- // return buffer;
63
- // }
64
- // }
65
- console.log('Done.');
66
- return outputPath;
67
- }
68
- ;
69
- function findFile(filename, outputPath /*, sourcePath: string */) {
70
- if (outputPath) {
71
- var outputPathFile = path.join(outputPath, filename);
72
- if (fs.existsSync(outputPathFile)) {
73
- return outputPathFile;
74
- }
75
- }
76
- return path.join(import.meta.dirname, filename);
77
- }
78
- ;
package/dist/server.d.ts DELETED
@@ -1 +0,0 @@
1
- export declare const serve: (directory: string, port: number) => void;
package/dist/server.js DELETED
@@ -1,15 +0,0 @@
1
- import finalhandler from 'finalhandler';
2
- import * as http from 'http';
3
- import serveStatic from 'serve-static';
4
- function startServer(dir, port) {
5
- var serve = serveStatic(dir, { index: ['index.html'] });
6
- var server = http.createServer(function (req, res) {
7
- var done = finalhandler(req, res);
8
- serve(req, res, done);
9
- });
10
- server.listen(port);
11
- }
12
- export const serve = (directory, port) => {
13
- startServer(directory, port);
14
- console.log('Started http://localhost:' + port + '/');
15
- };
package/dist/squiffy.d.ts DELETED
@@ -1 +0,0 @@
1
- export {};
package/dist/squiffy.js DELETED
@@ -1,29 +0,0 @@
1
- import yargs from 'yargs';
2
- import { hideBin } from 'yargs/helpers';
3
- import { SQUIFFY_VERSION } from './version.js';
4
- import { createPackage } from './packager.js';
5
- import { serve } from './server.js';
6
- const argv = yargs(hideBin(process.argv))
7
- .usage(`Usage: $0 filename.squiffy [options]`)
8
- .demand(1)
9
- .alias('s', 'serve')
10
- .alias('p', 'port')
11
- .describe('s', 'Start HTTP server after compiling')
12
- .describe('p', 'Port for HTTP server (only with --serve)')
13
- .describe('scriptonly', 'Only generate JavaScript file (and optionally specify a name)')
14
- .describe('zip', 'Create zip file')
15
- .parseSync();
16
- console.log('Squiffy ' + SQUIFFY_VERSION);
17
- var options = {
18
- serve: argv.s,
19
- scriptOnly: argv.scriptonly,
20
- pluginName: argv.pluginname,
21
- zip: argv.zip,
22
- write: true,
23
- };
24
- const inputFilename = argv._[0];
25
- var result = await createPackage(inputFilename);
26
- if (result && options.serve) {
27
- var port = argv.p || 8282;
28
- serve(result, port);
29
- }
@@ -1,34 +0,0 @@
1
- interface SquiffyInitOptions {
2
- element: HTMLElement;
3
- story: Story;
4
- scroll?: string;
5
- persist?: boolean;
6
- onSet?: (attribute: string, value: any) => void;
7
- }
8
- interface SquiffyApi {
9
- restart: () => void;
10
- get: (attribute: string) => any;
11
- set: (attribute: string, value: any) => void;
12
- }
13
- interface Story {
14
- js: (() => void)[];
15
- start: string;
16
- id?: string | null;
17
- sections: Record<string, Section>;
18
- }
19
- interface Section {
20
- text?: string;
21
- clear?: boolean;
22
- attributes?: string[];
23
- jsIndex?: number;
24
- passages?: Record<string, Passage>;
25
- passageCount?: number;
26
- }
27
- interface Passage {
28
- text?: string;
29
- clear?: boolean;
30
- attributes?: string[];
31
- jsIndex?: number;
32
- }
33
- export declare const init: (options: SquiffyInitOptions) => SquiffyApi;
34
- export {};
@@ -1,29 +0,0 @@
1
- interface SquiffyInitOptions {
2
- element: HTMLElement;
3
- }
4
- interface SquiffySettings {
5
- scroll: string;
6
- persist: boolean;
7
- restartPrompt: boolean;
8
- onSet: (attribute: string, value: any) => void;
9
- }
10
- interface Squiffy {
11
- init: (options: SquiffyInitOptions) => void;
12
- story: any;
13
- ui: {
14
- output: HTMLElement;
15
- settings: SquiffySettings;
16
- processText: (text: string) => string;
17
- write: (text: string) => void;
18
- clearScreen: () => void;
19
- scrollToEnd: () => void;
20
- transition: (f: any) => void;
21
- };
22
- storageFallback: any;
23
- set: (attribute: string, value: any) => void;
24
- get: (attribute: string) => any;
25
- }
26
- export declare const squiffy: Squiffy;
27
- export declare const get: (attribute: string) => any;
28
- export declare const set: (attribute: string, value: any) => void;
29
- export {};