Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for a file template #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ npm install --global fony
-h, --help output usage information
-V, --version output the version number
-t, --template <template> JSON template for data to be generated
-f, --file <filename> File with a JSON template for data to be generated
-c, --count [count] The number of elements to create, defaults to 1

```
Expand Down
5 changes: 5 additions & 0 deletions examples/test1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "name",
"age": "age",
"phone": "phone"
}
42 changes: 42 additions & 0 deletions package-lock.json

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

18 changes: 17 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#! /usr/bin/env node
'use strict';

const fs = require('fs');
const program = require('commander');

const _version_ = require('../package.json').version;
Expand All @@ -11,12 +12,27 @@ program

program
.option('-t, --template <template>', 'JSON template for data to be generated')
.option('-f, --file <filename>', 'File with a JSON template')
.option('-c, --count [count]', 'The number of elements to create, defaults to 1', 1)
.parse(process.argv);


if (program.template && program.file) {
return console.error('Cannot have both --file and --template arguments!');
}
if (!(program.template || program.file)) {
return console.error('Must have one of --file and --template arguments!');
}

var templateSource = program.template;
var template;

try {
template = JSON.parse(program.template);
if (program.file) {
templateSource = fs.readFileSync(program.file);
}

template = JSON.parse(templateSource);
} catch (error) {
return console.log(error);
}
Expand Down