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.
Files changed (54) hide show
  1. package/README.md +3 -0
  2. package/dist/compiler.d.ts +51 -0
  3. package/dist/compiler.js +542 -0
  4. package/dist/compiler.test.d.ts +1 -0
  5. package/dist/compiler.test.js +73 -0
  6. package/dist/external-files.d.ts +5 -0
  7. package/dist/external-files.js +21 -0
  8. package/dist/index.template.html +39 -0
  9. package/dist/packager.d.ts +1 -0
  10. package/dist/packager.js +78 -0
  11. package/dist/server.d.ts +1 -0
  12. package/dist/server.js +15 -0
  13. package/dist/squiffy.d.ts +1 -0
  14. package/dist/squiffy.js +29 -0
  15. package/dist/squiffy.runtime.d.ts +34 -0
  16. package/dist/squiffy.template.d.ts +29 -0
  17. package/dist/squiffy.template.js +598 -0
  18. package/dist/style.template.css +52 -0
  19. package/dist/version.d.ts +1 -0
  20. package/dist/version.js +1 -0
  21. package/examples/attributes/attributes.squiffy +81 -0
  22. package/examples/clearscreen/clearscreen.squiffy +15 -0
  23. package/examples/continue/continue.squiffy +18 -0
  24. package/examples/helloworld/helloworld.squiffy +1 -0
  25. package/examples/import/file2.squiffy +8 -0
  26. package/examples/import/test.js +3 -0
  27. package/examples/import/test.squiffy +5 -0
  28. package/examples/input/input.squiffy +22 -0
  29. package/examples/last/last.squiffy +32 -0
  30. package/examples/master/master.squiffy +35 -0
  31. package/examples/replace/replace.squiffy +27 -0
  32. package/examples/rotate/rotate.squiffy +25 -0
  33. package/examples/sectiontrack/sectiontrack.squiffy +16 -0
  34. package/examples/start/start.squiffy +7 -0
  35. package/examples/test/example.squiffy +52 -0
  36. package/examples/textprocessor/textprocessor.squiffy +21 -0
  37. package/examples/transitions/transitions.squiffy +53 -0
  38. package/examples/turncount/turncount.squiffy +41 -0
  39. package/examples/warnings/warnings.squiffy +23 -0
  40. package/examples/warnings/warnings2.squiffy +3 -0
  41. package/package.json +46 -0
  42. package/src/__snapshots__/compiler.test.ts.snap +716 -0
  43. package/src/compiler.test.ts +86 -0
  44. package/src/compiler.ts +546 -0
  45. package/src/external-files.ts +22 -0
  46. package/src/index.template.html +39 -0
  47. package/src/packager.ts +97 -0
  48. package/src/server.ts +19 -0
  49. package/src/squiffy.runtime.ts +670 -0
  50. package/src/squiffy.ts +36 -0
  51. package/src/style.template.css +52 -0
  52. package/src/version.ts +1 -0
  53. package/tsconfig.json +22 -0
  54. package/tsconfig.runtime.json +12 -0
@@ -0,0 +1,97 @@
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
+
7
+ export const createPackage = async (inputFilename: string) => {
8
+ return await generate(inputFilename);
9
+ }
10
+
11
+ async function generate(inputFilename: string) {
12
+ console.log('Loading ' + inputFilename);
13
+
14
+ var inputFile = fs.readFileSync(inputFilename);
15
+ var inputText = inputFile.toString();
16
+
17
+ const result = await compile({
18
+ scriptBaseFilename: path.basename(inputFilename),
19
+ script: inputText,
20
+ onWarning: console.warn,
21
+ externalFiles: externalFiles(inputFilename)
22
+ });
23
+
24
+ if (!result.success) {
25
+ console.log('Failed.');
26
+ return;
27
+ }
28
+
29
+ var storyJsName = /* typeof options.scriptOnly === 'string' ? options.scriptOnly : */ 'story.js';
30
+
31
+ console.log('Writing ' + storyJsName);
32
+
33
+ var storyJs = await result.getJs();
34
+
35
+ var outputPath = path.resolve(path.dirname(inputFilename));
36
+ fs.writeFileSync(path.join(outputPath, storyJsName), storyJs);
37
+
38
+ const uiInfo = result.getUiInfo();
39
+
40
+ console.log('Writing squiffy.runtime.js');
41
+ fs.copyFileSync(path.join(import.meta.dirname, 'squiffy.runtime.js'), path.join(outputPath, 'squiffy.runtime.js'));
42
+
43
+ var cssTemplateFile = fs.readFileSync(findFile('style.template.css', outputPath /*, sourcePath */));
44
+ var cssData = cssTemplateFile.toString();
45
+ fs.writeFileSync(path.join(outputPath, 'style.css'), cssData);
46
+
47
+ console.log('Writing index.html');
48
+
49
+ var htmlTemplateFile = fs.readFileSync(findFile('index.template.html', outputPath /*, sourcePath */));
50
+ var htmlData = htmlTemplateFile.toString();
51
+ htmlData = htmlData.replace('<!-- INFO -->', `<!--\n\nCreated with Squiffy ${SQUIFFY_VERSION}\n\n\nhttps://github.com/textadventures/squiffy\n\n-->`);
52
+ htmlData = htmlData.replace('<!-- TITLE -->', uiInfo.title);
53
+ var scriptData = uiInfo.externalScripts.map(script => `<script src="${script}"></script>`).join('\n');
54
+ htmlData = htmlData.replace('<!-- SCRIPTS -->', scriptData);
55
+
56
+ var stylesheetData = uiInfo.externalStylesheets.map(sheet => `<link rel="stylesheet" href="${sheet}"/>`).join('\n');
57
+ htmlData = htmlData.replace('<!-- STYLESHEETS -->', stylesheetData);
58
+
59
+ fs.writeFileSync(path.join(outputPath, 'index.html'), htmlData);
60
+
61
+ console.log('Writing style.css');
62
+ var cssTemplateFile = fs.readFileSync(findFile('style.template.css', outputPath /*, sourcePath */));
63
+ var cssData = cssTemplateFile.toString();
64
+ fs.writeFileSync(path.join(outputPath, 'style.css'), cssData);
65
+
66
+ // if (options.zip) {
67
+ // console.log('Creating zip file');
68
+ // var JSZip = require('jszip');
69
+ // var zip = new JSZip();
70
+ // zip.file(storyJsName, storyJs);
71
+ // zip.file('index.html', htmlData);
72
+ // zip.file('style.css', cssData);
73
+ // var buffer = zip.generate({
74
+ // type: 'nodebuffer'
75
+ // });
76
+ // if (options.write) {
77
+ // fs.writeFileSync(path.join(outputPath, 'output.zip'), buffer);
78
+ // }
79
+ // else {
80
+ // return buffer;
81
+ // }
82
+ // }
83
+
84
+ console.log('Done.');
85
+
86
+ return outputPath;
87
+ };
88
+
89
+ function findFile(filename: string, outputPath: string /*, sourcePath: string */) {
90
+ if (outputPath) {
91
+ var outputPathFile = path.join(outputPath, filename);
92
+ if (fs.existsSync(outputPathFile)) {
93
+ return outputPathFile;
94
+ }
95
+ }
96
+ return path.join(import.meta.dirname, filename);
97
+ };
package/src/server.ts ADDED
@@ -0,0 +1,19 @@
1
+ import finalhandler from 'finalhandler';
2
+ import * as http from 'http';
3
+ import serveStatic from 'serve-static';
4
+
5
+ function startServer(dir: string, port: number) {
6
+ var serve = serveStatic(dir, { index: ['index.html'] });
7
+
8
+ var server = http.createServer(function (req: any, res: any) {
9
+ var done = finalhandler(req, res);
10
+ serve(req, res, done);
11
+ });
12
+
13
+ server.listen(port);
14
+ }
15
+
16
+ export const serve = (directory: string, port: number) => {
17
+ startServer(directory, port);
18
+ console.log('Started http://localhost:' + port + '/');
19
+ }