squiffy-compiler 6.0.0-alpha.0
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/README.md +3 -0
- package/dist/compiler.d.ts +51 -0
- package/dist/compiler.js +542 -0
- package/dist/compiler.test.d.ts +1 -0
- package/dist/compiler.test.js +73 -0
- package/dist/external-files.d.ts +5 -0
- package/dist/external-files.js +21 -0
- package/dist/index.template.html +39 -0
- package/dist/packager.d.ts +1 -0
- package/dist/packager.js +78 -0
- package/dist/server.d.ts +1 -0
- package/dist/server.js +15 -0
- package/dist/squiffy.d.ts +1 -0
- package/dist/squiffy.js +29 -0
- package/dist/squiffy.runtime.d.ts +34 -0
- package/dist/squiffy.template.d.ts +29 -0
- package/dist/squiffy.template.js +598 -0
- package/dist/style.template.css +52 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +1 -0
- package/examples/attributes/attributes.squiffy +81 -0
- package/examples/clearscreen/clearscreen.squiffy +15 -0
- package/examples/continue/continue.squiffy +18 -0
- package/examples/helloworld/helloworld.squiffy +1 -0
- package/examples/import/file2.squiffy +8 -0
- package/examples/import/test.js +3 -0
- package/examples/import/test.squiffy +5 -0
- package/examples/input/input.squiffy +22 -0
- package/examples/last/last.squiffy +32 -0
- package/examples/master/master.squiffy +35 -0
- package/examples/replace/replace.squiffy +27 -0
- package/examples/rotate/rotate.squiffy +25 -0
- package/examples/sectiontrack/sectiontrack.squiffy +16 -0
- package/examples/start/start.squiffy +7 -0
- package/examples/test/example.squiffy +52 -0
- package/examples/textprocessor/textprocessor.squiffy +21 -0
- package/examples/transitions/transitions.squiffy +53 -0
- package/examples/turncount/turncount.squiffy +41 -0
- package/examples/warnings/warnings.squiffy +23 -0
- package/examples/warnings/warnings2.squiffy +3 -0
- package/package.json +46 -0
- package/src/__snapshots__/compiler.test.ts.snap +716 -0
- package/src/compiler.test.ts +86 -0
- package/src/compiler.ts +546 -0
- package/src/external-files.ts +22 -0
- package/src/index.template.html +39 -0
- package/src/packager.ts +97 -0
- package/src/server.ts +19 -0
- package/src/squiffy.runtime.ts +670 -0
- package/src/squiffy.ts +36 -0
- package/src/style.template.css +52 -0
- package/src/version.ts +1 -0
- package/tsconfig.json +22 -0
- package/tsconfig.runtime.json +12 -0
|
@@ -0,0 +1,21 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
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>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const createPackage: (inputFilename: string) => Promise<string | undefined>;
|
package/dist/packager.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
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
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const serve: (directory: string, port: number) => void;
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/squiffy.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
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 {};
|
|
@@ -0,0 +1,29 @@
|
|
|
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 {};
|