-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
132 lines (115 loc) · 4.36 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const chalk = require('chalk');
const clear = require('clear');
const figlet = require('figlet');
const inquirer = require('inquirer');
const Utils = require('./lib/utils');
if (require.main === module) {
(function () {
clear();
console.log(''); // prevents top from being chopped off
console.log(
chalk.yellow(
figlet.textSync('CSV Generator', {
horizontalLayout: 'full'
})
)
);
console.log('To show help or list available functions, type "help" or "help functions" at any question and press ENTER');
const help = input => {
if (typeof input === 'string') {
if (input.toLowerCase().trim() === 'help') {
return 'Testing...';
} else if (/^(help\s*)?func(tions?)?$/i.test(input.toLowerCase().trim())) {
return 'Available functions:\n' + Utils.listFunctions(require('./functions'));
}
}
return true;
};
inquirer.prompt([
{
name: 'file',
type: 'input',
message: 'Enter in the name of your output file:',
default: 'my.csv',
validate: function (input) {
let response = help(input);
if (response !== true) {
return response;
}
return true;
}
},
{
name: 'use-headers',
type: 'list',
message: 'Enable column headers?',
choices: ['Yes', 'No']
},
{
name: 'custom-headers',
type: 'list',
message: 'Specify custom column headers?',
choices: ['Yes', 'No'],
when: function (input) {
return input['use-headers'] === 'Yes';
}
},
{
name: 'headers',
message: 'Enter column headers separated by a comma.',
type: 'editor',
when: function (input) {
return input['custom-headers'] === 'Yes';
},
validate: function (input) {
let headers = input.trim().split(/\s*,\s*/);
if (headers.length === 0) {
return 'Invalid column headers';
}
return true;
}
},
{
name: 'columns',
type: 'input',
message: 'Enter in column definitions using a list of available functions separated by a comma. For examples and other documentation, type "help" and press ENTER:',
validate: function (input) {
let response = help(input);
if (response !== true) {
return response;
}
if (!input || (input && input.trim().split(/\s*,\s*/g).length === 0)) {
return 'Invalid column definitions.';
}
return true;
}
},
{
name: 'rows',
type: 'input',
message: 'Enter in the total number of rows to generate (e.g. 100, 100000, 100K, 10M, 1B, etc.):',
default: 100,
validate: function (input) {
let response = help(input);
if (response !== true) {
return response;
}
try {
Utils.convertNumber(input);
} catch (e) {
return 'Invalid number entered. Value must be an integer greater than 0.';
}
return true;
}
}
]).then(answers => {
Utils.generate(answers.file, answers.columns.split(/\s*,\s*/), {
rows: Utils.convertNumber(answers.rows),
chunks: 1000,
headers: answers['use-headers'] === 'Yes'
}, Array.isArray(answers.headers) ? answers.headers.split(/\s*,\s*/) : []);
});
}());
} else {
module.exports = require('./lib/generator');
}