1 | #!/usr/bin/env node
|
2 |
|
3 |
|
4 |
|
5 | 'use strict'
|
6 |
|
7 | if (process.argv.length <= 2) {
|
8 | throw new Error('Give config Yaml file as 1st parameter!')
|
9 | }
|
10 |
|
11 |
|
12 |
|
13 | const async = require('async')
|
14 | const fs = require('fs-extra')
|
15 | const klaw = require('klaw')
|
16 | const path = require('path')
|
17 |
|
18 | const renderer = require('./renderer.js')
|
19 | const render = new renderer(process.argv[2])
|
20 | const startDate = new Date()
|
21 |
|
22 |
|
23 |
|
24 | var sourcePugFiles = []
|
25 | var sourceStylusFiles = []
|
26 | var sourceJsFiles = []
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | var puildPugPaths = []
|
94 | var ignorePuildPugPaths = false
|
95 | var buildPug = false
|
96 | var buildStyle = false
|
97 | var buildJs = false
|
98 |
|
99 | try {
|
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 |
|
140 | klaw(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 | })
|