-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
85 lines (80 loc) · 2.47 KB
/
app.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
const { createReadStream, writeFileSync, existsSync } = require('fs');
const readline = require('readline');
const { parse } = require('csv-parse');
const checkIfJSONExists = () => {
if (existsSync('lendingLibraries.json')) {
const inquirer = readline.createInterface({
input: process.stdin,
output: process.stdout
});
inquirer.question(
'A json file already exists in this folder and will be overwritten. Press "Y" to proceed, or any other key to abort.',
(res) => {
if (res === 'Y' || res === 'y') {
console.log('Proceeding...');
extractDataFromCSV();
} else {
console.log(
'Conversion aborted. Please rename or delete the existing json file and try again.'
);
process.exit(1);
}
inquirer.close();
}
);
} else {
extractDataFromCSV();
}
};
const extractDataFromCSV = () => {
const localDataSource = validateFile();
const borrowingLibraries = [];
createReadStream(localDataSource)
.pipe(parse({ delimiter: '\t', from_line: 14, relax_column_count: true }))
.on('data', (row) => {
const locationInfo = {
name: row[0],
institutionSymbol: row[1],
institutionState: row[2],
lendingRequestsReceived: row[6],
lendingRequestsFilled: row[7],
lendingTurnAroundTime: row[12],
loansFilled: row[8],
copiesFilled: row[9]
};
borrowingLibraries.push(locationInfo);
})
.on('end', () => {
writeFileSync(
'lendingLibraries.json',
JSON.stringify(borrowingLibraries),
{
encoding: 'utf8',
flag: 'w'
}
);
});
};
const getSourceFileFromCommandLine = (args) => {
if (!args) {
console.log('Usage: node app.js <filename>');
process.exit(1);
} else if (args === 'help') {
console.log(
"Accepts WorldShare Lender Transaction Detail Report in .tsv format in the app's directory and outputs a JSON file with the library's name, coordinates, and number of requests filled."
);
console.log('Usage: node app.js <filename>');
console.log('Example: node app.js sep');
process.exit(1);
}
return args.endsWith('.tsv') ? args : args + '.tsv';
};
const validateFile = () => {
const args = getSourceFileFromCommandLine(process.argv[2]);
if (!existsSync(args)) {
console.log(`${args} does not exist in the current directory.`);
process.exit(1);
}
return args;
};
checkIfJSONExists();