Skip to content

Commit

Permalink
Decaffeinate the codebase back to ES2015 natively
Browse files Browse the repository at this point in the history
  • Loading branch information
kirsle committed Jun 14, 2018
1 parent 7ef6d0e commit 303e466
Show file tree
Hide file tree
Showing 49 changed files with 6,281 additions and 75 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ lib.es6/
dist/
node_modules/
.idea/
test/*.js
test.babel/

#OS X
.DS_Store
36 changes: 36 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
SHELL := /bin/bash

BUILD=$(shell git describe --always)
CURDIR=$(shell pwd)

# `make setup` to set up a new environment, pull dependencies, etc.
.PHONY: setup
setup: clean
npm install
npm install -g babel-cli webpack uglify-js nodeunit

# `make run` to run it in debug mode.
.PHONY: run
run:
node shell.js eg/brain

# `make test` to run unit tests.
.PHONY: test
test:
nodeunit test

# `make build` to build code with Babel for older JS clients.
.PHONY: build
build:
npm run build
npm run test

# `make dist` to create a distribution for npm.
.PHONY: dist
dist: build
npm run dist

# `make clean` cleans everything up.
.PHONY: clean
clean:
npm run clean
30 changes: 20 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,19 +180,29 @@ may work right now, a future update will probably rigidly enforce this).

I use npm run scripts to handle various build tasks.

* `npm run build` - Compiles the CoffeeScript in the `src/` folder into
ES2015+ JavaScript in the `lib/` folder.
* `npm run lint` - Runs `coffeelint` on the source.
* `npm run test` - Builds RiveScript, compiles the CoffeeScript unit test files,
and runs them with `nodeunit`
* `npm run dist` - Produces a full distributable build. CoffeeScript is compiled,
Babel translates it to ES5-compatible code, and webpack/uglify is applied.
* `npm run webpack` - Runs Babel to take the ES2015+ code in `lib/` and output
ES5-compatible code in `lib.babel/`, and then runs `webpack` to create
`dist/rivescript.js`
* `npm run build` - Compiles the ES2015+ sources from `src/` with Babel and
outputs them into `lib/`
* `npm run test` - Builds the source with Babel per above, builds the ES2015+
test scripts in `test/` and outputs them into `test.babel/` and then runs
`nodeunit` on it.
* `npm run dist` - Produces a full distributable build. Source is built with
Babel and then handed off to webpack and uglify for browser builds.
* `npm run webpack` - Creates `dist/rivescript.js` from the ES2015+ sources
directly from `src/` (using `babel-loader`). This command is independent from
`npm run build` and could be run without leaving any ES5 code sitting around.
* `npm run uglify` - Minifies `dist/rivescript.js` to `dist/rivescript.min.js`
* `npm run clean` - Clean up all the build files.

If your local Node version is >= 7 (supports Async/Await), you can run the
ES2015+ sources directly without needing to run any npm scripts. For that
purpose, I have a Makefile.

* `make setup` - sets up the dev environment, installs dependencies, etc.
* `make run` - runs `shell.js` pointed at the example brain. This script runs
natively on the ES2015+ sources, no build steps needed.
* `make test` - runs `nodeunit` on the ES2015+ test sources directly without
building them as `npm run test` would.

## GRUNT SERVER

This project uses [Grunt](http://gruntjs.com) for compiling to minified JS and
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
35 changes: 18 additions & 17 deletions eg/web-client/chat.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<link rel="stylesheet" type="text/css" href="chat.css">
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="datadumper.js"></script>
<script type="text/javascript" src="../lib/rivescript.js"></script>
<script type="text/javascript" src="../../dist/rivescript.js"></script>
</head>
<body>

Expand Down Expand Up @@ -65,22 +65,23 @@ <h1>Chat</h1>

// Load our files from the brain/ folder.
rs.loadFile([
"brain/begin.rive",
"brain/admin.rive",
"brain/clients.rive",
"brain/eliza.rive",
"brain/myself.rive",
"brain/rpg.rive",
"brain/javascript.rive"
"../brain/begin.rive",
"../brain/admin.rive",
"../brain/clients.rive",
"../brain/eliza.rive",
"../brain/myself.rive",
"../brain/rpg.rive",
"../brain/javascript.rive"
], on_load_success, on_load_error);

// You can register objects that can then be called
// You can register objects that can then be called
// using <call></call> syntax
rs.setSubroutine('fancyJSObject', function(rs, args){
// doing complex stuff here
});

function on_load_success () {
$("#dialogue").append("<div><span class='bot'>Bot:</span> Welcome, this is RiveScript.js v" + rs.version() + ", ready to chat!");
$("#message").removeAttr("disabled");
$("#message").attr("placeholder", "Send message");
$("#message").focus();
Expand All @@ -90,23 +91,23 @@ <h1>Chat</h1>
}

function on_load_error (err) {
console.log("Loading error: " + err);
window.alert("Loading error: " + err);
}

// Handle sending a message to the bot.
function sendMessage () {
var text = $("#message").val();
$("#message").val("");
$("#dialogue").append("<div><span class='user'>You:</span> " + text + "</div>");
try {
var reply = rs.reply("soandso", text);
reply = reply.replace(/\n/g, "<br>");
$("#dialogue").append("<div><span class='bot'>Bot:</span> " + reply + "</div>");
$("#dialogue").animate({ scrollTop: $("#dialogue").height() }, 1000);
} catch(e) {

rs.reply("soandso", text, this).then(function(reply) {
reply = reply.replace(/\n/g, "<br>");
$("#dialogue").append("<div><span class='bot'>Bot:</span> " + reply + "</div>");
$("#dialogue").animate({ scrollTop: $("#dialogue").height() }, 1000);
}).catch(function(e) {
window.alert(e.message + "\n" + e.line);
console.log(e);
}
});

return false;
}
Expand Down
Loading

1 comment on commit 303e466

@julien-c
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍👍 This is awesome @kirsle

Please sign in to comment.