-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgencsv
executable file
·188 lines (165 loc) · 5.41 KB
/
gencsv
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env node
const chalk = require('chalk');
const i18n = require('i18n');
const Utils = require('../lib/utils');
const Preferences = require('../lib/preferences');
i18n.configure({
locales: ['en'],
directory: `${__dirname}/locales`
});
const yargs = require('yargs');
const argv = yargs
.command('<file>', i18n.__('The output file name'))
.usage('$0 [options] <file> [columns..]\n$0 [options] <file> < columns.txt', {
chunk: {
describe: i18n.__('The number of rows to generate per pass'),
number: true,
default: 1000
},
functions: {
alias: 'func',
describe: i18n.__('Lists available functions')
},
rows: {
alias: 'r',
describe: i18n.__('The number of rows to generate (e.g. 100, 100000, 100k, 1M, 1B, etc.)'),
default: 100
},
'use-headers': {
alias: 'h',
describe: i18n.__('Use this flag to set column headers'),
boolean: true,
default: false
},
interactive: {
alias: 'i',
describe: i18n.__('Run the script in interactive mode with a series of questions and user-provided answers'),
conflicts: 'clear-settings'
},
'always-interactive': {
alias: 'a',
describe: i18n.__('Start the script in interactive mode and save this setting'),
conflicts: 'clear-settings'
},
'always-use-headers': {
describe: i18n.__('Use this flag to persist column headers preferences'),
conflicts: 'clear-settings'
},
'clear-settings': {
alias: 'c',
describe: i18n.__('Clear always-interactive settings')
},
silent: {
alias: 's',
describe: i18n.__('Minimal console output'),
boolean: true,
default: false
}
})
.help()
.version(require('../package.json').version)
.argv;
const showHelp = message => {
console.log(`${chalk.redBright(`${message}`)}\n${i18n.__('Usage')}:`);
yargs.showHelp();
process.exit(1);
};
if (argv.functions) {
console.log(`${i18n.__('Available functions')}:`);
Utils.functions(require('../functions'));
process.exit(0);
}
if (argv.c) {
console.log(`${i18n.__('Removing settings')}...`);
Preferences.truncate(
() => {
console.log(`${i18n.__('Successfully removed')} ${chalk.cyan(Preferences.file())}`);
if (argv._.length <= 1) {
process.exit(0);
}
},
() => {
console.log(`${i18n.__('Error removing')} ${chalk.redBright(Preferences.file())}`);
console.log(`${i18n.__('Attempting to run command')}...\n`);
}
);
}
if (Preferences.isInteractive() || argv.i) {
require('../index');
} else if (argv.a) {
Preferences.save('interactive', true, () => {
console.log(`${i18n.__('Settings written to')} ${chalk.cyan(Preferences.file())}`);
setTimeout(() => require('../index'), 500);
});
} else {
if (argv._.length <= 1 && process.stdin.isTTY) {
console.log(chalk.redBright(`${i18n.__('You must define an output file name and at least one column definition!')}`));
process.exit(1);
}
if (Preferences.alwaysUseHeaders()) {
argv['use-headers'] = true;
}
const outFile = argv._[0];
let columns = argv._.slice(1);
if (columns.length === 1) {
try {
const newCols = columns[0].split(' ');
if (newCols.length > columns.length) {
columns = newCols;
}
} catch (e) {
console.warn(e);
}
}
let rows = 100;
if (!outFile) {
showHelp(i18n.__('Output file must be defined!'));
} else if (!/^\.?[a-zA-Z0-9\.-_]+(\.\w+)?$/.test(outFile)) {
showHelp(`${i18n.__('Output file cannot end in a special character. Did you forget to add the output file?')}\n`);
}
if (argv.r !== 100) {
try {
rows = Utils.convertNumber(argv.r);
} catch (e) {
console.log(chalk.redBright(i18n.__('Invalid row number')));
process.exit(1);
}
}
if (columns.length === 0) {
let data = '';
const rl = require('readline').createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
rl.on('line', line => {
data += line;
data += '\n';
}).on('close', () => {
if (!argv.q) {
console.log(`${i18n.__('Columns definitions received! Validating')}...`);
}
let lines = data.split('\n');
let headers = '';
if (lines.length > 2) {
columns = lines[1].split(/\s*,\s*/);
headers = lines[0];
} else {
columns = lines[0].split(/\s*,\s*/);
}
Utils.generate(outFile, columns, {
rows: rows,
chunks: argv.chunk,
headers: argv['use-headers'] || headers.length > 0,
silent: argv.silent
}, headers.split(/\s*,\s*/));
});
} else {
Utils.generate(outFile, columns, {
rows: rows,
chunks: argv.chunk,
headers: argv['use-headers'],
silent: argv.silent
});
}
}