UNPKG

6.39 kBJavaScriptView Raw
1#! /usr/bin/env node
2
3const program = require('commander')
4const chalk = require('chalk')
5const fs = require('fs')
6const path = require('path')
7
8program
9 .command('new <projectname>')
10 .alias('n')
11 .description('New book (Setup manuscript)')
12 .option('-l, --leafs <number_of_leafs>', 'tentative number of leafs')
13 .option('-t, --template <template>', 'Use: [comics, magazine, novel, text, super]') // TODO: 1. fetch() templates. 2. Implement super template with first five pages
14 .action((projectname, options) => {
15 const project = require(path.join('..', 'lib', 'new.js'))
16
17 project.initialize(projectname, options)
18 }).on('--help', () => {
19 console.log(' Examples:')
20 console.log()
21 console.log(' $ bookiza new sun-and-sand -l 6')
22 console.log(' $ b new my-live-book --leafs 15')
23 console.log(chalk.bold.bgGreen(' $ b n wuthering-heights -l 100 -t novel'))
24 console.log()
25 })
26
27// Add leaf(ves)
28program
29 .command('add')
30 .alias('a')
31 .description('Add leaf(s) to the stack (End of book).')
32 .option('-l, --leafs <number_of_leafs>', 'Number of leafs')
33 .action(options => {
34 const manuscript = require(path.join('..', 'lib', 'add.js'))
35 manuscript.addLeafs(options)
36 }).on('--help', () => {
37 console.log(' Examples:')
38 console.log()
39 console.log(' $ bookiza add -l 5')
40 console.log(' $ b a -l 5')
41 console.log()
42 })
43
44// Insert leaf
45program
46 .command('insert <insertAt>')
47 .alias('i')
48 .description('Insert leaf(s) into the stack (In between book)')
49 .option('-l, --leafs <number_of_leafs>', 'Number of leafs')
50 .action((insertAt, options) => {
51 const manuscript = require(path.join('..', 'lib', 'insert.js'))
52 manuscript.insertLeafs(insertAt, options)
53 }).on('--help', () => {
54 console.log(' Examples:')
55 console.log()
56 console.log(' $ bookiza insert 15 -l 4')
57 console.log(' $ b i 24 --leafs 2')
58 console.log()
59 })
60
61// Remove page
62program
63 .command('remove <removeAt>')
64 .alias('r')
65 .description('Remove page (not leaf!) and append a blank one.')
66 .action((removeAt, options) => {
67 const manuscript = require(path.join('..', 'lib', 'remove.js'))
68 manuscript.removePage(removeAt)
69 }).on('--help', () => {
70 console.log(' Examples:')
71 console.log()
72 console.log(' $ bookiza remove 9')
73 console.log(' $ b r 9')
74 console.log()
75 })
76
77// Move page
78program
79 .command('move <moveFrom> <moveTo>')
80 .alias('m')
81 .description('Move a page (not leaf!) from A to B.')
82 .action((moveFrom, moveTo, options) => {
83 const manuscript = require(path.join('..', 'lib', 'move.js'))
84 manuscript.movePage(moveFrom, moveTo)
85 }).on('--help', () => {
86 console.log(' Examples:')
87 console.log()
88 console.log(' $ bookiza move 9 17')
89 console.log(' $ b m 9 17')
90 console.log()
91 })
92
93
94// Clip leaves (End of book)
95program
96 .command('clip')
97 .alias('c')
98 .description('Clip leaf(s) off the stack (End of book).')
99 .option('-l, --leafs <number_of_leafs>', 'Number of leafs')
100 .action(options => {
101 const manuscript = require(path.join('..', 'lib', 'clip.js'))
102 manuscript.clip(options)
103 }).on('--help', () => {
104 console.log(' Examples:')
105 console.log()
106 console.log(' $ bookiza clip -l 2')
107 console.log(' $ b c -l 2')
108 console.log()
109 })
110
111program
112 .command('length')
113 .alias('l')
114 .description('Book length')
115 .action(() => {
116 try {
117 const book = require('book-length')
118 console.log(book.length())
119 } catch (ex) {
120 console.error(ex.stack)
121 }
122 }).on('--help', () => {
123 console.log(' Examples:')
124 console.log()
125 console.log(' $ bookiza length')
126 console.log(' $ bookiza l')
127 console.log(' $ b l')
128 console.log()
129 })
130
131program
132 .command('publish')
133 .alias('p')
134 .option('-t, --token <api_token>', 'Author api_token')
135 .description('Publish book')
136 .action(options => {
137 const book = require(path.join('..', 'lib', 'publish.js'))
138 book.publish(options)
139 }).on('--help', () => {
140 console.log(' Examples:')
141 console.log()
142 console.log(' $ bookiza publish')
143 console.log(' $ b p')
144 console.log()
145 })
146
147// Render pages w/o starting the server.
148program
149 .command('render')
150 .alias('x')
151 .description('Build manuscript')
152 .action(options => {
153 const manuscript = require(path.join('..', 'lib', 'render.js'))
154 manuscript.render()
155 }).on('--help', () => {
156 console.log(' Examples:')
157 console.log()
158 console.log(' $ bookiza render')
159 console.log(' $ b x')
160 console.log()
161 })
162
163
164// Render pages and start local server
165program
166 .command('server')
167 .alias('s')
168 .option('-p, --port <port>', 'Optional port number')
169 .description('Start server')
170 .action(options => {
171 const server = require(path.join('..', 'lib', 'server.js'))
172
173 server.start(options)
174 }).on('--help', () => {
175 console.log(' Examples:')
176 console.log()
177 console.log(' $ bookiza server')
178 console.log(' $ b s -p 8080')
179 console.log()
180 })
181
182
183// Register Bookiza client with Bubblin
184program
185 .command('register')
186 .alias('z')
187 .description('Register bookiza')
188 .option('-u, --username <username>', 'Bubblin\'s username/email')
189 .option('-p, --password <password>', 'Bubblin\'s password')
190 .action(options => {
191 const client = require(path.join('..', 'lib', 'register.js'))
192 client.register(options)
193 }).on('--help', () => {
194 console.log(' Examples:')
195 console.log()
196 console.log(' $ bookiza register')
197 console.log(' $ b z')
198 console.log()
199 })
200
201// Registered as?
202program
203 .command('whoami')
204 .alias('w')
205 .description('Print whoami@bookizarc')
206 .action(options => {
207 const client = require(path.join('..', 'lib', 'whoami.js'))
208 client.print()
209 }).on('--help', () => {
210 console.log(' Examples:')
211 console.log()
212 console.log(' $ bookiza whoami')
213 console.log(' $ b w')
214 console.log()
215 })
216
217// Catchall
218program
219 .command('*')
220 .on('--help', () => {
221 console.log(' Examples:')
222 console.log()
223 console.log(' $ bookiza <cmd> [options]')
224 console.log()
225 })
226
227const packageJson = JSON.parse(fs.readFileSync(path.join(__dirname, '..', 'package.json')).toString())
228
229program
230 .version(packageJson.version)
231 .option('-v, --version', 'output the version number')
232 .parse(process.argv)
233
234if (!program.args.length) {
235 program.help()
236}