forked from bullhorn/dataloader-faker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
81 lines (78 loc) · 3.16 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
const argv = require('yargs')
.usage('Usage: $0 [options]')
.example('$0 -t templateFile.csv -o outputFile.csv -c 1000', 'generates 1000 lines of fake data into the outputFile based on the templateFile')
.example('$0 --template templateFile.csv --output outputFile.csv --rows 50', 'generates 50 lines of fake data into the outputFile based on the templateFile')
.example('$0 --template templateFile.csv --output outputFile.csv --rows 10000 --start 10001', 'generates 10000 lines of fake data starting the id fields at 10001')
.alias('t', 'template')
.alias('o', 'output')
.alias('r', 'rows')
.alias('s', 'start')
.nargs('t', 1)
.nargs('o', 1)
.nargs('r', 1)
.nargs('s', 1)
.describe('t', 'the template file which describes what data is to be faked')
.describe('o', 'the output file which points to the file to create as the output of this script')
.describe('r', 'the number of records to fake')
.describe('s', 'the starting index of the generated rows, for creating additional records')
.demandOption(['t', 'o', 'r'])
.argv;
const csv = require('fast-csv');
const faker = require('faker');
const fs = require('fs');
const path = require('path');
// Handle directory or single file
if (fs.lstatSync(argv.t).isDirectory()) {
const dir = fs.opendirSync(argv.t);
let dirEntry = null;
while ((dirEntry = dir.readSync()) !== null) {
if (dirEntry.isFile()) {
parseTemplateAndGenerate(path.join(dir.path, dirEntry.name), path.join(argv.o, dirEntry.name), argv.r);
}
}
dir.closeSync();
} else {
parseTemplateAndGenerate(argv.t, argv.o, argv.r);
}
function parseTemplateAndGenerate(templateFile, outputFile, rows) {
let template = null;
csv.parseFile(templateFile, { headers: true, strictColumnHandling: true, trim: true })
.on('data', (row) => {
if (!template) {
template = row;
}
})
.on('end', () => {
generate(template, outputFile, rows);
})
.on('error', (err) => {
console.error(err);
});
}
function generate(template, outputFile, rows) {
let generatedRows = [];
const start = argv.s ? argv.s : 1;
for (let i = 0; i < rows; i++) {
const row = {};
// Replace row number, options and faker
Object.keys(template).forEach(key => {
row[key] = template[key]
.replace(/{{i}}/ig, start + i)
.replace(/{{.+}}/ig, (match) => faker.fake(match))
.replace(/\[\[(.+)]]/ig, (match, $1) => {
const options = $1.split('|');
return options[i % options.length];
});
});
// Replace column names with values of other columns
Object.keys(row).forEach(key => {
row[key] = row[key].replace(/\${(.+)}/ig, (match, $1) => row[$1] ? row[$1] : '');
});
generatedRows.push(row);
}
if (!fs.existsSync(path.dirname(outputFile))) {
fs.mkdirSync(path.dirname(outputFile));
}
csv.writeToPath(outputFile, generatedRows, { headers: true });
console.log('Wrote', rows, 'lines to:', outputFile);
}