1 | #!/usr/bin/env node
|
2 |
|
3 | 'use strict'
|
4 | const fs = require('fs')
|
5 | const path = require('path')
|
6 |
|
7 | const chokidar = require('chokidar')
|
8 |
|
9 | const Script = require('./lib/script')
|
10 |
|
11 | const { getLogger } = require('log4js')
|
12 | const logger = getLogger()
|
13 |
|
14 |
|
15 | const postcss = require('postcss')
|
16 | const precss = require('precss')
|
17 |
|
18 | const cssnano = require('cssnano')
|
19 | const autoprefixer = require('autoprefixer')
|
20 |
|
21 | const bodyHandle = require('./lib/page')
|
22 | const Cut = require('./lib/cut')
|
23 |
|
24 | const DELDIR = require('./lib/delDir')
|
25 |
|
26 |
|
27 | const express = require('express')
|
28 | const app = express()
|
29 |
|
30 | const wsServe = require('express-ws')(app)
|
31 |
|
32 |
|
33 |
|
34 | let version = ''
|
35 |
|
36 |
|
37 |
|
38 | logger.level = 'info'
|
39 |
|
40 |
|
41 | const runPath = process.cwd()
|
42 | let startTime = null
|
43 |
|
44 |
|
45 | let htmlTemple = ''
|
46 |
|
47 |
|
48 | if (!fs.readFileSync(path.join(runPath, 'ozzx.js'))) {
|
49 | logger.error('ozzx.js file does not exist!')
|
50 | close()
|
51 | }
|
52 |
|
53 |
|
54 | const config = eval(fs.readFileSync(path.join(runPath, 'ozzx.js'), 'utf8'))
|
55 |
|
56 |
|
57 | const outPutPath = path.join(runPath, config.outFolder)
|
58 | const corePath = path.join(__dirname, 'core')
|
59 |
|
60 |
|
61 | function loadFile(path) {
|
62 | if (fs.existsSync(path)) {
|
63 | return fs.readFileSync(path, 'utf8')
|
64 | } else {
|
65 | logger.error(`file does not exist: ${path}`)
|
66 | return ''
|
67 | }
|
68 | }
|
69 |
|
70 |
|
71 | function handleStyle(dom, changePath) {
|
72 | let styleData = ''
|
73 |
|
74 | const versionString = config.outPut.outFileAddVersion ? `.${version}` : ''
|
75 | let outPutCss = dom.style
|
76 |
|
77 | if (config.outPut.globalStyle) {
|
78 | const mainStylePath = path.join(runPath, config.outPut.globalStyle)
|
79 | if (fs.existsSync(mainStylePath)) {
|
80 | const mainStyle = fs.readFileSync(path.join(runPath, config.outPut.globalStyle), 'utf8') + '\r\n'
|
81 |
|
82 | outPutCss = mainStyle + outPutCss
|
83 | } else {
|
84 | logger.error(`globalStyle file not find!`)
|
85 | }
|
86 | }
|
87 |
|
88 |
|
89 |
|
90 | if (config.outPut.choiceAnimation) {
|
91 | logger.debug('用户设置加载全部动画效果!')
|
92 |
|
93 | const animationFilePath = path.join(corePath, 'animation', `animations.css`)
|
94 | outPutCss += loadFile(animationFilePath)
|
95 | } else {
|
96 | const useAnimationList = config.outPut.useAnimationList || dom.useAnimationList
|
97 | useAnimationList.forEach(animationName => {
|
98 | const animationFilePath = path.join(corePath, 'animation', `${animationName}.css`)
|
99 | outPutCss += loadFile(animationFilePath)
|
100 | })
|
101 | }
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | let plugList = [precss, autoprefixer]
|
107 |
|
108 | if (config.minifyCss) {
|
109 | plugList.push(cssnano)
|
110 | }
|
111 | postcss(plugList).process(outPutCss, { from: undefined, cascade: true }).then( (result) => {
|
112 | const styleDir = path.join(outPutPath, 'css')
|
113 | result.warnings().forEach((warn) => {
|
114 | console.warn(warn.toString());
|
115 | })
|
116 |
|
117 | dom.style = result.css
|
118 |
|
119 | if (!changePath) {
|
120 | DELDIR(styleDir)
|
121 | logger.debug(`delete css dir success!`)
|
122 |
|
123 | fs.mkdirSync(styleDir)
|
124 | }
|
125 |
|
126 | styleData += `<link rel="stylesheet" href="./css/main${versionString}.css">`
|
127 |
|
128 | fs.writeFileSync(path.join(outPutPath, 'css', `main${versionString}.css`), dom.style)
|
129 |
|
130 |
|
131 | let completeNum = 0
|
132 |
|
133 |
|
134 | if (!config.styleList || config.styleList.length === 0) {
|
135 | htmlTemple = htmlTemple.replace(`<!-- css-output -->`, styleData)
|
136 | outPutHtml()
|
137 | return
|
138 | }
|
139 | for (let ind = 0; ind < config.styleList.length; ind++) {
|
140 | const element = config.styleList[ind]
|
141 |
|
142 | if (!element.src) {
|
143 | console.error('style path unset!', element)
|
144 | continue
|
145 | }
|
146 |
|
147 |
|
148 | if (element.src.startsWith('http')) {
|
149 | styleData += `\r\n <link rel="stylesheet" href="${element.src}">`
|
150 | if (++completeNum >= config.styleList.length) {
|
151 | htmlTemple = htmlTemple.replace(`<!-- css-output -->`, styleData)
|
152 | outPutHtml()
|
153 | }
|
154 |
|
155 | continue
|
156 | } else {
|
157 | styleData += `\r\n <link rel="stylesheet" href="./css/${element.name}.css">`
|
158 | }
|
159 |
|
160 | const outPutFile = path.join(outPutPath, 'css', `${element.name}.css`)
|
161 | if (changePath === undefined || changePath === path.join(runPath, element.src)) {
|
162 | moveFile(path.join(runPath, element.src), outPutFile)
|
163 | }
|
164 | if (++completeNum >= config.styleList.length) {
|
165 | htmlTemple = htmlTemple.replace(`<!-- css-output -->`, styleData)
|
166 | outPutHtml()
|
167 | }
|
168 | }
|
169 | })
|
170 | }
|
171 |
|
172 |
|
173 | function handleHrard(headList) {
|
174 |
|
175 | let heardData = '<!-- 页面的元信息 -->'
|
176 | headList.forEach(element => {
|
177 | let heard = `\r\n <meta`
|
178 | for (const key in element) {
|
179 | const value = element[key]
|
180 | heard += ` ${key}="${value}"`
|
181 | }
|
182 | heard += `/>`
|
183 | heardData += `${heard}`
|
184 | })
|
185 | htmlTemple = htmlTemple.replace(`<!-- *head* -->`, heardData)
|
186 | outPutHtml()
|
187 | }
|
188 |
|
189 |
|
190 | function moveFile (fromPath, toPath) {
|
191 | fs.readFile(fromPath, (err, fileData) => {
|
192 | if (err) throw err
|
193 | fs.writeFile(toPath, fileData, () => {
|
194 | logger.info(`copy file: ${toPath}`)
|
195 | })
|
196 | })
|
197 | }
|
198 |
|
199 |
|
200 | function handleScript (dom, changePath) {
|
201 |
|
202 | const versionString = config.outPut.outFileAddVersion ? `.${version}` : ''
|
203 |
|
204 |
|
205 | let coreScript = loadFile(path.join(corePath, 'main.js'))
|
206 | if (config.pageList.length === 1) {
|
207 |
|
208 | coreScript += loadFile(path.join(corePath, 'SinglePage.js'))
|
209 | } else {
|
210 |
|
211 | logger.info('multi page!')
|
212 | coreScript += loadFile(path.join(corePath, 'MultiPage.js'))
|
213 | }
|
214 |
|
215 |
|
216 | const useAnimationList = config.outPut.useAnimationList || dom.useAnimationList
|
217 | if (useAnimationList.length > 0 || config.outPut.choiceAnimation) {
|
218 | logger.info('animation!')
|
219 | coreScript += loadFile(path.join(corePath, 'animation.js'))
|
220 | }
|
221 |
|
222 | coreScript += dom.script
|
223 |
|
224 | let toolList = Cut.stringArray(coreScript, 'ozzx.tool.', '(')
|
225 |
|
226 | toolList = new Set(toolList)
|
227 | toolList.forEach(element => {
|
228 |
|
229 | coreScript += loadFile(path.join(corePath, 'tool', `${element}.js`))
|
230 | })
|
231 |
|
232 |
|
233 | dom.script = Script(coreScript, config.outPut.minifyJs).code
|
234 |
|
235 |
|
236 | const scriptDir = path.join(outPutPath, 'js')
|
237 | let scriptData = '<!-- 页面脚本 -->'
|
238 | if (!changePath) {
|
239 |
|
240 | DELDIR(scriptDir)
|
241 | logger.debug(`delete script dir success!`)
|
242 |
|
243 | fs.mkdirSync(scriptDir)
|
244 | }
|
245 |
|
246 | fs.writeFileSync(path.join(outPutPath, 'js' , `main${versionString}.js`), dom.script)
|
247 |
|
248 |
|
249 | if (config.autoReload) {
|
250 | if (!changePath) {
|
251 | moveFile(path.join(corePath, 'debug', 'autoReload.js'), path.join(outPutPath, 'js', `autoReload.js`))
|
252 | }
|
253 | scriptData += '\r\n <script src="./js/autoReload.js" type="text/javascript"></script>'
|
254 | }
|
255 |
|
256 |
|
257 | if (config.scriptList && config.scriptList.length > 0) {
|
258 |
|
259 | let completeNum = 0
|
260 | for (let ind = 0; ind < config.scriptList.length; ind++) {
|
261 | const element = config.scriptList[ind]
|
262 |
|
263 |
|
264 |
|
265 | if (!element.src) {
|
266 | console.error('script path unset!', element)
|
267 | continue
|
268 | }
|
269 |
|
270 | if (element.src.startsWith('http')) {
|
271 | scriptData += `\r\n <script src="${element.src}" type="text/javascript" ${element.defer ? 'defer="defer"' : ''}></script>`
|
272 |
|
273 | if (++completeNum >= config.scriptList.length) {
|
274 |
|
275 | if (config.outPut.globalScript) {
|
276 | const globalScriptData = fs.readFileSync(config.outPut.globalScript)
|
277 | if (globalScriptData) {
|
278 | logger.info(`add global script: ${config.outPut.globalScript}`)
|
279 | scriptData += '\r\n<script>' + globalScriptData + '\r\n</script>'
|
280 | } else {
|
281 | logger.error('global script is set but file not found!')
|
282 | }
|
283 | }
|
284 | scriptData += `\r\n <script src="./js/main${versionString}.js" type="text/javascript"></script>`
|
285 | htmlTemple = htmlTemple.replace(`<!-- script-output -->`, scriptData)
|
286 | outPutHtml()
|
287 | }
|
288 | continue
|
289 | } else {
|
290 | scriptData += `\r\n <script src="./js/${element.name}.js" type="text/javascript" ${element.defer ? 'defer="defer"' : ''}></script>`
|
291 | }
|
292 |
|
293 | const outPutFile = path.join(outPutPath, 'js', `${element.name}.js`)
|
294 |
|
295 | if (element.babel) {
|
296 | if (changePath === undefined || changePath === path.join(runPath, element.src)) {
|
297 | fs.readFile(path.join(runPath, element.src), (err, fileData) => {
|
298 | if (err) throw err
|
299 | fs.writeFile(outPutFile, Script(fileData, config.outPut.minifyJs).code, () => {
|
300 | logger.info(`bable and out put file: ${outPutFile}`)
|
301 |
|
302 | if (++completeNum >= config.scriptList.length) {
|
303 |
|
304 | if (config.outPut.globalScript) {
|
305 |
|
306 | const globalScriptData = fs.readFileSync(config.outPut.globalScript)
|
307 | if (globalScriptData) {
|
308 | logger.info(`add global script: ${config.outPut.globalScript}`)
|
309 | scriptData += '\r\n <script>\r\n' + globalScriptData + '\r\n </script>'
|
310 | } else {
|
311 | logger.error('global script is set but file not found!')
|
312 | }
|
313 | }
|
314 | scriptData += `\r\n <script src="./js/main${versionString}.js" type="text/javascript"></script>`
|
315 | htmlTemple = htmlTemple.replace(`<!-- script-output -->`, scriptData)
|
316 | outPutHtml()
|
317 | }
|
318 | })
|
319 | })
|
320 | } else {
|
321 | if (++completeNum >= config.scriptList.length) {
|
322 |
|
323 | if (config.outPut.globalScript) {
|
324 |
|
325 | const globalScriptData = fs.readFileSync(config.outPut.globalScript)
|
326 | if (globalScriptData) {
|
327 | logger.info(`add global script: ${config.outPut.globalScript}`)
|
328 | scriptData += '\r\n <script>\r\n' + globalScriptData + '\r\n </script>'
|
329 | } else {
|
330 | logger.error('global script is set but file not found!')
|
331 | }
|
332 | }
|
333 | scriptData += `\r\n <script src="./js/main${versionString}.js" type="text/javascript"></script>`
|
334 | htmlTemple = htmlTemple.replace(`<!-- script-output -->`, scriptData)
|
335 | outPutHtml()
|
336 | }
|
337 | }
|
338 | } else {
|
339 |
|
340 | if (changePath === undefined || changePath === path.join(runPath, element.src)) {
|
341 | moveFile(path.join(runPath, element.src), outPutFile)
|
342 | }
|
343 | if (++completeNum >= config.scriptList.length) {
|
344 |
|
345 | if (config.outPut.globalScript) {
|
346 |
|
347 | const globalScriptData = fs.readFileSync(config.outPut.globalScript)
|
348 | if (globalScriptData) {
|
349 | logger.info(`add global script: ${config.outPut.globalScript}`)
|
350 | scriptData += '\r\n <script>\r\n' + globalScriptData + '\r\n </script>'
|
351 | } else {
|
352 | logger.error('global script is set but file not found!')
|
353 | }
|
354 | }
|
355 | scriptData += `\r\n <script src="./js/main${versionString}.js" type="text/javascript"></script>`
|
356 | htmlTemple = htmlTemple.replace(`<!-- script-output -->`, scriptData)
|
357 | outPutHtml()
|
358 | }
|
359 | }
|
360 | }
|
361 | } else {
|
362 |
|
363 | htmlTemple = htmlTemple.replace(`<!-- script-output -->`, scriptData)
|
364 | outPutHtml()
|
365 | }
|
366 | }
|
367 |
|
368 |
|
369 | function outPutHtml () {
|
370 |
|
371 | if (!htmlTemple.includes('output')) {
|
372 | fs.writeFileSync(path.join(outPutPath, 'index.html'), htmlTemple)
|
373 | logger.info(`Package success! use time ${new Date().getTime() - startTime}`)
|
374 |
|
375 | if (config.autoReload) {
|
376 |
|
377 | wsServe.getWss().clients.forEach(client => client.send('reload'))
|
378 | }
|
379 | }
|
380 | }
|
381 |
|
382 | function pack (changePath) {
|
383 |
|
384 | if (!changePath) version = Math.random().toString(36).substr(2)
|
385 |
|
386 |
|
387 | if (!fs.existsSync(outPutPath)) {
|
388 | fs.mkdirSync(outPutPath)
|
389 | }
|
390 |
|
391 | startTime = new Date().getTime()
|
392 |
|
393 |
|
394 | htmlTemple = fs.readFileSync(path.join(__dirname, 'index.html'), 'utf8')
|
395 |
|
396 | htmlTemple = htmlTemple.replace('{{title}}', config.title || 'ozzx')
|
397 | handleHrard(config.headList)
|
398 | const dom = bodyHandle(htmlTemple, config)
|
399 | htmlTemple = dom.html
|
400 |
|
401 | handleStyle(dom, changePath)
|
402 |
|
403 | handleScript(dom, changePath)
|
404 | }
|
405 |
|
406 |
|
407 | pack()
|
408 |
|
409 |
|
410 | if (config.watcher.enable) {
|
411 | let watcherFolder = config.watcher.folder
|
412 | if (!watcherFolder) {
|
413 | watcherFolder = './src'
|
414 | logger.error('watcher is enable, but watcher.folder not set! use default value: "./src"')
|
415 | } else {
|
416 | watcherFolder = path.join(runPath, watcherFolder)
|
417 | logger.info(`watcher folder: ${watcherFolder}`)
|
418 | }
|
419 |
|
420 | const watcher = chokidar.watch(watcherFolder, {
|
421 |
|
422 | ignored: config.watcher.ignored ? config.watcher.ignored : config.outFolder + '/*',
|
423 | persistent: true,
|
424 | usePolling: true,
|
425 |
|
426 | depth: config.watcher.depth
|
427 | })
|
428 |
|
429 | watcher.on('change', changePath => {
|
430 | logger.info(`file change: ${changePath}`)
|
431 |
|
432 | pack(changePath)
|
433 | })
|
434 | }
|
435 |
|
436 |
|
437 | if (config.server) {
|
438 | app.use(express.static(path.join(runPath, config.outFolder)))
|
439 | }
|
440 |
|
441 |
|
442 |
|
443 | if (config.autoReload) {
|
444 | app.ws('/', function(ws, req) {
|
445 | ws.on('message', function(msg) {
|
446 | console.log(ws);
|
447 | })
|
448 | })
|
449 | }
|
450 |
|
451 |
|
452 | if (config.server || config.autoReload) {
|
453 | const port = config.serverPort || 8000
|
454 | app.listen(port)
|
455 | logger.info(`server is running at 127.0.0.1:${port}`)
|
456 | }
|
457 |
|