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