Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
34 changes: 32 additions & 2 deletions bin/datamaker.bin.js
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down