1 | const path = require('path')
|
2 | const fse = require('fs-extra')
|
3 | const chalk = require('chalk')
|
4 |
|
5 | function addPages({ startAt, pages, mode }) {
|
6 | let htmlExt = mode.HTML || 'html'
|
7 | let cssExt = mode.CSS || 'css'
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | let bodyTemplate = ''
|
13 |
|
14 | fse.readFile(path.join('.', 'templates', `body.${htmlExt}`), { encoding: 'utf-8' })
|
15 | .then(content => {
|
16 | process.stdout.write(chalk.yellow(`Generating [ ${chalk.magenta(pages)} ] blank page(s)… :`))
|
17 |
|
18 | bodyTemplate = content
|
19 |
|
20 |
|
21 | let blankPages = []
|
22 |
|
23 | for (let i = startAt, endAt = startAt + pages; i < endAt; i++) {
|
24 | let pageDir = path.join('.', 'manuscript', `page-${i}`)
|
25 |
|
26 | let thisDir = fse.ensureDir(pageDir)
|
27 |
|
28 | blankPages.push(thisDir)
|
29 | }
|
30 |
|
31 | return Promise.all(blankPages)
|
32 | }).then(() => {
|
33 | let pendingWrites = []
|
34 |
|
35 | for (let i = startAt, endAt = startAt + pages; i < endAt; i++) {
|
36 | let styleFile = path.join('.', 'manuscript', `page-${i}`, `style.${cssExt}`)
|
37 | let htmlFile = path.join('.', 'manuscript', `page-${i}`, `body.${htmlExt}`)
|
38 |
|
39 | let thisWrite = fse.writeFile(styleFile, '')
|
40 | let thatWrite = fse.writeFile(htmlFile, bodyTemplate)
|
41 |
|
42 | pendingWrites.push(thisWrite)
|
43 | pendingWrites.push(thatWrite)
|
44 | }
|
45 |
|
46 | return Promise.all(pendingWrites)
|
47 | }).then(() => {
|
48 | return process.stdout.write(`${chalk.blue(' done.')}` + '\n')
|
49 | }).catch((err) => {
|
50 | if (err) return console.log('Couldn\'t create pages', err)
|
51 | throw err
|
52 | })
|
53 | }
|
54 |
|
55 | module.exports.addPages = addPages |
\ | No newline at end of file |