diff --git a/README.md b/README.md index de3b6b6..155436f 100644 --- a/README.md +++ b/README.md @@ -45,8 +45,13 @@ $ npm install --save datamaker - `--template`/`-t` - the path of the template file e.g `--template /path/to/template.txt` - `--format`/`-f` - the target file format (`none`, `csv`, `json` or `xml`). Default `none` e.g. `--format json` +- `--pretty`/`-p` - Pretty Print the output. only avaible for `json`. Default: `false` - `--iterations`/`-i` - the number of records to create. Default `1` e.g. `--iterations 5000` -- `--list`/`-l` - list the available tags +- `--list`/`-l` - list the available tags +- `--save`/`-s` - Save the Files to disk (JSON and XML only). Default: `false`` +- `--directory`/`-d` - Folder to write the files to. Defaults to '' +- `--name`/`-n` - Filename Template to save. `{i}` is a counter, with format `json` you may use `{json:PROPERTY}` to use a property as filename. Defaults to `output-{i}` + ## Generating CSV files diff --git a/bin/datamaker.bin.js b/bin/datamaker.bin.js index e9d898b..b7ad31c 100755 --- a/bin/datamaker.bin.js +++ b/bin/datamaker.bin.js @@ -1,6 +1,7 @@ #!/usr/bin/env node const fs = require('fs') +const path = require('path') const datagen = require('../index.js') let template = '' const piped = (!process.stdin.isTTY) @@ -9,9 +10,13 @@ let rs = null // get command-line arguements var argv = require('yargs') .option('format', { alias: ['f', 'type'], describe: 'Format of output data: json,csv,none', demandOption: false, default: 'none' }) - .option('iterations', { alias: 'i', describe: 'Number of records to generater', demandOption: false, default: 1 }) + .option('pretty', { alias: 'p', boolean: true, describe: 'Pretty-Print JSON-Files', demandOption: false, default: false }) + .option('iterations', { alias: 'i', describe: 'Number of records to generate', demandOption: false, default: 1 }) .option('template', { alias: 't', describe: 'The path of the template file', demandOption: false }) .option('list', { alias: 'l', boolean: true, describe: 'List available tags', demandOption: false, default: false }) + .option('save', { alias: 's', boolean: true, describe: 'Save Files to Disk', demandOption: false, default: false }) + .option('directory', { alias: 'd', describe: 'Directory to Save Files to', demandOption: false, default: '' }) + .option('name', { alias: 'n', describe: 'Name of Files to Save', demandOption: false, default: 'output-{i}' }) .help('help') .argv @@ -50,8 +55,33 @@ rs.on('readable', () => { template = template.trim() }).on('end', () => { // when the template has loaded, generate the data + let i = 0 datagen.generate(template, argv.format, argv.iterations) - .on('data', (d) => { console.log(d) }) + .on('data', (d) => { + if (argv.format === 'json' && argv.pretty) { + console.log(JSON.stringify(JSON.parse(d), null, 2)) + } else { + console.log(d) + } + if (argv.save && (argv.format === 'json' || argv.format === 'xml')) { + let filename = '' + filename = argv.name.replace(/{i}/g, i) + if (argv.format === 'json' && argv.name.includes('{json:')) { + const field = argv.name.split('{json:')[1].split('}')[0].trim() + const value = JSON.parse(d)[field] + filename = filename.replace(/{json:.*}/g, value) + } + filename += '.' + argv.format + filename = path.join(argv.directory, filename) + let data = d + if (argv.format === 'json') { + data = JSON.stringify(JSON.parse(d), null, 2) + } + fs.writeFileSync(filename, data) + console.log(filename + ' written') + } + i++ + }) .on('end', (d) => { }) }).on('error', (e) => { die(e, 2) diff --git a/index.js b/index.js index 91bde48..0052706 100644 --- a/index.js +++ b/index.js @@ -139,7 +139,6 @@ const generate = (str, format, iterations) => { const newStr = swap(str, tags, formatter) // emit the data to the caller ee.emit('data', newStr) - // TODO: here will be the saving to a file i++ } while (i < iterations) cache.clear() diff --git a/package-lock.json b/package-lock.json index ed0d677..0b2e4f7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "datamaker", - "version": "0.6.0", + "version": "0.8.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 2d6c6af..9d399bd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "datamaker", - "version": "0.6.0", + "version": "0.8.0", "description": "Sample data generator - command-line utility and library", "main": "index.js", "scripts": {