-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
156 lines (147 loc) · 5.43 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
const moment = require('moment')
// Check if first argument is a Folder and writableor exit with error
try {
if (process.argv && process.argv.length > 2) {
fs.accessSync(process.argv[2], fs.constants.R_OK | fs.constants.W_OK)
} else {
console.error('Usage: todotxt-recurring FOLDER/OF/TODO.TXT/')
process.exit(1)
}
} catch (err) {
console.error(process.argv[2] + ' does not exist or is not readable')
process.exit(2)
}
// Check if is folder contains todo.txt, todo.recurring
let todoFolder = process.argv[2]
if (!fs.lstatSync(todoFolder).isDirectory()) {
console.error(todoFolder + 'is not a directory')
process.exit(3)
}
let todoTXT = path.join(todoFolder, 'todo.txt')
try {
fs.accessSync(todoTXT, fs.constants.W_OK)
} catch (err) {
console.error(todoTXT + 'does not exist or is not writable')
process.exit(4)
}
let todoRecurring = path.join(todoFolder, 'todo.recurring')
try {
fs.accessSync(todoRecurring, fs.constants.R_OK)
} catch (err) {
console.error(todoRecurring + 'does not exist or is not readable')
process.exit(4)
}
// Create todo.recurring.added if needed
let todoRecurringAdded = path.join(todoFolder, 'todo.recurring.added')
try {
fs.accessSync(todoRecurringAdded, fs.constants.R_OK | fs.constants.W_OK)
} catch (err) {
fs.writeFileSync(todoRecurringAdded, '', 'utf8')
console.log('Created a new and empty todo.recurring.added')
}
// backup todo.txt to todo.bak
console.log('Backing up todo.txt to todo.bak')
fs.copyFileSync(todoTXT, path.join(todoFolder, 'todo.bak'))
// read in recurring file line by line into array
console.log('Reading ' + todoRecurring)
let todosToAdd = fs.readFileSync(todoRecurring).toString().split('\n')
console.log('Got ' + todosToAdd.length + ' Lines')
// read in recurringAdded file line by line into array
console.log('Reading ' + todoRecurringAdded)
let todosAdded = fs.readFileSync(todoRecurringAdded).toString().split('\n')
console.log('Got ' + todosAdded.length + ' Lines')
// Getting Todays Date
const now = moment()
const today = now.format('YYYY-MM-DD')
console.log('Todays Date is ' + today)
// loop todo.recurring
console.log('Starting to create Todos')
for (let index = 0; index < todosToAdd.length; index++) {
const todo = todosToAdd[index]
let timer = todo.split(/-(.+)/)[0]
let unit = timer.split(';')[0].trim()
let line = todo.split(/-(.+)/)[1]
let numberString
let numbers
var m
switch (unit) {
case 'daily':
line = today + line.replace(/\$DATE/g, today)
if (todosAdded.indexOf(line) < 0) {
fs.appendFileSync(todoTXT, line + '\n', 'utf8')
fs.appendFileSync(todoRecurringAdded, line + '\n', 'utf8')
console.log('Added Line: ' + line)
}
break
case 'weekly':
let startOfWeek = moment().startOf('week')
let endOfWeek = moment().endOf('week')
numberString = timer.split(';')[1].trim()
numbers = []
if (numberString.indexOf(',') > -1) {
numbers = numberString.split(',')
} else {
numbers.push(numberString)
}
for (m = moment(startOfWeek); m.isBefore(endOfWeek); m.add(1, 'days')) {
let day = m.format('YYYY-MM-DD')
let dayOfWeek = m.format('d')
let addLine = day + line.replace(/\$DATE/g, day)
if (numbers.indexOf(dayOfWeek) > -1 && todosAdded.indexOf(addLine) < 0) {
fs.appendFileSync(todoTXT, addLine + '\n', 'utf8')
fs.appendFileSync(todoRecurringAdded, addLine + '\n', 'utf8')
console.log('Added Line: ' + addLine)
}
}
break
case 'monthly':
let startOfMonth = moment().startOf('month')
let endOfMonth = moment().endOf('month')
numberString = timer.split(';')[1].trim()
numbers = []
if (numberString.indexOf(',') > -1) {
numbers = numberString.split(',')
} else {
numbers.push(numberString)
}
for (m = moment(startOfMonth); m.isBefore(endOfMonth); m.add(1, 'days')) {
let day = m.format('YYYY-MM-DD')
let dayOfMonth = m.format('D')
let addLine = day + line.replace(/\$DATE/g, day)
if (numbers.indexOf(dayOfMonth) > -1 && todosAdded.indexOf(addLine) < 0) {
fs.appendFileSync(todoTXT, addLine + '\n', 'utf8')
fs.appendFileSync(todoRecurringAdded, addLine + '\n', 'utf8')
console.log('Added Line: ' + addLine)
}
}
break
case 'yearly':
let startOfYear = moment().startOf('year')
let endOfYear = moment().endOf('year')
numberString = timer.split(';')[1].trim()
numbers = []
if (numberString.indexOf(',') > -1) {
numbers = numberString.split(',')
} else {
numbers.push(numberString)
}
for (m = moment(startOfYear); m.isBefore(endOfYear); m.add(1, 'days')) {
let day = m.format('YYYY-MM-DD')
let dayOfYear = m.format('DDD')
let addLine = day + line.replace(/\$DATE/g, day)
if (numbers.indexOf(dayOfYear) > -1 && todosAdded.indexOf(addLine) < 0) {
fs.appendFileSync(todoTXT, addLine + '\n', 'utf8')
fs.appendFileSync(todoRecurringAdded, addLine + '\n', 'utf8')
console.log('Added Line: ' + addLine)
}
}
break
default:
console.error(unit + ' is not a valid unit.')
continue
}
}
console.log('Done')