Skip to content

Commit

Permalink
Added redirect output feature
Browse files Browse the repository at this point in the history
  • Loading branch information
manuel committed Mar 16, 2017
1 parent adac69a commit f1782db
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 12 deletions.
4 changes: 3 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"--path",
".",
"-i",
".git/**/*"
".git/**/*",
"-o",
"./test.txt"
]
}
]
Expand Down
26 changes: 20 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
Creates a tree representation of any directory. Handy if you want to add a tree
representation of your project to your documentation.

## To-dos

- Add capability to redirect output(print on file, print on console, return result as string);

## Installation ##

Run
Expand All @@ -27,6 +23,7 @@ It takes the following parameters:
- `show-files`
- `log-level`
- `ignore-pattern`
- `output`

### Path ###

Expand Down Expand Up @@ -82,6 +79,21 @@ It runs `minimatch` under the hood, thus it takes a glob as argument.
directree --path . --ignore-pattern **/*.js
```

### Output ###

If you mind, you can write your output on a file using the `output` parameters.

_Please note: this is an async feature, so keep that in mind in case of programmatic usage!_

#### Example ####

```
directree --path . -f -o ./tree.txt
```
```
directree --path . --output ./tree.txt
```

## Programmatic usage ##

You can use this utility in your Node.js apps too. You only need to provide the same parameters
Expand All @@ -93,7 +105,8 @@ Thus the parameters object looks like:
path: {string},
showFiles: {boolean},
logLevel: {number},
ignorePattern: {string}
ignorePattern: {string},
output: {string}
}

#### Example ####
Expand All @@ -105,7 +118,8 @@ Thus the parameters object looks like:
path: './',
showFiles: true,
logLevel: 3,
ignorePattern: 'node_modules'
ignorePattern: 'node_modules',
output: './tree.txt'
})
```
Expand Down
2 changes: 2 additions & 0 deletions bin/cli-parameters.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
.option('-f, --show-files [optional]', 'Whether or not showing files along folders')
.option('-l, --log-level [optional]', 'From 0 (debug) to 3 (errors only)', parseInt)
.option('-i, --ignore-pattern [optional]', 'A glob to identify stuff you don\'t want to be represented', String)
.option('-o, --output [optional]', 'The path of the file you want your te to be stored to', String)
.parse(process.argv);

let parameters = new Parameters();
parameters.path = program.path || null;
parameters.showFiles = program.showFiles || false;
parameters.logLevel = program.logLevel || 3;
parameters.ignorePattern = program.ignorePattern || "";
parameters.output = program.output || null;

module.exports = { parameters };
}())
7 changes: 4 additions & 3 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

const { parameters } = require('./cli-parameters.service');
const { drawTree } = require('../src/service/tree-drawer.service');
const { parseFolderStructure } = require('../src/service/tree-parser.service')
const { parseFolderStructure } = require('../src/service/tree-parser.service');
const { redirectOutput } = require('../src/service/redirect-output.service')
const { setLogLevel } = require('../src/service/logging.service');

/**
Expand All @@ -24,7 +25,7 @@
let result = drawTree(tree);

/**
* Prints output to the console
* Prints output
*/
console.log(result);
redirectOutput(result, parameters);
}());
1 change: 1 addition & 0 deletions bin/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bin
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
const { parseFolderStructure } = require('./src/service/tree-parser.service');
const { drawTree } = require('./src/service/tree-drawer.service');
const { setLogLevel } = require('./src/service/logging.service');
const { redirectOutput } = require('./src/service/redirect-output.service');

module.exports = function (parameters) {
/**
Expand All @@ -22,8 +23,8 @@
const result = drawTree(tree);

/**
* Prints output to the console
* Prints output
*/
console.log(result);
redirectOutput(result, parameters);
};
}());
27 changes: 27 additions & 0 deletions src/service/redirect-output.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"use strict";

(function(){
const fs = require('fs');
const logger = require('./logging.service');

function redirectOutput(result, parameters){
let output = parameters.output;

if(output){
fs.writeFile(output, result, (error) => {
if(error){
logger.error('Unable to write on: ', output);
logger.debug(error);
return;
}

logger.info('Successfully written: ', output)
});
}
else{
return result
}
}

module.exports = { redirectOutput }
})();
40 changes: 40 additions & 0 deletions src/service/redirect-output.service.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"use strict";

(function () {
const { expect } = require('chai');
const sinon = require('sinon');
const fs = require('fs');
const service = require('./redirect-output.service');
const logger = require('./logging.service');
const Parameters = require('../model/parameters.model');

describe('Service: ReedirectOutput', () => {
logger.setLogLevel(3);
it('should be defined', () => {
expect(!!service).to.equal(true);
});

describe('Method: redirectOutput', () => {
it('should be defined', () => {
expect(!!service.redirectOutput).to.equal(true);
});

it('should return a string if no output is provided', () => {
let x = service.redirectOutput('asd', new Parameters());

expect(x).to.equal('asd');
})

it('should call fs.writeFile if output is provided', () => {
let params = new Parameters();
params.output = '.';

let fsStub = sinon.stub(fs, 'writeFile');

service.redirectOutput('asdf', params);

expect(fsStub.called).to.equal(true);
})
})
})
}());

0 comments on commit f1782db

Please sign in to comment.