UNPKG

8.33 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3
4
5'use strict'
6
7if (process.argv.length <= 2) {
8 throw new Error('Give config Yaml file as 1st parameter!')
9}
10
11
12
13const async = require('async')
14const fs = require('fs-extra')
15const klaw = require('klaw')
16const path = require('path')
17
18const renderer = require('./renderer.js')
19const render = new renderer(process.argv[2])
20const startDate = new Date()
21
22
23
24var sourcePugFiles = []
25var sourceStylusFiles = []
26var sourceJsFiles = []
27
28
29
30// const theEnd = () => {
31// let endDate = new Date()
32// let duration = (endDate.getTime() - startDate.getTime()) / 1000
33// let files = []
34// let filesForDelete = []
35// let filesDeleted = 0
36//
37// for (let i = 0; i < buildFiles.length; i++) {
38// if (files.indexOf(buildFiles[i].path.replace(/\\/g, '/')) === -1) {
39// files.push(buildFiles[i].path.replace(/\\/g, '/'))
40// }
41// }
42//
43// console.log(files.length + ' files created - ' + duration.toFixed(2) + 's - ' + (files.length / duration).toFixed(2) + 'fps')
44//
45// if (process.argv[3] === 'cleanup') {
46// buildErrors = buildErrors + sourceFiles.length
47// if (buildErrors === 0) {
48//
49// klaw(appConf.build).on('data', (item) => {
50// if (files.indexOf(item.path.replace(/\\/g, '/')) === -1 && item.path.replace(/\\/g, '/') !== appConf.build.replace(/\\/g, '/')) {
51// filesForDelete.push(item.path)
52// }
53// }).on('end', function () {
54// filesForDelete.reverse()
55// for (var i = 0; i < filesForDelete.length; i++) {
56// let file = filesForDelete[i]
57// let protect = false
58//
59// for (let i = 0; i < appConf.protectedFromCleanup.length; i++) {
60// if (appConf.protectedFromCleanup[i] && file.startsWith(path.join(appConf.build, appConf.protectedFromCleanup[i]))) {
61// protect = true
62// break
63// }
64// }
65//
66// if (!protect) {
67// try {
68// if (fs.lstatSync(file).isFile()) {
69// fs.unlinkSync(file)
70// filesDeleted++
71// console.log('DELETED: ' + file)
72// } else if (fs.lstatSync(file).isDirectory() && fs.readdirSync(file).length === 0) {
73// fs.rmdirSync(file)
74// filesDeleted++
75// console.log('DELETED: ' + file)
76// }
77// } catch (e) {
78// console.log(e)
79// }
80// }
81//
82// }
83// console.log(filesDeleted + (filesDeleted > 1 ? ' files deleted' : ' file deleted'))
84// })
85// } else {
86// console.log('No files deleted because ' + buildErrors + (buildErrors === 0 || buildErrors > 1 ? ' errors' : ' error'))
87// }
88// }
89// }
90
91
92
93var puildPugPaths = []
94var ignorePuildPugPaths = false
95var buildPug = false
96var buildStyle = false
97var buildJs = false
98
99try {
100 const lastBuild = JSON.parse(fs.readFileSync(path.join(render.buildDir, 'build.json'), 'utf8'))
101
102 if (lastBuild.commit) {
103 const gitPath = require('child_process').execSync(`git -C "${render.sourceDir}" rev-parse --show-toplevel`).toString().trim()
104 const changedFiles = require('child_process').execSync(`git -C "${render.sourceDir}" diff --name-only ${lastBuild.commit}`).toString().trim().split('\n')
105 const addedFiles = require('child_process').execSync(`git -C "${render.sourceDir}" ls-files -o --exclude-standard --full-name`).toString().trim().split('\n')
106
107 changedFiles.concat(addedFiles).forEach(f => {
108 let file = path.join(gitPath, f)
109
110 if (!f || !file.startsWith(render.sourceDir)) { return }
111
112 if (file.endsWith('.pug') || file.endsWith('.yaml')) {
113 let pathname = path.dirname(path.join(gitPath, f))
114 if (puildPugPaths.indexOf(pathname) === -1) {
115 puildPugPaths.push(path.dirname(file))
116 }
117 buildPug = true
118 }
119
120 if (file.endsWith('.styl')) {
121 buildStyle = true
122 }
123
124 if (file.endsWith('.js')) {
125 buildJs = true
126 }
127 })
128 }
129} catch (e) {
130 console.error(e.message)
131
132 ignorePuildPugPaths = true
133 buildPug = true
134 buildStyle = true
135 buildJs = true
136}
137
138
139
140klaw(render.sourceDir).on('data', (item) => {
141 if (!fs.lstatSync(item.path).isFile()) { return }
142
143 const dirName = path.dirname(item.path)
144 const fileName = path.basename(item.path)
145
146 if (fileName.startsWith('_')) { return }
147 if (!fileName.endsWith('.pug') && !fileName.endsWith('.js') && !fileName.endsWith('.styl')) { return }
148
149 if (render.paths.length) {
150 var ignore = true
151 for (var i = 0; i < render.paths.length; i++) {
152 if (render.paths[i] && item.path.startsWith(path.join(render.sourceDir, render.paths[i]))) {
153 ignore = false
154 break
155 }
156 }
157 if (ignore) { return }
158 }
159
160 if (buildPug && fileName.startsWith('index.') && fileName.endsWith('.pug') && sourcePugFiles.indexOf(dirName) === -1 && (ignorePuildPugPaths || puildPugPaths.indexOf(dirName) !== -1)) {
161 sourcePugFiles.push(dirName)
162 }
163 if (buildJs && !fileName.startsWith('_') && fileName.endsWith('.js') && sourceJsFiles.indexOf(dirName) === -1) {
164 sourceJsFiles.push(item.path)
165 }
166 if (buildStyle && !fileName.startsWith('_') && fileName.endsWith('.styl') && sourceStylusFiles.indexOf(dirName) === -1) {
167 sourceStylusFiles.push(item.path)
168 }
169}).on('end', function () {
170 sourcePugFiles.sort()
171
172 console.log(sourcePugFiles.length + ' .pug files to render')
173 console.log(sourceJsFiles.length + ' .js files to render')
174 console.log(sourceStylusFiles.length + ' .styl files to render')
175
176 async.parallel({
177 html: (callback) => {
178 let buildFiles = []
179 async.each(sourcePugFiles, (source, callback) => {
180 render.makeHTML(source, (err, files) => {
181 if (files && files.length) { buildFiles = buildFiles.concat(files) }
182
183 callback(null)
184 })
185 }, (err) => {
186 const duration = ((new Date()).getTime() - startDate.getTime()) / 1000
187 console.log(`${buildFiles.length} .html files created - ${duration.toFixed(2)}s - ${(buildFiles.length / duration).toFixed(2)}fps`)
188
189 callback(null, buildFiles)
190 })
191 },
192 css: (callback) => {
193 render.makeCSS(sourceStylusFiles, (err, files) => {
194 if (err) {
195 console.log(err.message)
196 } else {
197 const duration = ((new Date()).getTime() - startDate.getTime()) / 1000
198 console.log(`${files.length} .css files created - ${duration.toFixed(2)}s - ${(files.length / duration).toFixed(2)}fps`)
199 }
200
201 callback(null, files || [])
202 })
203 },
204 js: (callback) => {
205 render.makeJS(sourceJsFiles, (err, files) => {
206 if (err) {
207 console.log(err.message)
208 } else {
209 const duration = ((new Date()).getTime() - startDate.getTime()) / 1000
210 console.log(`${files.length} .js files created - ${duration.toFixed(2)}s - ${(files.length / duration).toFixed(2)}fps`)
211 }
212
213 callback(null, files || [])
214 })
215 }
216 }, (err, build) => {
217 if (err) { console.log(err) }
218
219 const state = {
220 time: startDate.getTime(),
221 commit: require('child_process').execSync(`git -C "${render.sourceDir}" rev-parse HEAD`).toString().trim(),
222 ms: (new Date()).getTime() - startDate.getTime()
223 }
224
225 fs.outputFileSync(path.join(render.buildDir, 'build.json'), JSON.stringify(state))
226 })
227})