UNPKG

6.21 kBJavaScriptView Raw
1function createProject (projectname, leafs, templateName) {
2 const fse = require('fs-extra')
3 const chalk = require('chalk')
4
5 fse.ensureDir(projectname)
6 .then(() => {
7 const shell = require('shelljs')
8 shell.cd(projectname) // Duration of function. See shelljs documentation.
9 }).then(() => {
10 setUp(projectname, leafs, templateName)
11 }).catch((err) => {
12 if (err) { return console.log(chalk.bold.red('Failed to create project', err)) }
13 })
14}
15
16function setUp (projectname, leafs, templateName) {
17 const fse = require('fs-extra')
18 const path = require('path')
19 const chalk = require('chalk')
20
21 fse.ensureDir('assets')
22 .then(() => {
23 const make = require(path.join('..', 'helpers', 'makeDir.js'))
24 make.directories(['css', 'javascript', 'images'], 'assets')
25 make.directories(['trash', 'cover', 'build'])
26 }).catch(err => {
27 if (err) { return console.log(chalk.bold.red('Failed to create subdirectories…', err)) }
28 })
29
30 fse.outputFile(path.join('.', 'README.md'), projectname)
31 .then(() => {
32 console.log(chalk.yellow(`README initialization… :${chalk.blue('success.')}`))
33 }).catch((err) => {
34 if (err) { return console.error(chalk.red('README not initialized.'), err) }
35 })
36
37 fse.outputFile(path.join('.', '.gitignore'), 'node_modules\nbuild\n*/*.DS_Store')
38 .then(() => {
39 console.log(chalk.yellow(`Gitignoring… /build /node_modules :${chalk.blue('done.')}`))
40 }).catch((err) => {
41 if (err) { return console.error(chalk.red('.gitignore not initialized.'), err) }
42 })
43
44 fse.outputFile(path.join('.', 'license.md'), '')
45 .then(() => {
46 console.log(chalk.yellow(`License (blank) initialization:${chalk.blue('complete.')}`))
47 }).catch((err) => {
48 if (err) { return console.error(chalk.red('Licensing not initialized.'), err) }
49 })
50
51 let promises = []
52
53 promises.push(
54 fse.copy(path.join(__dirname, '..', 'templates', templateName), path.join('.', 'templates'))
55 .then(() => {
56 return console.log(chalk.yellow(`Applying a ${chalk.magenta(`${templateName}`)} layout… :${chalk.blue('success.')}`))
57 }).catch(err => {
58 if (err) { return console.error(chalk.red('Could\'nt copy templates folder', err)) }
59 })
60 )
61
62 promises.push(
63 fse.copy(path.join(__dirname, '..', 'crust'), path.join('.', 'crust'))
64 .then(() => {
65 console.log(chalk.yellow(`Mobilizing crust… :${chalk.blue('success.')}`))
66 }).catch((err) => {
67 if (err) { return console.error(chalk.red('Copying crust failed', err)) }
68 })
69 )
70
71 return Promise.all(promises)
72 .then(() => {
73 fse.move(path.join('.', 'crust', 'gulpfile.js'), path.join('.', 'gulpfile.js'))
74 .then(() => {
75 console.log(chalk.yellow(`Server setup… :${chalk.blue('completed.')}`))
76 }).catch((err) => {
77 if (err) { return console.error(chalk.red('Failed. Gulpfile unavailable.', err)) }
78 })
79 }).then(() => {
80 const osHomeDir = require('os').homedir()
81 const arc = require('arc-bookiza')
82 const location = path.join(osHomeDir, '.', '.bookizarc')
83
84 let packageJson = null
85 let promises = []
86
87 promises.push(arc.read(location))
88
89 promises.push(fse.readJson(path.join('.', 'crust', 'package.json')))
90
91 return Promise.all(promises)
92 .then((values) => {
93 let bookizArc = JSON.parse(values[0])
94 packageJson = values[1]
95
96 packageJson.name = projectname
97 packageJson.author = `${bookizArc.username} <${bookizArc.email}> (https://bubblin.io/${bookizArc.username})`
98 packageJson.homepage = `https://bubblin.io/${bookizArc.username}`
99 packageJson.description = `Superbook: ${projectname} by ${bookizArc.username}`
100
101 fse.outputFile(path.join('.', 'package.json'), JSON.stringify(packageJson, null, 2))
102 .then(() => {
103 console.log(chalk.yellow(`PackageJson configured… :${chalk.blue('preparing for installation.')}`))
104 }).catch((err) => {
105 if (err) return Error('Couldn\'t write package.json', err)
106 })
107
108 let bookrc = {}
109
110 bookrc.name = projectname
111
112 bookrc.type = templateName
113
114 bookrc.has_page_numbers = false
115
116 bookrc.cover = { 'punchline': '', 'toc': '', 'author_detail': '', 'colophon': '', 'synopsis': '' }
117
118 bookrc.status = 'draft'
119
120 bookrc.asset_url = '' // rawgit or cloudinary path
121
122 bookrc.book_url = '';
123
124 [ bookrc.mode = { 'HTML': 'html', 'CSS': 'css', 'JS': 'js', 'HEAD': 'html' }] = [ bookizArc.mode ]
125
126
127 return bookrc
128 }).then(bookrc => {
129 fse.outputFile(path.join('.', '.bookrc'), JSON.stringify(bookrc, null, 2))
130 .then(() => {
131 console.log(chalk.yellow(`Default bookrc values… :${chalk.blue('done.')}`))
132 })
133 .catch(err => {
134 if (err) return new Error('Couldn\'t write .bookrc', err)
135 })
136 return bookrc.mode
137 }).then(mode => {
138 fse.ensureDir('manuscript')
139 .then(() => {
140 const pulp = require(path.join('..', 'generators', 'addPages.js'))
141 let startAt = 1
142 let pages = leafs * 2
143
144 pulp.addPages({ startAt, pages, mode })
145
146 const install = require('spawn-npm-install')
147
148 install(Object.keys(packageJson.dependencies), { stdio: 'inherit' }, function (err) {
149 if (err) {
150 return console.error(chalk.red(`Could\n't install modules:\n${err.message}`))
151 } else {
152 console.log(chalk.yellow(`Installing npm modules… :${chalk.blue('successful.')}`))
153 }
154 })
155 }).catch((err) => {
156 if (err) { return console.log(chalk.bold.red('Failed to write directory', err)) }
157 })
158 }).catch((err) => {
159 if (err) { return console.log(chalk.red('Arcvalues & package json unavailable', err)) }
160 })
161 }).catch((err) => {
162 if (err) { return console.log(chalk.red('Moving crust or template failed:', err)) }
163 })
164}
165
166module.exports.create = createProject