-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
executable file
·76 lines (67 loc) · 2.21 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
#!/usr/bin/env node
var fs = require('fs'),
GitHubApi = require('github'),
github = new GitHubApi({
version: '3.0.0',
protocol: 'https'
}),
dsv = require('dsv'),
rw = require('rw'),
argv = require('minimist')(process.argv.slice(2), {
boolean: ['help', 'print-url']
}),
open = require('open');
var charts = fs.readdirSync(__dirname + '/templates/');
var typenames = charts.map(function(c) {
return c.replace(/\.html$/, '');
}).join(',');
if (argv.help) {
console.log('input:');
console.log(' chartpipe < data');
console.log(' process | chartpipe');
console.log(' chartpipe file.csv');
console.log('');
console.log('arguments:');
console.log(' --type=CHARTTYPE');
console.log(' --format=INPUTFORMAT');
console.log(' --print-url (instead of opening)');
console.log('');
console.log('examples:');
console.log(' chartpipe --type=groupedbars file.csv');
console.log(' chartpipe --type=histogram file.csv');
console.log(' chartpipe --type=line --print-url file.csv')
console.log(' chartpipe --type=stackedbars file.csv')
console.log(' chartpipe --format=tsv file.tsv')
console.log('');
console.log('available charts: ' + typenames);
console.log('input formats: csv, tsv (default: csv)');
return;
}
var type = argv.type || 'groupedbars';
if (charts.indexOf(type + '.html') === -1) {
throw new Error('chart type ' + type + ' not found, choices: ' + typenames);
}
var input = argv._.length ?
fs.readFileSync(argv._[0], 'utf8') :
rw.readFileSync('/dev/stdin', 'utf8');
var indexhtml = fs.readFileSync(__dirname + '/templates/' + type + '.html', 'utf8');
if (argv.format === 'tsv') input = dsv.csv.format(dsv.tsv.parse(input));
github.gists.create({
description: '/dev/chartpipe',
public: true,
files: {
'data.csv': { content: input },
'index.html': { content: indexhtml }
}
}, function (err, res) {
if (err) {
console.error('Unable to create Gist:' + JSON.stringify(res));
} else {
var url = 'http://bl.ocks.org/' + res.id;
if (argv['print-url']) {
console.log(url);
} else {
open(url);
}
}
});