-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·166 lines (149 loc) · 4.01 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
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env node
const fs = require('fs')
const execSync = require('child_process').execSync
const args = process.argv
const googleTasks = JSON.parse(fs.readFileSync(args[2], 'utf8'))
/**
* Run a shell command synchronously
*
* @param {string} command - shell command to run
* @returns {string} - stdio/err
*/
function runCommand (command) {
return execSync(command).toString()
}
/**
* Flatten Google Tasks export object into an object with the
* task list name as the key for an array of task objects.
* @returns {object}
*/
function flattenTaskLists (input) {
let taskListsArray = []
input.items.forEach((list, index) => {
taskListsArray.push(list)
})
const tasksFlattened = {}
taskListsArray.forEach(function (list) {
tasksFlattened[list.title] = list.items
})
return tasksFlattened
}
/**
* Make a `task add` command as a string.
* @param {object} task - object with values for passing to `task`
* @returns {string}
*/
function taskAdd (task) {
let taskCommand
let taskAction
if (task.status === 'needsAction') {
taskAction = 'add'
} else if (task.status === 'completed') {
taskAction = 'log'
}
taskCommand = `task ${taskAction} "${task.title}" project:"${task.project}"`
let tags = task.tags || []
tags.forEach(function (tag) {
taskCommand += ` +"${tag}"`
})
if (task.depends) taskCommand += ` depends:${task.depends}`
console.log(`Running command: ${taskCommand}`)
return runCommand(taskCommand)
}
/**
* Add annotation to a task already in taskwarrior
*
* @param {string|interger} taskID
* @param {string} annotation
* @returns {undefined}
*/
function annotateTask (taskID, annotation) {
if (taskID) {
let taskCommand = `task ${taskID} annotate "${annotation}"`
return runCommand(taskCommand)
}
}
/**
* extractTaskID
*
* @param {string} output - output of a `task add` command
* @returns {string} - ID of the task referenced in @output
*/
function extractTaskID (output) {
if (output) {
let re = /(Created task )([0-9]*)/
let match = output.match(re)
let taskshID
if (match && match.length > 2) {
taskshID = match[match.length - 1]
}
return taskshID || null
} else {
return null
}
}
/**
* Loop through lists and make tasks
*/
const lists = flattenTaskLists(googleTasks)
for (let list in lists) {
if (
lists.hasOwnProperty(list) &&
typeof lists[list] === 'undefined'
) {
console.log(`Skipping empty list "${list}"\n`)
} else {
const listName = list
const tasks = lists[list]
console.log('Processing "' + listName + '" (' + tasks.length + ' tasks)...')
tasks.forEach(function (task) {
const uuid = task.id
let subtasks = []
tasks.find(obj => {
if (obj.parent === uuid) subtasks.push(obj)
})
let subtaskIDs = []
if (subtasks.length) {
subtasks.forEach(subtask => {
const taskAddCommand = taskAdd({
title: subtask.title,
status: subtask.status,
project: list,
tags: ['googleTasks']
})
let taskID = extractTaskID(taskAddCommand)
if (subtask.notes && taskID) {
const taskAnnotateCommand = annotateTask(taskID, subtask.notes)
console.log(taskAnnotateCommand)
}
if (taskID) subtaskIDs.push(taskID)
})
}
if (!task.parent) {
const taskAddCommand = taskAdd({
title: task.title,
status: task.status,
project: list,
tags: ['googleTasks'],
depends: subtaskIDs
})
console.log(taskAddCommand)
let taskID = extractTaskID(taskAddCommand)
if (task.notes) {
const taskAnnotateCommand = annotateTask(taskID, task.notes)
console.log(taskAnnotateCommand)
}
}
})
}
}
/**
* Debug code
*/
// const outFile = 'debug.json'
// const tasksOutputString = JSON.stringify(lists, null, 4)
// fs.writeFile(outFile, tasksOutputString, function (err) {
// if (err) {
// return console.log(err)
// }
// })