Skip to content

Commit

Permalink
foi-toy: starting setup (including build script with moduloze) for fo…
Browse files Browse the repository at this point in the history
…i-toy web tool
  • Loading branch information
getify committed Dec 27, 2022
1 parent 37c6cae commit ceaf8b2
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules/
foi-toy/web/js/tokenizer.js
foi-toy/web/js/highlighter.js
package-lock.json
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.npmignore
.gitignore
foi-toy/web/
node_modules/
8 changes: 5 additions & 3 deletions foi-toy/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var args = require("minimist")(process.argv.slice(2));
const { tokenize, } = require(path.join(__dirname,"src","tokenizer.js"));
const { highlight, } = require(path.join(__dirname,"src","highlighter.js"));

main();
main().catch(console.log);


// **********************
Expand All @@ -25,8 +25,10 @@ async function main() {
var tokens = tokenize(fileStream);

if (args.color) {
let tmplHTML = await fsp.readFile(path.join(__dirname,"src","tmpl.html"),"utf-8");
let tmplCSS = await fsp.readFile(path.join(__dirname,"src","tmpl.css"),"utf-8");
let [ tmplHTML, tmplCSS, ] = await Promise.all([
fsp.readFile(path.join(__dirname,"src","tmpl.html"),"utf-8"),
fsp.readFile(path.join(__dirname,"src","tmpl.css"),"utf-8"),
]);
let tmplParts = tmplHTML.split(/\<\/?(?:pre|style)\>/);

process.stdout.write(tmplParts[0]);
Expand Down
76 changes: 76 additions & 0 deletions foi-toy/scripts/build-web
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/env node

"use strict";

var path = require("path");
var fsp = require("fs/promises");

var { build, } = require("moduloze");

var config = {
buildESM: true
};

var depMap = {
"src/tokenizer.js": "Tokenizer",
"src/highlighter.js": "Highlighter"
};

const ROOT_DIR = path.join(__dirname,"..");
const SRC_DIR = path.join(ROOT_DIR,"src");
const WEB_JS_DIR = path.join(ROOT_DIR,"web","js");


main().catch(console.log);


// **********************

async function main() {
console.log("*** Building Foi-Toy Web ***");

var [
packageJSON,
copyrightHeader,
tokenizerCode,
highlighterCode
] = await Promise.all([
fsp.readFile(path.join(ROOT_DIR,"..","package.json"),"utf-8"),
fsp.readFile(path.join(SRC_DIR,"copyright-header.txt"),"utf-8"),
fsp.readFile(path.join(SRC_DIR,"tokenizer.js"),"utf-8"),
fsp.readFile(path.join(SRC_DIR,"highlighter.js"),"utf-8"),
]);

packageJSON = JSON.parse(packageJSON);

// read version number from package.json
var version = packageJSON.version;
var year = (new Date()).getFullYear();

copyrightHeader = copyrightHeader
.replace(/`/g,"")
.replace(/#VERSION#/g,version)
.replace(/#YEAR#/g,year);

var { esm: { code: tokenizerESM, }, } = build(
config,
"src/tokenizer.js",
tokenizerCode,
depMap
);
var { esm: { code: highlighterESM, }, } = build(
config,
"src/highlighter.js",
highlighterCode,
depMap
);
tokenizerESM = `${copyrightHeader.replace("#FILENAME#","tokenizer.js")}${tokenizerESM}`;
highlighterESM = `${copyrightHeader.replace("#FILENAME#","highlighter.js")}${highlighterESM}`;

await Promise.all([
fsp.writeFile(path.join(__dirname,"..","web","js","tokenizer.js"),tokenizerESM,"utf-8"),
fsp.writeFile(path.join(__dirname,"..","web","js","highlighter.js"),highlighterESM,"utf-8"),
]);

console.log("Complete.");
}
4 changes: 4 additions & 0 deletions foi-toy/src/copyright-header.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/*! Foi-Toy: #FILENAME#
v#VERSION# (c) #YEAR# Kyle Simpson
MIT License: http://getify.mit-license.org
*/
10 changes: 1 addition & 9 deletions foi-toy/src/highlighter.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
"use strict";

const {
OPERATORS,
NATIVES,
KEYWORDS,
BUILTINS,
COMPREHENSIONS,
BOOLEAN_NAMED_OPERATORS,
WHITESPACE,
} = require("./tokenizer.js");
const { OPERATORS, } = require("./tokenizer.js");

module.exports = {
highlight,
Expand Down
Empty file added foi-toy/web/css/style.css
Empty file.
12 changes: 12 additions & 0 deletions foi-toy/web/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Foi-Toy</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<h1>Foi-Toy</h1>
</body>
<script src="js/app.js"></script>
</html>
15 changes: 15 additions & 0 deletions foi-toy/web/js/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { tokenize, } from "./tokenizer.js";
import { highlight, } from "./highlighter.js";


main().catch(console.log);

// ****************************

async function main() {
var tokens = await tokenize("def x: \"Hello world!\"; log(x);");

for await (let htmlChunk of highlight(tokens)) {
console.log(htmlChunk);
}
}
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
{
"name": "foi-lang",
"version": "0.0.11",
"version": "0.0.12",
"description": "Foi: a different kind of functional programming language",
"repository": "getify/foi-lang",
"scripts": {
"build-foi-toy-web": "foi-toy/scripts/build-web",
"build": "npm run build-foi-toy-web"
},
"devDependencies": {
"minimist": "~1.2.7"
"minimist": "~1.2.7",
"moduloze": "~0.9.1"
},
"keywords": [
"fp",
Expand Down

0 comments on commit ceaf8b2

Please sign in to comment.