1 | #!/usr/bin/env node
|
2 |
|
3 | 'use strict'
|
4 |
|
5 | const fs = require('fs')
|
6 | const gulp = require('gulp')
|
7 |
|
8 | const chokidar = require('chokidar')
|
9 |
|
10 | const minifier = require('sqwish')
|
11 |
|
12 | const UglifyJS = require("uglify-js")
|
13 |
|
14 | const { getLogger } = require('log4js')
|
15 | const logger = getLogger()
|
16 |
|
17 | const heardHandle = require('./lib/heard')
|
18 | const bodyHandle = require('./lib/page')
|
19 |
|
20 |
|
21 | logger.level = 'debug'
|
22 |
|
23 |
|
24 | const runPath = process.cwd()
|
25 |
|
26 |
|
27 | if (!fs.readFileSync(`${runPath}/ozzx.json`)) {
|
28 | logger.error('ozzx.json file does not exist!')
|
29 | close()
|
30 | }
|
31 |
|
32 |
|
33 | const config = JSON.parse(fs.readFileSync(`${runPath}/ozzx.json`, 'utf8'))
|
34 |
|
35 | const path = runPath + config.root
|
36 |
|
37 | const outPutPath = `${runPath}/${config.outFolder}/`
|
38 | const corePath = `${__dirname}/core/`
|
39 |
|
40 |
|
41 |
|
42 | function pack () {
|
43 |
|
44 | let templet = fs.readFileSync(`${path}/index.html`, 'utf8')
|
45 |
|
46 | templet = heardHandle(`${path}/${config.headFolder}/`, templet)
|
47 |
|
48 |
|
49 | const dom = bodyHandle(templet, config)
|
50 |
|
51 |
|
52 | const configData = `
|
53 | window.ozzx = {
|
54 | script: {}
|
55 | };
|
56 | var globalConfig = ${JSON.stringify(config)};
|
57 | `
|
58 | const coreData = configData + fs.readFileSync(`${corePath}main.js`, 'utf8')
|
59 |
|
60 |
|
61 |
|
62 | const coreStyle = fs.readFileSync(`${path}/main.css`, 'utf8') + '\r\n'
|
63 |
|
64 |
|
65 | let outPutCss = coreStyle + dom.style
|
66 | if (config.minifyCss) {
|
67 | outPutCss = minifier.minify(outPutCss)
|
68 | }
|
69 |
|
70 |
|
71 | let outPutJs = coreData + dom.script
|
72 | if (config.minifyJs) {
|
73 | outPutJs = UglifyJS.minify(outPutJs).code
|
74 | }
|
75 |
|
76 | if (!fs.existsSync(outPutPath)) {
|
77 | fs.mkdirSync(outPutPath)
|
78 | }
|
79 |
|
80 | fs.writeFileSync(`${outPutPath}main.css`, outPutCss)
|
81 | fs.writeFileSync(`${outPutPath}main.js`, outPutJs)
|
82 | fs.writeFileSync(`${outPutPath}index.html`, dom.html)
|
83 | console.log('Package success!')
|
84 | }
|
85 |
|
86 |
|
87 | pack()
|
88 |
|
89 |
|
90 | if (config.autoPack) {
|
91 |
|
92 | const watcher = chokidar.watch(path, {
|
93 | ignored: './' + config.outFolder + '/*',
|
94 | persistent: true,
|
95 | usePolling: true
|
96 | })
|
97 |
|
98 | watcher.on('change', path => {
|
99 | console.log(`file change: ${path}`)
|
100 |
|
101 | pack()
|
102 | })
|
103 | }
|