fakeit
Version:
Command-line utility that generates fake data which can be output as JSON, YAML, CSON, or CSV formats based on models defined in YAML.
310 lines (244 loc) • 13.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _regenerator = require('babel-runtime/regenerator');
var _regenerator2 = _interopRequireDefault(_regenerator);
var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
exports.code = code;
exports.dim = dim;
var _commander = require('commander');
var _commander2 = _interopRequireDefault(_commander);
var _index = require('./index.js');
var _index2 = _interopRequireDefault(_index);
var _updateNotifier = require('update-notifier');
var _updateNotifier2 = _interopRequireDefault(_updateNotifier);
var _package = require('./../package.json');
var _package2 = _interopRequireDefault(_package);
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _lodash = require('lodash');
var _chalk = require('chalk');
var _chalk2 = _interopRequireDefault(_chalk);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// this is not a function that is to be called by anything other than the `bin/fakeit` file in the project
exports.default = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee5() {
var _this = this;
// this function is used as a helper to run the different actions
var run = function () {
var _ref6 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee4() {
var output = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var output_path, models, fakeit;
return _regenerator2.default.wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
output = typeof output === 'string' ? { output: output } : output;
// get the base options from commander
opts = (0, _lodash.pick)((0, _lodash.extend)((0, _lodash.pick)(_commander2.default, base_options), opts), base_options);
// set the babel config
opts.babel_config = opts.babel;
delete opts.babel;
// get the output options
output = (0, _lodash.extend)((0, _lodash.pick)(_commander2.default, output_options), (0, _lodash.pick)(output, output_options));
// get the output path
output_path = _path2.default.join(output.output, output.archive || '');
// get the model files that have been passed into fakeit
models = _commander2.default.args.filter(function (str, i, args) {
var prev = args[i - 1] || '';
if (typeof str !== 'string' ||
// if the previous str was one of these commands that has options then remove it
/^(\-\-(?:commander|root|babel|count|format|spacing|seed|limit)|(?:\-[cfnxl]{1,}))$/.test(prev) ||
// if the str is one of the commands then remove it
/^(([-]{1,2}[a-zA-Z]+)|\-\-no\-[a-z]+)$/.test(str) ||
// if the str is the same as the output path then remove it
str === output_path) {
return false;
}
return true;
});
fakeit = new _index2.default(opts);
if (models.length) {
_context4.next = 12;
break;
}
fakeit.log('warning', 'you must pass in models to use');
(0, _lodash.find)(_commander2.default.commands, ['_name', output.output]).help();
return _context4.abrupt('return');
case 12:
_context4.prev = 12;
_context4.next = 15;
return fakeit.generate(models, output);
case 15:
process.exit();
_context4.next = 22;
break;
case 18:
_context4.prev = 18;
_context4.t0 = _context4['catch'](12);
process.exit(1);
throw _context4.t0;
case 22:
case 'end':
return _context4.stop();
}
}
}, _callee4, this, [[12, 18]]);
}));
return function run() {
return _ref6.apply(this, arguments);
};
}();
var base_options, output_options, runServer;
return _regenerator2.default.wrap(function _callee5$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
runServer = function runServer(output) {
var _this2 = this;
/* istanbul ignore next : too difficult to test the servers via the cli */
return function () {
var _ref3 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2() {
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
var options;
return _regenerator2.default.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
options = (0, _lodash.pick)(args.pop(), ['server', 'bucket', 'username', 'password', 'timeout', 'useStreams', 'highWaterMark']);
options.output = output;
_context2.next = 4;
return run(options);
case 4:
case 'end':
return _context2.stop();
}
}
}, _callee2, _this2);
}));
return function () {
return _ref3.apply(this, arguments);
};
}();
};
// check for update and notify
(0, _updateNotifier2.default)({ pkg: _package2.default }).notify();
base_options = ['root', 'babel', 'count', 'verbose', 'log', 'spinners', 'timestamp', 'seed'];
output_options = [
// global options
'format', 'spacing', 'limit',
// action specific options
'highlight', 'archive', 'server', 'bucket', 'username', 'password', 'timeout', 'useStreams', 'highWaterMark',
// gets set based off the command that's used
'output'];
// get the inputs
_commander2.default.version(_package2.default.version).usage('[command] [<file|directory|glob> ...]')
// these are all the base options across the different actions
.option('--root <directory>', 'Defines the root directory from which paths are resolve from (' + dim('process.cwd()') + ')', process.cwd()).option('--babel <glob>', 'The location to the babel config (' + dim('+(.babelrc|package.json)') + ')', '+(.babelrc|package.json)').option('-c, --count <n>', 'Overrides the number of documents to generate specified by the model. Defaults to model defined count', parseInt).option('-v, --verbose', 'Enables verbose logging mode (' + dim(false) + ')').option('-S, --no-spinners', 'Disables progress spinners', false).option('-L, --no-log', 'Disables all logging except for errors', false).option('-T, --no-timestamp', 'Disables timestamps from logging output', false)
// global output options
.option('-f, --format <type>', 'this determines the output format to use. Supported formats: ' + code('json', 'csv', 'yaml', 'yml', 'cson') + '. (' + dim('json') + ')', 'json') // eslint-disable-line
.option('-n, --spacing <n>', 'the number of spaces to use for indention (' + dim('2') + ')', 2).option('-l, --limit <n>', 'limit how many files are output at a time (' + dim('10') + ')', Number, 10).option('-x, --seed <seed>', 'The global seed to use for repeatable data', function (seed) {
var number = parseInt(seed);
if (number > 0 || seed === '0') {
return number;
}
return seed;
});
// i.e. `fakeit console`
_commander2.default.command('console').option('-h, --no-highlight', 'This turns off the cli-table when a csv format', Boolean, false).description('outputs the result to the console').action(function () {
var _ref2 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee() {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
var _args$pop, highlight;
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_args$pop = args.pop(), highlight = _args$pop.highlight;
_context.next = 3;
return run({ output: 'console', highlight: highlight });
case 3:
case 'end':
return _context.stop();
}
}
}, _callee, _this);
}));
return function () {
return _ref2.apply(this, arguments);
};
}());
// helper function used for couchbase and sync-gateway below
// i.e. `fakeit couchbase`
_commander2.default.command('couchbase').option('-s, --server [server]', 'The server address (' + dim('127.0.0.1') + ')').option('-b, --bucket [bucket]', 'The bucket name (' + dim('default') + ')').option('-u, --username [username]', 'The username to use (optional pre-5.0)').option('-p, --password [password]', 'the password for the account (optional)').option('-t, --timeout [timeout]', 'timeout for the servers (' + dim(5000) + ')', Number).option('-r, --use-streams [useStreams]', _chalk2.default.red('**experimental**') + '\n Whether or not to use node streams. Used for high output\n documents and can only be used when there are no dependencies (' + dim(false) + ')', Boolean).option('-w, --high-water-mark [highWaterMark]', _chalk2.default.red('**experimental**') + '\n The # of objects to process through the stream at a time (' + dim(16) + ')', Number).description('This will output to a Couchbase Server').action(runServer('couchbase'));
// i.e. `fakeit sync-gateway`
_commander2.default.command('sync-gateway').option('-s, --server [server]', 'The server address (' + dim('127.0.0.1') + ')').option('-b, --bucket [bucket]', 'The bucket name (' + dim('default') + ')').option('-u, --username [username]', 'the username to use (optional)').option('-p, --password [password]', 'the password for the account (optional)').option('-t, --timeout [timeout]', 'timeout for the servers').description('This will output to a Couchbase Sync-Gateway Server').action(runServer('sync-gateway'));
_commander2.default.command('directory [<dir|file.zip>] [<models...>]').alias('folder').option('-a, --archive [file.zip]', 'If an archive file is passed then the data will be output as a zip file', '').description('Output the file(s) into a directory').action(function () {
var _ref4 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee3(output, models, _ref5) {
var archive = _ref5.archive;
var parsed;
return _regenerator2.default.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
parsed = _path2.default.parse(output);
if (parsed.ext) {
archive = parsed.base;
output = parsed.dir || _commander2.default.root;
}
// update the commander args to be correct
_commander2.default.args = process.argv.slice(4);
_context3.next = 5;
return run({ archive: archive, output: output });
case 5:
case 'end':
return _context3.stop();
}
}
}, _callee3, _this);
}));
return function (_x, _x2, _x3) {
return _ref4.apply(this, arguments);
};
}());
// i.e. `fakeit help`
_commander2.default.command('help').action(function () {
_commander2.default.help();
});
_commander2.default.parse(process.argv);
if (!_commander2.default.args.length) {
_commander2.default.help();
}
case 12:
case 'end':
return _context5.stop();
}
}
}, _callee5, this);
}));
/* istanbul ignore next : too hard to test */
process.on('uncaughtException', function (err) {
console.log('An uncaughtException was found:', err);
process.exit(1);
});
function code() {
for (var _len3 = arguments.length, args = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
args[_key3] = arguments[_key3];
}
return (0, _lodash.flattenDeep)(args).map(function (str) {
return _chalk2.default.bold(str);
}).join(', ');
}
function dim() {
for (var _len4 = arguments.length, args = Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
args[_key4] = arguments[_key4];
}
return (0, _lodash.flattenDeep)(args).map(function (str) {
return _chalk2.default.dim(str);
}).join(', ');
}