-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·90 lines (73 loc) · 1.73 KB
/
index.js
File metadata and controls
executable file
·90 lines (73 loc) · 1.73 KB
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
#!/usr/bin/env node
const path = require('path');
const minimist = require('minimist');
const homedir = require('homedir')();
const leanpub = require('leanpub');
try {
var apiKey;
try {
apiKey = require(path.join(homedir, '.leanpub.json')).apiKey;
} catch(err) {
throw new Error('Failed to find ~/.leanpub.json apiKey field');
}
if (!apiKey) {
throw new Error('No apiKey');
}
const argv = minimist(process.argv.slice(2));
main(argv._[0], argv._[1], apiKey);
} catch(e) {
console.error(e);
}
function main(target, bookSlug, apiKey) {
const targets = {
'job-status': jobStatus,
'preview': preview,
'publish-silent': publishSilent
};
if (!(target in targets)) {
throw new Error(`No matching target. Try one of ${Object.keys(targets).join(', ')}.`);
}
if (!bookSlug) {
throw new Error('Missing target book after target');
}
targets[target](
leanpub({ apiKey, bookSlug })
);
}
function jobStatus(client) {
var previousMessage;
// Poll for job status now until done
pollJobStatus(1000);
function pollJobStatus(delay) {
setTimeout(function() {
client.jobStatus(function(err, d) {
if(err) {
return console.error(err);
}
if (d.status) {
if(previousMessage !== d.message) {
console.log(d.message);
previousMessage = d.message;
}
pollJobStatus(delay);
} else {
console.log('Done');
}
});
}, delay);
}
}
function preview(client) {
client.previewFull(function(err) {
if(err) {
return console.error(err);
}
});
}
function publishSilent(client) {
client.publish(function(err) {
if(err) {
return console.error(err);
}
});
}