From e5889897917921745697213a77584703208239eb Mon Sep 17 00:00:00 2001 From: "Krefeldt, Jens" Date: Tue, 12 Apr 2016 11:05:07 +0200 Subject: [PATCH 01/12] Correct docs and include logo --- README.md | 4 +++- docs/BADGES.md | 2 ++ docs/USAGE.md | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b09b8e6..0a2b123 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +![jy-transform logo](https://raw.githubusercontent.com/deadratfink/jy-transform/master/image/jytransform.png) + # Stats | [Github License](https://github.com/deadratfink/jy-transform/blob/master/LICENSE.md) | [Github Issues](https://github.com/deadratfink/jy-transform/issues) | [Github Release](https://github.com/deadratfink/jy-transform/releases) | [Github Tags](https://github.com/deadratfink/jy-transform/tags) | [Travis CI](https://travis-ci.org) | [Waffle](https://waffle.io/deadratfink/jy-transform) | [Code Climate](https://codeclimate.com/github/deadratfink/jy-transform) | @@ -295,7 +297,7 @@ Options: file name, e.g. in case of foo.yaml it would be foo(1).yaml. -m, --imports STRING Define a 'module.exports[.identifier] = ' - identifier (to read from JS _source_ file only, must + identifier (to read from JS source file only, must be a valid JS identifier!). -x, --exports STRING Define a 'module.exports[.identifier] = ' identifier, for usage in JS destination file only, diff --git a/docs/BADGES.md b/docs/BADGES.md index f8a311f..738c50c 100644 --- a/docs/BADGES.md +++ b/docs/BADGES.md @@ -1,3 +1,5 @@ +![jy-transform logo](https://raw.githubusercontent.com/deadratfink/jy-transform/master/image/jytransform.png) + # Stats | [Github License](https://github.com/deadratfink/jy-transform/blob/master/LICENSE.md) | [Github Issues](https://github.com/deadratfink/jy-transform/issues) | [Github Release](https://github.com/deadratfink/jy-transform/releases) | [Github Tags](https://github.com/deadratfink/jy-transform/tags) | [Travis CI](https://travis-ci.org) | [Waffle](https://waffle.io/deadratfink/jy-transform) | [Code Climate](https://codeclimate.com/github/deadratfink/jy-transform) | diff --git a/docs/USAGE.md b/docs/USAGE.md index 748650c..1d27972 100644 --- a/docs/USAGE.md +++ b/docs/USAGE.md @@ -147,7 +147,7 @@ Options: file name, e.g. in case of foo.yaml it would be foo(1).yaml. -m, --imports STRING Define a 'module.exports[.identifier] = ' - identifier (to read from JS _source_ file only, must + identifier (to read from JS source file only, must be a valid JS identifier!). -x, --exports STRING Define a 'module.exports[.identifier] = ' identifier, for usage in JS destination file only, From 33aed6fb79923fc8c73bdd952ca50778160c7b15 Mon Sep 17 00:00:00 2001 From: "Krefeldt, Jens" Date: Wed, 13 Apr 2016 22:59:20 +0200 Subject: [PATCH 02/12] Doc update and corrections --- README.md | 79 +++++++++++++++++++++++++++++++---------------------- VERSION.txt | 1 + 2 files changed, 48 insertions(+), 32 deletions(-) create mode 100644 VERSION.txt diff --git a/README.md b/README.md index 0a2b123..a182237 100644 --- a/README.md +++ b/README.md @@ -2,22 +2,30 @@ # Stats +## General + | [Github License](https://github.com/deadratfink/jy-transform/blob/master/LICENSE.md) | [Github Issues](https://github.com/deadratfink/jy-transform/issues) | [Github Release](https://github.com/deadratfink/jy-transform/releases) | [Github Tags](https://github.com/deadratfink/jy-transform/tags) | [Travis CI](https://travis-ci.org) | [Waffle](https://waffle.io/deadratfink/jy-transform) | [Code Climate](https://codeclimate.com/github/deadratfink/jy-transform) | | --- | --- | --- | --- | --- | --- | --- | | [![License][gh-license-image]][gh-license-url] | [![Issue Stats][gh-issues-image]][gh-issues-url] | [![Github Releases][gh-releases-image]][gh-releases-url] | [![Github Tags][gh-tags-image]][gh-tags-url] | [![Build Status][ci-image]][ci-url] | [![Waffle][waffle-image]][waffle-url] | [![Code Climate][cocl-image]][cocl-url] | +## Branches + | Branch | [Codecov](https://codecov.io) | [Coveralls](https://coveralls.io) | [Inch CI](http://inch-ci.org) | [David](https://david-dm.org) DM | [David](https://david-dm.org) DM (dev) | | --- | --- | --- | --- | --- | --- | | master | [![codecov.io][cc-image-master]][cc-url-master] | [![coveralls.io][ca-image-master]][ca-url-master] | [![inch-ci.org][inch-image-master]][inch-url-master] | [![Dependency Status][dep-image-master]][dep-url-master] | [![devDependency Status][devdep-image-master]][devdep-url-master] | | development | [![codecov.io][cc-image-development]][cc-url-development] | [![coveralls.io][ca-image-development]][ca-url-development] | [![inch-ci.org][inch-image-development]][inch-url-development] | [![Dependency Status][dep-image-development]][dep-url-development] | [![devDependency Status][devdep-image-development]][devdep-url-development] | -## Coverage Graphs +### Coverage Graphs | Branch | Graph | | --- | --- | | master | ![codecov.io](https://codecov.io/github/deadratfink/jy-transform/branch.svg?branch=master&vg=true) | | development| ![codecov.io](https://codecov.io/github/deadratfink/jy-transform/branch.svg?branch=development&vg=true) | +## NPM + +[![NPM](https://nodei.co/npm/jy-transform.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/jy-transform/) +[![NPM](https://nodei.co/npm-dl/jy-transform.png?height=3&months=9)](https://nodei.co/npm-dl/jy-transform/) [gh-license-image]: https://img.shields.io/github/license/deadratfink/jy-transform.svg?style=flat-square [gh-license-url]: https://github.com/deadratfink/jy-transform/blob/master/LICENSE.md @@ -197,15 +205,15 @@ Additionally, on API level: The transformation can take place into several directions: -- YAML => JS -- YAML => JSON -- JS => YAML -- JSON => YAML -- JS => JSON -- JSON => JS -- YAML => YAML -- JSON => JSON -- JS => JS +- YAML ⇒ JS +- YAML ⇒ JSON +- JS ⇒ YAML +- JSON ⇒ YAML +- JS ⇒ JSON +- JSON ⇒ JS +- YAML ⇒ YAML +- JSON ⇒ JSON +- JS ⇒ JS while: @@ -337,7 +345,7 @@ have a YAML file located in _foo.yaml_ holding this data: ```yaml foo: bar ``` -#### Example: YAML => JSON +#### Example: YAML ⇒ JSON then we can transform it to a JSON file _foo.json_ @@ -369,7 +377,7 @@ $ jyt -s foo.js -t json -i 2 then the `js` value for `origin` is automatically inferred from file extension. Accordingly, this is also true for the `target` option. -#### Example: JSON => JS +#### Example: JSON ⇒ JS ``` $ jyt -s foo.json -i 2 @@ -380,7 +388,7 @@ module.exports = { } ``` -#### Example: JS => YAML +#### Example: JS ⇒ YAML ``` $ jyt -s foo.js -t yaml @@ -400,13 +408,13 @@ $ jyt -s foo.json -d results/foobar.yaml #### Example: Transformation with Unsupported Source File Extension As said, normally we infer from file extension to the type but assume the source -file has a file name which does not imply the type (here JS +file has a file name which does not imply the type (here a JSON type in a TEXT file), then you can simply provide the `-o` option with the correct `origin` type (of course, the `-t` option works analogous): ``` -$ jyt -s foo.txt -o js -d foobar.yaml +$ jyt -s foo.txt -o json -d foobar.yaml ``` #### Example: Read from File with Exports Identifier @@ -510,7 +518,7 @@ $ jyt -s foo.js -f Of course, leaving out the `-f` switch creates a new file relatively to the `origin`, named as _foo(1).js_ (note the consecutive number). Naturally, -another run of the command would result int a file called _foo(2).js_ +another run of the command would result in a file called _foo(2).js_ and so forth. ## Origin and Target Type Inference @@ -532,16 +540,13 @@ specify the origin or target type! Since the usage on CLI is a 2-step process: -1. Read from source file to JS object => -2. Write out (maybe to other type) +1. Read from source file to JS object ⇒ 2. Write out (maybe to other type) the direct API calls additionally provide the usage of a _middleware_ function where you can alter the input JS object before it is written and therefore, which turns this into a 3-step process: -1. Read from source => -2. Alter the JS object => -3. Write out (maybe to other type) +1. Read from source ⇒ 2. Alter the JS object ⇒ 3. Write out (maybe to other type) For more details about this and all the functions provided by this module please refer to the [API Reference](https://github.com/deadratfink/jy-transform/wiki/API-v1.0). @@ -590,7 +595,7 @@ The `middleware` is optional but if provided it must be of type `Function` and a [Promise](http://bluebirdjs.com/docs/api-reference.html). One of the easiest ones is the identity function -_f(data) -> data_ +_f(data) → data_ which could be expressed as [Promise](http://bluebirdjs.com/docs/api-reference.html) function as follows: @@ -634,7 +639,7 @@ will result in such JSON file: ```javascript { - "foo": "new bar" + "foo": "new bar" } ``` @@ -661,6 +666,11 @@ function key2(data) { objectPath.set(data, 'key2', 'value2'); return Promise.resolve(data); } + +function key3(data) { + objectPath.set(data, 'key3', 'value3'); + return Promise.resolve(data); +} ``` These can be collected by different aggregation or composition functions of the underlying @@ -670,7 +680,7 @@ function. This one can collect all three functions above and ensure their proper ```javascript var middleware = function (data) { - return Promise.all([key1(data), key2(data)]) + return Promise.all([key1(data), key2(data), key3(data)]) .then(function(result) { return Promise.resolve(result[result.length - 1]); }); @@ -678,7 +688,9 @@ var middleware = function (data) { var transformer = new Transformer(logger); var logger = ...; -var options = {...}; +var options = { + src: {} +}; return transformer.transform(options, middleware) .then(function (msg){ @@ -691,13 +703,15 @@ return transformer.transform(options, middleware) Then the result in the `middleware` function can be retrieved from the returned array, i.e. in case of [`Promise.all([...])`](http://bluebirdjs.com/docs/api/promise.all.html) -you have to pick the _last_ element which contains the "final product". -From our example above it would be +you have to pick the _last_ element which contains the "final product". + +From our example above it would be result in this object ```javascript { key1: 'value1', - key2: 'value2' + key2: 'value2', + key3: 'value3' } ``` @@ -727,7 +741,7 @@ var transformer = new Transformer(logger); var writer = new Writer(logger); ``` -At least, the passed logger object _has to_ support the following functions: +At least, the passed logger object _has_ to support the following functions: ```javascript function info(msg) @@ -755,7 +769,7 @@ into this project. For bugs and feature requests, please create an [issue](https://github.com/deadratfink/jy-transform/issues). When contributing as coder, please take care of the following conventions: -- Enter yourself in the `constributors` section of _package.json_. +- Enter yourself in the `contributors` section of _package.json_. - We strictly follow [Semantic Versioning 2](http://semver.org) rules. - The `development` branch is the leading branch and is protected. Create bugfix and feature branches (or fork into you own namespace) and create pull @@ -774,12 +788,13 @@ When contributing as coder, please take care of the following conventions: - Doc everything with [JSDocs](http://usejsdoc.org/) and document concepts in [README.md](https://github.com/deadratfink/jy-transform/blob/development/README.md) or [Wiki](https://github.com/deadratfink/jy-transform/wiki). -- Use single parenthesis (`'...'`) in _*.js_ files instead of double parenthesis (`"..."`). +- Use _single_ parenthesis (`'...'`) in _*.js_ files instead of _double_ parenthesis (`"..."`). - Avoid the of use parenthesis for keys in JSON objects. - Use the strict mode (`'use strict';`) in _*.js_ files. - File names should be lower-case with hyphens as divider, e.g. _options-handler.js_. - Markdown documentation files should be upper-case with _.md_ as extension, placed in _./docs_, e.g. _USAGE.md_. The _README.md_ is build up by these files concatenated by `npm run docs` command. Any new files have to be added to `scripts.docs` section of - _package.json_. Don't forget to regenerate _README.md_ before committing. + _package.json_. Don't forget to regenerate _README.md_ (`$ npm run docs`) and wiki + (`$ npm run wiki`) before committing. diff --git a/VERSION.txt b/VERSION.txt new file mode 100644 index 0000000..21e8796 --- /dev/null +++ b/VERSION.txt @@ -0,0 +1 @@ +1.0.3 From 3da74b1be534eb09fcda0205fcc4c622eeeae60e Mon Sep 17 00:00:00 2001 From: "Krefeldt, Jens" Date: Thu, 28 Apr 2016 00:30:38 +0200 Subject: [PATCH 03/12] Implement #25, #26, #32 and fix #31 --- .gitignore | 3 +- LICENSE.md | 2 +- README.md | 200 ++++++++++++++++-------------- bin/test.sh | 30 +++++ docs/BADGES.md | 12 +- CHANGELOG.md => docs/CHANGELOG.md | 18 ++- docs/CONTRIBUTING.md | 10 +- docs/LOGO.md | 2 + docs/USAGE.md | 181 ++++++++++++++------------- jyt | 24 ++-- lib/constants.js | 28 ++--- lib/options-handler.js | 129 +++++++++++++++---- lib/reader.js | 41 +++++- lib/writer.js | 33 ++++- package.json | 6 +- test/test-options-handler.js | 2 +- 16 files changed, 474 insertions(+), 247 deletions(-) create mode 100644 bin/test.sh rename CHANGELOG.md => docs/CHANGELOG.md (66%) create mode 100644 docs/LOGO.md diff --git a/.gitignore b/.gitignore index b7ea353..606b8ac 100755 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,10 @@ +/bin /docs/API.md .idea *.iml node_modules -!/bin /.project /.settings/* npm-debug.log coverage +nohup.out diff --git a/LICENSE.md b/LICENSE.md index c0c4498..cd94487 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2016 Jens Krefeldt +Copyright (c) 2016 [Jens Krefeldt](https://github.com/deadratfink) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index a182237..50e4f7b 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -![jy-transform logo](https://raw.githubusercontent.com/deadratfink/jy-transform/master/image/jytransform.png) +![jy-transform logo](https://github.com/deadratfink/jy-transform/blob/master/image/jytransform.png) # Stats @@ -100,14 +100,14 @@ - [API Usage](#api-usage) - [Using Custom Logger](#using-custom-logger) - [API Reference](#api-reference) -- [Changelog](#changelog) - [Contributing](#contributing) +- [Changelog](#changelog) # jy-transform -This project aims to read, write and transform YAML, JS or JSON objects into each other using CLI or API. The source and destination resources can be files on CLI and additionally, objects or streams on API level. Besides the transformation feature this module can also be used for simple loading and/or writing YAML, JS or JSON files. +This project aims to read, write and transform YAML, JS or JSON objects into each other using CLI or API, while the source and destination resources can be files on CLI and additionally, objects, buffers or streams on API level. ## Installation @@ -196,9 +196,16 @@ Reading from: - _*.js_ file - _*.json_ file -Additionally, on API level: +Additionally, on API level to: -- `stream.Readable` (requires `options.origin` property set, reads as UTF-8) +- a `stream.Readable` + - Serialized JSON and YAML + - Requires `options.origin` property set + - Reads as UTF-8 +- a `buffer.Buffer` + - Serialized JSON and YAML + - Requires `options.origin` property set + - Reads as UTF-8 - any JS `object` (actually, this means the reading phase is skipped, because object is in-memory already) ### Transformation @@ -218,7 +225,7 @@ The transformation can take place into several directions: while: - [YAML](http://http://yaml.org/) = _*.yaml_, _*.yml_ -- [JS](https://developer.mozilla.org/en-US/docs/Web/JavaScript) = _*.js_ (JS object) +- [JS](https://developer.mozilla.org/en-US/docs/Web/JavaScript) = _*.js_ (JS object) - [JSON](http://json.org) = _*.json_ (JS object serialized as JSON) ### Middleware @@ -234,9 +241,16 @@ Writing to: - _*.js_ file - _*.json_ file -Additionally, on API level: +Additionally, on API level to: -- `stream.Writable` (requires `options.target` property set, writes UTF-8) +- a `stream.Writable` + - Serialized JSON and YAML + - Requires `options.target` property set + - Writes UTF-8 +- a `buffer.Buffer` + - Serialized JSON and YAML + - Requires `options.target` property set + - Writes UTF-8 - any JS `object` ## Limitations @@ -272,58 +286,50 @@ Additionally, on API level: The CLI provides the `jyt` command (actually, this requires the use of options). After the global installation you can access the `Transformer` command options -with the usual help command as follows: - -``` -$ jyt --help -``` - -### CLI Options - -The `--help` option prints an overview about all available CLI properties: +with the usual command option `--help` option which prints an overview about all +available CLI properties: ``` $ jyt --help Usage: - jyt [OPTIONS] + jyt [INPUT-FILE] [OUTPUT-FILE] [OPTIONS] Options: - -o, --origin [STRING] The conversion origin: [ js | json | yaml ]. (Default is : if not given, the type is tried to be inferred from the extension of source path, else it is yaml) - -t, --target [STRING] The conversion target: [ js | json | yaml ]. (Default is : if not given, the type is tried to be inferred from the extension of destination path, else it is js) - -s, --src PATH The absolute/relative input file path. - -d, --dest [PATH] The absolute/relative output file path. When this - options is ommited then the output file is stored - relative to the input file (same base name but with - another extension if type differs). If input and - output type are the same then the file overwriting is - handled depending on the '--force' value! (Default is storing relative to input file) - -i, --indent [NUMBER] The indention for pretty-print: 1 - 8. (Default is 4) - -f, --force Force overwriting of existing output files on write - phase. When files are not overwritten (which is - default), then the next transformation with same - output file name gets a consecutive number on the base - file name, e.g. in case of foo.yaml it would be - foo(1).yaml. - -m, --imports STRING Define a 'module.exports[.identifier] = ' - identifier (to read from JS source file only, must - be a valid JS identifier!). - -x, --exports STRING Define a 'module.exports[.identifier] = ' - identifier, for usage in JS destination file only, - must be a valid JS identifier! + -o, --origin [STRING] The origin type of INPUT-FILE: [ js | json | yaml ]. (Default is if not given, the type is tried to be inferred from the extension of source path, else it is 'yaml') + -t, --target [STRING] The target type of OUTPUT-FILE: [ js | json | yaml ]. (Default is if not given, the type is tried to be inferred from the extension of destination path, else it is 'js') + -i, --indent [NUMBER] The indention for pretty-print: 1 - 8. (Default is 4) + -f, --force Force overwriting of existing output files on write phase. When files are not overwritten (which is default), + then the next transformation with same output file name gets a consecutive number on the base file name, e.g. in + case of foo.yaml it would be foo(1).yaml. + -m, --imports STRING Define a 'module.exports[.identifier] = ' identifier (to read from JS _source_ file only, must be a valid JS + identifier!). + -x, --exports STRING Define a 'module.exports[.identifier] = ' identifier (for usage in JS destination file only, must be a valid JS + identifier!). -k, --no-color Omit color from output --debug Show debug information -v, --version Display the current version -h, --help Display help and usage details ``` -These are more formally defined in the following table: +### CLI Args + +The ARGS are more formally defined in the following table: + +| Arg | Type | Description | Default | Required | +| --- | --- | --- | --- | --- | +| `INPUT-FILE` | URI | The source file path for transformation. | - | yes | +| `OUTPUT-FILE` | URI | The destination file path to transform to. | When this options is omitted then the output file is stored relative to the input file (same base name but with another extension if type differs). If input and output type are the same then the file overwriting is handled depending on the `--force` value! | no | + +**NOTE:** the input file has to be specified and should _first_ argument (in fact, it can be anywhere but it must be before an out file argument)! + +### CLI Options + +The OPTIONS are more formally defined in the following table: | Option (short) | Option (long) | Type | Description | Default | Required | | --- | --- | --- | --- | --- | --- | -| `-o` | `--origin` | [ _js_ | _json_ | _yaml_ ] | The transformation origin type. | if not given, the type is tried to be inferred from the extension of source path, else it is _yaml_ | no | -| `-t` | `--target` | [ _js_ | _json_ | _yaml_ ] | The transformation target type. | if not given, the type is tried to be inferred from the extension of destination path, else it is _js_ | no | -| `-s` | `--src` | URI | The source file path for transformation. | - | yes | -| `-d` | `--dest` | URI | The destination file path to transform to. | When this options is ommited then the output file is stored relative to the input file (same base name but with another extension if type differs). If input and output type are the same then the file overwriting is handled depending on the `--force` value! | no | +| `-o` | `--origin` | string of: [ _js_ | _json_ | _yaml_ ] | The transformation origin type. | if not given, the type is tried to be inferred from the extension of source path, else it is _yaml_ | no | +| `-t` | `--target` | string of: [ _js_ | _json_ | _yaml_ ] | The transformation target type. | if not given, the type is tried to be inferred from the extension of destination path, else it is _js_ | no | | `-i` | `--indent` | integer
- [ 1 - 8 ]
| The code indention used in destination files. | 4 | no | | `-f` | `--force` | n/a | Force overwriting of existing output files on write phase. When files are not overwritten (which is default), then the next transformation with same output file name gets a consecutive number on the base file name, e.g. in case of foo.yaml it would be foo(1).yaml. | _false_ | no | | `-m` | `--imports` | string | Define a 'module.exports[.identifier] = ' identifier (to read from JS _source_ file only, must be a valid JS identifier!) | _undefined_ | no | @@ -334,7 +340,6 @@ These are more formally defined in the following table: | `-h` | `--help` | n/a | Display help and usage details. | n/a | no | - **NOTE:** an invalid indention setting (1 > `-i`, `--indent` > 8) does not raise an error but a default of 4 SPACEs is applied instead. ### Examples @@ -358,7 +363,7 @@ then we can transform it to a JSON file _foo.json_ using this command: ``` -$ jyt -s foo.yaml -t json -i 2 +$ jyt foo.yaml -t json -i 2 ``` In this example we have overwritten the standard target type (which is `js`) @@ -371,7 +376,7 @@ default `js` would have been applied! If the source would have been a `js` type like ``` -$ jyt -s foo.js -t json -i 2 +$ jyt foo.js -t json -i 2 ``` then the `js` value for `origin` is automatically inferred from file extension. @@ -380,7 +385,7 @@ Accordingly, this is also true for the `target` option. #### Example: JSON ⇒ JS ``` -$ jyt -s foo.json -i 2 +$ jyt foo.json -i 2 ``` ```javascript module.exports = { @@ -391,7 +396,7 @@ module.exports = { #### Example: JS ⇒ YAML ``` -$ jyt -s foo.js -t yaml +$ jyt foo.js -t yaml ``` ```yaml foo: bar @@ -399,10 +404,10 @@ foo: bar #### Example: Transformation with Different Destination -Simply provide the `-d` with a different file name: +Simply specify the _output_ file with a different file name: ``` -$ jyt -s foo.json -d results/foobar.yaml +$ jyt foo.json results/foobar.yaml ``` #### Example: Transformation with Unsupported Source File Extension @@ -414,7 +419,7 @@ correct `origin` type (of course, the `-t` option works analogous): ``` -$ jyt -s foo.txt -o json -d foobar.yaml +$ jyt foo.txt foobar.yaml -o json ``` #### Example: Read from File with Exports Identifier @@ -437,7 +442,7 @@ module.exports.bar = { but you want to convert `bar` object, then call: ``` -$ jyt -s foo.js -m bar -d bar.yaml +$ jyt foo.js bar.yaml -m bar ``` to get the YAML result: @@ -485,7 +490,7 @@ foo: bar using this command: ``` -$ jyt -s foo.yaml -d foobar.js -x foobar +$ jyt foo.yaml foobar.js -x foobar ``` This generates the following output in JS file using `foobar` as identifier: @@ -513,7 +518,7 @@ But let's say we want to overwrite the original source now because you want to change the indention from 2 to 4 SPACEs, then we can do this as follows: ``` -$ jyt -s foo.js -f +$ jyt foo.js -f ``` Of course, leaving out the `-f` switch creates a new file relatively to @@ -755,46 +760,55 @@ For more details on how to use the API, please refer to the [API Reference](https://github.com/deadratfink/jy-transform/wiki/API-v1.0) wiki which describes the full API and provides more examples. -# Changelog - -The complete changelog is listed in the wiki [Changelog](https://github.com/deadratfink/jy-transform/wiki/Changelog) section. - - - - # Contributing Pull requests and stars are always welcome. Anybody is invited to take part into this project. For bugs and feature requests, please create an [issue](https://github.com/deadratfink/jy-transform/issues). -When contributing as coder, please take care of the following conventions: - -- Enter yourself in the `contributors` section of _package.json_. -- We strictly follow [Semantic Versioning 2](http://semver.org) rules. -- The `development` branch is the leading branch and is protected. Create bugfix and feature - branches (or fork into you own namespace) and create pull - requests to `development` when finished. Any of these should be prefixed with - `bugfix/#...` or `feature/#...` (followed by issue number and a short, "underscored" - proper meaning), e.g. - - `bugfix/#8_fix_js_reading_with_require` - - `feature/#14_multidocument_support` -- Remember that name could need to be enclosed in quotes, e.g. - ```$ git checkout -b 'feature/#19_...'``` - when using git shell command. -- The `master` branch is protected and is the stable branch after a release. - It will never be pushed directly (only on release build). -- Indention for any file is 4 SPACEs. -- Keep code coverage high (> 95%). -- Doc everything with [JSDocs](http://usejsdoc.org/) and document concepts in - [README.md](https://github.com/deadratfink/jy-transform/blob/development/README.md) - or [Wiki](https://github.com/deadratfink/jy-transform/wiki). -- Use _single_ parenthesis (`'...'`) in _*.js_ files instead of _double_ parenthesis (`"..."`). -- Avoid the of use parenthesis for keys in JSON objects. -- Use the strict mode (`'use strict';`) in _*.js_ files. -- File names should be lower-case with hyphens as divider, e.g. _options-handler.js_. -- Markdown documentation files should be upper-case with _.md_ as extension, placed - in _./docs_, e.g. _USAGE.md_. The _README.md_ is build up by these files concatenated - by `npm run docs` command. Any new files have to be added to `scripts.docs` section of - _package.json_. Don't forget to regenerate _README.md_ (`$ npm run docs`) and wiki - (`$ npm run wiki`) before committing. +See the wiki [Contributing](https://github.com/deadratfink/jy-transform/wiki/Changelog) +section for more details about conventions. + + + + +# Changelog +### v2.0.0 + +- [[#32](https://github.com/deadratfink/jy-transform/issues/32)] Introduce input and output on CLI as ARGS instead of OPTIONS (non-backwards compatible change for CLI usage!) + - E.g. type `$ jyt foo.js bar.yaml` instead of `$ jyt -s foo.js -d bar.yaml` +- [[#31](https://github.com/deadratfink/jy-transform/issues/31)] Fix: given `Object` source results in 'yaml' for origin (API) +- [[#26](https://github.com/deadratfink/jy-transform/issues/26)] API level `dest`: support for writing serialized JSON and YAML to _single_ (i.e. non-streamed) `Buffer` + - Requires `options.target` property set + - Writes UTF-8 to Buffer +- [[#25](https://github.com/deadratfink/jy-transform/issues/25)] API level `src`: support for reading serialized JSON and YAML from _single_ (i.e. non-streamed) `Buffer` + - Requires `options.origin` property set + - Expects UTF-8 data in Buffer + +### v1.0.2 + +- [[#30](https://github.com/deadratfink/jy-transform/issues/30)] Fix README and externalize API reference to wiki +- [[#29](https://github.com/deadratfink/jy-transform/issues/29)] Fix Promise warning on write process + +### v1.0.1 + +Initial public release. This covers the basic implementation and tests. The following features and fixes and part of this release: + +- [[#27](https://github.com/deadratfink/jy-transform/issues/27)] Export variable for JS input +- [[#22](https://github.com/deadratfink/jy-transform/issues/22)] Integrate Coveralls +- [[#21](https://github.com/deadratfink/jy-transform/issues/21)] Check and fix CodeClimate issues +- [[#20](https://github.com/deadratfink/jy-transform/issues/20)] Cleanup test dir +- [[#19](https://github.com/deadratfink/jy-transform/issues/19)] File overwrite switch (`-f`, `-force`) +- [[#18](https://github.com/deadratfink/jy-transform/issues/18)] Read and Write from other sources than file path +- [[#16](https://github.com/deadratfink/jy-transform/issues/16)] ERROR: Error: Invalid target option found while creating destination file extension +- [[#15](https://github.com/deadratfink/jy-transform/issues/15)] Measure test code coverage and add a badge +- [[#12](https://github.com/deadratfink/jy-transform/issues/12)] Create middleware collection file to use by clients and internally +- [[#11](https://github.com/deadratfink/jy-transform/issues/11)] Check all Promises for optimization possibilities +- [[#10](https://github.com/deadratfink/jy-transform/issues/10)] Integrate project with Travis +- [[#9](https://github.com/deadratfink/jy-transform/issues/9)] Resolve origin and target from file extension whenever possible +- [[#8](https://github.com/deadratfink/jy-transform/issues/8)] Enable JS reading with `require(...)` +- [[#7](https://github.com/deadratfink/jy-transform/issues/7)] YAML indent is not set to `Constants.MIN_YAML_INDENT` when indent is set to 0 +- [[#6](https://github.com/deadratfink/jy-transform/issues/6)] Finish full JSDoc for all methods +- [[#5](https://github.com/deadratfink/jy-transform/issues/5)] Write unit tests +- [[#4](https://github.com/deadratfink/jy-transform/issues/4)] Export variable for JS output +- [[#3](https://github.com/deadratfink/jy-transform/issues/3)] Promise array as middleware solved with `Promise.all([...])` diff --git a/bin/test.sh b/bin/test.sh new file mode 100644 index 0000000..1a11dad --- /dev/null +++ b/bin/test.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash + +TMP=bin/tmp/ +PREPARE_LOG=bin/tmp/release-prepare.log +file="VERSION.txt" +VERSION=$(cat "$file") +current=$(pwd) +echo -e $current +echo -e "Release prepare: $VERSION" +BIN=bin/prepare-release.sh + +if [ ! -d "$TMP" ]; then + mkdir bin/tmp/ +else + rm -Rf bin/tmp/ + mkdir bin/tmp/ +fi + +#git reset --hard +#git checkout --track origin/master +#git checkout --track origin/development +#export GIT_MERGE_AUTOEDIT=no +#git config gitflow.branch.develop development +#git config gitflow.prefix.versiontag v +#git flow init -fd +#git flow release start $VERSION +#npm --no-git-tag-version version $VERSION +#json -I -o json-4 -f package.json +#git commit -am 'Published $VERSION release branch' +#git flow release publish $VERSION diff --git a/docs/BADGES.md b/docs/BADGES.md index 738c50c..47ff09c 100644 --- a/docs/BADGES.md +++ b/docs/BADGES.md @@ -1,23 +1,29 @@ -![jy-transform logo](https://raw.githubusercontent.com/deadratfink/jy-transform/master/image/jytransform.png) - # Stats +## General + | [Github License](https://github.com/deadratfink/jy-transform/blob/master/LICENSE.md) | [Github Issues](https://github.com/deadratfink/jy-transform/issues) | [Github Release](https://github.com/deadratfink/jy-transform/releases) | [Github Tags](https://github.com/deadratfink/jy-transform/tags) | [Travis CI](https://travis-ci.org) | [Waffle](https://waffle.io/deadratfink/jy-transform) | [Code Climate](https://codeclimate.com/github/deadratfink/jy-transform) | | --- | --- | --- | --- | --- | --- | --- | | [![License][gh-license-image]][gh-license-url] | [![Issue Stats][gh-issues-image]][gh-issues-url] | [![Github Releases][gh-releases-image]][gh-releases-url] | [![Github Tags][gh-tags-image]][gh-tags-url] | [![Build Status][ci-image]][ci-url] | [![Waffle][waffle-image]][waffle-url] | [![Code Climate][cocl-image]][cocl-url] | +## Branches + | Branch | [Codecov](https://codecov.io) | [Coveralls](https://coveralls.io) | [Inch CI](http://inch-ci.org) | [David](https://david-dm.org) DM | [David](https://david-dm.org) DM (dev) | | --- | --- | --- | --- | --- | --- | | master | [![codecov.io][cc-image-master]][cc-url-master] | [![coveralls.io][ca-image-master]][ca-url-master] | [![inch-ci.org][inch-image-master]][inch-url-master] | [![Dependency Status][dep-image-master]][dep-url-master] | [![devDependency Status][devdep-image-master]][devdep-url-master] | | development | [![codecov.io][cc-image-development]][cc-url-development] | [![coveralls.io][ca-image-development]][ca-url-development] | [![inch-ci.org][inch-image-development]][inch-url-development] | [![Dependency Status][dep-image-development]][dep-url-development] | [![devDependency Status][devdep-image-development]][devdep-url-development] | -## Coverage Graphs +### Coverage Graphs | Branch | Graph | | --- | --- | | master | ![codecov.io](https://codecov.io/github/deadratfink/jy-transform/branch.svg?branch=master&vg=true) | | development| ![codecov.io](https://codecov.io/github/deadratfink/jy-transform/branch.svg?branch=development&vg=true) | +## NPM + +[![NPM](https://nodei.co/npm/jy-transform.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/jy-transform/) +[![NPM](https://nodei.co/npm-dl/jy-transform.png?height=3&months=9)](https://nodei.co/npm-dl/jy-transform/) [gh-license-image]: https://img.shields.io/github/license/deadratfink/jy-transform.svg?style=flat-square [gh-license-url]: https://github.com/deadratfink/jy-transform/blob/master/LICENSE.md diff --git a/CHANGELOG.md b/docs/CHANGELOG.md similarity index 66% rename from CHANGELOG.md rename to docs/CHANGELOG.md index a7bb3bf..ffea462 100644 --- a/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,11 +1,25 @@ +# Changelog + +### v2.0.0 + +- [[#32](https://github.com/deadratfink/jy-transform/issues/32)] Introduce input and output on CLI as ARGS instead of OPTIONS (non-backwards compatible change for CLI usage!) + - E.g. type `$ jyt foo.js bar.yaml` instead of `$ jyt -s foo.js -d bar.yaml` +- [[#31](https://github.com/deadratfink/jy-transform/issues/31)] Fix: given `Object` source results in 'yaml' for origin (API) +- [[#26](https://github.com/deadratfink/jy-transform/issues/26)] API level `dest`: support for writing serialized JSON and YAML to _single_ (i.e. non-streamed) `Buffer` + - Requires `options.target` property set + - Writes UTF-8 to Buffer +- [[#25](https://github.com/deadratfink/jy-transform/issues/25)] API level `src`: support for reading serialized JSON and YAML from _single_ (i.e. non-streamed) `Buffer` + - Requires `options.origin` property set + - Expects UTF-8 data in Buffer + ### v1.0.2 -- [[#30](https://github.com/deadratfink/jy-transform/issues/30)] Fix README and externalize API Reference to wiki +- [[#30](https://github.com/deadratfink/jy-transform/issues/30)] Fix README and externalize API reference to wiki - [[#29](https://github.com/deadratfink/jy-transform/issues/29)] Fix Promise warning on write process ### v1.0.1 -Initial release. This covers the basic implementation and tests. The following features and fixes and part of this release: +Initial public release. This covers the basic implementation and tests. The following features and fixes and part of this release: - [[#27](https://github.com/deadratfink/jy-transform/issues/27)] Export variable for JS input - [[#22](https://github.com/deadratfink/jy-transform/issues/22)] Integrate Coveralls diff --git a/docs/CONTRIBUTING.md b/docs/CONTRIBUTING.md index 6175669..f99835c 100644 --- a/docs/CONTRIBUTING.md +++ b/docs/CONTRIBUTING.md @@ -1,11 +1,8 @@ # Contributing -Pull requests and stars are always welcome. Anybody is invited to take part -into this project. For bugs and feature requests, please create an -[issue](https://github.com/deadratfink/jy-transform/issues). When contributing as coder, please take care of the following conventions: -- Enter yourself in the `constributors` section of _package.json_. +- Enter yourself in the `contributors` section of _package.json_. - We strictly follow [Semantic Versioning 2](http://semver.org) rules. - The `development` branch is the leading branch and is protected. Create bugfix and feature branches (or fork into you own namespace) and create pull @@ -24,12 +21,13 @@ When contributing as coder, please take care of the following conventions: - Doc everything with [JSDocs](http://usejsdoc.org/) and document concepts in [README.md](https://github.com/deadratfink/jy-transform/blob/development/README.md) or [Wiki](https://github.com/deadratfink/jy-transform/wiki). -- Use single parenthesis (`'...'`) in _*.js_ files instead of double parenthesis (`"..."`). +- Use _single_ parenthesis (`'...'`) in _*.js_ files instead of _double_ parenthesis (`"..."`). - Avoid the of use parenthesis for keys in JSON objects. - Use the strict mode (`'use strict';`) in _*.js_ files. - File names should be lower-case with hyphens as divider, e.g. _options-handler.js_. - Markdown documentation files should be upper-case with _.md_ as extension, placed in _./docs_, e.g. _USAGE.md_. The _README.md_ is build up by these files concatenated by `npm run docs` command. Any new files have to be added to `scripts.docs` section of - _package.json_. Don't forget to regenerate _README.md_ before committing. + _package.json_. Don't forget to regenerate _README.md_ (`$ npm run docs`) and wiki + (`$ npm run wiki`) before committing. diff --git a/docs/LOGO.md b/docs/LOGO.md new file mode 100644 index 0000000..cbb067c --- /dev/null +++ b/docs/LOGO.md @@ -0,0 +1,2 @@ +![jy-transform logo](https://github.com/deadratfink/jy-transform/blob/master/image/jytransform.png) + diff --git a/docs/USAGE.md b/docs/USAGE.md index 1d27972..2bbc539 100644 --- a/docs/USAGE.md +++ b/docs/USAGE.md @@ -38,29 +38,36 @@ Reading from: - _*.js_ file - _*.json_ file -Additionally, on API level: - -- `stream.Readable` (requires `options.origin` property set, reads as UTF-8) +Additionally, on API level to: + +- a `stream.Readable` + - Serialized JSON and YAML + - Requires `options.origin` property set + - Reads as UTF-8 +- a `buffer.Buffer` + - Serialized JSON and YAML + - Requires `options.origin` property set + - Reads as UTF-8 - any JS `object` (actually, this means the reading phase is skipped, because object is in-memory already) ### Transformation The transformation can take place into several directions: -- YAML => JS -- YAML => JSON -- JS => YAML -- JSON => YAML -- JS => JSON -- JSON => JS -- YAML => YAML -- JSON => JSON -- JS => JS +- YAML ⇒ JS +- YAML ⇒ JSON +- JS ⇒ YAML +- JSON ⇒ YAML +- JS ⇒ JSON +- JSON ⇒ JS +- YAML ⇒ YAML +- JSON ⇒ JSON +- JS ⇒ JS while: - [YAML](http://http://yaml.org/) = _*.yaml_, _*.yml_ -- [JS](https://developer.mozilla.org/en-US/docs/Web/JavaScript) = _*.js_ (JS object) +- [JS](https://developer.mozilla.org/en-US/docs/Web/JavaScript) = _*.js_ (JS object) - [JSON](http://json.org) = _*.json_ (JS object serialized as JSON) ### Middleware @@ -76,9 +83,16 @@ Writing to: - _*.js_ file - _*.json_ file -Additionally, on API level: +Additionally, on API level to: -- `stream.Writable` (requires `options.target` property set, writes UTF-8) +- a `stream.Writable` + - Serialized JSON and YAML + - Requires `options.target` property set + - Writes UTF-8 +- a `buffer.Buffer` + - Serialized JSON and YAML + - Requires `options.target` property set + - Writes UTF-8 - any JS `object` ## Limitations @@ -114,58 +128,50 @@ Additionally, on API level: The CLI provides the `jyt` command (actually, this requires the use of options). After the global installation you can access the `Transformer` command options -with the usual help command as follows: - -``` -$ jyt --help -``` - -### CLI Options - -The `--help` option prints an overview about all available CLI properties: +with the usual command option `--help` option which prints an overview about all +available CLI properties: ``` $ jyt --help Usage: - jyt [OPTIONS] + jyt [INPUT-FILE] [OUTPUT-FILE] [OPTIONS] Options: - -o, --origin [STRING] The conversion origin: [ js | json | yaml ]. (Default is : if not given, the type is tried to be inferred from the extension of source path, else it is yaml) - -t, --target [STRING] The conversion target: [ js | json | yaml ]. (Default is : if not given, the type is tried to be inferred from the extension of destination path, else it is js) - -s, --src PATH The absolute/relative input file path. - -d, --dest [PATH] The absolute/relative output file path. When this - options is ommited then the output file is stored - relative to the input file (same base name but with - another extension if type differs). If input and - output type are the same then the file overwriting is - handled depending on the '--force' value! (Default is storing relative to input file) - -i, --indent [NUMBER] The indention for pretty-print: 1 - 8. (Default is 4) - -f, --force Force overwriting of existing output files on write - phase. When files are not overwritten (which is - default), then the next transformation with same - output file name gets a consecutive number on the base - file name, e.g. in case of foo.yaml it would be - foo(1).yaml. - -m, --imports STRING Define a 'module.exports[.identifier] = ' - identifier (to read from JS source file only, must - be a valid JS identifier!). - -x, --exports STRING Define a 'module.exports[.identifier] = ' - identifier, for usage in JS destination file only, - must be a valid JS identifier! + -o, --origin [STRING] The origin type of INPUT-FILE: [ js | json | yaml ]. (Default is if not given, the type is tried to be inferred from the extension of source path, else it is 'yaml') + -t, --target [STRING] The target type of OUTPUT-FILE: [ js | json | yaml ]. (Default is if not given, the type is tried to be inferred from the extension of destination path, else it is 'js') + -i, --indent [NUMBER] The indention for pretty-print: 1 - 8. (Default is 4) + -f, --force Force overwriting of existing output files on write phase. When files are not overwritten (which is default), + then the next transformation with same output file name gets a consecutive number on the base file name, e.g. in + case of foo.yaml it would be foo(1).yaml. + -m, --imports STRING Define a 'module.exports[.identifier] = ' identifier (to read from JS _source_ file only, must be a valid JS + identifier!). + -x, --exports STRING Define a 'module.exports[.identifier] = ' identifier (for usage in JS destination file only, must be a valid JS + identifier!). -k, --no-color Omit color from output --debug Show debug information -v, --version Display the current version -h, --help Display help and usage details ``` -These are more formally defined in the following table: +### CLI Args + +The ARGS are more formally defined in the following table: + +| Arg | Type | Description | Default | Required | +| --- | --- | --- | --- | --- | +| `INPUT-FILE` | URI | The source file path for transformation. | - | yes | +| `OUTPUT-FILE` | URI | The destination file path to transform to. | When this options is omitted then the output file is stored relative to the input file (same base name but with another extension if type differs). If input and output type are the same then the file overwriting is handled depending on the `--force` value! | no | + +**NOTE:** the input file has to be specified and should _first_ argument (in fact, it can be anywhere but it must be before an out file argument)! + +### CLI Options + +The OPTIONS are more formally defined in the following table: | Option (short) | Option (long) | Type | Description | Default | Required | | --- | --- | --- | --- | --- | --- | -| `-o` | `--origin` | [ _js_ | _json_ | _yaml_ ] | The transformation origin type. | if not given, the type is tried to be inferred from the extension of source path, else it is _yaml_ | no | -| `-t` | `--target` | [ _js_ | _json_ | _yaml_ ] | The transformation target type. | if not given, the type is tried to be inferred from the extension of destination path, else it is _js_ | no | -| `-s` | `--src` | URI | The source file path for transformation. | - | yes | -| `-d` | `--dest` | URI | The destination file path to transform to. | When this options is ommited then the output file is stored relative to the input file (same base name but with another extension if type differs). If input and output type are the same then the file overwriting is handled depending on the `--force` value! | no | +| `-o` | `--origin` | string of: [ _js_ | _json_ | _yaml_ ] | The transformation origin type. | if not given, the type is tried to be inferred from the extension of source path, else it is _yaml_ | no | +| `-t` | `--target` | string of: [ _js_ | _json_ | _yaml_ ] | The transformation target type. | if not given, the type is tried to be inferred from the extension of destination path, else it is _js_ | no | | `-i` | `--indent` | integer
- [ 1 - 8 ]
| The code indention used in destination files. | 4 | no | | `-f` | `--force` | n/a | Force overwriting of existing output files on write phase. When files are not overwritten (which is default), then the next transformation with same output file name gets a consecutive number on the base file name, e.g. in case of foo.yaml it would be foo(1).yaml. | _false_ | no | | `-m` | `--imports` | string | Define a 'module.exports[.identifier] = ' identifier (to read from JS _source_ file only, must be a valid JS identifier!) | _undefined_ | no | @@ -176,7 +182,6 @@ These are more formally defined in the following table: | `-h` | `--help` | n/a | Display help and usage details. | n/a | no | - **NOTE:** an invalid indention setting (1 > `-i`, `--indent` > 8) does not raise an error but a default of 4 SPACEs is applied instead. ### Examples @@ -187,7 +192,7 @@ have a YAML file located in _foo.yaml_ holding this data: ```yaml foo: bar ``` -#### Example: YAML => JSON +#### Example: YAML ⇒ JSON then we can transform it to a JSON file _foo.json_ @@ -200,7 +205,7 @@ then we can transform it to a JSON file _foo.json_ using this command: ``` -$ jyt -s foo.yaml -t json -i 2 +$ jyt foo.yaml -t json -i 2 ``` In this example we have overwritten the standard target type (which is `js`) @@ -213,16 +218,16 @@ default `js` would have been applied! If the source would have been a `js` type like ``` -$ jyt -s foo.js -t json -i 2 +$ jyt foo.js -t json -i 2 ``` then the `js` value for `origin` is automatically inferred from file extension. Accordingly, this is also true for the `target` option. -#### Example: JSON => JS +#### Example: JSON ⇒ JS ``` -$ jyt -s foo.json -i 2 +$ jyt foo.json -i 2 ``` ```javascript module.exports = { @@ -230,10 +235,10 @@ module.exports = { } ``` -#### Example: JS => YAML +#### Example: JS ⇒ YAML ``` -$ jyt -s foo.js -t yaml +$ jyt foo.js -t yaml ``` ```yaml foo: bar @@ -241,22 +246,22 @@ foo: bar #### Example: Transformation with Different Destination -Simply provide the `-d` with a different file name: +Simply specify the _output_ file with a different file name: ``` -$ jyt -s foo.json -d results/foobar.yaml +$ jyt foo.json results/foobar.yaml ``` #### Example: Transformation with Unsupported Source File Extension As said, normally we infer from file extension to the type but assume the source -file has a file name which does not imply the type (here JS +file has a file name which does not imply the type (here a JSON type in a TEXT file), then you can simply provide the `-o` option with the correct `origin` type (of course, the `-t` option works analogous): ``` -$ jyt -s foo.txt -o js -d foobar.yaml +$ jyt foo.txt foobar.yaml -o json ``` #### Example: Read from File with Exports Identifier @@ -279,7 +284,7 @@ module.exports.bar = { but you want to convert `bar` object, then call: ``` -$ jyt -s foo.js -m bar -d bar.yaml +$ jyt foo.js bar.yaml -m bar ``` to get the YAML result: @@ -327,7 +332,7 @@ foo: bar using this command: ``` -$ jyt -s foo.yaml -d foobar.js -x foobar +$ jyt foo.yaml foobar.js -x foobar ``` This generates the following output in JS file using `foobar` as identifier: @@ -355,12 +360,12 @@ But let's say we want to overwrite the original source now because you want to change the indention from 2 to 4 SPACEs, then we can do this as follows: ``` -$ jyt -s foo.js -f +$ jyt foo.js -f ``` Of course, leaving out the `-f` switch creates a new file relatively to the `origin`, named as _foo(1).js_ (note the consecutive number). Naturally, -another run of the command would result int a file called _foo(2).js_ +another run of the command would result in a file called _foo(2).js_ and so forth. ## Origin and Target Type Inference @@ -382,16 +387,13 @@ specify the origin or target type! Since the usage on CLI is a 2-step process: -1. Read from source file to JS object => -2. Write out (maybe to other type) +1. Read from source file to JS object ⇒ 2. Write out (maybe to other type) the direct API calls additionally provide the usage of a _middleware_ function where you can alter the input JS object before it is written and therefore, which turns this into a 3-step process: -1. Read from source => -2. Alter the JS object => -3. Write out (maybe to other type) +1. Read from source ⇒ 2. Alter the JS object ⇒ 3. Write out (maybe to other type) For more details about this and all the functions provided by this module please refer to the [API Reference](https://github.com/deadratfink/jy-transform/wiki/API-v1.0). @@ -440,7 +442,7 @@ The `middleware` is optional but if provided it must be of type `Function` and a [Promise](http://bluebirdjs.com/docs/api-reference.html). One of the easiest ones is the identity function -_f(data) -> data_ +_f(data) → data_ which could be expressed as [Promise](http://bluebirdjs.com/docs/api-reference.html) function as follows: @@ -484,7 +486,7 @@ will result in such JSON file: ```javascript { - "foo": "new bar" + "foo": "new bar" } ``` @@ -511,6 +513,11 @@ function key2(data) { objectPath.set(data, 'key2', 'value2'); return Promise.resolve(data); } + +function key3(data) { + objectPath.set(data, 'key3', 'value3'); + return Promise.resolve(data); +} ``` These can be collected by different aggregation or composition functions of the underlying @@ -520,7 +527,7 @@ function. This one can collect all three functions above and ensure their proper ```javascript var middleware = function (data) { - return Promise.all([key1(data), key2(data)]) + return Promise.all([key1(data), key2(data), key3(data)]) .then(function(result) { return Promise.resolve(result[result.length - 1]); }); @@ -528,7 +535,9 @@ var middleware = function (data) { var transformer = new Transformer(logger); var logger = ...; -var options = {...}; +var options = { + src: {} +}; return transformer.transform(options, middleware) .then(function (msg){ @@ -541,13 +550,15 @@ return transformer.transform(options, middleware) Then the result in the `middleware` function can be retrieved from the returned array, i.e. in case of [`Promise.all([...])`](http://bluebirdjs.com/docs/api/promise.all.html) -you have to pick the _last_ element which contains the "final product". -From our example above it would be +you have to pick the _last_ element which contains the "final product". + +From our example above it would be result in this object ```javascript { key1: 'value1', - key2: 'value2' + key2: 'value2', + key3: 'value3' } ``` @@ -577,7 +588,7 @@ var transformer = new Transformer(logger); var writer = new Writer(logger); ``` -At least, the passed logger object _has to_ support the following functions: +At least, the passed logger object _has_ to support the following functions: ```javascript function info(msg) @@ -591,7 +602,11 @@ For more details on how to use the API, please refer to the [API Reference](https://github.com/deadratfink/jy-transform/wiki/API-v1.0) wiki which describes the full API and provides more examples. -# Changelog +# Contributing -The complete changelog is listed in the wiki [Changelog](https://github.com/deadratfink/jy-transform/wiki/Changelog) section. +Pull requests and stars are always welcome. Anybody is invited to take part +into this project. For bugs and feature requests, please create an +[issue](https://github.com/deadratfink/jy-transform/issues). +See the wiki [Contributing](https://github.com/deadratfink/jy-transform/wiki/Changelog) +section for more details about conventions. diff --git a/jyt b/jyt index ff7ba05..631a210 100755 --- a/jyt +++ b/jyt @@ -18,7 +18,7 @@ var transformer = new Transformer(cli); * @type {string} * @private */ -var usage = path.basename(__filename) + ' [OPTIONS]'; +var usage = path.basename(__filename) + ' [INPUT-FILE] [OUTPUT-FILE] [OPTIONS]'; /** * The path to package.json. @@ -37,10 +37,8 @@ var packagePath = __dirname + '/package.json'; * @private */ var options = { - origin: [ 'o', 'The conversion origin: [ ' + constants.JS + ' | ' + constants.JSON + ' | ' + constants.YAML + ' ].', 'string', constants.DEFAULT_OPTIONS.origin ], - target: [ 't', 'The conversion target: [ ' + constants.JS + ' | ' + constants.JSON + ' | ' + constants.YAML + ' ].', 'string', constants.DEFAULT_OPTIONS.target ], - src: [ 's', 'The absolute/relative input file path.', 'path'], - dest: [ 'd', 'The absolute/relative output file path. When this options is ommited then the output file is stored relative to the input file (same base name but with another extension if type differs). If input and output type are the same then the file overwriting is handled depending on the \'--force\' value!', 'path', constants.DEFAULT_OPTIONS.dest], + origin: [ 'o', 'The origin type of INPUT-FILE: [ ' + constants.JS + ' | ' + constants.JSON + ' | ' + constants.YAML + ' ].', 'string', constants.DEFAULT_OPTIONS.origin ], + target: [ 't', 'The target type of OUTPUT-FILE: [ ' + constants.JS + ' | ' + constants.JSON + ' | ' + constants.YAML + ' ].', 'string', constants.DEFAULT_OPTIONS.target ], indent: [ 'i', 'The indention for pretty-print: 1 - 8.', 'int', constants.DEFAULT_INDENT ], force: [ 'f', 'Force overwriting of existing output files on write phase. When files are not overwritten (which is default), then the next transformation with same output file name gets a consecutive number on the base file name, e.g. in case of foo.yaml it would be foo(1).yaml.' ], imports: [ 'm', 'Define a \'module.exports[.identifier] = \' identifier (to read from JS _source_ file only, must be a valid JS identifier!).', 'string', constants.DEFAULT_OPTIONS.imports ], @@ -48,15 +46,20 @@ var options = { }; /** - * The main entry callback. When calling cli.main() this receives the `options` - * given on CLI, does the transformation with these options and prints the - * result to the CLI. + * The main entry callback. When calling `cli.main()` this receives the `options` + * given on CLI, then does the transformation with these options and finally, it + * prints the result to the CLI. * - * @param args - Not used inside this method! - * @param options - The options set on CLI. + * @param {array} args - Not used inside this method! + * @param {object} options - The options set on CLI. * @private */ function main(args, options) { + cli.debug('ARGS: ' + args[0] + ', ' + args[1]); + + options.src = args[0]; + options.dest = args[1]; + return transformer.transform(options) .then(function (msg) { cli.info(msg); @@ -73,6 +76,7 @@ function main(args, options) { /** * Init the CLI instance. */ +cli.width = 120; cli.setUsage(usage); cli.setApp(packagePath); cli.enable('version', 'status', 'timeout'); diff --git a/lib/constants.js b/lib/constants.js index e76b16b..26355cf 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -126,7 +126,7 @@ Constants.prototype.DEFAULT_FORCE_FILE_OVERWRITE = false; * @type {string} * @public */ -Constants.prototype.ORIGIN_DESCRIPTION = ': if not given, the type is tried to be inferred from the extension of source path, else it is \'' + constants.DEFAULT_ORIGIN + '\''; +Constants.prototype.ORIGIN_DESCRIPTION = 'if not given, the type is tried to be inferred from the extension of source path, else it is \'' + constants.DEFAULT_ORIGIN + '\''; /** * The `target` description value. @@ -134,7 +134,7 @@ Constants.prototype.ORIGIN_DESCRIPTION = ': if not given, the type is tried to b * @type {string} * @public */ -Constants.prototype.TARGET_DESCRIPTION = ': if not given, the type is tried to be inferred from the extension of destination path, else it is \'' + constants.DEFAULT_TARGET + '\''; +Constants.prototype.TARGET_DESCRIPTION = 'if not given, the type is tried to be inferred from the extension of destination path, else it is \'' + constants.DEFAULT_TARGET + '\''; /** * The `dest` description value. @@ -174,9 +174,9 @@ Constants.prototype.DEFAULT_JS_EXPORTS_IDENTIFIER = undefined; * @property {boolean} force=false - Whether to overwrite existing file on output. * @property {string} imports=undefined - The exports name for reading from JS source file or objects only. * @property {string} exports=undefined - The exports name for usage in JS file or object only. - * @see {@link Constants#ORIGIN_DESCRIPTION} - * @see {@link Constants#TARGET_DESCRIPTION} - * @see {@link Constants#DEST_DESCRIPTION} + * @see {@link ORIGIN_DESCRIPTION} + * @see {@link TARGET_DESCRIPTION} + * @see {@link DEST_DESCRIPTION} */ Constants.prototype.DEFAULT_OPTIONS = { origin: constants.ORIGIN_DESCRIPTION, @@ -189,7 +189,7 @@ Constants.prototype.DEFAULT_OPTIONS = { }; /** - * The transformation direction YAML => JS. + * The transformation direction YAML ⇒ JS. * * @type {string} * @constant @@ -198,7 +198,7 @@ Constants.prototype.DEFAULT_OPTIONS = { Constants.prototype.YAML_TO_JS = 'yaml2js'; /** - * The transformation direction YAML => JSON. + * The transformation direction YAML ⇒ JSON. * * @type {string} * @constant @@ -207,7 +207,7 @@ Constants.prototype.YAML_TO_JS = 'yaml2js'; Constants.prototype.YAML_TO_JSON = 'yaml2json'; /** - * The transformation direction JS => YAML. + * The transformation direction JS ⇒ YAML. * * @type {string} * @constant @@ -216,7 +216,7 @@ Constants.prototype.YAML_TO_JSON = 'yaml2json'; Constants.prototype.JS_TO_YAML = 'js2yaml'; /** - * The transformation direction JSON => YAML. + * The transformation direction JSON ⇒ YAML. * * @type {string} * @constant @@ -225,7 +225,7 @@ Constants.prototype.JS_TO_YAML = 'js2yaml'; Constants.prototype.JSON_TO_YAML = 'json2yaml'; /** - * The transformation direction JSON => JS. + * The transformation direction JSON ⇒ JS. * * @type {string} * @constant @@ -234,7 +234,7 @@ Constants.prototype.JSON_TO_YAML = 'json2yaml'; Constants.prototype.JSON_TO_JS = 'json2js'; /** - * The transformation direction JS => JSON. + * The transformation direction JS ⇒ JSON. * * @type {string} * @constant @@ -243,7 +243,7 @@ Constants.prototype.JSON_TO_JS = 'json2js'; Constants.prototype.JS_TO_JSON = 'js2json'; /** - * The transformation direction YAML => YAML. + * The transformation direction YAML ⇒ YAML. * * @type {string} * @constant @@ -252,7 +252,7 @@ Constants.prototype.JS_TO_JSON = 'js2json'; Constants.prototype.YAML_TO_YAML = 'yaml2yaml'; /** - * The transformation direction JSON => JSON. + * The transformation direction JSON ⇒ JSON. * * @type {string} * @constant @@ -261,7 +261,7 @@ Constants.prototype.YAML_TO_YAML = 'yaml2yaml'; Constants.prototype.JSON_TO_JSON = 'json2json'; /** - * The transformation direction JS => JS. + * The transformation direction JS ⇒ JS. * * @type {string} * @constant diff --git a/lib/options-handler.js b/lib/options-handler.js index b550579..13e6f50 100644 --- a/lib/options-handler.js +++ b/lib/options-handler.js @@ -124,7 +124,7 @@ function OptionsHandler(logger) { * * @param {string} pathStr - The file path with or without extension. * @param {boolean} origin - If the type is origin (true) or target (false) - * @param {string} defaultValue - The default value to use if it cannot inferred from path. + * @param {string} defaultValue - The default value to use if type cannot be inferred from path. * @returns {string} - A type value. * @private */ @@ -172,13 +172,17 @@ function OptionsHandler(logger) { .then(function (assertedOptions) { var srcType; var destType; - if (assertedOptions.src && (typeof assertedOptions.src === 'string')) { - srcType = getTypeFromFilePath(assertedOptions.src, true, Constants.YAML); + if (assertedOptions.src) { + if (typeof assertedOptions.src === 'string') { + srcType = getTypeFromFilePath(assertedOptions.src, true, Constants.YAML); + } else if (typeof assertedOptions.src === 'object') { + srcType = Constants.JS; + } // TODO: what about Buffer/stream? } self.logInstance.debug('srcType: ' + srcType); if (assertedOptions.dest && (typeof assertedOptions.dest === 'string')) { self.logInstance.debug('options.dest: ' + assertedOptions.dest); - destType = getTypeFromFilePath(assertedOptions.dest, false, Constants.JS); + destType = getTypeFromFilePath(assertedOptions.dest, false, Constants.JS); // TODO: what about Buffer/stream? } self.logInstance.debug('destType: ' + destType); @@ -212,31 +216,108 @@ function OptionsHandler(logger) { * @public */ this.ensureSrc = function (options) { + //return assertOptions(options, ['src']) + // .then(function (assertedOptions) { + // if (typeof assertedOptions.src === 'string') { + // self.logInstance.debug('options.src is to be verfied as File path: ' + assertedOptions.src); + // // check for existing source file + // try { + // var stats = fs.statSync(assertedOptions.src); + // if (stats.isDirectory()) { + // return Promise.reject(new Error('Source file (options.src) is a directory, pls specify a valid file resource!')); + // } + // } catch (err) { + // err.message = 'Error occurred while checking input file \'' + assertedOptions.src + '\' which should be existing and accessible, code: ' + err.code + ', cause: ' + err.message; + // return Promise.reject(err); + // } + // } else if (isStream.readable(assertedOptions.src)) { + // self.logInstance.debug('options.src is Readable stream'); + // if (!options.origin) { + // return Promise.reject(new Error('When options.src is a Readable stream then setting options.origin is mandatory!')); + // } + // } else { + // self.logInstance.debug('options.src is JSON Object'); + // } + // return assertedOptions; + //}); + return assertOptions(options, ['src']) - .then(function (assertedOptions) { - if (typeof assertedOptions.src === 'string') { - self.logInstance.debug('options.src is to be verfied as File path: ' + assertedOptions.src); - // check for existing source file - try { - var stats = fs.statSync(assertedOptions.src); - if (stats.isDirectory()) { - return Promise.reject(new Error('Source file (options.src) is a directory, pls specify a valid file resource!')); - } - } catch (err) { - err.message = 'Error occurred while checking input file \'' + assertedOptions.src + '\' which should be existing and accessible, code: ' + err.code + ', cause: ' + err.message; - return Promise.reject(err); - } - } else if (isStream.readable(assertedOptions.src)) { - self.logInstance.debug('options.src is Readable stream'); - if (!options.origin) { - return Promise.reject(new Error('When options.src is a Readable stream then setting options.origin is mandatory!')); + .then(function (options) { + return assertFileSrc(options); + }) + .spread(function (checked, options) { + if (!checked) { + return assertStreamSrc(options); + } + return [checked, options]; + }) + .spread(function (checked, options) { + if (!checked) { + return assertBufferSrc(options); + } + return [checked, options]; + }) + .spread(function (checked, options) { + if (!checked) { + self.logInstance.debug('options.src is JSON Object'); + } + return Promise.resolve(options); + }) + .catch(function (err) { + self.logInstance.error('options.src is unknown or invalid object: ' + options.src); + return Promise.reject(err); + }); + }; + + function assertFileSrc(options) { + return new Promise(function (resolve, reject) { + if (typeof options.src === 'string') { + self.logInstance.debug('options.src is to be verified as File path: ' + options.src); + // check for existing source file + try { + var stats = fs.statSync(options.src); + if (stats.isDirectory()) { + return reject(new Error('Source file (options.src) is a directory, pls specify a valid file resource!')); } + return resolve([true, options]); + } catch (err) { + err.message = 'Error occurred while checking input file \'' + options.src + '\' which should be existing and accessible, code: ' + err.code + ', cause: ' + err.message; + return reject(err); + } + } else { + return resolve([false, options]); + } + }); + } + + function assertStreamSrc(options) { + return new Promise(function (resolve, reject) { + if (isStream.readable(options.src)) { + self.logInstance.debug('options.src is Readable stream'); + if (!options.origin) { + return reject(new Error('when options.src is a Readable stream, then setting options.origin is mandatory!')); + } + return resolve([true, options]); + } else { + return resolve([false, options]); + } + }); + } + + function assertBufferSrc(options) { + return new Promise(function (resolve, reject) { + if (options.src instanceof Buffer) { + self.logInstance.debug('options.src is Buffer'); + if (!options.origin) { + return reject(new Error('when options.src is a Buffer, then setting options.origin is mandatory!')); } else { - self.logInstance.debug('options.src is JSON Object'); + return resolve([true, options]); } - return assertedOptions; + } else { + return resolve([false, options]); + } }); - }; + } /** * This method ensures that destination file path is created if not set in diff --git a/lib/reader.js b/lib/reader.js index 07beccd..2c9de85 100644 --- a/lib/reader.js +++ b/lib/reader.js @@ -23,7 +23,7 @@ var stringify = require('json-stringify-safe'); * @returns {Reader} The instance. * @constructor * @class This class provides utility methods usable to read YAML, JSON or JS - * from a source (file, {object} or {@link stream.Readable}) to JS memory objects. + * from a source (file, {object}, {@link Buffer} or {@link stream.Readable}) to JS memory objects. * @example * var Reader = require('jy-transform').Reader; * var logger = ...; @@ -80,7 +80,7 @@ function Reader(logger) { * @param {stream.Readable} readable - The source to read from. * @param {function} resolve - Callback for success case. * @param {function} reject - Callback for Error case. - * @param {string} origin - Origin type, must be 'yaml' or 'json'. + * @param {string} origin - Origin type, must be 'yaml' or 'json'/'js'. * @private */ function readFromStream(readable, resolve, reject, origin) { @@ -93,12 +93,14 @@ function Reader(logger) { .on('end', function () { var buffer = Buffer.concat(bufs); try { - if (origin === Constants.JSON) { + self.logInstance.debug(origin + ' reading from Readable'); + if (origin === Constants.JSON || origin === Constants.JS) { resolve(JSON.parse(buffer.toString(Constants.UTF8))); } else if (origin === Constants.YAML) { resolve(jsYaml.safeLoad(buffer.toString(Constants.UTF8))); + } else { + reject(new Error('Unsupported type: ' + origin + ' to read from Readable')); } - self.logInstance.debug(origin + ' loaded from stream'); } catch (err) { // probably a SyntaxError for JSON or a YAMLException self.logInstance.error('Unexpected error: ' + err.stack); readable.emit('error', err); // send to .on('error',... @@ -106,6 +108,32 @@ function Reader(logger) { }); } + /** + * Reads from a passed _single_ {@link Buffer} (i.e. non-streamed) and + * resolves by callback. + * + * @param {Buffer} buffer - The source to read from. + * @param {function} resolve - Callback for success case. + * @param {function} reject - Callback for Error case. + * @param {string} origin - Origin type, must be 'yaml' or 'json'/'js'. + * @private + */ + function readFromBuffer(buffer, resolve, reject, origin) { + try { + self.logInstance.debug(origin + ' reading from Buffer'); + if (origin === Constants.JSON || origin === Constants.JS) { + resolve(JSON.parse(buffer.toString(Constants.UTF8))); + } else if (origin === Constants.YAML) { + resolve(jsYaml.safeLoad(buffer.toString(Constants.UTF8))); + } else { + reject(new Error('Unsupported type: ' + origin + ' to read from Buffer')); + } + } catch (err) { // probably a SyntaxError for JSON or a YAMLException + self.logInstance.error('Unexpected error: ' + err.stack); + reject(err); + } + } + /////////////////////////////////////////////////////////////////////////////// // API METHODS (PUBLIC) /////////////////////////////////////////////////////////////////////////////// @@ -187,6 +215,8 @@ function Reader(logger) { } } else if (isStream.readable(assertedOptions.src)) { readFromStream(assertedOptions.src, resolve, reject, Constants.JSON); + } else if (assertedOptions.src instanceof Buffer) { + readFromBuffer(assertedOptions.src, resolve, reject, Constants.JSON); } else if (options.imports && options.imports !== '') { if (!self.validator.validateIdentifier(options.imports)) { @@ -258,8 +288,9 @@ function Reader(logger) { } }); } else if (isStream.readable(assertedOptions.src)) { - // read YAML readFromStream(assertedOptions.src, resolve, reject, Constants.YAML); + } else if (Buffer.isBuffer(assertedOptions.src)) { + readFromBuffer(assertedOptions.src, resolve, reject, Constants.YAML); } else { resolve(assertedOptions.src); } diff --git a/lib/writer.js b/lib/writer.js index 320c6f9..51f089f 100644 --- a/lib/writer.js +++ b/lib/writer.js @@ -224,6 +224,31 @@ function Writer(logger) { dest.end(); } + /** + * Writes a string serialized data object to a `Buffer`. + * + * @param {string} object - The data to write into stream. + * @param {string} dest - The {@link Buffer} destination. + * @param {string} target - The target type, one of [ 'yaml' | 'json' | 'js' ]. + * @param {function} resolve - The Promise `resolve` callback. + * @param {function} reject - The Promise `reject` callback. + * @see {@link Constants#YAML} + * @see {@link Constants#JSON} + * @see {@link Constants#JS} + * @returns {Promise} - Containing the write success message to handle by caller (e.g. for logging). + * @throws {Error} - If serialized JS object could not be written due to any reason. + * @private + */ + function writeToBuffer(object, dest, target, resolve, reject) { + // write stringified data + try { + dest.write(object, Constants.UTF8); + resolve('Writing ' + target + ' to Buffer successful.'); + } catch (err) { + reject(err); + } + } + /////////////////////////////////////////////////////////////////////////////// // API METHODS (PUBLIC) /////////////////////////////////////////////////////////////////////////////// @@ -294,7 +319,9 @@ function Writer(logger) { writeToFile(yaml, dest, Constants.YAML, resolve, reject, ensuredOptions.force); } else if (isStream.writable(dest)) { // stream writeToStream(yaml, dest, Constants.YAML, resolve, reject); - } else { // file + } else if (Buffer.isBuffer(dest)) { // Buffer + writeToBuffer(yaml, dest, Constants.YAML, resolve, reject); + } else { // object ensuredOptions.dest = yaml; resolve('Writing YAML to options.dest successful.'); } @@ -372,6 +399,8 @@ function Writer(logger) { writeToFile(serializeJsToJsonString(object, indent), dest, Constants.JSON, resolve, reject, ensuredOptions.force); } else if (isStream.writable(dest)) { // stream writeToStream(serializeJsToJsonString(object, indent), dest, Constants.JSON, resolve, reject); + } else if (Buffer.isBuffer(dest)) { // Buffer + writeToBuffer(serializeJsToJsonString(object, indent), dest, Constants.JSON, resolve, reject); } else { // object ensuredOptions.dest = serializeJsToJsonString(object, indent); resolve('Writing JSON to options.dest successful.'); @@ -462,6 +491,8 @@ function Writer(logger) { .catch(function (err) { reject(err); }); + } else if (Buffer.isBuffer(dest)) { // Buffer + writeToBuffer(serializeJsToJsonString(object, indent), dest, Constants.JSON, resolve, reject); } else { // object var msg; if (ensuredOptions.exports && ensuredOptions.exports !== '') { diff --git a/package.json b/package.json index 1b86989..a91ac8f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jy-transform", - "description": "This project aims to read, write and transform YAML, JS or JSON objects into each other using CLI or API. The source and destination resources can be files on CLI and additionally, objects or streams on API level. Besides the transformation feature this module can also be used for simple loading and/or writing YAML, JS or JSON files.", + "description": "This project aims to read, write and transform YAML, JS or JSON objects into each other using CLI or API, while the source and destination resources can be files on CLI and additionally, objects, buffers or streams on API level.", "version": "1.0.2", "homepage": "https://github.com/deadratfink/jy-transform", "author": { @@ -35,8 +35,8 @@ } }, "scripts": { - "docs": "cat docs/BADGES.md > README.md && cat docs/TOC.md >> README.md && package-json-to-readme --no-footer ./package.json >> README.md && cat docs/USAGE.md >> README.md && echo '\n\n' >> README.md && cat docs/CONTRIBUTING.md >> README.md && doctoc README.md --github --title '# TOC' --maxlevel 2", - "wiki": "jsdoc2md lib/*.js index.js > docs/API.md && doctoc docs/API.md --github --title '### TOC' --maxlevel 2 && cat docs/API.md > '../jy-transform.wiki/API-v1.0.md' && cat CHANGELOG.md > ../jy-transform.wiki/Changelog.md && doctoc ../jy-transform.wiki/Changelog.md --github --title '### TOC' --maxlevel 3", + "docs": "cat docs/LOGO.md > README.md && cat docs/BADGES.md >> README.md && cat docs/TOC.md >> README.md && package-json-to-readme --no-footer ./package.json >> README.md && cat docs/USAGE.md >> README.md && echo '\n\n' >> README.md && cat docs/CHANGELOG.md >> README.md && doctoc README.md --github --title '# TOC' --maxlevel 2", + "wiki": "jsdoc2md ./jyt lib/*.js index.js > docs/API.md && doctoc docs/API.md --github --title '### TOC' --maxlevel 2 && cat docs/API.md > '../jy-transform.wiki/API-v1.0.md' && cat docs/CONTRIBUTING.md >> ../jy-transform.wiki/Contributing.md", "test": "istanbul cover _mocha --report lcovonly -- -R $npm_package_config_test_mocha_unit_reporter ./test/test*.js" }, "engines": { diff --git a/test/test-options-handler.js b/test/test-options-handler.js index b4aff4d..a7ea1af 100644 --- a/test/test-options-handler.js +++ b/test/test-options-handler.js @@ -464,7 +464,7 @@ describe('Executing \'jy-transform\' project OptionsHandler test suite.', functi describe('Testing OptionsHandler.ensureSrc(...)', function () { it('should reject when options is missing', function (done) { - assertOptionsError(null, optionsHandler.ensureSrc, done); + assertOptionsError(null, optionsHandler.ensureSrc, done, TypeError); // TODO heck if this really TypeError!?! }); it('should reject when options.src is not given', function (done) { From 9cf1a239abe9720de70dd9092662e7a169d19ecd Mon Sep 17 00:00:00 2001 From: "Krefeldt, Jens" Date: Thu, 28 Apr 2016 00:36:49 +0200 Subject: [PATCH 04/12] Update wiki doc generation --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a91ac8f..01a93a7 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,7 @@ }, "scripts": { "docs": "cat docs/LOGO.md > README.md && cat docs/BADGES.md >> README.md && cat docs/TOC.md >> README.md && package-json-to-readme --no-footer ./package.json >> README.md && cat docs/USAGE.md >> README.md && echo '\n\n' >> README.md && cat docs/CHANGELOG.md >> README.md && doctoc README.md --github --title '# TOC' --maxlevel 2", - "wiki": "jsdoc2md ./jyt lib/*.js index.js > docs/API.md && doctoc docs/API.md --github --title '### TOC' --maxlevel 2 && cat docs/API.md > '../jy-transform.wiki/API-v1.0.md' && cat docs/CONTRIBUTING.md >> ../jy-transform.wiki/Contributing.md", + "wiki": "jsdoc2md ./jyt lib/*.js index.js > docs/API.md && doctoc docs/API.md --github --title '### TOC' --maxlevel 2 && cat docs/API.md > '../jy-transform.wiki/API-v2.md' && cat docs/CONTRIBUTING.md > ../jy-transform.wiki/Contributing.md", "test": "istanbul cover _mocha --report lcovonly -- -R $npm_package_config_test_mocha_unit_reporter ./test/test*.js" }, "engines": { From b04219e24fc9ead7524f9a8c18eeaac492b14d3a Mon Sep 17 00:00:00 2001 From: "Krefeldt, Jens" Date: Thu, 28 Apr 2016 01:04:09 +0200 Subject: [PATCH 05/12] Fix docs, add Buffer examples --- README.md | 2 +- docs/USAGE.md | 2 +- lib/writer.js | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 50e4f7b..b0c46aa 100644 --- a/README.md +++ b/README.md @@ -757,7 +757,7 @@ function error(msg) # API Reference For more details on how to use the API, please refer to the -[API Reference](https://github.com/deadratfink/jy-transform/wiki/API-v1.0) +[API Reference](https://github.com/deadratfink/jy-transform/wiki/API-v2) wiki which describes the full API and provides more examples. # Contributing diff --git a/docs/USAGE.md b/docs/USAGE.md index 2bbc539..090068a 100644 --- a/docs/USAGE.md +++ b/docs/USAGE.md @@ -599,7 +599,7 @@ function error(msg) # API Reference For more details on how to use the API, please refer to the -[API Reference](https://github.com/deadratfink/jy-transform/wiki/API-v1.0) +[API Reference](https://github.com/deadratfink/jy-transform/wiki/API-v2) wiki which describes the full API and provides more examples. # Contributing diff --git a/lib/writer.js b/lib/writer.js index 51f089f..385030f 100644 --- a/lib/writer.js +++ b/lib/writer.js @@ -295,6 +295,19 @@ function Writer(logger) { * .catch(function (err) { * logger.error(err.stack); * }); + * + * options = { + * dest: new Buffer(), + * indent: 2 + * } + * + * writer.writeYaml(obj, options) + * .then(function (msg){ + * logger.info(msg); + * }) + * .catch(function (err) { + * logger.error(err.stack); + * }); */ this.writeYaml = function (object, options) { return self.optionsHandler.ensureIndent(options) @@ -384,6 +397,19 @@ function Writer(logger) { * .catch(function (err) { * logger.error(err.stack); * }); + * + * options = { + * dest: new Buffer(), + * indent: 2 + * } + * + * writer.writeJson(obj, options) + * .then(function (msg){ + * logger.info(msg); + * }) + * .catch(function (err) { + * logger.error(err.stack); + * }); */ this.writeJson = function (object, options) { return self.optionsHandler.ensureIndent(options) @@ -464,6 +490,19 @@ function Writer(logger) { * .catch(function (err) { * logger.error(err.stack); * }); + * + * options = { + * dest: new Buffer(), + * indent: 2 + * } + * + * writer.writeJs(obj, options) + * .then(function (msg){ + * logger.info(msg); + * }) + * .catch(function (err) { + * logger.error(err.stack); + * }); */ this.writeJs = function (object, options) { return self.optionsHandler.ensureIndent(options) From ac4bbbf965e503f49c0d2e8dc71e5fa48b82991a Mon Sep 17 00:00:00 2001 From: "Krefeldt, Jens" Date: Wed, 25 May 2016 17:37:14 +0200 Subject: [PATCH 06/12] README.me reverted to latest published --- README.md | 270 ++++++++++++++++++++++++------------------------------ 1 file changed, 119 insertions(+), 151 deletions(-) diff --git a/README.md b/README.md index b0c46aa..dc3886f 100644 --- a/README.md +++ b/README.md @@ -1,31 +1,21 @@ -![jy-transform logo](https://github.com/deadratfink/jy-transform/blob/master/image/jytransform.png) - # Stats -## General - | [Github License](https://github.com/deadratfink/jy-transform/blob/master/LICENSE.md) | [Github Issues](https://github.com/deadratfink/jy-transform/issues) | [Github Release](https://github.com/deadratfink/jy-transform/releases) | [Github Tags](https://github.com/deadratfink/jy-transform/tags) | [Travis CI](https://travis-ci.org) | [Waffle](https://waffle.io/deadratfink/jy-transform) | [Code Climate](https://codeclimate.com/github/deadratfink/jy-transform) | | --- | --- | --- | --- | --- | --- | --- | | [![License][gh-license-image]][gh-license-url] | [![Issue Stats][gh-issues-image]][gh-issues-url] | [![Github Releases][gh-releases-image]][gh-releases-url] | [![Github Tags][gh-tags-image]][gh-tags-url] | [![Build Status][ci-image]][ci-url] | [![Waffle][waffle-image]][waffle-url] | [![Code Climate][cocl-image]][cocl-url] | -## Branches - | Branch | [Codecov](https://codecov.io) | [Coveralls](https://coveralls.io) | [Inch CI](http://inch-ci.org) | [David](https://david-dm.org) DM | [David](https://david-dm.org) DM (dev) | | --- | --- | --- | --- | --- | --- | | master | [![codecov.io][cc-image-master]][cc-url-master] | [![coveralls.io][ca-image-master]][ca-url-master] | [![inch-ci.org][inch-image-master]][inch-url-master] | [![Dependency Status][dep-image-master]][dep-url-master] | [![devDependency Status][devdep-image-master]][devdep-url-master] | | development | [![codecov.io][cc-image-development]][cc-url-development] | [![coveralls.io][ca-image-development]][ca-url-development] | [![inch-ci.org][inch-image-development]][inch-url-development] | [![Dependency Status][dep-image-development]][dep-url-development] | [![devDependency Status][devdep-image-development]][devdep-url-development] | -### Coverage Graphs +## Coverage Graphs | Branch | Graph | | --- | --- | | master | ![codecov.io](https://codecov.io/github/deadratfink/jy-transform/branch.svg?branch=master&vg=true) | | development| ![codecov.io](https://codecov.io/github/deadratfink/jy-transform/branch.svg?branch=development&vg=true) | -## NPM - -[![NPM](https://nodei.co/npm/jy-transform.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/jy-transform/) -[![NPM](https://nodei.co/npm-dl/jy-transform.png?height=3&months=9)](https://nodei.co/npm-dl/jy-transform/) [gh-license-image]: https://img.shields.io/github/license/deadratfink/jy-transform.svg?style=flat-square [gh-license-url]: https://github.com/deadratfink/jy-transform/blob/master/LICENSE.md @@ -100,14 +90,14 @@ - [API Usage](#api-usage) - [Using Custom Logger](#using-custom-logger) - [API Reference](#api-reference) -- [Contributing](#contributing) - [Changelog](#changelog) +- [Contributing](#contributing) # jy-transform -This project aims to read, write and transform YAML, JS or JSON objects into each other using CLI or API, while the source and destination resources can be files on CLI and additionally, objects, buffers or streams on API level. +This project aims to read, write and transform YAML, JS or JSON objects into each other using CLI or API. The source and destination resources can be files on CLI and additionally, objects or streams on API level. Besides the transformation feature this module can also be used for simple loading and/or writing YAML, JS or JSON files. ## Installation @@ -196,36 +186,29 @@ Reading from: - _*.js_ file - _*.json_ file -Additionally, on API level to: +Additionally, on API level: -- a `stream.Readable` - - Serialized JSON and YAML - - Requires `options.origin` property set - - Reads as UTF-8 -- a `buffer.Buffer` - - Serialized JSON and YAML - - Requires `options.origin` property set - - Reads as UTF-8 +- `stream.Readable` (requires `options.origin` property set, reads as UTF-8) - any JS `object` (actually, this means the reading phase is skipped, because object is in-memory already) ### Transformation The transformation can take place into several directions: -- YAML ⇒ JS -- YAML ⇒ JSON -- JS ⇒ YAML -- JSON ⇒ YAML -- JS ⇒ JSON -- JSON ⇒ JS -- YAML ⇒ YAML -- JSON ⇒ JSON -- JS ⇒ JS +- YAML => JS +- YAML => JSON +- JS => YAML +- JSON => YAML +- JS => JSON +- JSON => JS +- YAML => YAML +- JSON => JSON +- JS => JS while: - [YAML](http://http://yaml.org/) = _*.yaml_, _*.yml_ -- [JS](https://developer.mozilla.org/en-US/docs/Web/JavaScript) = _*.js_ (JS object) +- [JS](https://developer.mozilla.org/en-US/docs/Web/JavaScript) = _*.js_ (JS object) - [JSON](http://json.org) = _*.json_ (JS object serialized as JSON) ### Middleware @@ -241,16 +224,9 @@ Writing to: - _*.js_ file - _*.json_ file -Additionally, on API level to: +Additionally, on API level: -- a `stream.Writable` - - Serialized JSON and YAML - - Requires `options.target` property set - - Writes UTF-8 -- a `buffer.Buffer` - - Serialized JSON and YAML - - Requires `options.target` property set - - Writes UTF-8 +- `stream.Writable` (requires `options.target` property set, writes UTF-8) - any JS `object` ## Limitations @@ -286,50 +262,58 @@ Additionally, on API level to: The CLI provides the `jyt` command (actually, this requires the use of options). After the global installation you can access the `Transformer` command options -with the usual command option `--help` option which prints an overview about all -available CLI properties: +with the usual help command as follows: + +``` +$ jyt --help +``` + +### CLI Options + +The `--help` option prints an overview about all available CLI properties: ``` $ jyt --help Usage: - jyt [INPUT-FILE] [OUTPUT-FILE] [OPTIONS] + jyt [OPTIONS] Options: - -o, --origin [STRING] The origin type of INPUT-FILE: [ js | json | yaml ]. (Default is if not given, the type is tried to be inferred from the extension of source path, else it is 'yaml') - -t, --target [STRING] The target type of OUTPUT-FILE: [ js | json | yaml ]. (Default is if not given, the type is tried to be inferred from the extension of destination path, else it is 'js') - -i, --indent [NUMBER] The indention for pretty-print: 1 - 8. (Default is 4) - -f, --force Force overwriting of existing output files on write phase. When files are not overwritten (which is default), - then the next transformation with same output file name gets a consecutive number on the base file name, e.g. in - case of foo.yaml it would be foo(1).yaml. - -m, --imports STRING Define a 'module.exports[.identifier] = ' identifier (to read from JS _source_ file only, must be a valid JS - identifier!). - -x, --exports STRING Define a 'module.exports[.identifier] = ' identifier (for usage in JS destination file only, must be a valid JS - identifier!). + -o, --origin [STRING] The conversion origin: [ js | json | yaml ]. (Default is : if not given, the type is tried to be inferred from the extension of source path, else it is yaml) + -t, --target [STRING] The conversion target: [ js | json | yaml ]. (Default is : if not given, the type is tried to be inferred from the extension of destination path, else it is js) + -s, --src PATH The absolute/relative input file path. + -d, --dest [PATH] The absolute/relative output file path. When this + options is ommited then the output file is stored + relative to the input file (same base name but with + another extension if type differs). If input and + output type are the same then the file overwriting is + handled depending on the '--force' value! (Default is storing relative to input file) + -i, --indent [NUMBER] The indention for pretty-print: 1 - 8. (Default is 4) + -f, --force Force overwriting of existing output files on write + phase. When files are not overwritten (which is + default), then the next transformation with same + output file name gets a consecutive number on the base + file name, e.g. in case of foo.yaml it would be + foo(1).yaml. + -m, --imports STRING Define a 'module.exports[.identifier] = ' + identifier (to read from JS _source_ file only, must + be a valid JS identifier!). + -x, --exports STRING Define a 'module.exports[.identifier] = ' + identifier, for usage in JS destination file only, + must be a valid JS identifier! -k, --no-color Omit color from output --debug Show debug information -v, --version Display the current version -h, --help Display help and usage details ``` -### CLI Args - -The ARGS are more formally defined in the following table: - -| Arg | Type | Description | Default | Required | -| --- | --- | --- | --- | --- | -| `INPUT-FILE` | URI | The source file path for transformation. | - | yes | -| `OUTPUT-FILE` | URI | The destination file path to transform to. | When this options is omitted then the output file is stored relative to the input file (same base name but with another extension if type differs). If input and output type are the same then the file overwriting is handled depending on the `--force` value! | no | - -**NOTE:** the input file has to be specified and should _first_ argument (in fact, it can be anywhere but it must be before an out file argument)! - -### CLI Options - -The OPTIONS are more formally defined in the following table: +These are more formally defined in the following table: | Option (short) | Option (long) | Type | Description | Default | Required | | --- | --- | --- | --- | --- | --- | -| `-o` | `--origin` | string of: [ _js_ | _json_ | _yaml_ ] | The transformation origin type. | if not given, the type is tried to be inferred from the extension of source path, else it is _yaml_ | no | -| `-t` | `--target` | string of: [ _js_ | _json_ | _yaml_ ] | The transformation target type. | if not given, the type is tried to be inferred from the extension of destination path, else it is _js_ | no | +| `-o` | `--origin` | [ _js_ | _json_ | _yaml_ ] | The transformation origin type. | if not given, the type is tried to be inferred from the extension of source path, else it is _yaml_ | no | +| `-t` | `--target` | [ _js_ | _json_ | _yaml_ ] | The transformation target type. | if not given, the type is tried to be inferred from the extension of destination path, else it is _js_ | no | +| `-s` | `--src` | URI | The source file path for transformation. | - | yes | +| `-d` | `--dest` | URI | The destination file path to transform to. | When this options is ommited then the output file is stored relative to the input file (same base name but with another extension if type differs). If input and output type are the same then the file overwriting is handled depending on the `--force` value! | no | | `-i` | `--indent` | integer
- [ 1 - 8 ]
| The code indention used in destination files. | 4 | no | | `-f` | `--force` | n/a | Force overwriting of existing output files on write phase. When files are not overwritten (which is default), then the next transformation with same output file name gets a consecutive number on the base file name, e.g. in case of foo.yaml it would be foo(1).yaml. | _false_ | no | | `-m` | `--imports` | string | Define a 'module.exports[.identifier] = ' identifier (to read from JS _source_ file only, must be a valid JS identifier!) | _undefined_ | no | @@ -340,6 +324,7 @@ The OPTIONS are more formally defined in the following table: | `-h` | `--help` | n/a | Display help and usage details. | n/a | no | + **NOTE:** an invalid indention setting (1 > `-i`, `--indent` > 8) does not raise an error but a default of 4 SPACEs is applied instead. ### Examples @@ -350,7 +335,7 @@ have a YAML file located in _foo.yaml_ holding this data: ```yaml foo: bar ``` -#### Example: YAML ⇒ JSON +#### Example: YAML => JSON then we can transform it to a JSON file _foo.json_ @@ -363,7 +348,7 @@ then we can transform it to a JSON file _foo.json_ using this command: ``` -$ jyt foo.yaml -t json -i 2 +$ jyt -s foo.yaml -t json -i 2 ``` In this example we have overwritten the standard target type (which is `js`) @@ -376,16 +361,16 @@ default `js` would have been applied! If the source would have been a `js` type like ``` -$ jyt foo.js -t json -i 2 +$ jyt -s foo.js -t json -i 2 ``` then the `js` value for `origin` is automatically inferred from file extension. Accordingly, this is also true for the `target` option. -#### Example: JSON ⇒ JS +#### Example: JSON => JS ``` -$ jyt foo.json -i 2 +$ jyt -s foo.json -i 2 ``` ```javascript module.exports = { @@ -393,10 +378,10 @@ module.exports = { } ``` -#### Example: JS ⇒ YAML +#### Example: JS => YAML ``` -$ jyt foo.js -t yaml +$ jyt -s foo.js -t yaml ``` ```yaml foo: bar @@ -404,22 +389,22 @@ foo: bar #### Example: Transformation with Different Destination -Simply specify the _output_ file with a different file name: +Simply provide the `-d` with a different file name: ``` -$ jyt foo.json results/foobar.yaml +$ jyt -s foo.json -d results/foobar.yaml ``` #### Example: Transformation with Unsupported Source File Extension As said, normally we infer from file extension to the type but assume the source -file has a file name which does not imply the type (here a JSON +file has a file name which does not imply the type (here JS type in a TEXT file), then you can simply provide the `-o` option with the correct `origin` type (of course, the `-t` option works analogous): ``` -$ jyt foo.txt foobar.yaml -o json +$ jyt -s foo.txt -o js -d foobar.yaml ``` #### Example: Read from File with Exports Identifier @@ -442,7 +427,7 @@ module.exports.bar = { but you want to convert `bar` object, then call: ``` -$ jyt foo.js bar.yaml -m bar +$ jyt -s foo.js -m bar -d bar.yaml ``` to get the YAML result: @@ -490,7 +475,7 @@ foo: bar using this command: ``` -$ jyt foo.yaml foobar.js -x foobar +$ jyt -s foo.yaml -d foobar.js -x foobar ``` This generates the following output in JS file using `foobar` as identifier: @@ -518,12 +503,12 @@ But let's say we want to overwrite the original source now because you want to change the indention from 2 to 4 SPACEs, then we can do this as follows: ``` -$ jyt foo.js -f +$ jyt -s foo.js -f ``` Of course, leaving out the `-f` switch creates a new file relatively to the `origin`, named as _foo(1).js_ (note the consecutive number). Naturally, -another run of the command would result in a file called _foo(2).js_ +another run of the command would result int a file called _foo(2).js_ and so forth. ## Origin and Target Type Inference @@ -545,13 +530,16 @@ specify the origin or target type! Since the usage on CLI is a 2-step process: -1. Read from source file to JS object ⇒ 2. Write out (maybe to other type) +1. Read from source file to JS object => +2. Write out (maybe to other type) the direct API calls additionally provide the usage of a _middleware_ function where you can alter the input JS object before it is written and therefore, which turns this into a 3-step process: -1. Read from source ⇒ 2. Alter the JS object ⇒ 3. Write out (maybe to other type) +1. Read from source => +2. Alter the JS object => +3. Write out (maybe to other type) For more details about this and all the functions provided by this module please refer to the [API Reference](https://github.com/deadratfink/jy-transform/wiki/API-v1.0). @@ -600,7 +588,7 @@ The `middleware` is optional but if provided it must be of type `Function` and a [Promise](http://bluebirdjs.com/docs/api-reference.html). One of the easiest ones is the identity function -_f(data) → data_ +_f(data) -> data_ which could be expressed as [Promise](http://bluebirdjs.com/docs/api-reference.html) function as follows: @@ -644,7 +632,7 @@ will result in such JSON file: ```javascript { - "foo": "new bar" + "foo": "new bar" } ``` @@ -671,11 +659,6 @@ function key2(data) { objectPath.set(data, 'key2', 'value2'); return Promise.resolve(data); } - -function key3(data) { - objectPath.set(data, 'key3', 'value3'); - return Promise.resolve(data); -} ``` These can be collected by different aggregation or composition functions of the underlying @@ -685,7 +668,7 @@ function. This one can collect all three functions above and ensure their proper ```javascript var middleware = function (data) { - return Promise.all([key1(data), key2(data), key3(data)]) + return Promise.all([key1(data), key2(data)]) .then(function(result) { return Promise.resolve(result[result.length - 1]); }); @@ -693,9 +676,7 @@ var middleware = function (data) { var transformer = new Transformer(logger); var logger = ...; -var options = { - src: {} -}; +var options = {...}; return transformer.transform(options, middleware) .then(function (msg){ @@ -708,15 +689,13 @@ return transformer.transform(options, middleware) Then the result in the `middleware` function can be retrieved from the returned array, i.e. in case of [`Promise.all([...])`](http://bluebirdjs.com/docs/api/promise.all.html) -you have to pick the _last_ element which contains the "final product". - -From our example above it would be result in this object +you have to pick the _last_ element which contains the "final product". +From our example above it would be ```javascript { key1: 'value1', - key2: 'value2', - key3: 'value3' + key2: 'value2' } ``` @@ -746,7 +725,7 @@ var transformer = new Transformer(logger); var writer = new Writer(logger); ``` -At least, the passed logger object _has_ to support the following functions: +At least, the passed logger object _has to_ support the following functions: ```javascript function info(msg) @@ -757,58 +736,47 @@ function error(msg) # API Reference For more details on how to use the API, please refer to the -[API Reference](https://github.com/deadratfink/jy-transform/wiki/API-v2) +[API Reference](https://github.com/deadratfink/jy-transform/wiki/API-v1.0) wiki which describes the full API and provides more examples. -# Contributing +# Changelog -Pull requests and stars are always welcome. Anybody is invited to take part -into this project. For bugs and feature requests, please create an -[issue](https://github.com/deadratfink/jy-transform/issues). -See the wiki [Contributing](https://github.com/deadratfink/jy-transform/wiki/Changelog) -section for more details about conventions. +The complete changelog is listed in the wiki [Changelog](https://github.com/deadratfink/jy-transform/wiki/Changelog) section. -# Changelog +# Contributing -### v2.0.0 - -- [[#32](https://github.com/deadratfink/jy-transform/issues/32)] Introduce input and output on CLI as ARGS instead of OPTIONS (non-backwards compatible change for CLI usage!) - - E.g. type `$ jyt foo.js bar.yaml` instead of `$ jyt -s foo.js -d bar.yaml` -- [[#31](https://github.com/deadratfink/jy-transform/issues/31)] Fix: given `Object` source results in 'yaml' for origin (API) -- [[#26](https://github.com/deadratfink/jy-transform/issues/26)] API level `dest`: support for writing serialized JSON and YAML to _single_ (i.e. non-streamed) `Buffer` - - Requires `options.target` property set - - Writes UTF-8 to Buffer -- [[#25](https://github.com/deadratfink/jy-transform/issues/25)] API level `src`: support for reading serialized JSON and YAML from _single_ (i.e. non-streamed) `Buffer` - - Requires `options.origin` property set - - Expects UTF-8 data in Buffer - -### v1.0.2 - -- [[#30](https://github.com/deadratfink/jy-transform/issues/30)] Fix README and externalize API reference to wiki -- [[#29](https://github.com/deadratfink/jy-transform/issues/29)] Fix Promise warning on write process - -### v1.0.1 - -Initial public release. This covers the basic implementation and tests. The following features and fixes and part of this release: - -- [[#27](https://github.com/deadratfink/jy-transform/issues/27)] Export variable for JS input -- [[#22](https://github.com/deadratfink/jy-transform/issues/22)] Integrate Coveralls -- [[#21](https://github.com/deadratfink/jy-transform/issues/21)] Check and fix CodeClimate issues -- [[#20](https://github.com/deadratfink/jy-transform/issues/20)] Cleanup test dir -- [[#19](https://github.com/deadratfink/jy-transform/issues/19)] File overwrite switch (`-f`, `-force`) -- [[#18](https://github.com/deadratfink/jy-transform/issues/18)] Read and Write from other sources than file path -- [[#16](https://github.com/deadratfink/jy-transform/issues/16)] ERROR: Error: Invalid target option found while creating destination file extension -- [[#15](https://github.com/deadratfink/jy-transform/issues/15)] Measure test code coverage and add a badge -- [[#12](https://github.com/deadratfink/jy-transform/issues/12)] Create middleware collection file to use by clients and internally -- [[#11](https://github.com/deadratfink/jy-transform/issues/11)] Check all Promises for optimization possibilities -- [[#10](https://github.com/deadratfink/jy-transform/issues/10)] Integrate project with Travis -- [[#9](https://github.com/deadratfink/jy-transform/issues/9)] Resolve origin and target from file extension whenever possible -- [[#8](https://github.com/deadratfink/jy-transform/issues/8)] Enable JS reading with `require(...)` -- [[#7](https://github.com/deadratfink/jy-transform/issues/7)] YAML indent is not set to `Constants.MIN_YAML_INDENT` when indent is set to 0 -- [[#6](https://github.com/deadratfink/jy-transform/issues/6)] Finish full JSDoc for all methods -- [[#5](https://github.com/deadratfink/jy-transform/issues/5)] Write unit tests -- [[#4](https://github.com/deadratfink/jy-transform/issues/4)] Export variable for JS output -- [[#3](https://github.com/deadratfink/jy-transform/issues/3)] Promise array as middleware solved with `Promise.all([...])` +Pull requests and stars are always welcome. Anybody is invited to take part +into this project. For bugs and feature requests, please create an +[issue](https://github.com/deadratfink/jy-transform/issues). +When contributing as coder, please take care of the following conventions: + +- Enter yourself in the `constributors` section of _package.json_. +- We strictly follow [Semantic Versioning 2](http://semver.org) rules. +- The `development` branch is the leading branch and is protected. Create bugfix and feature + branches (or fork into you own namespace) and create pull + requests to `development` when finished. Any of these should be prefixed with + `bugfix/#...` or `feature/#...` (followed by issue number and a short, "underscored" + proper meaning), e.g. + - `bugfix/#8_fix_js_reading_with_require` + - `feature/#14_multidocument_support` +- Remember that name could need to be enclosed in quotes, e.g. + ```$ git checkout -b 'feature/#19_...'``` + when using git shell command. +- The `master` branch is protected and is the stable branch after a release. + It will never be pushed directly (only on release build). +- Indention for any file is 4 SPACEs. +- Keep code coverage high (> 95%). +- Doc everything with [JSDocs](http://usejsdoc.org/) and document concepts in + [README.md](https://github.com/deadratfink/jy-transform/blob/development/README.md) + or [Wiki](https://github.com/deadratfink/jy-transform/wiki). +- Use single parenthesis (`'...'`) in _*.js_ files instead of double parenthesis (`"..."`). +- Avoid the of use parenthesis for keys in JSON objects. +- Use the strict mode (`'use strict';`) in _*.js_ files. +- File names should be lower-case with hyphens as divider, e.g. _options-handler.js_. +- Markdown documentation files should be upper-case with _.md_ as extension, placed + in _./docs_, e.g. _USAGE.md_. The _README.md_ is build up by these files concatenated + by `npm run docs` command. Any new files have to be added to `scripts.docs` section of + _package.json_. Don't forget to regenerate _README.md_ before committing. From a6b9178d40d0bb23b9979d2c45ba6666f76b07d6 Mon Sep 17 00:00:00 2001 From: "Krefeldt, Jens" Date: Fri, 22 Jul 2016 04:38:00 +0200 Subject: [PATCH 07/12] First buffer impl --- .gitignore | 1 + docs/BADGES.md | 16 ++++--- docs/CHANGELOG.md | 6 ++- docs/USAGE.md | 47 +++++++++++---------- jyt | 57 ++++++++++++++++++------- lib/constants.js | 8 ++++ lib/log-wrapper.js | 30 +++++++++++--- lib/options-handler.js | 30 +++++++------- lib/reader.js | 52 ++++++++++++++++------- lib/transformer.js | 10 ++--- lib/validator.js | 6 +-- lib/writer.js | 54 ++++++++++++++++++++---- package.json | 9 +--- test/data/readable-test-dummy.txt | 4 +- test/test-log-wrapper.js | 69 +++++++++++++++++++++++++++++-- test/test-middleware.js | 4 +- test/test-options-handler.js | 52 +++++++++++++++++++++-- test/test-reader.js | 6 +-- test/test-transformer.js | 4 +- test/test-validator.js | 2 +- test/test-writer.js | 40 ++++++++++++++++-- 21 files changed, 384 insertions(+), 123 deletions(-) diff --git a/.gitignore b/.gitignore index 606b8ac..000f680 100755 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ /docs/API.md .idea *.iml +/VERSION.txt node_modules /.project /.settings/* diff --git a/docs/BADGES.md b/docs/BADGES.md index 47ff09c..7262b7b 100644 --- a/docs/BADGES.md +++ b/docs/BADGES.md @@ -2,9 +2,9 @@ ## General -| [Github License](https://github.com/deadratfink/jy-transform/blob/master/LICENSE.md) | [Github Issues](https://github.com/deadratfink/jy-transform/issues) | [Github Release](https://github.com/deadratfink/jy-transform/releases) | [Github Tags](https://github.com/deadratfink/jy-transform/tags) | [Travis CI](https://travis-ci.org) | [Waffle](https://waffle.io/deadratfink/jy-transform) | [Code Climate](https://codeclimate.com/github/deadratfink/jy-transform) | +| [License](https://github.com/deadratfink/jy-transform/blob/master/LICENSE.md) | [Issues](https://github.com/deadratfink/jy-transform/issues) | [Releases](https://github.com/deadratfink/jy-transform/releases) | [Tags](https://github.com/deadratfink/jy-transform/tags) | [Travis CI](https://travis-ci.org) | [Waffle](https://waffle.io/deadratfink/jy-transform) | [Code Climate](https://codeclimate.com/github/deadratfink/jy-transform) | | --- | --- | --- | --- | --- | --- | --- | -| [![License][gh-license-image]][gh-license-url] | [![Issue Stats][gh-issues-image]][gh-issues-url] | [![Github Releases][gh-releases-image]][gh-releases-url] | [![Github Tags][gh-tags-image]][gh-tags-url] | [![Build Status][ci-image]][ci-url] | [![Waffle][waffle-image]][waffle-url] | [![Code Climate][cocl-image]][cocl-url] | +| [![License][gh-license-image]][gh-license-url] | [![Issue Stats][gh-issues-image]][gh-issues-url] | [![Releases][gh-releases-image]][gh-releases-url] | [![Tags][gh-tags-image]][gh-tags-url] | [![Build Status][ci-image]][ci-url] | [![Waffle][waffle-image]][waffle-url] | [![Code Climate][cocl-image]][cocl-url] | ## Branches @@ -13,12 +13,16 @@ | master | [![codecov.io][cc-image-master]][cc-url-master] | [![coveralls.io][ca-image-master]][ca-url-master] | [![inch-ci.org][inch-image-master]][inch-url-master] | [![Dependency Status][dep-image-master]][dep-url-master] | [![devDependency Status][devdep-image-master]][devdep-url-master] | | development | [![codecov.io][cc-image-development]][cc-url-development] | [![coveralls.io][ca-image-development]][ca-url-development] | [![inch-ci.org][inch-image-development]][inch-url-development] | [![Dependency Status][dep-image-development]][dep-url-development] | [![devDependency Status][devdep-image-development]][devdep-url-development] | -### Coverage Graphs +### Coverage -| Branch | Graph | +| master | development | | --- | --- | -| master | ![codecov.io](https://codecov.io/github/deadratfink/jy-transform/branch.svg?branch=master&vg=true) | -| development| ![codecov.io](https://codecov.io/github/deadratfink/jy-transform/branch.svg?branch=development&vg=true) | +| ![codecov.io](https://codecov.io/gh/deadratfink/jy-transform/branch/master/graphs/tree.svg) | ![codecov.io](https://codecov.io/gh/deadratfink/jy-transform/branch/development/graphs/tree.svg) | + + + + + ## NPM diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index ffea462..a855166 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,8 +1,10 @@ # Changelog -### v2.0.0 +### v2.0.0 (NOT RELEASED YET!!!) -- [[#32](https://github.com/deadratfink/jy-transform/issues/32)] Introduce input and output on CLI as ARGS instead of OPTIONS (non-backwards compatible change for CLI usage!) + +- [[#33](https://github.com/deadratfink/jy-transform/issues/33)] Enhance `LogWrapper` with `TRACE` level (API) +- [[#32](https://github.com/deadratfink/jy-transform/issues/32)] Introduce input and output on CLI as ARGS instead of OPTIONS (non-backwards compatible change for CLI usage, _no_ impact on API level!) - E.g. type `$ jyt foo.js bar.yaml` instead of `$ jyt -s foo.js -d bar.yaml` - [[#31](https://github.com/deadratfink/jy-transform/issues/31)] Fix: given `Object` source results in 'yaml' for origin (API) - [[#26](https://github.com/deadratfink/jy-transform/issues/26)] API level `dest`: support for writing serialized JSON and YAML to _single_ (i.e. non-streamed) `Buffer` diff --git a/docs/USAGE.md b/docs/USAGE.md index 090068a..e355072 100644 --- a/docs/USAGE.md +++ b/docs/USAGE.md @@ -38,17 +38,17 @@ Reading from: - _*.js_ file - _*.json_ file -Additionally, on API level to: +Additionally, on API level to a: -- a `stream.Readable` +- `stream.Readable` - Serialized JSON and YAML - Requires `options.origin` property set - Reads as UTF-8 -- a `buffer.Buffer` - - Serialized JSON and YAML +- `buffer.Buffer` + - Serialized JSON and YAML only (no JS) - Requires `options.origin` property set - Reads as UTF-8 -- any JS `object` (actually, this means the reading phase is skipped, because object is in-memory already) +- JS `object` (actually, this means the reading phase is skipped, because object is in-memory already) ### Transformation @@ -83,17 +83,17 @@ Writing to: - _*.js_ file - _*.json_ file -Additionally, on API level to: +Additionally, on API level to a: -- a `stream.Writable` - - Serialized JSON and YAML +- `stream.Writable` + - Serialized JS, JSON and YAML - Requires `options.target` property set - Writes UTF-8 -- a `buffer.Buffer` - - Serialized JSON and YAML +- `buffer.Buffer` + - Serialized JS, JSON and YAML - Requires `options.target` property set - Writes UTF-8 -- any JS `object` +- JS `object` ## Limitations @@ -172,8 +172,8 @@ The OPTIONS are more formally defined in the following table: | --- | --- | --- | --- | --- | --- | | `-o` | `--origin` | string of: [ _js_ | _json_ | _yaml_ ] | The transformation origin type. | if not given, the type is tried to be inferred from the extension of source path, else it is _yaml_ | no | | `-t` | `--target` | string of: [ _js_ | _json_ | _yaml_ ] | The transformation target type. | if not given, the type is tried to be inferred from the extension of destination path, else it is _js_ | no | -| `-i` | `--indent` | integer
- [ 1 - 8 ]
| The code indention used in destination files. | 4 | no | -| `-f` | `--force` | n/a | Force overwriting of existing output files on write phase. When files are not overwritten (which is default), then the next transformation with same output file name gets a consecutive number on the base file name, e.g. in case of foo.yaml it would be foo(1).yaml. | _false_ | no | +| `-i` | `--indent` | integer
[ 1 - 8 ]
| The code indention used in destination files. | 4 | no | +| `-f` | `--force` | n/a | Force overwriting of existing output files on write phase. When files are not overwritten (which is default), then the next transformation with same output file name gets a consecutive number on the base file name, e.g. in case of _foo.yaml_ it would be _foo(1).yaml_. | _false_ | no | | `-m` | `--imports` | string | Define a 'module.exports[.identifier] = ' identifier (to read from JS _source_ file only, must be a valid JS identifier!) | _undefined_ | no | | `-x` | `--exports` | string | Define a 'module.exports[.identifier] = ' identifier (for usage in JS _destination_ file only, must be a valid JS identifier!) | _undefined_ | no | | `-k` | `--no-color` | n/a | Omit color from output. | _color_ | no | @@ -209,7 +209,7 @@ $ jyt foo.yaml -t json -i 2 ``` In this example we have overwritten the standard target type (which is `js`) -and applying an indent of _2_ instead of the default _4_. As default the output +and applying an indent of 2 instead of the default 4. As default the output file _foo.json_ is written relative to the input file (simply omitting the `dest` option here). @@ -396,7 +396,7 @@ this into a 3-step process: 1. Read from source ⇒ 2. Alter the JS object ⇒ 3. Write out (maybe to other type) For more details about this and all the functions provided by this module please refer to the -[API Reference](https://github.com/deadratfink/jy-transform/wiki/API-v1.0). +[API Reference](https://github.com/deadratfink/jy-transform/wiki/API-v2). The `origin` and `target` type inference is also standard for the API level. @@ -415,12 +415,12 @@ The `options` object has to follow this key-values table: | --- | --- | --- | --- | --- | | origin | string | The origin type. | If not given, the type is tried to be inferred from the extension of source path, else it is _yaml_. | no | | target | string | The target type. | If not given, the type is tried to be inferred from the extension of destination path, else it is _js_ | no | -| src | string | Readable | object | The source information object: `string` is used as file path, `Readable` stream provides a stringified source and `object` is used as direct JS source. | - | yes | -| dest | string | Writable | object | The destination information object: `string` is used as file path, `Writable` stream writes a stringified source and `object` is used as direct JS object for assignment. | The output file is stored relative to the input file (same base name but with another extension if type differs). If input and output type are the same then the file overwriting is handled depending on the 'force' value! | no | +| src | string | Readable | object | Buffer | The source information object: `string` is used as file path, `Readable` stream provides a stringified source and `object` is used as direct JS source. | - | yes | +| dest | string | Writable | object | Buffer | The destination information object: `string` is used as file path, `Writable` stream writes a stringified source and `object` is used as direct JS object for assignment. | The output file is stored relative to the input file (same base name but with another extension if type differs). If input and output type are the same then the file overwriting is handled depending on the 'force' value! | no | | indent | number | The indention in files. | 4 | no | | force | boolean | Force overwriting of existing output files on write phase. When files are not overwritten, then the next transformation with same output file name gets a consecutive number on the base file name, e.g. in case of _foo.yaml_ it would be _foo(1).yaml_. | _false_ | no | -| imports | string | Define a 'module.exports[.identifier] = ' identifier (to read from JS _source_ only, must be a valid JS identifier!) | _undefined_ | no | -| exports | string | Define a 'module.exports[.identifier] = ' identifier (for usage in JS _destination_ only, must be a valid JS identifier!) | _undefined_ | no | +| imports | string | Define a `module.exports[.identifier] = ...` identifier (to read from JS _source_ only, must be a valid JS identifier!) | _undefined_ | no | +| exports | string | Define a `module.exports[.identifier] = ...` identifier (for usage in JS _destination_ only, must be a valid JS identifier!) | _undefined_ | no | **NOTE:** an invalid indention setting (1 > indent > 8) does not raise an error but a default of 4 SPACEs is applied instead. @@ -588,13 +588,18 @@ var transformer = new Transformer(logger); var writer = new Writer(logger); ``` -At least, the passed logger object _has_ to support the following functions: +At least, the passed `logger` object _has_ to support the following functions: ```javascript function info(msg) function debug(msg) -function error(msg) +function trace(msg) +function error(err|msg) ``` +Anyway, there are some fallbacks if a level is not supported: + +- DEBUG ⇒ INFO +- TRACE ⇒ DEBUG # API Reference diff --git a/jyt b/jyt index 631a210..a25c3ed 100755 --- a/jyt +++ b/jyt @@ -37,39 +37,66 @@ var packagePath = __dirname + '/package.json'; * @private */ var options = { - origin: [ 'o', 'The origin type of INPUT-FILE: [ ' + constants.JS + ' | ' + constants.JSON + ' | ' + constants.YAML + ' ].', 'string', constants.DEFAULT_OPTIONS.origin ], - target: [ 't', 'The target type of OUTPUT-FILE: [ ' + constants.JS + ' | ' + constants.JSON + ' | ' + constants.YAML + ' ].', 'string', constants.DEFAULT_OPTIONS.target ], - indent: [ 'i', 'The indention for pretty-print: 1 - 8.', 'int', constants.DEFAULT_INDENT ], - force: [ 'f', 'Force overwriting of existing output files on write phase. When files are not overwritten (which is default), then the next transformation with same output file name gets a consecutive number on the base file name, e.g. in case of foo.yaml it would be foo(1).yaml.' ], - imports: [ 'm', 'Define a \'module.exports[.identifier] = \' identifier (to read from JS _source_ file only, must be a valid JS identifier!).', 'string', constants.DEFAULT_OPTIONS.imports ], - exports: [ 'x', 'Define a \'module.exports[.identifier] = \' identifier (for usage in JS destination file only, must be a valid JS identifier!).', 'string', constants.DEFAULT_OPTIONS.exports ] + origin: [ 'o', 'The origin type of INPUT-FILE: [ ' + constants.JS + ' | ' + constants.JSON + ' | ' + constants.YAML + ' ]', 'string', constants.DEFAULT_OPTIONS.origin ], + target: [ 't', 'The target type of OUTPUT-FILE: [ ' + constants.JS + ' | ' + constants.JSON + ' | ' + constants.YAML + ' ]', 'string', constants.DEFAULT_OPTIONS.target ], + indent: [ 'i', 'The indention for pretty-print: 1 - 8', 'int', constants.DEFAULT_INDENT ], + force: [ 'f', 'Force overwriting of existing output files on write phase: when files are not overwritten (which is default), then the next transformation with same output file name gets a consecutive number on the base file name, e.g. in case of foo.yaml it would be foo(1).yaml' ], + imports: [ 'm', 'Define a \'module.exports[.identifier] = \' identifier (to read from JS _source_ file only, must be a valid JS identifier!)', 'string', constants.DEFAULT_OPTIONS.imports ], + exports: [ 'x', 'Define a \'module.exports[.identifier] = \' identifier (for usage in JS destination file only, must be a valid JS identifier!)', 'string', constants.DEFAULT_OPTIONS.exports ] }; +/** + * Prints the error to console and exit with 1. + * + * @param {string|Error} err - The error to print. + * @private + */ +function error(err) { + cli.error('////////////////////////////////////////////////////////////////////////////////'); + cli.error(err); + if (err.stack) { + cli.debug(err.stack); + } + cli.error('////////////////////////////////////////////////////////////////////////////////'); + cli.getUsage(1); +} + /** * The main entry callback. When calling `cli.main()` this receives the `options` * given on CLI, then does the transformation with these options and finally, it * prints the result to the CLI. * - * @param {array} args - Not used inside this method! + * @param {array} args - The first mandatory argument is the input file + * (`args[0]`), the second (optional) argument is the + * output file (`args[0]`). * @param {object} options - The options set on CLI. * @private */ function main(args, options) { - cli.debug('ARGS: ' + args[0] + ', ' + args[1]); - options.src = args[0]; - options.dest = args[1]; + // read file args and set to options + + if (args.length > 0) { + cli.debug('input file: ' + args[0]); + options.src = args[0]; + } else { + error('please specify an input file as first argument!'); + } + if (args.length > 1) { + cli.debug('output file: ' + args[1]); + options.dest = args[1]; + } else { + cli.debug('output file not specified, using default'); + } + + // transform with options return transformer.transform(options) .then(function (msg) { cli.info(msg); }) .catch(function (err) { - cli.error('////////////////////////////////////////////////////////////////////////////////'); - cli.error(err); - cli.debug(err.stack); - cli.error('////////////////////////////////////////////////////////////////////////////////'); - cli.getUsage(1); + error(err); }); } diff --git a/lib/constants.js b/lib/constants.js index 26355cf..fab466e 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -100,6 +100,7 @@ Constants.prototype.MAX_INDENT = 8; * The default `origin` value: 'yaml'. * * @type {string} + * @constant * @public */ Constants.prototype.DEFAULT_ORIGIN = constants.YAML; @@ -108,6 +109,7 @@ Constants.prototype.DEFAULT_ORIGIN = constants.YAML; * The default `origin` value: 'js'. * * @type {string} + * @constant * @public */ Constants.prototype.DEFAULT_TARGET = constants.JS; @@ -116,6 +118,7 @@ Constants.prototype.DEFAULT_TARGET = constants.JS; * Whether to overwrite existing file or object on output. * * @type {boolean} + * @constant * @public */ Constants.prototype.DEFAULT_FORCE_FILE_OVERWRITE = false; @@ -124,6 +127,7 @@ Constants.prototype.DEFAULT_FORCE_FILE_OVERWRITE = false; * The `origin` description value. * * @type {string} + * @constant * @public */ Constants.prototype.ORIGIN_DESCRIPTION = 'if not given, the type is tried to be inferred from the extension of source path, else it is \'' + constants.DEFAULT_ORIGIN + '\''; @@ -132,6 +136,7 @@ Constants.prototype.ORIGIN_DESCRIPTION = 'if not given, the type is tried to be * The `target` description value. * * @type {string} + * @constant * @public */ Constants.prototype.TARGET_DESCRIPTION = 'if not given, the type is tried to be inferred from the extension of destination path, else it is \'' + constants.DEFAULT_TARGET + '\''; @@ -140,6 +145,7 @@ Constants.prototype.TARGET_DESCRIPTION = 'if not given, the type is tried to be * The `dest` description value. * * @type {string} + * @constant * @public */ Constants.prototype.DEST_DESCRIPTION = 'storing relative to input file'; @@ -149,6 +155,7 @@ Constants.prototype.DEST_DESCRIPTION = 'storing relative to input file'; * * @type {string} * @public + * @constant * @example * module.exports.foo = {...}; // here 'foo' is the identifier for an object to read from the source! */ @@ -159,6 +166,7 @@ Constants.prototype.DEFAULT_JS_IMPORTS_IDENTIFIER = undefined; * * @type {string} * @public + * @constant */ Constants.prototype.DEFAULT_JS_EXPORTS_IDENTIFIER = undefined; diff --git a/lib/log-wrapper.js b/lib/log-wrapper.js index 2f68987..2bbb053 100644 --- a/lib/log-wrapper.js +++ b/lib/log-wrapper.js @@ -39,7 +39,22 @@ LogWrapper.prototype.constructor = LogWrapper; /////////////////////////////////////////////////////////////////////////////// /** - * Log the options with DEBUG level (logger supports it, else with INFO). + * Log the options with INFO level. + * + * @param {string} msg - The message to log. + * @example + * var logger = ...; + * var logWrapper = new LogWrapper(logger); + * var msg = '...'; + * logWrapper.info(msg); + * @public + */ +LogWrapper.prototype.info = function (msg) { + this.logInstance.info(msg); +}; + +/** + * Log the options with DEBUG level (if logger supports it, else with INFO). * * @param {string} msg - The message to log. * @example @@ -58,18 +73,23 @@ LogWrapper.prototype.debug = function (msg) { }; /** - * Log the options with INFO level. + * Log the options with TRACE level (if logger supports it, else with DEBUG). * * @param {string} msg - The message to log. * @example * var logger = ...; * var logWrapper = new LogWrapper(logger); * var msg = '...'; - * logWrapper.info(msg); + * logWrapper.trace(msg); * @public + * @see {@link #debug} */ -LogWrapper.prototype.info = function (msg) { - this.logInstance.info(msg); +LogWrapper.prototype.trace = function (msg) { + if (this.logInstance.trace && typeof this.logInstance.trace === 'function') { + this.logInstance.trace(msg); + } else { + this.debug(msg); + } }; /** diff --git a/lib/options-handler.js b/lib/options-handler.js index 13e6f50..da6bfa4 100644 --- a/lib/options-handler.js +++ b/lib/options-handler.js @@ -1,7 +1,7 @@ 'use strict'; -var Constants = require('./constants.js'); -var LogWrapper = require('./log-wrapper.js'); +var Constants = require('./constants'); +var LogWrapper = require('./log-wrapper'); var Promise = require('bluebird'); var path = require('path'); var fs = require('fs'); @@ -9,13 +9,13 @@ var isStream = require('is-stream'); /** * @typedef {object} Options - * @property {string} [origin=yaml] - The origin type. - * @property {string} [target=js] - The target type. - * @property {(string|Readable|object)} src - The source. - * @property {(string|Writable|object)} [dest] - The destination. - * @property {number} [indent=4] - The indention in files. - * @property {string} [imports=undefined] - The exports name for reading from JS source file or objects only. - * @property {string} [exports=undefined] - The exports name for usage in JS destination files only. + * @property {string} [origin=yaml] - The origin type. + * @property {string} [target=js] - The target type. + * @property {(string|Readable|object|Buffer)} src - The source (`string` type is treated as a file path). + * @property {(string|Writable|object|Buffer)} [dest] - The destination (`string` type is treated as a file path). + * @property {number} [indent=4] - The indention in files. + * @property {string} [imports=undefined] - The exports name for reading from JS source file or objects only. + * @property {string} [exports=undefined] - The exports name for usage in JS destination files only. */ /////////////////////////////////////////////////////////////////////////////// @@ -34,7 +34,7 @@ var isStream = require('is-stream'); * **NOTE:** This class is not to be intended to be called from * outside this module! * @example - * var OptionsHandler = require('./options-handler.js'); + * var OptionsHandler = require('./options-handler'); * var logger = ...; * * var optionsHandler = new OptionsHandler(logger); @@ -352,10 +352,10 @@ function OptionsHandler(logger) { self.logInstance.debug('Destination file: ' + assertedOptions.dest); return assertedOptions; }); - } else if (isStream.writable(assertedOptions.dest)) { - self.logInstance.debug('options.dest is Writable stream'); + } else if (isStream.writable(assertedOptions.dest) || (assertedOptions.dest instanceof Buffer)) { + self.logInstance.debug('options.dest is Writable stream of Buffer'); if (!assertedOptions.target) { - return Promise.reject(new Error('When options.dest is a Writable stream then setting options.target is mandatory!')); + return Promise.reject(new Error('When options.dest is a Writable stream or Buffer then setting options.target is mandatory!')); } } else { self.logInstance.debug('Destination file: ' + assertedOptions.dest); @@ -428,7 +428,7 @@ function OptionsHandler(logger) { this.ensureIndent = function (options) { return assertOptions(options) .then(function (assertedOptions) { - self.logInstance.debug('options before ensureIndent():: ' + JSON.stringify(assertedOptions, null, 4)); + self.logInstance.trace('options before ensureIndent():: ' + JSON.stringify(assertedOptions, null, 4)); if (assertedOptions.indent === undefined) { self.logInstance.info('Missing indention, reset to default: ' + Constants.DEFAULT_INDENT); assertedOptions.indent = Constants.DEFAULT_INDENT; @@ -439,7 +439,7 @@ function OptionsHandler(logger) { self.logInstance.info('Indention \'' + assertedOptions.indent + '\' is too wide, reset to default: ' + Constants.DEFAULT_INDENT); assertedOptions.indent = Constants.DEFAULT_INDENT; } - self.logInstance.debug('options after ensureIndent():: ' + JSON.stringify(assertedOptions, null, 4)); + self.logInstance.trace('options after ensureIndent():: ' + JSON.stringify(assertedOptions, null, 4)); return assertedOptions; }); }; diff --git a/lib/reader.js b/lib/reader.js index 2c9de85..f93b2e9 100644 --- a/lib/reader.js +++ b/lib/reader.js @@ -1,9 +1,9 @@ 'use strict'; -var Constants = require('./constants.js'); -var LogWrapper = require('./log-wrapper.js'); -var OptionsHandler = require('./options-handler.js'); -var Validator = require('./validator.js'); +var Constants = require('./constants'); +var LogWrapper = require('./log-wrapper'); +var OptionsHandler = require('./options-handler'); +var Validator = require('./validator'); var jsYaml = require('js-yaml'); var Promise = require('bluebird'); var fs = Promise.promisifyAll(require('fs')); @@ -23,7 +23,7 @@ var stringify = require('json-stringify-safe'); * @returns {Reader} The instance. * @constructor * @class This class provides utility methods usable to read YAML, JSON or JS - * from a source (file, {object}, {@link Buffer} or {@link stream.Readable}) to JS memory objects. + * from a source (file, object, {@link Buffer} or {@link stream.Readable}) to JS memory objects. * @example * var Reader = require('jy-transform').Reader; * var logger = ...; @@ -68,7 +68,7 @@ function Reader(logger) { return function () { var chunk; while (null !== (chunk = readable.read())) { - self.logInstance.debug('JSON chunk: ', chunk); + self.logInstance.trace('JSON chunk: ', chunk); bufs.push(chunk); } }; @@ -139,7 +139,7 @@ function Reader(logger) { /////////////////////////////////////////////////////////////////////////////// /** - * Reads the data from a given JS or JSON source. + * Reads the data from a given JS or JSON source. * * @param {Options} options - Contains the JS/JSON source reference to read from. * @returns {Promise} - Contains the read JS object. @@ -147,10 +147,14 @@ function Reader(logger) { * @example * var Reader = require('jy-transform').Reader; * var logger = ...; + * var reader = new Reader(logger); + * + * // --- from file path + * * var options = { * src: 'foo.js' * }; - * var reader = new Reader(logger); + * * reader.readJs(options) * .then(function (obj){ * logger.info(JSON.stringify(obj)); @@ -159,9 +163,13 @@ function Reader(logger) { * logger.error(err.stack); * }); * + * + * // --- from Readable + * * options = { * src: fs.createReadStream('foo.js') * }; + * * reader.readJs(options) * .then(function (obj){ * logger.info(JSON.stringify(obj)); @@ -170,11 +178,15 @@ function Reader(logger) { * logger.error(err.stack); * }); * + * + * // --- from object + * * options = { * src: { * foo: 'bar' * } * }; + * * reader.readJs(options) * .then(function (obj){ * logger.info(JSON.stringify(obj)); @@ -184,7 +196,7 @@ function Reader(logger) { * }); */ this.readJs = function (options) { - self.logInstance.debug('OPTIONS BEFORE ASSERTING IN readJs:::' + JSON.stringify(options)); + self.logInstance.trace('OPTIONS BEFORE ASSERTING IN readJs:::' + JSON.stringify(options)); return self.optionsHandler.assertOptions(options, ['src']) .then(function (assertedOptions) { return new Promise(function (resolve, reject) { @@ -198,7 +210,7 @@ function Reader(logger) { reject(new Error('found invalid identifier for reading from exports: ' + stringify(options.imports, null, 4))); } else { var json = require(resolvedPath)[options.imports]; - self.logInstance.debug('LOADED JSON object (' + options.imports + '):: ' + stringify(json, null, 4)); + self.logInstance.trace('LOADED JSON object (' + options.imports + '):: ' + stringify(json, null, 4)); if (!json) { reject(new Error('an identifier string \'' + options.imports + '\' was specified for JS object but could not find this object, pls ensure that file ' + assertedOptions.src + ' contains it.')); } else { @@ -223,7 +235,7 @@ function Reader(logger) { reject(new Error('found invalid identifier for reading from object: ' + stringify(options.imports, null, 4))); } else { var obj = assertedOptions.src[options.imports]; - self.logInstance.debug('LOADED JSON object (' + options.imports + '):: ' + stringify(obj, null, 4)); + self.logInstance.trace('LOADED JSON object (' + options.imports + '):: ' + stringify(obj, null, 4)); if (!obj) { reject(new Error('an identifier string \'' + options.imports + '\' was specified for JS object but could not find this object, pls ensure that object source contains it.')); } else { @@ -249,9 +261,15 @@ function Reader(logger) { * @example * var Reader = require('jy-transform').Reader; * var logger = ...; - * * var reader = new Reader(logger); - * reader.readYaml('foo.yaml') + * + * // --- from file path + * + * options = { + * src: 'foo.yml' + * }; + * + * reader.readYaml(options) * .then(function (obj){ * logger.info(JSON.stringify(obj)); * }) @@ -259,9 +277,13 @@ function Reader(logger) { * logger.error(err.stack); * }); * + * + * // --- from Readable + * * options = { * src: fs.createReadStream('foo.yml') * }; + * * reader.readJs(options) * .then(function (obj){ * logger.info(JSON.stringify(obj)); @@ -271,7 +293,7 @@ function Reader(logger) { * }); */ this.readYaml = function (options) { - self.logInstance.debug('OPTIONS BEFORE ASSERTING IN readYaml::: ' + JSON.stringify(options)); + self.logInstance.trace('OPTIONS BEFORE ASSERTING IN readYaml::: ' + JSON.stringify(options)); return self.optionsHandler.assertOptions(options, ['src']) .then(function (assertedOptions) { return new Promise(function (resolve, reject) { @@ -315,7 +337,7 @@ function Reader(logger) { // return Promise.resolve().then(function () { // var jsDocs = []; // return jsYaml.safeLoadAll(yaml, function (doc) { // TOD this will not work in Promise environment!!! -// self.logger.debug(doc); +// self.logger.trace(doc); // jsDocs.push(doc); // }); // }); diff --git a/lib/transformer.js b/lib/transformer.js index 3ecdea1..e1e5790 100644 --- a/lib/transformer.js +++ b/lib/transformer.js @@ -1,10 +1,10 @@ 'use strict'; -var Writer = require('./writer.js'); -var Reader = require('./reader.js'); -var LogWrapper = require('./log-wrapper.js'); -var OptionsHandler = require('./options-handler.js'); -var middleware = require('./middleware.js'); +var Writer = require('./writer'); +var Reader = require('./reader'); +var LogWrapper = require('./log-wrapper'); +var OptionsHandler = require('./options-handler'); +var middleware = require('./middleware'); var path = require('path'); /////////////////////////////////////////////////////////////////////////////// diff --git a/lib/validator.js b/lib/validator.js index c5ad71f..e1064f7 100644 --- a/lib/validator.js +++ b/lib/validator.js @@ -1,8 +1,8 @@ 'use strict'; -var Constants = require('./constants.js'); -var LogWrapper = require('./log-wrapper.js'); -var OptionsHandler = require('./options-handler.js'); +var Constants = require('./constants'); +var LogWrapper = require('./log-wrapper'); +var OptionsHandler = require('./options-handler'); var Promise = require('bluebird'); var stringify = require('json-stringify-safe'); var path = require('path'); diff --git a/lib/writer.js b/lib/writer.js index 385030f..3b8c203 100644 --- a/lib/writer.js +++ b/lib/writer.js @@ -1,9 +1,9 @@ 'use strict'; -var Constants = require('./constants.js'); -var LogWrapper = require('./log-wrapper.js'); -var OptionsHandler = require('./options-handler.js'); -var Validator = require('./validator.js'); +var Constants = require('./constants'); +var LogWrapper = require('./log-wrapper'); +var OptionsHandler = require('./options-handler'); +var Validator = require('./validator'); var serializeJs = require('serialize-js'); var jsYaml = require('js-yaml'); var Promise = require('bluebird'); @@ -26,7 +26,7 @@ var isStream = require('is-stream'); * @constructor * @class This class provides utility methods usable to write JS objects * from memory to a JSON/JS/YAML destination - * (file, {object} or {@link stream.Readable}). + * (file, object or {@link stream.Readable}). * @example * var Writer = require('jy-transform').Writer; * var logger = ...; @@ -34,6 +34,7 @@ var isStream = require('is-stream'); * var writer = new Writer(logger); */ function Writer(logger) { + /** * The logger instance. * @@ -95,6 +96,7 @@ function Writer(logger) { * @param {string} [exportsTo] - Name for export (*IMPORTANT:* must be a valid ES6 identifier). * @returns {Promise} - Promise resolve with the serialized JS object. * @private + * @todo [[#35](https://github.com/deadratfink/jy-transform/issues/35)] Add `'use strict';` in JS output file (-> `'\'use strict\';' + os.EOL + os.EOL + ...`)? */ function serializeJsToString(object, indent, exportsTo) { return createExportsString(exportsTo) @@ -269,6 +271,8 @@ function Writer(logger) { * var logger = ...; * var writer = new Writer(logger); * + * // ---- write obj to file + * * var obj = {...}, * var options = { * dest: 'result.yml', @@ -283,6 +287,9 @@ function Writer(logger) { * logger.error(err.stack); * }); * + * + * // ---- write obj to Writable + * * options = { * dest: fs.createWriteStream('result.yml'), * indent: 4 @@ -296,8 +303,10 @@ function Writer(logger) { * logger.error(err.stack); * }); * + * // ---- write obj to Buffer + * * options = { - * dest: new Buffer(), + * dest: new Buffer('...', Constants.UTF8), * indent: 2 * } * @@ -358,6 +367,8 @@ function Writer(logger) { * var logger = ...; * var writer = new Writer(logger); * + * // ---- write obj to file + * * var obj = {...}; * var options = { * dest: 'result.json', @@ -372,6 +383,9 @@ function Writer(logger) { * logger.error(err.stack); * }); * + * + * // ---- write obj to Writable + * * options = { * dest: fs.createWriteStream('result.json'), * indent: 4 @@ -385,6 +399,8 @@ function Writer(logger) { * logger.error(err.stack); * }); * + * // ---- write obj to object + * * options = { * dest: {}, * indent: 4 @@ -398,8 +414,10 @@ function Writer(logger) { * logger.error(err.stack); * }); * + * // ---- write obj to Buffer + * * options = { - * dest: new Buffer(), + * dest: new Buffer('...', Constants.UTF8), * indent: 2 * } * @@ -451,6 +469,8 @@ function Writer(logger) { * var logger = ...; * var writer = new Writer(logger); * + * // ---- write obj to file + * * var obj = {...}; * var options = { * dest: 'result.js', @@ -465,6 +485,9 @@ function Writer(logger) { * logger.error(err.stack); * }); * + * + * // ---- write obj to Writable + * * options = { * dest: fs.createWriteStream('result.json'), * indent: 4 @@ -478,6 +501,9 @@ function Writer(logger) { * logger.error(err.stack); * }); * + * + * // ---- write obj to object + * * options = { * dest: {}, * indent: 2 @@ -491,8 +517,11 @@ function Writer(logger) { * logger.error(err.stack); * }); * + * + * // ---- write obj to Buffer + * * options = { - * dest: new Buffer(), + * dest: new Buffer('...', Constants.UTF8), * indent: 2 * } * @@ -531,7 +560,14 @@ function Writer(logger) { reject(err); }); } else if (Buffer.isBuffer(dest)) { // Buffer - writeToBuffer(serializeJsToJsonString(object, indent), dest, Constants.JSON, resolve, reject); + serializeJsToString(object, indent, ensuredOptions.exports) + .then(function (data) { + return writeToBuffer(data, dest, Constants.JS, resolve, reject); + }) + .catch(function (err) { + reject(err); + }); + } else { // object var msg; if (ensuredOptions.exports && ensuredOptions.exports !== '') { diff --git a/package.json b/package.json index 01a93a7..d3cbe67 100644 --- a/package.json +++ b/package.json @@ -9,11 +9,6 @@ "url": "https://github.com/deadratfink" }, "contributors": [ - { - "name": "Jens Krefeldt", - "email": "j.krefeldt@gmail.com", - "url": "https://github.com/deadratfink" - } ], "license": "SEE LICENSE IN [LICENSE.md](https://github.com/deadratfink/jy-transform/blob/master/LICENSE.md)", "repository": { @@ -36,7 +31,7 @@ }, "scripts": { "docs": "cat docs/LOGO.md > README.md && cat docs/BADGES.md >> README.md && cat docs/TOC.md >> README.md && package-json-to-readme --no-footer ./package.json >> README.md && cat docs/USAGE.md >> README.md && echo '\n\n' >> README.md && cat docs/CHANGELOG.md >> README.md && doctoc README.md --github --title '# TOC' --maxlevel 2", - "wiki": "jsdoc2md ./jyt lib/*.js index.js > docs/API.md && doctoc docs/API.md --github --title '### TOC' --maxlevel 2 && cat docs/API.md > '../jy-transform.wiki/API-v2.md' && cat docs/CONTRIBUTING.md > ../jy-transform.wiki/Contributing.md", + "wiki": "jsdoc2md ./jyt lib/*.js index.js > docs/API.md && doctoc docs/API.md --github --title '### TOC' --maxlevel 2 && cat docs/API.md > '../jy-transform.wiki/API-v2.md' && cat docs/CONTRIBUTING.md > ../jy-transform.wiki/Contributing.md && cat docs/CHANGELOG.md > ../jy-transform.wiki/Changelog.md && doctoc ../jy-transform.wiki/Changelog.md --github --title '# TOC' --maxlevel 3", "test": "istanbul cover _mocha --report lcovonly -- -R $npm_package_config_test_mocha_unit_reporter ./test/test*.js" }, "engines": { @@ -56,7 +51,7 @@ "codecov": "^1.0.1", "coveralls": "^2.11.9", "doctoc": "^1.0.0", - "fs-extra": "^0.26.7", + "fs-extra": "^0.30.0", "istanbul": "^0.4.2", "jsdoc-parse": "^1.2.7", "jsdoc-to-markdown": "^1.3.3", diff --git a/test/data/readable-test-dummy.txt b/test/data/readable-test-dummy.txt index 41fa1b9..1ae5c7d 100644 --- a/test/data/readable-test-dummy.txt +++ b/test/data/readable-test-dummy.txt @@ -1 +1,3 @@ -DO NOT DELETE THIS FILE, IT IS NEEDED FOR test-option-handler.js#ensureSrc() TEST! +{ + "important": "DO NOT DELETE THIS FILE, IT IS NEEDED FOR test-option-handler.js#ensureSrc() TEST!" +} diff --git a/test/test-log-wrapper.js b/test/test-log-wrapper.js index c64d502..0d91add 100644 --- a/test/test-log-wrapper.js +++ b/test/test-log-wrapper.js @@ -1,6 +1,6 @@ 'use strict'; -var LogWrapper = require('../lib/log-wrapper.js'); +var LogWrapper = require('../lib/log-wrapper'); var assert = require('assert'); /** @@ -10,12 +10,14 @@ describe('Executing \'jy-transform\' project log wrapper test suite.', function var infoMsg; var debugMsg; + var traceMsg; var errorMsg; var verboseResultArray = []; var logWrapper; var INFO = 'INFO'; var DEBUG = 'DEBUG'; + var TRACE = 'TRACE'; var ERROR = 'ERROR'; /** @@ -31,6 +33,9 @@ describe('Executing \'jy-transform\' project log wrapper test suite.', function debug: function (msg) { debugMsg = msg; }, + trace: function (msg) { + traceMsg = msg; + }, error: function (msg) { errorMsg = msg; } @@ -40,20 +45,32 @@ describe('Executing \'jy-transform\' project log wrapper test suite.', function info: function (msg) { infoMsg = msg; }, + trace: function (msg) { + traceMsg = msg; + }, error: function (msg) { errorMsg = msg; } }; - var mockLoggerWithVerboseFunction = { + var mockLoggerWithoutTraceFunction = { + info: function (msg) { + infoMsg = msg; + }, + debug: function (msg) { + debugMsg = msg; + }, + error: function (msg) { + errorMsg = msg; + } + }; + var mockLoggerWithVerboseFunction = { info: function (msg) { verboseResultArray.push(msg); } }; - - describe('Testing LogWrapper with mockLogger', function () { /** @@ -62,6 +79,7 @@ describe('Executing \'jy-transform\' project log wrapper test suite.', function beforeEach(function () { infoMsg = undefined; debugMsg = undefined; + traceMsg = undefined; errorMsg = undefined; logWrapper = new LogWrapper(mockLogger); }); @@ -80,6 +98,13 @@ describe('Executing \'jy-transform\' project log wrapper test suite.', function done(); }); + expected = TRACE; + it('should log with ' + expected, function (done) { + logWrapper.trace(expected); + assert.equal(traceMsg, expected, 'logger message should contain value ' + expected); + done(); + }); + expected = ERROR; it('should log with ' + expected, function (done) { logWrapper.error(expected); @@ -122,6 +147,7 @@ describe('Executing \'jy-transform\' project log wrapper test suite.', function beforeEach(function () { infoMsg = undefined; debugMsg = undefined; + traceMsg = undefined; errorMsg = undefined; logWrapper = new LogWrapper(mockLoggerWithoutDebugFunction); }); @@ -149,4 +175,39 @@ describe('Executing \'jy-transform\' project log wrapper test suite.', function }); + describe('Testing LogWrapper with mockLoggerWithoutTraceFunction', function () { + + /** + * Resets the mock logger message targets. + */ + beforeEach(function () { + infoMsg = undefined; + debugMsg = undefined; + errorMsg = undefined; + logWrapper = new LogWrapper(mockLoggerWithoutTraceFunction); + }); + + var expected = INFO; + it('should log with ' + expected, function (done) { + logWrapper.info(expected); + assert.equal(infoMsg, expected, 'logger message should contain value ' + expected); + done(); + }); + + expected = TRACE; + it('should log with ' + expected, function (done) { + logWrapper.trace(expected); + assert.equal(debugMsg, expected, 'logger message should contain value ' + expected); + done(); + }); + + expected = ERROR; + it('should log with ' + expected, function (done) { + logWrapper.error(expected); + assert.equal(errorMsg, expected, 'logger message should contain value ' + expected); + done(); + }); + + }); + }); diff --git a/test/test-middleware.js b/test/test-middleware.js index b8374c3..ee4c67b 100644 --- a/test/test-middleware.js +++ b/test/test-middleware.js @@ -1,7 +1,7 @@ 'use strict'; -var Transformer = require('../index.js'); -var Middleware = require('../index.js').middleware; +var Transformer = require('../index'); +var Middleware = require('../index').middleware; var identityMiddleware = Middleware.identityMiddleware; var transformer; var Promise = require('bluebird'); diff --git a/test/test-options-handler.js b/test/test-options-handler.js index a7ea1af..18f9e9d 100644 --- a/test/test-options-handler.js +++ b/test/test-options-handler.js @@ -1,11 +1,11 @@ 'use strict'; -var Constants = require('../lib/constants.js'); +var Constants = require('../lib/constants'); var assert = require('assert'); -var YAMLException = require('js-yaml/lib/js-yaml/exception.js'); +//var YAMLException = require('js-yaml/lib/js-yaml/exception'); var fs = require('fs'); var path = require('path'); -var OptionsHandler = require('../lib/options-handler.js'); +var OptionsHandler = require('../lib/options-handler'); var optionsHandler; var logger; @@ -103,7 +103,7 @@ describe('Executing \'jy-transform\' project OptionsHandler test suite.', functi assertOptionsError(null, optionsHandler.completeOptions, done); }); - it('should resolve options.src/orign and origin.dest/target with default values (' + Constants.DEFAULT_ORIGIN + '/' + Constants.DEFAULT_TARGET + ')', function (done) { + it('should resolve options.src/origin and options.dest/target with default values (' + Constants.DEFAULT_ORIGIN + '/' + Constants.DEFAULT_TARGET + ')', function (done) { var PATH_WITH_INVALID_EXT = 'PATH_WITH_INVALID.EXT'; var options = { src: PATH_WITH_INVALID_EXT, @@ -513,6 +513,50 @@ describe('Executing \'jy-transform\' project OptionsHandler test suite.', functi assertOptionsError(options, optionsHandler.ensureSrc, done); }); + it('should resolve original options.src Readable', function (done) { + var readable = fs.createReadStream('./test/data/readable-test-dummy.txt'); + var options = { + src: readable, + origin: Constants.JSON + }; + optionsHandler.ensureSrc(options) + .then(function (resultOptions) { + assert.notEqual(resultOptions.src, null, 'options should contain src but is missing'); + assert.equal(resultOptions.src, readable, 'result options.src should have type Readable'); + done(); + }) + .catch(function (err) { + logger.error('UNEXPECTED ERROR: ' + err.stack); + done(err); + }); + }); + + it('should reject when Buffer is given but not origin', function (done) { + var buffer = new Buffer('{"msg": "I\'m a string!"}', Constants.UTF8); + var options = { + src: buffer + }; + assertOptionsError(options, optionsHandler.ensureSrc, done); + }); + + it('should resolve original options.src Buffer', function (done) { + var buffer = new Buffer('{"msg": "I\'m a string!"}', Constants.UTF8); + var options = { + src: buffer, + origin: Constants.JSON + }; + optionsHandler.ensureSrc(options) + .then(function (resultOptions) { + assert.notEqual(resultOptions.src, null, 'options should contain src but is missing'); + assert.equal(resultOptions.src, buffer, 'result options.src should have type Buffer'); + done(); + }) + .catch(function (err) { + logger.error('UNEXPECTED ERROR: ' + err.stack); + done(err); + }); + }); + it('should resolve original options.src object', function (done) { var srcObj = {}; var options = { diff --git a/test/test-reader.js b/test/test-reader.js index 19717e8..ef9bac3 100644 --- a/test/test-reader.js +++ b/test/test-reader.js @@ -1,10 +1,10 @@ 'use strict'; var assert = require('assert'); -var YAMLException = require('js-yaml/lib/js-yaml/exception.js'); +var YAMLException = require('js-yaml/lib/js-yaml/exception'); var fs = require('fs'); -var Reader = require('../index.js').Reader; -var Constants = require('../index.js').constants; +var Reader = require('../index').Reader; +var Constants = require('../index').constants; var logger; var reader; diff --git a/test/test-transformer.js b/test/test-transformer.js index 5b6cbad..695e092 100644 --- a/test/test-transformer.js +++ b/test/test-transformer.js @@ -1,8 +1,8 @@ 'use strict'; -var Transformer = require('../index.js'); +var Transformer = require('../index'); var transformer; -var Constants = require('../lib/constants.js'); +var Constants = require('../lib/constants'); var logger; var jsYaml = require('js-yaml'); var assert = require('assert'); diff --git a/test/test-validator.js b/test/test-validator.js index c8a388b..d0a0725 100644 --- a/test/test-validator.js +++ b/test/test-validator.js @@ -6,7 +6,7 @@ var fs = require('fs'); var os = require('os'); var stringify = require('json-stringify-safe'); var stream = require('stream'); -var Validator = require('../lib/validator.js'); +var Validator = require('../lib/validator'); var logger; var validator; diff --git a/test/test-writer.js b/test/test-writer.js index 140eaa1..2a0e749 100644 --- a/test/test-writer.js +++ b/test/test-writer.js @@ -2,11 +2,12 @@ var assert = require('assert'); var Promise = require('bluebird'); -var YAMLException = require('js-yaml/lib/js-yaml/exception.js'); +var YAMLException = require('js-yaml/lib/js-yaml/exception'); var fs = require('fs'); var os = require('os'); var stream = require('stream'); -var Writer = require('../index.js').Writer; +var Writer = require('../index').Writer; +var Constants = require('../index').constants; var logger; var writer; @@ -77,11 +78,23 @@ describe('Executing \'jy-transform\' project Writer test suite.', function () { } } + function assertDestBuffer(src, buffer, done) { + //var bufferToString = buffer.toString(); + //logger.info('bufferToString::: ' + bufferToString); + var bufferResult = buffer.toJSON(); + for (var property in bufferResult) { + if (bufferResult.hasOwnProperty(property)) { + assert(src.hasOwnProperty(property), 'src should have same property \'' + property + '\' as buffer result object'); + assert.equal(src[property], bufferResult[property], 'property \'' + property + '\' should have equal value in src (\'' + src[property] + '\') and buffer result object (\'' + bufferResult[property] + '\')'); + } + } + done(); + } + var json = { test: 'value' }; - var errorThrowingStream = new stream.Writable(); errorThrowingStream._write = function (chunk, encoding, done) { logger.info('stream emitting Error now'); @@ -395,6 +408,27 @@ describe('Executing \'jy-transform\' project Writer test suite.', function () { }); }); + it('should write JSON to Buffer', function (done) { + + var json = { + foo: 'bar' + }; + var buffer = new Buffer(JSON.stringify(json, null, 4).length); + var options = { + dest: buffer + }; + + writer.writeJson(json, options) + .then(function (msg) { + assert.notEqual(msg, null, 'msg should not be null, was: ' + msg); + assertDestBuffer(json, buffer, done); + }) + .catch(function (err) { + logger.error(err.stack); + done(err); + }); + }); + it('should write JS to JS object', function (done) { var options = { From 68f6bbb8acd78b1da4a6fc28dd6e61fa1589a439 Mon Sep 17 00:00:00 2001 From: "Krefeldt, Jens" Date: Fri, 22 Jul 2016 05:01:01 +0200 Subject: [PATCH 08/12] Removal of Buffer I/O --- docs/CHANGELOG.md | 6 ---- lib/reader.js | 32 +--------------------- lib/writer.js | 70 ----------------------------------------------- 3 files changed, 1 insertion(+), 107 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index ffea462..2e2e5d7 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -5,12 +5,6 @@ - [[#32](https://github.com/deadratfink/jy-transform/issues/32)] Introduce input and output on CLI as ARGS instead of OPTIONS (non-backwards compatible change for CLI usage!) - E.g. type `$ jyt foo.js bar.yaml` instead of `$ jyt -s foo.js -d bar.yaml` - [[#31](https://github.com/deadratfink/jy-transform/issues/31)] Fix: given `Object` source results in 'yaml' for origin (API) -- [[#26](https://github.com/deadratfink/jy-transform/issues/26)] API level `dest`: support for writing serialized JSON and YAML to _single_ (i.e. non-streamed) `Buffer` - - Requires `options.target` property set - - Writes UTF-8 to Buffer -- [[#25](https://github.com/deadratfink/jy-transform/issues/25)] API level `src`: support for reading serialized JSON and YAML from _single_ (i.e. non-streamed) `Buffer` - - Requires `options.origin` property set - - Expects UTF-8 data in Buffer ### v1.0.2 diff --git a/lib/reader.js b/lib/reader.js index 2c9de85..83b69b2 100644 --- a/lib/reader.js +++ b/lib/reader.js @@ -23,7 +23,7 @@ var stringify = require('json-stringify-safe'); * @returns {Reader} The instance. * @constructor * @class This class provides utility methods usable to read YAML, JSON or JS - * from a source (file, {object}, {@link Buffer} or {@link stream.Readable}) to JS memory objects. + * from a source (file, {object} or {@link stream.Readable}) to JS memory objects. * @example * var Reader = require('jy-transform').Reader; * var logger = ...; @@ -108,32 +108,6 @@ function Reader(logger) { }); } - /** - * Reads from a passed _single_ {@link Buffer} (i.e. non-streamed) and - * resolves by callback. - * - * @param {Buffer} buffer - The source to read from. - * @param {function} resolve - Callback for success case. - * @param {function} reject - Callback for Error case. - * @param {string} origin - Origin type, must be 'yaml' or 'json'/'js'. - * @private - */ - function readFromBuffer(buffer, resolve, reject, origin) { - try { - self.logInstance.debug(origin + ' reading from Buffer'); - if (origin === Constants.JSON || origin === Constants.JS) { - resolve(JSON.parse(buffer.toString(Constants.UTF8))); - } else if (origin === Constants.YAML) { - resolve(jsYaml.safeLoad(buffer.toString(Constants.UTF8))); - } else { - reject(new Error('Unsupported type: ' + origin + ' to read from Buffer')); - } - } catch (err) { // probably a SyntaxError for JSON or a YAMLException - self.logInstance.error('Unexpected error: ' + err.stack); - reject(err); - } - } - /////////////////////////////////////////////////////////////////////////////// // API METHODS (PUBLIC) /////////////////////////////////////////////////////////////////////////////// @@ -215,8 +189,6 @@ function Reader(logger) { } } else if (isStream.readable(assertedOptions.src)) { readFromStream(assertedOptions.src, resolve, reject, Constants.JSON); - } else if (assertedOptions.src instanceof Buffer) { - readFromBuffer(assertedOptions.src, resolve, reject, Constants.JSON); } else if (options.imports && options.imports !== '') { if (!self.validator.validateIdentifier(options.imports)) { @@ -289,8 +261,6 @@ function Reader(logger) { }); } else if (isStream.readable(assertedOptions.src)) { readFromStream(assertedOptions.src, resolve, reject, Constants.YAML); - } else if (Buffer.isBuffer(assertedOptions.src)) { - readFromBuffer(assertedOptions.src, resolve, reject, Constants.YAML); } else { resolve(assertedOptions.src); } diff --git a/lib/writer.js b/lib/writer.js index 385030f..bf77fdd 100644 --- a/lib/writer.js +++ b/lib/writer.js @@ -224,31 +224,6 @@ function Writer(logger) { dest.end(); } - /** - * Writes a string serialized data object to a `Buffer`. - * - * @param {string} object - The data to write into stream. - * @param {string} dest - The {@link Buffer} destination. - * @param {string} target - The target type, one of [ 'yaml' | 'json' | 'js' ]. - * @param {function} resolve - The Promise `resolve` callback. - * @param {function} reject - The Promise `reject` callback. - * @see {@link Constants#YAML} - * @see {@link Constants#JSON} - * @see {@link Constants#JS} - * @returns {Promise} - Containing the write success message to handle by caller (e.g. for logging). - * @throws {Error} - If serialized JS object could not be written due to any reason. - * @private - */ - function writeToBuffer(object, dest, target, resolve, reject) { - // write stringified data - try { - dest.write(object, Constants.UTF8); - resolve('Writing ' + target + ' to Buffer successful.'); - } catch (err) { - reject(err); - } - } - /////////////////////////////////////////////////////////////////////////////// // API METHODS (PUBLIC) /////////////////////////////////////////////////////////////////////////////// @@ -295,19 +270,6 @@ function Writer(logger) { * .catch(function (err) { * logger.error(err.stack); * }); - * - * options = { - * dest: new Buffer(), - * indent: 2 - * } - * - * writer.writeYaml(obj, options) - * .then(function (msg){ - * logger.info(msg); - * }) - * .catch(function (err) { - * logger.error(err.stack); - * }); */ this.writeYaml = function (object, options) { return self.optionsHandler.ensureIndent(options) @@ -332,8 +294,6 @@ function Writer(logger) { writeToFile(yaml, dest, Constants.YAML, resolve, reject, ensuredOptions.force); } else if (isStream.writable(dest)) { // stream writeToStream(yaml, dest, Constants.YAML, resolve, reject); - } else if (Buffer.isBuffer(dest)) { // Buffer - writeToBuffer(yaml, dest, Constants.YAML, resolve, reject); } else { // object ensuredOptions.dest = yaml; resolve('Writing YAML to options.dest successful.'); @@ -397,19 +357,6 @@ function Writer(logger) { * .catch(function (err) { * logger.error(err.stack); * }); - * - * options = { - * dest: new Buffer(), - * indent: 2 - * } - * - * writer.writeJson(obj, options) - * .then(function (msg){ - * logger.info(msg); - * }) - * .catch(function (err) { - * logger.error(err.stack); - * }); */ this.writeJson = function (object, options) { return self.optionsHandler.ensureIndent(options) @@ -425,8 +372,6 @@ function Writer(logger) { writeToFile(serializeJsToJsonString(object, indent), dest, Constants.JSON, resolve, reject, ensuredOptions.force); } else if (isStream.writable(dest)) { // stream writeToStream(serializeJsToJsonString(object, indent), dest, Constants.JSON, resolve, reject); - } else if (Buffer.isBuffer(dest)) { // Buffer - writeToBuffer(serializeJsToJsonString(object, indent), dest, Constants.JSON, resolve, reject); } else { // object ensuredOptions.dest = serializeJsToJsonString(object, indent); resolve('Writing JSON to options.dest successful.'); @@ -490,19 +435,6 @@ function Writer(logger) { * .catch(function (err) { * logger.error(err.stack); * }); - * - * options = { - * dest: new Buffer(), - * indent: 2 - * } - * - * writer.writeJs(obj, options) - * .then(function (msg){ - * logger.info(msg); - * }) - * .catch(function (err) { - * logger.error(err.stack); - * }); */ this.writeJs = function (object, options) { return self.optionsHandler.ensureIndent(options) @@ -530,8 +462,6 @@ function Writer(logger) { .catch(function (err) { reject(err); }); - } else if (Buffer.isBuffer(dest)) { // Buffer - writeToBuffer(serializeJsToJsonString(object, indent), dest, Constants.JSON, resolve, reject); } else { // object var msg; if (ensuredOptions.exports && ensuredOptions.exports !== '') { From a6d7aa489021ff2b88ba182c19c4dce3c42bf8ed Mon Sep 17 00:00:00 2001 From: "Krefeldt, Jens" Date: Fri, 22 Jul 2016 06:01:48 +0200 Subject: [PATCH 09/12] Removal of Buffer I/O --- docs/CHANGELOG.md | 6 ---- lib/reader.js | 32 +---------------- lib/writer.js | 84 --------------------------------------------- test/test-writer.js | 35 ------------------- 4 files changed, 1 insertion(+), 156 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index a855166..607ff34 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -7,12 +7,6 @@ - [[#32](https://github.com/deadratfink/jy-transform/issues/32)] Introduce input and output on CLI as ARGS instead of OPTIONS (non-backwards compatible change for CLI usage, _no_ impact on API level!) - E.g. type `$ jyt foo.js bar.yaml` instead of `$ jyt -s foo.js -d bar.yaml` - [[#31](https://github.com/deadratfink/jy-transform/issues/31)] Fix: given `Object` source results in 'yaml' for origin (API) -- [[#26](https://github.com/deadratfink/jy-transform/issues/26)] API level `dest`: support for writing serialized JSON and YAML to _single_ (i.e. non-streamed) `Buffer` - - Requires `options.target` property set - - Writes UTF-8 to Buffer -- [[#25](https://github.com/deadratfink/jy-transform/issues/25)] API level `src`: support for reading serialized JSON and YAML from _single_ (i.e. non-streamed) `Buffer` - - Requires `options.origin` property set - - Expects UTF-8 data in Buffer ### v1.0.2 diff --git a/lib/reader.js b/lib/reader.js index f93b2e9..dccae98 100644 --- a/lib/reader.js +++ b/lib/reader.js @@ -23,7 +23,7 @@ var stringify = require('json-stringify-safe'); * @returns {Reader} The instance. * @constructor * @class This class provides utility methods usable to read YAML, JSON or JS - * from a source (file, object, {@link Buffer} or {@link stream.Readable}) to JS memory objects. + * from a source (file, {object} or {@link stream.Readable}) to JS memory objects. * @example * var Reader = require('jy-transform').Reader; * var logger = ...; @@ -108,32 +108,6 @@ function Reader(logger) { }); } - /** - * Reads from a passed _single_ {@link Buffer} (i.e. non-streamed) and - * resolves by callback. - * - * @param {Buffer} buffer - The source to read from. - * @param {function} resolve - Callback for success case. - * @param {function} reject - Callback for Error case. - * @param {string} origin - Origin type, must be 'yaml' or 'json'/'js'. - * @private - */ - function readFromBuffer(buffer, resolve, reject, origin) { - try { - self.logInstance.debug(origin + ' reading from Buffer'); - if (origin === Constants.JSON || origin === Constants.JS) { - resolve(JSON.parse(buffer.toString(Constants.UTF8))); - } else if (origin === Constants.YAML) { - resolve(jsYaml.safeLoad(buffer.toString(Constants.UTF8))); - } else { - reject(new Error('Unsupported type: ' + origin + ' to read from Buffer')); - } - } catch (err) { // probably a SyntaxError for JSON or a YAMLException - self.logInstance.error('Unexpected error: ' + err.stack); - reject(err); - } - } - /////////////////////////////////////////////////////////////////////////////// // API METHODS (PUBLIC) /////////////////////////////////////////////////////////////////////////////// @@ -227,8 +201,6 @@ function Reader(logger) { } } else if (isStream.readable(assertedOptions.src)) { readFromStream(assertedOptions.src, resolve, reject, Constants.JSON); - } else if (assertedOptions.src instanceof Buffer) { - readFromBuffer(assertedOptions.src, resolve, reject, Constants.JSON); } else if (options.imports && options.imports !== '') { if (!self.validator.validateIdentifier(options.imports)) { @@ -311,8 +283,6 @@ function Reader(logger) { }); } else if (isStream.readable(assertedOptions.src)) { readFromStream(assertedOptions.src, resolve, reject, Constants.YAML); - } else if (Buffer.isBuffer(assertedOptions.src)) { - readFromBuffer(assertedOptions.src, resolve, reject, Constants.YAML); } else { resolve(assertedOptions.src); } diff --git a/lib/writer.js b/lib/writer.js index 3b8c203..0326e34 100644 --- a/lib/writer.js +++ b/lib/writer.js @@ -226,31 +226,6 @@ function Writer(logger) { dest.end(); } - /** - * Writes a string serialized data object to a `Buffer`. - * - * @param {string} object - The data to write into stream. - * @param {string} dest - The {@link Buffer} destination. - * @param {string} target - The target type, one of [ 'yaml' | 'json' | 'js' ]. - * @param {function} resolve - The Promise `resolve` callback. - * @param {function} reject - The Promise `reject` callback. - * @see {@link Constants#YAML} - * @see {@link Constants#JSON} - * @see {@link Constants#JS} - * @returns {Promise} - Containing the write success message to handle by caller (e.g. for logging). - * @throws {Error} - If serialized JS object could not be written due to any reason. - * @private - */ - function writeToBuffer(object, dest, target, resolve, reject) { - // write stringified data - try { - dest.write(object, Constants.UTF8); - resolve('Writing ' + target + ' to Buffer successful.'); - } catch (err) { - reject(err); - } - } - /////////////////////////////////////////////////////////////////////////////// // API METHODS (PUBLIC) /////////////////////////////////////////////////////////////////////////////// @@ -302,21 +277,6 @@ function Writer(logger) { * .catch(function (err) { * logger.error(err.stack); * }); - * - * // ---- write obj to Buffer - * - * options = { - * dest: new Buffer('...', Constants.UTF8), - * indent: 2 - * } - * - * writer.writeYaml(obj, options) - * .then(function (msg){ - * logger.info(msg); - * }) - * .catch(function (err) { - * logger.error(err.stack); - * }); */ this.writeYaml = function (object, options) { return self.optionsHandler.ensureIndent(options) @@ -341,8 +301,6 @@ function Writer(logger) { writeToFile(yaml, dest, Constants.YAML, resolve, reject, ensuredOptions.force); } else if (isStream.writable(dest)) { // stream writeToStream(yaml, dest, Constants.YAML, resolve, reject); - } else if (Buffer.isBuffer(dest)) { // Buffer - writeToBuffer(yaml, dest, Constants.YAML, resolve, reject); } else { // object ensuredOptions.dest = yaml; resolve('Writing YAML to options.dest successful.'); @@ -413,21 +371,6 @@ function Writer(logger) { * .catch(function (err) { * logger.error(err.stack); * }); - * - * // ---- write obj to Buffer - * - * options = { - * dest: new Buffer('...', Constants.UTF8), - * indent: 2 - * } - * - * writer.writeJson(obj, options) - * .then(function (msg){ - * logger.info(msg); - * }) - * .catch(function (err) { - * logger.error(err.stack); - * }); */ this.writeJson = function (object, options) { return self.optionsHandler.ensureIndent(options) @@ -443,8 +386,6 @@ function Writer(logger) { writeToFile(serializeJsToJsonString(object, indent), dest, Constants.JSON, resolve, reject, ensuredOptions.force); } else if (isStream.writable(dest)) { // stream writeToStream(serializeJsToJsonString(object, indent), dest, Constants.JSON, resolve, reject); - } else if (Buffer.isBuffer(dest)) { // Buffer - writeToBuffer(serializeJsToJsonString(object, indent), dest, Constants.JSON, resolve, reject); } else { // object ensuredOptions.dest = serializeJsToJsonString(object, indent); resolve('Writing JSON to options.dest successful.'); @@ -516,22 +457,6 @@ function Writer(logger) { * .catch(function (err) { * logger.error(err.stack); * }); - * - * - * // ---- write obj to Buffer - * - * options = { - * dest: new Buffer('...', Constants.UTF8), - * indent: 2 - * } - * - * writer.writeJs(obj, options) - * .then(function (msg){ - * logger.info(msg); - * }) - * .catch(function (err) { - * logger.error(err.stack); - * }); */ this.writeJs = function (object, options) { return self.optionsHandler.ensureIndent(options) @@ -559,15 +484,6 @@ function Writer(logger) { .catch(function (err) { reject(err); }); - } else if (Buffer.isBuffer(dest)) { // Buffer - serializeJsToString(object, indent, ensuredOptions.exports) - .then(function (data) { - return writeToBuffer(data, dest, Constants.JS, resolve, reject); - }) - .catch(function (err) { - reject(err); - }); - } else { // object var msg; if (ensuredOptions.exports && ensuredOptions.exports !== '') { diff --git a/test/test-writer.js b/test/test-writer.js index 2a0e749..2b7a88c 100644 --- a/test/test-writer.js +++ b/test/test-writer.js @@ -7,7 +7,6 @@ var fs = require('fs'); var os = require('os'); var stream = require('stream'); var Writer = require('../index').Writer; -var Constants = require('../index').constants; var logger; var writer; @@ -78,19 +77,6 @@ describe('Executing \'jy-transform\' project Writer test suite.', function () { } } - function assertDestBuffer(src, buffer, done) { - //var bufferToString = buffer.toString(); - //logger.info('bufferToString::: ' + bufferToString); - var bufferResult = buffer.toJSON(); - for (var property in bufferResult) { - if (bufferResult.hasOwnProperty(property)) { - assert(src.hasOwnProperty(property), 'src should have same property \'' + property + '\' as buffer result object'); - assert.equal(src[property], bufferResult[property], 'property \'' + property + '\' should have equal value in src (\'' + src[property] + '\') and buffer result object (\'' + bufferResult[property] + '\')'); - } - } - done(); - } - var json = { test: 'value' }; @@ -408,27 +394,6 @@ describe('Executing \'jy-transform\' project Writer test suite.', function () { }); }); - it('should write JSON to Buffer', function (done) { - - var json = { - foo: 'bar' - }; - var buffer = new Buffer(JSON.stringify(json, null, 4).length); - var options = { - dest: buffer - }; - - writer.writeJson(json, options) - .then(function (msg) { - assert.notEqual(msg, null, 'msg should not be null, was: ' + msg); - assertDestBuffer(json, buffer, done); - }) - .catch(function (err) { - logger.error(err.stack); - done(err); - }); - }); - it('should write JS to JS object', function (done) { var options = { From 7990a158ef292e8fc126e6b5ea01970cc36aa356 Mon Sep 17 00:00:00 2001 From: "Krefeldt, Jens" Date: Fri, 22 Jul 2016 07:54:28 +0200 Subject: [PATCH 10/12] Add some tests --- test/test-reader.js | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/test/test-reader.js b/test/test-reader.js index ef9bac3..2ede742 100644 --- a/test/test-reader.js +++ b/test/test-reader.js @@ -13,6 +13,8 @@ var reader; */ describe('Executing \'jy-transform\' project Reader test suite.', function () { + var invalidOrigin = 'INVALID_ORIGIN'; + /** * Init the test logger and Reader. */ @@ -148,6 +150,25 @@ describe('Executing \'jy-transform\' project Reader test suite.', function () { }); }); + it('should reject reading JS from stream with Error on invalid value for options.origin: ' + invalidOrigin, function (done) { + + var options = { + src: fs.createReadStream('./test/data/test-imports.js'), + origin: invalidOrigin + }; + + reader.readJs(options) + .then(function (msg) { + done(new Error('Error expected')); + }) + .catch(function (err) { + logger.info('EXPECTED ERROR: ' + err.stack); + assert.notEqual(err, null, 'err should not be null'); + assert(err instanceof Error, 'expected Error should equal Error, was: ' + (typeof err)); + done(); + }); + }); + it('should read JSON from file', function (done) { var options = { src: './test/data/test-data.json' @@ -507,5 +528,24 @@ describe('Executing \'jy-transform\' project Reader test suite.', function () { }); }); + it('should reject reading YAML from stream with Error on invalid value for options.origin: ' + invalidOrigin, function (done) { + + var options = { + src: fs.createReadStream('./test/data/test-data.yaml'), + origin: invalidOrigin + }; + + reader.readYaml(options) + .then(function (msg) { + done(new Error('Error expected')); + }) + .catch(function (err) { + logger.info('EXPECTED ERROR: ' + err.stack); + assert.notEqual(err, null, 'err should not be null'); + assert(err instanceof Error, 'expected Error should equal Error, was: ' + (typeof err)); + done(); + }); + }); + }); }); From acb0ed144fb28e3d0b50f19563474dfbb325ec9a Mon Sep 17 00:00:00 2001 From: "Krefeldt, Jens" Date: Fri, 22 Jul 2016 08:12:42 +0200 Subject: [PATCH 11/12] Fix test coverage --- lib/reader.js | 8 +++++--- test/test-reader.js | 41 ----------------------------------------- 2 files changed, 5 insertions(+), 44 deletions(-) diff --git a/lib/reader.js b/lib/reader.js index dccae98..1c64e2d 100644 --- a/lib/reader.js +++ b/lib/reader.js @@ -96,11 +96,13 @@ function Reader(logger) { self.logInstance.debug(origin + ' reading from Readable'); if (origin === Constants.JSON || origin === Constants.JS) { resolve(JSON.parse(buffer.toString(Constants.UTF8))); - } else if (origin === Constants.YAML) { + } else {// HINT: commented (see below): if (origin === Constants.YAML) { resolve(jsYaml.safeLoad(buffer.toString(Constants.UTF8))); - } else { - reject(new Error('Unsupported type: ' + origin + ' to read from Readable')); } + // HINT: for the sake of test coverage it's commented, since this is a private method we have control over options.origin inside this class! + //else { + // reject(new Error('Unsupported type: ' + origin + ' to read from Readable')); + //} } catch (err) { // probably a SyntaxError for JSON or a YAMLException self.logInstance.error('Unexpected error: ' + err.stack); readable.emit('error', err); // send to .on('error',... diff --git a/test/test-reader.js b/test/test-reader.js index 2ede742..da4fe0c 100644 --- a/test/test-reader.js +++ b/test/test-reader.js @@ -13,8 +13,6 @@ var reader; */ describe('Executing \'jy-transform\' project Reader test suite.', function () { - var invalidOrigin = 'INVALID_ORIGIN'; - /** * Init the test logger and Reader. */ @@ -150,25 +148,6 @@ describe('Executing \'jy-transform\' project Reader test suite.', function () { }); }); - it('should reject reading JS from stream with Error on invalid value for options.origin: ' + invalidOrigin, function (done) { - - var options = { - src: fs.createReadStream('./test/data/test-imports.js'), - origin: invalidOrigin - }; - - reader.readJs(options) - .then(function (msg) { - done(new Error('Error expected')); - }) - .catch(function (err) { - logger.info('EXPECTED ERROR: ' + err.stack); - assert.notEqual(err, null, 'err should not be null'); - assert(err instanceof Error, 'expected Error should equal Error, was: ' + (typeof err)); - done(); - }); - }); - it('should read JSON from file', function (done) { var options = { src: './test/data/test-data.json' @@ -527,25 +506,5 @@ describe('Executing \'jy-transform\' project Reader test suite.', function () { done(); }); }); - - it('should reject reading YAML from stream with Error on invalid value for options.origin: ' + invalidOrigin, function (done) { - - var options = { - src: fs.createReadStream('./test/data/test-data.yaml'), - origin: invalidOrigin - }; - - reader.readYaml(options) - .then(function (msg) { - done(new Error('Error expected')); - }) - .catch(function (err) { - logger.info('EXPECTED ERROR: ' + err.stack); - assert.notEqual(err, null, 'err should not be null'); - assert(err instanceof Error, 'expected Error should equal Error, was: ' + (typeof err)); - done(); - }); - }); - }); }); From fe0631d0cb96cff29e8a39528bca3d11d448ab92 Mon Sep 17 00:00:00 2001 From: "Krefeldt, Jens" Date: Fri, 22 Jul 2016 09:00:38 +0200 Subject: [PATCH 12/12] Fix some api doc --- lib/writer.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/writer.js b/lib/writer.js index ea4f68d..0326e34 100644 --- a/lib/writer.js +++ b/lib/writer.js @@ -443,7 +443,6 @@ function Writer(logger) { * }); * * -<<<<<<< HEAD * // ---- write obj to object * * options = { @@ -451,8 +450,6 @@ function Writer(logger) { * indent: 2 * } * -======= ->>>>>>> development * writer.writeJs(obj, options) * .then(function (msg){ * logger.info(msg);