UNPKG

1.96 kBJavaScriptView Raw
1const path = require('path')
2const fse = require('fs-extra')
3const chalk = require('chalk')
4
5function addPages({ startAt, pages, mode }) {
6 let htmlExt = mode.HTML || 'html'
7 let cssExt = mode.CSS || 'css'
8
9 // let headExt = mode.HEAD || 'html'
10 // let javascriptExt = mode.JS || 'js'
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 // Set bodyTemplate for next promise. Ugly :(
19
20 // Blank manuscript structure:
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) // promise { pending }
27
28 blankPages.push(thisDir)
29 }
30
31 return Promise.all(blankPages)
32 }).then(() => {
33 let pendingWrites = [] // TODO: Consider fse.copy instead of IO per page.
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
55module.exports.addPages = addPages
\No newline at end of file