-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·129 lines (116 loc) · 3.58 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
#!/usr/bin/env node
'use strict';
/* Config Options */
const authKey = 'Bearer YOURSUPERLONGTOKENHERE';
const WordPressComUrl = 'YOURAWESOMESITE.wordpress.com';
/* End Config */
const apiPath = 'https://public-api.wordpress.com/wp/v2/sites/' + WordPressComUrl;
// Program Modules
const program = require( 'commander' );
const co = require( 'co' );
const prompt = require( 'co-prompt' );
const request = require( 'superagent' );
const chalk = require( 'chalk' );
const ent = require( 'ent' );
const Spinner = require('cli-spinner').Spinner;
const spinner = new Spinner();
spinner.setSpinnerString( 26 );
// Spit out an error message and exit
const errorAndExit = ( error ) => {
console.error( chalk.red( error ) );
process.exit( 1 );
}
// Pesky HTML entities don't look good on the cmd line
const decodeTitle = ( title ) => {
return ent.decode( title );
}
// Add a new todo item ( post )
program
.description( 'create a new todo item' )
.command( 'add' )
.action( () => {
co( function* () {
const todo = yield prompt( 'What do you need todo? ' );
spinner.start();
// Send the request to WP REST API
request
.post( apiPath + '/posts' )
.send( { title: todo, status: 'publish' } )
.set( 'Authorization', authKey )
.end( ( error, result ) => {
spinner.stop( true );
if ( error ) {
errorAndExit( error )
}
const post = result.body;
const title = decodeTitle( post.title.raw );
console.log( chalk.bold.yellow( '\n\ncreated todo #' + post.id + ': ' + title + '\n\n' ) );
process.exit( 1 );
} );
} )
} );
// Mark todo item as done ( trash post )
program
.description( 'mark an item as done' )
.command( 'done [todoNumber]' )
.action( ( todoNumber ) => {
spinner.start();
// Make sure we have a number
if ( ! isNaN( parseInt( todoNumber ) ) ) {
// Send the DELETE request to WP REST API
request
.delete( apiPath + '/posts/' + todoNumber )
.set( 'Authorization', authKey )
.end( ( error, result ) => {
spinner.stop( true );
if ( error ) {
const message = error.status === 404 ? 'Invalid Todo Number, try `wptodo list` to get a valid number.' : error;
errorAndExit( message );
}
const post = result.body;
const title = decodeTitle( post.title.raw );
console.log( chalk.bold.green( '\n\nDONE! #' + post.id + ': ' + title + '\n\n' ) );
process.exit( 1 );
} );
} else {
errorAndExit( 'wat? that wasn\'t a number!' );
}
} );
// List all todos ( post titles )
program
.description( 'list todo items' )
.command( 'list' )
.action( () => {
// Send the request to WP REST API
spinner.start();
// Request posts from our site, if we have so many todos
// that we need pagination, well that is just unfortunate for us
// Using auth key here for edit context, and our site is private
request
.get( apiPath + '/posts?context=edit' )
.set( 'Authorization', authKey )
.end( ( error, result ) => {
spinner.stop( true );
if ( error ) {
errorAndExit( error )
}
// If we have posts, list them
if ( result.body.length ) {
console.log( chalk.bold.yellow( '\n\nYour Todo Items:' ) );
result.body.forEach( ( post ) => {
const { id, title } = post;
const decodedTitle = decodeTitle( title.raw );
console.log( chalk.yellow( '#' + id + ' - ' + decodedTitle ) );
} );
console.log( '\n' );
} else {
// Otherwise show a celebratory message
console.log( chalk.bold.green( '\n\nOMG NO TODOS!\n\n' ) );
}
process.exit( 1 );
} );
} );
program.parse( process.argv );
if ( program.args.length === 0 ) {
program.help();
}