diff --git a/.vscode/launch.json b/.vscode/launch.json index dd06f64..f3db37e 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,7 +10,9 @@ "--path", ".", "-i", - ".git/**/*" + ".git/**/*", + "-o", + "./test.txt" ] } ] diff --git a/README.md b/README.md index 3a238e7..9506d8d 100644 --- a/README.md +++ b/README.md @@ -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 @@ -27,6 +23,7 @@ It takes the following parameters: - `show-files` - `log-level` - `ignore-pattern` +- `output` ### Path ### @@ -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 @@ -93,7 +105,8 @@ Thus the parameters object looks like: path: {string}, showFiles: {boolean}, logLevel: {number}, - ignorePattern: {string} + ignorePattern: {string}, + output: {string} } #### Example #### @@ -105,7 +118,8 @@ Thus the parameters object looks like: path: './', showFiles: true, logLevel: 3, - ignorePattern: 'node_modules' + ignorePattern: 'node_modules', + output: './tree.txt' }) ``` diff --git a/bin/cli-parameters.service.js b/bin/cli-parameters.service.js index 49bd3a3..01afc18 100644 --- a/bin/cli-parameters.service.js +++ b/bin/cli-parameters.service.js @@ -14,6 +14,7 @@ .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(); @@ -21,6 +22,7 @@ parameters.showFiles = program.showFiles || false; parameters.logLevel = program.logLevel || 3; parameters.ignorePattern = program.ignorePattern || ""; + parameters.output = program.output || null; module.exports = { parameters }; }()) \ No newline at end of file diff --git a/bin/index.js b/bin/index.js index e28db90..9b1ee34 100755 --- a/bin/index.js +++ b/bin/index.js @@ -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'); /** @@ -24,7 +25,7 @@ let result = drawTree(tree); /** - * Prints output to the console + * Prints output */ - console.log(result); + redirectOutput(result, parameters); }()); \ No newline at end of file diff --git a/bin/test.txt b/bin/test.txt new file mode 100644 index 0000000..ba077a4 --- /dev/null +++ b/bin/test.txt @@ -0,0 +1 @@ +bin diff --git a/index.js b/index.js index 145c383..9f70411 100755 --- a/index.js +++ b/index.js @@ -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) { /** @@ -22,8 +23,8 @@ const result = drawTree(tree); /** - * Prints output to the console + * Prints output */ - console.log(result); + redirectOutput(result, parameters); }; }()); \ No newline at end of file diff --git a/src/service/redirect-output.service.js b/src/service/redirect-output.service.js new file mode 100644 index 0000000..e606a58 --- /dev/null +++ b/src/service/redirect-output.service.js @@ -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 } +})(); \ No newline at end of file diff --git a/src/service/redirect-output.service.spec.js b/src/service/redirect-output.service.spec.js new file mode 100644 index 0000000..37ca292 --- /dev/null +++ b/src/service/redirect-output.service.spec.js @@ -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); + }) + }) + }) +}()); \ No newline at end of file