-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwf.js
executable file
·227 lines (198 loc) · 6.68 KB
/
wf.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#! /usr/bin/env node
const RTM = require('rtm-api')
var Workflowy = require('./node_modules/opusfluxus/')
var exec = require('child_process').exec
var prompt = require('prompt')
var fs = require('fs')
var argv = require('minimist')(process.argv.slice(2))
var userhome = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME']
var rc_path = userhome+"/.wfrc"
var exists = fs.existsSync(rc_path)
var rc = exists && fs.readFileSync(rc_path, 'utf8')
function handleErr(reason) {
while (reason.reason) {reason = reason.reason}
console.log("Error " + reason.status + ": ", reason.message)
if (reason.status == 404) {
console.log("It seems your sessionid has expired. Let's log you in again.")
auth()
} else {
process.exit(1)
}
}
function printHelp () {
console.log("usage: wf <command> [<args>]\n")
console.log("The commands currently available are:\n")
console.log(" tree n "+"print your workflowy nodes up to depth n (default: 2)")
console.log(" [--id=<id>] "+"print sub nodes under the <id> (default: whole tree)")
console.log(" [--withnote] "+"print the note of nodes (default: false)")
console.log(" [--hiddencompleted] "+"hide the completed lists (default: false)")
console.log(" [--withid] "+"print id of nodes (default: false)")
console.log("")
console.log(" capture "+"add something to a particular node")
console.log(" --parentid=<id> "+"<36-digit uuid of parent> (required)")
console.log(" --name=<str> "+"what to actually put on the node")
console.log(" [--priority=#] "+"0 as first child, 1 as second (default 0 (top))")
console.log(" "+" (use a number like 10000 for bottom)")
console.log(" [--note=<str>] "+"a note for the node (default '')")
console.log("")
}
var withnote = false
var hiddencompleted = false
var withid = false
var id = null
function recursivePrint (node, prefix, spaces, maxDepth) {
if (hiddencompleted && node.cp) {return}
if (!prefix) {prefix = '\u21b3 '}
if (!spaces) {spaces = ''}
var println = ''
println = spaces + prefix + node.nm
if (withnote && node.no) {
println += '\n'+spaces+' '+ node.no
}
if (withid) {println += '\n[' + node.id + ']'}
console.log(println)
if (maxDepth < 1) {return}
var children = node.ch
for (var i in children) {
recursivePrint(children[i], prefix, spaces+' ', maxDepth-1)
}
}
function exit () {
process.exit()
}
var regex = {
sessionid: /sessionid: (\w+)/,
rtm_api_key: /rtm_api_key: (\w+)/,
rtm_shared_secret: /rtm_shared_secret: (\w+)/
}
console.log("~~~~~~~ Workflowy ~~~~~~~ ")
if (argv.help) {
printHelp()
} else if (rc && regex.sessionid.test(rc)) {
var sessionid = rc.match(regex.sessionid)[1]
var wf = new Workflowy({sessionid: sessionid})
var command = argv._[0]
if (command === 'capture') { console.log("• • • creating workflowy node • • •")
var parentid = argv.parentid
var priority = argv.priority
var name = argv.name
var note = argv.note
wf.create(parentid, name, priority, note).then(function (result) {
console.log("created!")
}, handleErr).fin(exit)
} else if (command === 'tree') { console.log("• • • fetching workflowy tree • • •")
depth = argv.depth || argv._[1] || 2
id = argv.id
withnote = argv.withnote
hiddencompleted = argv.hiddencompleted
withid = argv.withid
if (id) {
wf.nodes.then(function (nodes) {
var node = nodes.find(function (node) {
return node.id == id
})
if (node) {
//recursivePrint(node, null, '', depth)
} else {
console.log('node ' + id + ' not found')
}
}, handleErr).fin(exit)
} else {
wf.outline.then(function (outline) {
var rootnode = {
nm: 'root',
ch: outline,
id: ''
}
//recursivePrint(rootnode, null, '', depth)
}, handleErr)
}
} else { console.log("• • • fetching workflowy data • • •")
wf.meta.then(function (meta) {
console.log("logged in as " + meta.settings.username)
console.log(meta.projectTreeData.mainProjectTreeInfo.rootProjectChildren.length + " top-level nodes")
if (command === 'meta') {
console.log("meta", meta)
} else {
console.log("(to view commands, run with --help)")
}
}, handleErr)
}
} else {
console.log("No ~/.wfrc detected... starting authentication process...")
auth()
}
console.log("------- RTM ---------")
if (rc && regex.rtm_api_key.test(rc)) {
var api_key = rc.match(regex.rtm_api_key)[1]
regex.rtm_shared_secret.test(rc)
var shared_secret = rc.match(regex.rtm_shared_secret)[1]
let client = new RTM(api_key,shared_secret,'RTM.PERM_DELETE')
client.auth.getAuthUrl(function(err, authUrl, frob) {
if ( err ) {
return console.error(err.toString());
}
console.log("Auth: " + authUrl);
console.log("FROB: " + frob);
var schema = {
properties: {
auth: {
required: true
}
}
}
prompt.start()
prompt.get(schema, function (err, result) {
// Get an Auth Token for the User once they've authorized the frob
client.auth.getAuthToken(frob, function(err, user) {
console.log("invoked with " + frob)
if ( err ) {
return console.error(err.toString());
}
// If successful, the returned user will include the property `authToken`
console.log(user.authToken);
// Save the user for making authenticated API calls via user.get()
})
})
})
console.log("RTM creds - " + api_key + " " + shared_secret)
} else {
console.log("No RTM credentials found")
}
function print_lists() {
}
function auth () {
console.log("What is your workflowy login info? This will not be saved, merely used once to authenticate.")
var schema = {
properties: {
email: {
required: true
},
password: {
required: true,
hidden: true
}
}
}
prompt.start()
prompt.get(schema, function (err, result) {
if (err) {console.log('CANCELLED'); return}
var wf = new Workflowy({username: result.email, password: result.password})
wf._login.then(function () {
if (wf.sessionid) {
console.log("Login successful." + rc_path)
fs.writeFileSync(rc_path, "sessionid: "+wf.sessionid+"\n")
/*
, (errf) => {
if (errf) {
return console.log("Failed to write sessionid to ~/.wfrc")
}
console.log("Successfully wrote sessionid to ~/.wfrc")
})
*/
} else {
console.log("Failed to get sessionid. Check your username/password.")
}
}, handleErr).fin(exit)
})
}