Skip to content
This repository has been archived by the owner on May 28, 2019. It is now read-only.

Commit

Permalink
Package dist/gherkin.js with license. Should be AMD compatible. Example.
Browse files Browse the repository at this point in the history
  • Loading branch information
aslakhellesoy committed Apr 2, 2015
1 parent 9138a76 commit 55d2419
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 17 deletions.
18 changes: 7 additions & 11 deletions javascript/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ TOKENS = $(patsubst ../testdata/%.feature,acceptance/testdata/%.feature.tokens
ASTS = $(patsubst ../testdata/%.feature,acceptance/testdata/%.feature.ast.json,$(GOOD_FEATURE_FILES))
ERRORS = $(patsubst ../testdata/%.feature,acceptance/testdata/%.feature.errors,$(BAD_FEATURE_FILES))

JAVASCRIPT_FILES = $(shell find . -name "*.js")
JAVASCRIPT_FILES = $(shell find lib -name "*.js") index.js

all: .compared
.PHONY: all

.compared: .built $(TOKENS) $(ASTS) $(ERRORS)
touch $@

.built: lib/gherkin/parser.js lib/gherkin/dialects.json $(JAVASCRIPT_FILES) node_modules
.built: lib/gherkin/parser.js lib/gherkin/dialects.json $(JAVASCRIPT_FILES) dist/gherkin.js node_modules
./node_modules/.bin/mocha
touch $@

Expand Down Expand Up @@ -47,16 +47,12 @@ lib/gherkin/parser.js: ../gherkin.berp gherkin-javascript.razor ../bin/berp.exe
lib/gherkin/dialects.json: ../dialects.json
cp $^ $@

dist/gherkin.js: .compared node_modules/tea-error/lib-cov/error.js
mkdir -p dist
./node_modules/.bin/browserify index.js --outfile dist/gherkin.js --ignore-missing

# node-modules/tea-error/index.js requires this file (for its own code coverage build)
# this is a hack to make amdify work (browserify has an --ignore-missing flag, but amdify
# does not)
node_modules/tea-error/lib-cov/error.js:
dist/gherkin.js: lib/gherkin/parser.js ../LICENSE
mkdir -p `dirname $@`
touch $@
echo '/*' > $@
cat ../LICENSE >> $@
echo '*/' >> $@
./node_modules/.bin/browserify index.js --ignore-missing >> $@

clean:
rm -rf .compared .built acceptance lib/gherkin/parser.js lib/gherkin/dialects.json dist
Expand Down
1 change: 1 addition & 0 deletions javascript/examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
5 changes: 5 additions & 0 deletions javascript/examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
This is a minimal example demonstrating Gherkin in the browser.
Edit a feature file, see the AST below.

To run the example, run `make dist/gherkin.js` from the `javascript` directory.
Then open `index.html` in a browser and type in the text area.
25 changes: 25 additions & 0 deletions javascript/examples/demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var input = document.getElementById('input');
var output = document.getElementById('output');
var parser = new Gherkin.Parser();

function parse() {
console.log('parsing');
var result;
try {
var builder = new Gherkin.AstBuilder();
parser.stopAtFirstError = false;

var scanner = new Gherkin.TokenScanner(input.value);
var ast = parser.parse(scanner, builder, new Gherkin.TokenMatcher());
result = JSON.stringify(ast, null, 2);
} catch (e) {
result = e.message;
}
output.innerText = result;
}

input.onkeyup = function () {
parse();
};

parse();
13 changes: 13 additions & 0 deletions javascript/examples/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html>
<head>
</head>

<body>
<textarea id="input" cols="30" rows="10">Feature: Hello</textarea>
<br>
<pre id="output"></pre>
<script src="../dist/gherkin.js"></script>
<script src="demo.js"></script>
</body>
</html>
25 changes: 19 additions & 6 deletions javascript/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
module.exports = {
Parser: require('./lib/gherkin/parser'),
TokenScanner: require('./lib/gherkin/token_scanner'),
TokenMatcher: require('./lib/gherkin/token_matcher'),
AstBuilder: require('./lib/gherkin/ast_builder')
};
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof window === 'object') {
// Browser globals (root is window)
window.Gherkin = factory();
} else {
// Node.js/IO.js
module.exports = factory();
}
}(this, function () {
return {
Parser: require('./lib/gherkin/parser'),
TokenScanner: require('./lib/gherkin/token_scanner'),
TokenMatcher: require('./lib/gherkin/token_matcher'),
AstBuilder: require('./lib/gherkin/ast_builder')
};
}));

0 comments on commit 55d2419

Please sign in to comment.