Skip to content

Commit

Permalink
0.0.0
Browse files Browse the repository at this point in the history
first commit
  • Loading branch information
busterc committed Apr 13, 2018
0 parents commit 1f1ee0d
Show file tree
Hide file tree
Showing 13 changed files with 9,599 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
root = true

[*]
indent_style = space
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
coverage
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
coverage
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: node_js
node_js:
- v6
after_script: cat ./coverage/lcov.info | coveralls
15 changes: 15 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Copyright (c) 2018 Buster Collings <busterc@gmail.com> (https://about.me/buster)

Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all
copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
102 changes: 102 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# ndjson-generate-schema [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][daviddm-image]][daviddm-url] [![Coverage percentage][coveralls-image]][coveralls-url]

> Effortlessly convert your NDJSON data to a JSON Schema.
* Powered by: [`generate-schema`](https://github.com/nijikokun/generate-schema)

## Installation

```sh
$ npm install --save ndjson-generate-schema

# Or to use the CLI globally
$ npm install --global ndjson-generate-schema
```

## Usage

Given an NDJSON file `dogs.db`:

```
{"id":1,"name":"Buddy","breed":"Boston Terrier"}
{"id":2,"name":"Charley"}
```

### CLI

```sh
$ ndjson-generate-schema

Effortlessly convert your NDJSON data to a JSON Schema.

Usage

$ ndjson-generate-schema <title> <file> [outfile]

Inputs

title Required, Title of Schema
file Required, NDJSON file to read
outfile Optional, filename to write Schema out to
```

### Module

```js
const path = require('path');
const ndjsonGenerateSchema = require('ndjson-generate-schema');

ndjsonGenerateSchema('Dogs', path.resolve(__dirname, 'dogs.db')).then(
schema => {
console.log(schema);
/*
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Dogs Set",
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "number"
},
"name": {
"type": "string"
},
"breed": {
"type": "string"
}
},
"required": [
"id",
"name"
],
"title": "Dogs"
}
}
*/
}
);
```

## API

### `ndjsonGenerateSchema(title, file)`

* ### `title`
* `Required` : `String` the title of the Schema
* ### `file`
* `Required` : `String` the NDJSON file to read

## License

ISC © [Buster Collings](https://about.me/buster)

[npm-image]: https://badge.fury.io/js/ndjson-generate-schema.svg
[npm-url]: https://npmjs.org/package/ndjson-generate-schema
[travis-image]: https://travis-ci.org/busterc/ndjson-generate-schema.svg?branch=master
[travis-url]: https://travis-ci.org/busterc/ndjson-generate-schema
[daviddm-image]: https://david-dm.org/busterc/ndjson-generate-schema.svg?theme=shields.io
[daviddm-url]: https://david-dm.org/busterc/ndjson-generate-schema
[coveralls-image]: https://coveralls.io/repos/busterc/ndjson-generate-schema/badge.svg
[coveralls-url]: https://coveralls.io/r/busterc/ndjson-generate-schema
2 changes: 2 additions & 0 deletions lib/__tests__/dogs.db
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{"id":1,"name":"Buddy","breed":"Boston Terrier"}
{"id":2,"name":"Charley"}
12 changes: 12 additions & 0 deletions lib/__tests__/ndjsonGenerateSchema.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const path = require('path');
const assert = require('assert');
const ndjsonGenerateSchema = require('../index.js');

describe('ndjsonGenerateSchema', () => {
it('generates schema', () => {
ndjsonGenerateSchema('Dogs', path.resolve(__dirname, 'dogs.db')).then(schema => {
assert(schema.items.required.find(x => x === 'id'));
assert(!schema.items.required.find(x => x === 'breed'));
});
});
});
37 changes: 37 additions & 0 deletions lib/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env node
'use strict';
const path = require('path');
const fs = require('fs');
const meow = require('meow');
const ndjsonGenerateSchema = require('./');

const cli = meow(`
Usage
$ ndjson-generate-schema <title> <file> [outfile]
Inputs
title Required, Title of Schema
file Required, NDJSON file to read
outfile Optional, filename to write Schema out to
`);

(function() {
if (cli.input.length < 2) {
return cli.showHelp();
}
ndjsonGenerateSchema(cli.input[0], cli.input[1])
.then(schema => {
const schemaJson = JSON.stringify(schema, null, 2);
if (cli.input[2]) {
const outfile = path.resolve(process.cwd(), cli.input[2]);
return fs.writeFileSync(outfile, schemaJson);
}
console.log(schemaJson);
})
.catch(error => {
console.error(error);
});
})();
10 changes: 10 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

const GenerateSchema = require('generate-schema');
const fs = require('fs-ndjson');

module.exports = function(title, file) {
return fs.readFile(file).then(data => {
return GenerateSchema.json(title, data);
});
};
Loading

0 comments on commit 1f1ee0d

Please sign in to comment.