-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·102 lines (81 loc) · 2.5 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env node
const moment = require('moment-timezone');
const csv = require('csvtojson');
const jsonexport = require('jsonexport');
const fs = require('fs');
const meow = require('meow');
const chalk = require('chalk');
const terminalLink = require('terminal-link')
// Link to tz database
const tz_database = terminalLink('tz database', 'https://en.wikipedia.org/wiki/List_of_tz_database_time_zones')
const cli = meow(`
Usage
$ plees2charter <plees-tracker-export.csv> [<output>]
Options
--verbose, -v Print output contents
--timezone, -t Specify timezone (must be a string from the ${tz_database})
Examples
$ plees2charter sleep.csv
=> output defaults to sleepcharter.csv
$ plees2charter sleep.csv sleepcharter.csv
$ plees2charter sleep.csv -t "Europe/Berlin"
`, {
flags: {
verbose: {
type: 'boolean',
alias: 'v'
},
timezone: {
type: 'string',
alias: 't'
}
}
});
const error = (err) => {
console.log()
console.error(chalk.red.bold(` Error: ${err}`))
cli.showHelp()
}
// Format date for SleepCharter consumption
const formatDate = (date) => {
if(timezone) {
return moment(Number(date)).tz(timezone).format("YYYY-MM-DD HH:mm")
} else {
return moment(Number(date)).format("YYYY-MM-DD HH:mm")
}
}
const {verbose, timezone} = cli.flags
// Output defaults to sleepcharter.csv
const targetFile = cli.input[1] || 'sleepcharter.csv'
// Require input file
if (!cli.input[0]) {
error("Please provide an input file.")
}
// Check if input file exists
if (!fs.existsSync(cli.input[0])) {
error(`Input file ${cli.input[0]} not found.`)
}
// Throw error if too many arguments are given
if (cli.input.length > 2) {
error("Too many arguments.")
}
(async () => {
const data = await csv().fromFile(cli.input[0])
const mapped = data.map(el => {
return {
SleepStart: formatDate(el.start),
SleepEnd: formatDate(el.stop)
}
})
jsonexport(mapped, { rowDelimiter: ',' }, function (err, csv) {
if (err) return error(err);
verbose && console.log(csv);
try {
// Write results to CSV
const data = fs.writeFileSync(targetFile, csv)
console.log(chalk.green.bold(`Successfully converted ${cli.input[0]} to ${targetFile}.`))
} catch (err) {
error(err)
}
});
})();