-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (55 loc) · 1.76 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
'use strict';
var fs = require('fs'),
downloader = require('./lib/downloader'),
exporter = require('./lib/exporter');
function saveOutput(err, csvData) {
fs.writeFile('export.csv', csvData, function (err) {
if (err) {
return console.log('Cannot write export.csv: ' + (err && err.message));
}
console.log('Done!');
});
}
function downloadIssues(url) {
downloader.getIssues(url, function (err, issuesData) {
if (err || !issuesData) {
return console.log('Cannot download GitHub issues: ' + (err && err.message));
}
fs.writeFile('import.json', issuesData, function (errWrite) {
if (errWrite) {
return console.log('Cannot write import.json: ' + (errWrite && errWrite.message));
}
console.log('Done!');
});
});
}
function getArgs() {
var argv = process.argv,
file;
if (!argv || !argv.length || argv.length < 3) {
return console.log('Please pass an argument: URL or .json file');
}
file = argv[2];
if (!file || !file.length) {
return console.log('Invalid Argument!');
}
if (file.indexOf('http://') === 0 || file.indexOf('https://') === 0) {
downloadIssues(file);
} else {
fs.readFile(file, {
encoding: 'utf8'
}, function (err, data) {
if (err) {
return console.log('Cannot open file: ' + (err && err.message));
}
var jsonData;
try {
jsonData = JSON.parse(data);
} catch (e) {
return console.log('File is not valid JSON: ' + (e && e.message));
}
exporter.generateCSV(jsonData, saveOutput);
});
}
}
getArgs();