-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·205 lines (175 loc) · 6.87 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
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
#! /usr/bin/env node
var fs = require('fs');
var repl = require('repl');
var replHistory = require('repl.history');
var util = require('util');
var path = require('path');
var osHomedir = require('os-homedir');
var qs = require('query-string');
var JSON5 = require('json5');
var _ = require('lodash');
var chalk = require('chalk');
var argv = require('minimist')(process.argv.slice(2));
var tumblr = require('tumblr.js');
// Style output with ANSI color codes
function print(object) {
console.log(util.inspect(object, null, null, true));
}
// Load credentials
var credentials = (function() {
var credsFile = argv.credentials || _.find([
'tumblr-credentials.json',
'credentials.json',
path.join(osHomedir(), 'tumblr-credentials.json'),
], function(credsFile) {
return fs.existsSync(credsFile);
}) || 'credentials.json';
try {
var credentials = JSON5.parse(fs.readFileSync(credsFile).toString());
} catch (e) {
console.error(chalk.red('Error loading credentials!'));
console.error('Make sure %s exists or specify %s', chalk.cyan(credsFile || 'credentials.json'), chalk.cyan('--credentials=path/to/credentials.json'));
process.exit();
}
console.log('\nUsing OAuth creds from %s\n', chalk.magenta(path.resolve(credsFile)));
var missingCredentials = _.remove(['consumer_key', 'consumer_secret', 'token', 'token_secret'], _.partial(_.negate(_.has), credentials));
if (!_.isEmpty(missingCredentials)) {
console.warn(chalk.yellow('Credentials is missing keys:'));
missingCredentials.forEach(function(key) {
console.warn(chalk.yellow(' * %s'), key);
});
if (credentials.consumer_key && credentials.consumer_secret) {
console.log('\nYou can generate user tokens by going to the API console:\n');
console.log(chalk.magenta([
'https://api.tumblr.com/console/auth',
'?consumer_key=', credentials.consumer_key,
'&consumer_secret=', credentials.consumer_secret,
].join('')));
}
}
return credentials;
})();
// Print intro message
(function printIntro() {
var ascii = fs.readFileSync(path.join(__dirname, 'ascii.md')).toString();
// Logo: Blue
ascii = ascii.replace(/```\n?([\s\S]*?)```\n?/m, chalk.blue('$1'));
// Version numbers: White
ascii = ascii.replace(/^(.* |)(tumblr-repl v)(0\.0\.0)(.*)/m, chalk.white('$1$2' + require('./package.json').version, '$4'));
ascii = ascii.replace(/^(.* |)(tumblr.js v)(0\.0\.0)(.*)/m, chalk.white('$1$2' + require('tumblr.js/package.json').version, '$4'));
// Indented text: Gray
ascii = ascii.replace(/^(\s{4}\S.*)/gm, chalk.gray('$1'));
// Highlights: Green
ascii = ascii.replace(/(^|\s)`([^`]+)`(\s|$)/g, chalk.green('$1$2$3'));
console.log(ascii);
})();
// Start REPL server
var server = repl.start({
prompt: argv.prompt || 'tumblr.js > ',
source: null,
eval: null,
useGlobal: false,
useColors: true,
ignoreUndefined: true,
});
// Save REPL history
replHistory(server, path.join(osHomedir(), '.tumblrjs_repl_history'));
// Save the last response
var _resp;
// Create the Tumblr API Client
var client = (function createTumblrClient() {
var client = new tumblr.Client(credentials);
// Provide a default callback for inspection
var getRequest = client.getRequest;
client.getRequest = function(apiPath, params, callback) {
if (callback) {
return getRequest.call(this, apiPath, params, callback);
}
var queryString = qs.stringify(params);
var requestMessage = 'GET ' + apiPath + (queryString ? '?' + queryString : '');
getRequest.call(this, apiPath, params, function(err, resp) {
_resp = err || resp;
if (err) {
console.error('\n', chalk.red(requestMessage));
console.error(chalk.red(err));
} else {
console.log('\n', chalk.green(requestMessage));
print(resp, null, null, true);
}
server.displayPrompt(true);
});
};
var postRequest = client.postRequest;
client.postRequest = function(apiPath, params, callback) {
if (callback) {
return postRequest.call(this, apiPath, params, callback);
}
var requestMessage = 'POST ' + apiPath;
postRequest.call(this, apiPath, params, function(err, resp) {
_resp = err || resp;
if (err) {
console.error('\n', chalk.red(requestMessage));
console.error(chalk.red(err));
} else {
console.log('\n', chalk.green(requestMessage));
print(resp, null, null, true);
}
server.displayPrompt(true);
});
};
return client;
})();
// Set REPL context variables
(function(context) {
context.lodash = _;
context.tumblr = client;
context.print = print;
// Callback function that can be used to store an API response object in the REPL context
context.set = function(err, object) {
if (err) {
return console.log(err);
}
context.result = object;
print(object);
console.log('Stored in variable: \'result\'');
};
Object.defineProperty(context, '_response', {
get: function() {
return _resp;
},
});
Object.defineProperty(context, 'help', {
get: function() {
console.log('\n%s has the following methods:\n', chalk.blue('tumblr'));
Object.keys(client).forEach(function(method) {
if (method === 'postRequest' || method === 'getRequest' || typeof client[method] !== 'function') {
return;
}
if (typeof client[method] === 'function') {
var methodName = chalk.cyan(method);
var methodArgs = (client[method].toString().match(/\([^\)]*\)/) || [])[0]
.replace(/[\s\(\)]/gm, '')
.split(',')
.map(function(arg) {
if (arg === 'params' || arg === 'callback') {
return chalk.yellow(arg);
} else {
return chalk.green(arg);
}
})
.join(', ');
console.log(' %s(%s)', methodName, methodArgs);
}
});
console.log('');
console.log('%s stores the response from the last API request response.', chalk.magenta('_response'));
console.log('');
},
});
})(server.context);
// Control + L should clear the REPL
process.stdin.on('keypress', function(s, key) {
if (key && key.ctrl && key.name == 'l') {
process.stdout.write('\u001B[2J\u001B[0;0f');
}
});