Skip to content

Commit

Permalink
Check if directory exists on loadDirectory()
Browse files Browse the repository at this point in the history
  • Loading branch information
kirsle committed May 16, 2016
1 parent f246777 commit 74c5e52
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 23 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ through npm:

`npm install rivescript`

For the web you can use the npmcdn:

```html
<script src="https://npmcdn.com/rivescript@latest/dist/rivescript.min.js"></script>
```

The GitHub repository for this project only includes the CoffeeScript sources.
To download a compiled JavaScript release of this library, check the
[Releases](https://github.com/aichaos/rivescript-js/releases) tab. The compiled
Expand Down
8 changes: 8 additions & 0 deletions contrib/coffeescript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

This provides a CoffeeScript language handler for RiveScript object macros.

# Installation

```npm install rivescript-contrib-coffeescript```

```html
<script src="https://npmcdn.com/rivescript-contrib-coffeescript@latest/dist/rivescript-contrib-coffeescript.min.js"></script>
```

# Usage

## In nodejs
Expand Down
14 changes: 6 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@
"author": "Noah Petherbridge <[email protected]> (http://www.kirsle.net/)",
"main": "lib/rivescript.js",
"files": [
"lib/rivescript.js",
"lib/brain.js",
"lib/inheritance.js",
"lib/parser.js",
"lib/sorting.js",
"lib/utils.js",
"lib/lang/coffee.js",
"lib/lang/javascript.js"
"README.md",
"LICENSE",
"Changes.md",
"lib/",
"dist/",
"docs/"
],
"directories": {
"lib": "lib",
Expand Down
2 changes: 2 additions & 0 deletions shell.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ bot.loadDirectory opts.brain, (batch_num) ->
rl.prompt()
.on "close", () ->
process.exit 0
, (err, loadBatch) ->
console.error "Loading error: #{err}"

help = () ->
console.log """Supported commands:
Expand Down
6 changes: 5 additions & 1 deletion shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var bot = new RiveScript({
debug: opts.debug,
utf8: opts.utf8,
});
bot.loadDirectory(opts.brain, loading_done);
bot.loadDirectory(opts.brain, loading_done, loading_error);

function loading_done(batch_num) {
bot.sortReplies();
Expand Down Expand Up @@ -103,6 +103,10 @@ function loading_done(batch_num) {
});
}

function loading_error(error, loadBatch) {
console.error("Loading error: " + error);
}

function help() {
console.log("Supported commands:");
console.log("/help : Show this text.");
Expand Down
42 changes: 28 additions & 14 deletions src/rivescript.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -373,20 +373,34 @@ class RiveScript
@_pending[loadCount] = {}
toLoad = []

@say "Loading batch #{loadCount} from directory #{path}"

# Load all the files.
files = readDir(path)
for file in files
if file.match(/\.(rive|rs)$/i)
# Keep track of the file's status.
@_pending[loadCount][path+"/"+file] = 1
toLoad.push path+"/"+file

# Load all the files.
for file in toLoad
@say "Parsing file #{file} from directory"
@_nodeLoadFile loadCount, file, onSuccess, onError
# Default error handler/dummy function.
if not onError?
onError = () ->
return

# Verify the directory exists.
@_node.fs.stat(path, (err, stats) =>
if err
return onError.call undefined, err, loadCount

if not stats.isDirectory()
return onError.call undefined, "#{path} is not a directory", loadCount

@say "Loading batch #{loadCount} from directory #{path}"

# Load all the files.
files = readDir(path)
for file in files
if file.match(/\.(rive|rs)$/i)
# Keep track of the file's status.
@_pending[loadCount][path+"/"+file] = 1
toLoad.push path+"/"+file

# Load all the files.
for file in toLoad
@say "Parsing file #{file} from directory"
@_nodeLoadFile loadCount, file, onSuccess, onError
)

##
# bool stream (string code[, func onError])
Expand Down

0 comments on commit 74c5e52

Please sign in to comment.