UNPKG

1.67 kBJavaScriptView Raw
1function initialize (projectname, options) {
2 'use strong'
3
4 const co = require('co')
5 const prompt = require('co-prompt')
6 const check = require('check-types')
7 const chalk = require('chalk')
8 const path = require('path')
9 const isSafePositiveInteger = require('is-positive-integer').isSafePositiveInteger
10
11 // maxSafeInteger for older node engines
12 // const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991
13
14 // Restrict length of book to 2000 pages.
15 const LEAF_LIMIT = 500
16
17 // Generator
18 co(function * () {
19 let leafs = options.leafs
20
21 if (!isSafePositiveInteger(parseInt(leafs))) {
22 leafs = yield prompt('Number of leafs: ')
23
24 if (!isSafePositiveInteger(parseInt(leafs))) {
25 leafs = 2
26 }
27 }
28
29 if (check.greater(parseInt(leafs), LEAF_LIMIT)) {
30 leafs = 1000 // Restrict booklength and warn user.
31 console.log(chalk.yellow('Restricting book length to 1000 leafs (or 2000 pages).'))
32 }
33
34 return leafs
35 }).then(leafs => {
36 let template = options.template
37
38 if (template === undefined || check.not.contains(['comics', 'magazine', 'novel', 'text', 'super', 'photo'], template)) {
39 template = 'blank'
40 }
41
42 console.log(`${`${chalk.yellow('Initializing… ')} [ manuscript=${chalk.magenta(`${projectname}`)}`} | leafs=${chalk.magenta(`${leafs}`)} | template=${chalk.magenta(`${template}`)} ]`)
43
44 const project = require(path.join('..', 'scripts', 'createProject.js'))
45
46 project.create(projectname, leafs, template)
47 }).catch((err) => {
48 if (err) return console.error(chalk.bold.red('Failed to initialize new project', err))
49 })
50}
51
52module.exports.initialize = initialize