Skip to content
This repository has been archived by the owner on Nov 9, 2022. It is now read-only.

Commit

Permalink
gherkin: Add TypeScript definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
aslakhellesoy committed Sep 13, 2018
1 parent a7cfd9e commit 6e1878f
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 22 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ ERRORS = $(patsubst testdata/%.feature,acceptance/testdata/%.feature.error
-npm link c21e
touch $@

.tested: .compared
.tested: dist/src/index.d.ts .compared

.compared: $(ASTS) $(PICKLES) $(ERRORS) $(SOURCES)
touch $@

dist/src/index.d.ts: src/index.d.ts
cp $< $@

acceptance/testdata/%.feature.ast.ndjson: testdata/%.feature testdata/%.feature.ast.ndjson .deps
mkdir -p `dirname $@`
bin/gherkin --no-source --no-pickles $< | jq --sort-keys --compact-output "." > $@
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ Gherkin parser/compiler for JavaScript. Please see [Gherkin](https://github.com/
## Usage

```javascript
const Gherkin = require('gherkin')
const gherkin = require('gherkin')

const options = {
includeSource: true,
includeGherkinDocument: true,
includePickles: true,
}
const stream = Gherkin.fromPaths(['features/hello.feature'])
const stream = gherkin.fromPaths(['features/hello.feature'])

// Pipe the stream to another stream that can read messages.
stream.pipe(...)
```
```
4 changes: 2 additions & 2 deletions bin/gherkin
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node
'use strict'
const Stream = require('stream')
const Gherkin = require('../src/Gherkin')
const gherkin = require('../src/index')
const args = process.argv.slice(2)
const options = {
includeSource: true,
Expand Down Expand Up @@ -44,4 +44,4 @@ const protobufNdjsonStream = new Stream.Transform({
}
})

Gherkin.fromPaths(paths, options).pipe(protobufNdjsonStream).pipe(process.stdout)
gherkin.fromPaths(paths, options).pipe(protobufNdjsonStream).pipe(process.stdout)
15 changes: 10 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
"name": "gherkin",
"version": "5.1.0",
"description": "Gherkin parser",
"main": "dist/src/Gherkin.js",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
"bin": {
"gherkin-javascript": "bin/gherkin"
},
"scripts": {
"test": "mocha",
"test": "mocha test/**/*.{ts,js}",
"eslint-fix": "eslint --fix src test",
"coverage": "nyc --reporter=html --reporter=text mocha",
"build": "babel src --out-dir dist/src",
Expand Down Expand Up @@ -36,17 +37,21 @@
"@babel/plugin-transform-async-to-generator": "^7.0.0",
"@babel/polyfill": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"@types/mocha": "^5.2.5",
"@types/node": "^10.9.4",
"eslint": "^5.3.0",
"eslint-config-eslint": "^5.0.1",
"eslint-config-prettier": "^2.9.0",
"eslint-plugin-node": "^6.0.1",
"eslint-plugin-prettier": "^2.6.2",
"mocha": "^5.2.0",
"nyc": "^12.0.2",
"prettier": "^1.8.2"
"prettier": "^1.8.2",
"ts-node": "^7.0.1",
"typescript": "^3.0.3"
},
"dependencies": {
"cucumber-messages": "cucumber/cucumber-messages-javascript",
"c21e": "cucumber/c21e-javascript"
"c21e": "cucumber/c21e-javascript",
"cucumber-messages": "cucumber/cucumber-messages-javascript"
}
}
12 changes: 12 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {io} from "cucumber-messages/src/cucumber-messages";
import * as Stream from "stream";
import Source = io.cucumber.messages.Source;

export interface IGherkinOptions {
includeSource: boolean,
includeGherkinDocument: boolean,
includePickles: boolean,
}

export function fromPaths(args: string[], options?: IGherkinOptions): Stream.Readable;
export function fromSources(args: Source[], options?: IGherkinOptions): Stream.Readable;
19 changes: 12 additions & 7 deletions src/Gherkin.js → src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@ const ExeFile = require('c21e')
const cm = require('cucumber-messages').io.cucumber.messages
const ProtobufMessageStream = require('./ProtobufMessageStream')

module.exports = class Gherkin {
static fromPaths(paths, options) {
return new this(paths, [], options).messageStream()
}
function fromPaths(paths, options) {
return new Gherkin(paths, [], options).messageStream()
}

static fromSources(sources, options) {
return new this([], sources, options).messageStream()
}
function fromSources(sources, options) {
return new Gherkin([], sources, options).messageStream()
}

module.exports = {
fromPaths,
fromSources,
}

class Gherkin {
constructor(paths, sources, options) {
this._paths = paths
this._sources = sources
Expand Down
8 changes: 4 additions & 4 deletions test/GherkinTest.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* eslint-env mocha */
const assert = require('assert')
const cm = require('cucumber-messages').io.cucumber.messages
const Gherkin = require('../src/Gherkin')
const { fromPaths, fromSources } = require('../src/index')

describe('Gherkin', () => {
describe('gherkin', () => {
it('parses gherkin from the file system', async () => {
const messages = await streamToArray(
Gherkin.fromPaths(['testdata/good/minimal.feature'])
fromPaths(['testdata/good/minimal.feature'])
)
assert.strictEqual(messages.length, 3)
})
Expand All @@ -25,7 +25,7 @@ describe('Gherkin', () => {
}),
})

const messages = await streamToArray(Gherkin.fromSources([source]))
const messages = await streamToArray(fromSources([source]))
assert.strictEqual(messages.length, 3)
})
})
Expand Down
44 changes: 44 additions & 0 deletions test/GherkinTypeScriptTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* eslint-env mocha */
import * as assert from 'assert'
import * as gherkin from "../src";
import {Readable} from "stream";
import {io} from "cucumber-messages";
import Wrapper = io.cucumber.messages.Wrapper;
import Source = io.cucumber.messages.Source;
import Media = io.cucumber.messages.Media;

describe('gherkin', () => {
it('parses gherkin from the file system', async () => {
const wrappers = await streamToArray(
gherkin.fromPaths(['testdata/good/minimal.feature'])
)
assert.strictEqual(wrappers.length, 3)
})

it('parses gherkin from STDIN', async () => {
const source = Source.fromObject({
uri: 'test.feature',
data: `Feature: Minimal
Scenario: minimalistic
Given the minimalism
`,
media: Media.fromObject({
encoding: 'UTF-8',
contentType: 'text/x.cucumber.gherkin+plain',
}),
})

const wrappers = await streamToArray(gherkin.fromSources([source]))
assert.strictEqual(wrappers.length, 3)
})
})

async function streamToArray(readableStream: Readable): Promise<Wrapper[]> {
return new Promise<Wrapper[]>((resolve: (wrappers:Wrapper[]) => void, reject: (err:Error) => void) => {
const items: Wrapper[] = []
readableStream.on('data', items.push.bind(items))
readableStream.on('error', (err:Error) => reject(err))
readableStream.on('end', () => resolve(items))
})
}
1 change: 1 addition & 0 deletions test/mocha.opts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
--recursive
--require ts-node/register
14 changes: 14 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"lib": ["es6"],
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"sourceMap": true,
"rootDir": ".",
"outDir": "dist"
},
"files": [
"test/GherkinTypeScriptTest.ts"
]
}

0 comments on commit 6e1878f

Please sign in to comment.