Skip to content

Commit

Permalink
improvements to be executed via CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego ZoracKy authored and Diego ZoracKy committed Jul 10, 2018
1 parent b42b91a commit 9b50681
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 47 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,22 @@ For all the examples, lets suppose that our excel file has two sheets, named as

### CLI

OBS: All the following examples can be used via command-line, in this case, the `--config` parameter expects a JSON string. `"outputJSON": true` must be used to *console.log* the result.
OBS: All the following examples can be used via command-line, in this case, the `--config` parameter expects a valid JSON string.

```javascript
$ convert-excel-to-json --config='{"sourceFile": "tests/test-data.xlsx", "outputJSON": true}'
$ convert-excel-to-json --config='{"sourceFile": "tests/test-data.xlsx"}'
```

In order to use it passing in only the **sourceFile** without extra configuration:

```javascript
$ convert-excel-to-json --sourceFile="tests/test-data.xlsx"
```

To check the help section:

```javascript
$ convert-excel-to-json --help
```

### Simple conversion
Expand Down
38 changes: 0 additions & 38 deletions app.js

This file was deleted.

21 changes: 20 additions & 1 deletion bin/cli.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
#!/usr/bin/env node

require('magicli')();
require('magicli')({
commands: {
'convert-excel-to-json': {
options: [{
name: 'config',
description: 'A full config in a valid JSON format',
type: 'JSON'

},{
name: 'sourceFile',
description: `The sourceFile path (to be used without the 'config' parameter`,
type: 'String'

}]
}
},
pipe: {
after: JSON.stringify
}
});
6 changes: 2 additions & 4 deletions lib/convert-excel-to-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ const excelToJson = (function() {
return rows;
};

const convertExcelToJson = function(config) {
const convertExcelToJson = function(config = {}, sourceFile) {
_config = config.constructor === String ? JSON.parse(config) : config;
_config.sourceFile = _config.sourceFile || sourceFile

for (let i in requiredConfig) {
if (Object.keys(_config).indexOf(requiredConfig[i]) < 0)
Expand All @@ -130,9 +131,6 @@ const excelToJson = (function() {
parsedData[sheet.name] = parseSheet(sheet, workbook);
});

if(_config.outputJSON){
console.log(JSON.stringify(parsedData, null, '\t'));
}
return parsedData;
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "convert-excel-to-json",
"version": "1.3.0",
"version": "1.4.0",
"description": "Convert Excel to JSON",
"homepage": "https://github.com/DiegoZoracKy/convert-excel-to-json/",
"main": "./lib/convert-excel-to-json.js",
Expand Down
25 changes: 24 additions & 1 deletion tests/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

const excelToJson = require('../');
const assert = require('assert');
const child_process = require('child_process');
const path = require('path');

// This Excel contains dummy data and it was generated by https://www.mockaroo.com
const sourceFile = `${__dirname}/test-data.xlsx`.replace(/\\/g, '/');
Expand Down Expand Up @@ -498,6 +500,28 @@ describe('Conversion', function() {
});
});

describe('Execution via CLI', function() {
const cliPath = path.resolve(`${__dirname}/../bin/cli.js`);
const sourceFileTest = path.resolve(`${__dirname}/../tests/test-data-2.xlsx`);
const expectedResult = `{"sheet1":[{"A":"date","B":"number"},{"A":"2017-10-10T03:00:00.000Z","B":1},{"A":"2017-10-11T03:00:00.000Z","B":2},{"A":"2017-10-12T03:00:00.000Z","B":3},{"A":"2017-10-13T03:00:00.000Z","B":4},{"A":"2017-10-14T03:00:00.000Z","B":5},{"A":"2017-10-15T03:00:00.000Z","B":6},{"A":"2017-10-16T03:00:00.000Z","B":7},{"A":"2017-10-17T03:00:00.000Z","B":8},{"A":"2017-10-18T03:00:00.000Z","B":9},{"A":"2017-10-19T03:00:00.000Z","B":10}]}`

it('should work when passing in a JSON to --config', function(done) {
child_process.exec(`${cliPath} --config='{"sourceFile": "${sourceFileTest}"}' `, (err, stdout, stderr) => {
const result = stdout.replace(/\n/, '');
assert.equal(result, expectedResult);
done();
});
});

it('should work when passing a file path to --sourceFile', function(done) {
child_process.exec(`${cliPath} --sourceFile=${sourceFileTest}`, (err, stdout, stderr) => {
const result = stdout.replace(/\n/, '');
assert.equal(result, expectedResult);
done();
});
});
});

function simple(config) {

const jsonResult = excelToJson(config);
Expand Down Expand Up @@ -588,5 +612,4 @@ function simple(config) {
});
});
});

}

0 comments on commit 9b50681

Please sign in to comment.