-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathcmd.js
executable file
·155 lines (147 loc) · 4.11 KB
/
cmd.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
#!/usr/bin/env node
const yargs = require( 'yargs' );
const fs = require( 'fs' );
const fetch = require( 'node-fetch' );
const argv = yargs
.option( 'graph', {
alias: 'g',
description: 'Your graph name',
type: 'string',
} )
.option( 'email', {
alias: 'e',
description: 'Your Roam Email',
type: 'string',
} )
.option( 'password', {
alias: 'p',
description: 'Your Roam Password',
type: 'string',
} )
.option( 'debug', {
description: 'enable debug mode',
type: 'boolean',
default: false,
} )
.option( 'stdin', {
alias: 'i',
description: 'Read from STDIN',
type: 'boolean',
default: false,
} )
.option( 'removezip', {
description: 'If downloading the Roam Graph, should the timestamp zip file be removed after downloading?',
type: 'boolean',
default: true,
} )
.command(
'query [query]',
'Query your Roam Graph using datalog syntax',
() => {},
( argv ) => {
let input = '';
if ( argv.stdin ) {
input = fs.readFileSync( 0, 'utf-8' );
} else {
input = argv['query'];
}
if ( ! input || input.length < 3 ) {
console.warn( 'You have to provide a query at least 3 chars long' );
return;
}
console.log( "Logging in to your Roam and running query:" );
console.log( input );
const RoamPrivateApi = require( '../' );
const api = new RoamPrivateApi( argv.graph, argv.email, argv.password, {
headless: ! argv.debug,
} );
api.logIn()
.then( () => api.runQuery( input ) )
.then( result => {
console.log( JSON.stringify( result, null, 4 ) );
api.close();
} );
}
)
.command(
'search <query>',
'Query your Roam Graph blocks using simple text search.',
() => {},
( argv ) => {
const RoamPrivateApi = require( '../' );
const api = new RoamPrivateApi( argv.graph, argv.email, argv.password, {
headless: ! argv.debug,
} );
api.logIn()
.then( () => api.runQuery( api.getQueryToFindBlocks( argv['query'] ) ) )
.then( result => {
result = result.map( result => ( {
blockUid: result[0],
pageTitle: result[2],
string: result[1]
} ) );
console.log( JSON.stringify( result, null, 4 ) );
api.close();
} );
}
)
.command(
'create [text] [parentuid]',
'Append a block to a block with a selected uid. If no uid is provided, block will be appended to the daily page. You can also pass data from stdin.',
() => {},
( argv ) => {
let input = '';
if ( argv.stdin ) {
input = fs.readFileSync( 0, 'utf-8' );
} else {
input = argv['text'];
}
if ( ! input || input.length < 3 ) {
console.warn( 'You have to provide content at least 3 chars long' );
return;
}
const RoamPrivateApi = require( '../' );
const api = new RoamPrivateApi( argv.graph, argv.email, argv.password, {
headless: ! argv.debug,
} );
if ( ! argv['parentuid'] ) {
argv['parentuid'] = api.dailyNoteUid();
}
api.logIn()
.then( () => api.createBlock( input, argv['parentuid'] ) )
.then( result => api.close() );
}
)
.command(
'export <dir> [exporturl]',
'Export your Roam database to a selected directory. If URL is provided, then the concent will be sent by POST request to the specified URL.',
() => {},
( argv ) => {
const RoamPrivateApi = require( '../' );
const api = new RoamPrivateApi( argv.graph, argv.email, argv.password, {
headless: ! argv.debug,
folder: argv['dir']
} );
let promises = api.getExportData( argv['removezip'] );
promises.then( data => console.log( 'Downloaded' ) );
if ( argv['exporturl'] ) {
promises.then( data => fetch( argv['exporturl'], {
method: 'post',
body: JSON.stringify( {
graphContent: data,
graphName: api.db
} ),
headers: {'Content-Type': 'application/json'}
} ) )
.catch( err => console.log( err ) )
.then( () => console.log( "Uploaded to export url." ) )
}
}
)
.help()
.alias( 'help', 'h' )
.env( 'ROAM_API' )
.demandOption(
[ 'graph', 'email', 'password' ],
'You need to provide graph name, email and password'
).argv;