UNPKG

1.36 kBJavaScriptView Raw
1function clip (options) {
2 'use strong'
3
4 const book = require('book-length')
5 const co = require('co')
6 const prompt = require('co-prompt')
7 const path = require('path')
8 const chalk = require('chalk')
9
10 let leafs = options.leafs
11
12 co(function * () {
13 if (options !== undefined && options.leafs % 1 === 0) {
14 leafs = options.leafs
15 } else {
16 leafs = yield prompt('No. of leafs?: ')
17 }
18 return leafs
19 }).then((leafs) => {
20 const bookLength = book.length()
21 const fse = require('fs-extra')
22 const dateFormat = require('dateformat')
23 const now = new Date()
24 const timestamp = dateFormat(now, 'dddd-mmmm-dS-yyyy-hh-MM-ss-TT')
25
26 process.stdout.write(chalk.yellow(` Clipping [ ${chalk.magenta(leafs)} ] leaf(s) from the end… : `))
27
28 let promises = []
29
30 for (let pageIndex = bookLength, endIndex = bookLength - (parseInt(leafs) * 2); pageIndex > endIndex; pageIndex--) {
31 promises.push(
32 fse.move(path.join('__dirname', '..', 'manuscript', `page-${pageIndex}`), path.join('__dirname', '..', 'trash', `page-${pageIndex}-${timestamp}`))
33 )
34 }
35 return Promise.all(promises)
36 }).then((resolutions) => {
37 process.stdout.write(chalk.blue('done.') + '\n')
38 }).catch((err) => {
39 if (err) { return console.log(chalk.red('Clip execution failed', err)) }
40 })
41}
42
43module.exports.clip = clip