1 | #!/usr/bin/env node
|
2 |
|
3 | require('isomorphic-fetch')
|
4 | const rollup = require('rollup').rollup
|
5 | const uglify = require('uglify-js')
|
6 | const babel = require('rollup-plugin-babel')
|
7 | const nodeResolve = require('rollup-plugin-node-resolve')
|
8 | const commonjs = require('rollup-plugin-commonjs')
|
9 | const archiver = require('archiver')
|
10 | const exec = require('child_process').execSync
|
11 | const fs = require('fs')
|
12 | const path = require('path')
|
13 | const deploy = require('../util/qiniu')
|
14 | const paths = require('../config/path')
|
15 | const md5File = require('md5-file')
|
16 | const NODE_ENV = process.env.NODE_ENV
|
17 | let cache
|
18 | let config
|
19 |
|
20 | const date = new Date()
|
21 | const dateString = `${date.getFullYear()}${date.getMonth()+1}${date.getDate()}${date.getHours()}${date.getMinutes()}${date.getSeconds()}`
|
22 | process.env.BUILD_VERSION = dateString
|
23 |
|
24 | if (NODE_ENV === 'production') {
|
25 | config = require('../config/rollup.pro')
|
26 | } else if (NODE_ENV === 'test') {
|
27 | config = Object.assign({} , require('../config/rollup.test.js'), {
|
28 | cache: cache
|
29 | })
|
30 | } else {
|
31 | config = Object.assign({} , require('../config/rollup.dev.js'), {
|
32 | cache: cache
|
33 | })
|
34 | }
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | let packageBundleName
|
40 | const appConfig = require(paths.appEnvoyConfig)
|
41 | try {
|
42 | if (NODE_ENV === 'production') {
|
43 | if (!appConfig.name) {
|
44 | console.log(red('No appConfig name provided in envoy.config.js'))
|
45 | return
|
46 | }
|
47 | if (!appConfig.engineVersion) {
|
48 | console.log(red('No engineVersion provided in envoy.config.js'))
|
49 | return
|
50 | }
|
51 | if (!appConfig.accessKey) {
|
52 | console.log(red('No accessKey provided in envoy.config.js'))
|
53 | return
|
54 | }
|
55 | if (!appConfig.privateKey) {
|
56 | console.log(red('No privateKey provided in envoy.config.js'))
|
57 | return
|
58 | }
|
59 | packageBundleName = `${appConfig.name.replace(' ', '_')}_${appConfig.engineVersion.replace('.', '_')}_${process.env.BUILD_VERSION}`
|
60 | } else {
|
61 | packageBundleName = appConfig.name || 'test'
|
62 | }
|
63 | } catch (error) {
|
64 | packageBundleName = 'test'
|
65 |
|
66 | }
|
67 | const check = require('../util/checkVersion')
|
68 |
|
69 | build()
|
70 |
|
71 | function build() {
|
72 | exec(`rm -rf ${paths.appDist} && mkdir ${paths.appDist}`)
|
73 | exec(`rm -rf ${paths.appZip} && mkdir ${paths.appZip}`)
|
74 | exec(`cp -r ${paths.appResource} ${paths.appDistResource}`)
|
75 |
|
76 | rollup(config).then(bundle => {
|
77 | cache = bundle
|
78 | const code = bundle.generate({
|
79 | format: 'iife',
|
80 | useStrict: false,
|
81 | }).code
|
82 |
|
83 | if (NODE_ENV === 'production') {
|
84 | const minified = uglify.minify(code, {
|
85 | fromString: true,
|
86 | output: {
|
87 | ascii_only: true
|
88 | }
|
89 | }).code
|
90 | return write(paths.appOutput, minified)
|
91 | } else {
|
92 | return write(paths.appOutput, code)
|
93 | }
|
94 | }).then(() => {
|
95 |
|
96 | let output = fs.createWriteStream(path.join(paths.appZip, `${packageBundleName}.zip`))
|
97 | const archive = archiver('zip')
|
98 | archive.on('error', e => {
|
99 | throw err
|
100 | })
|
101 | archive.pipe(output)
|
102 | archive.directory(paths.appDist, packageBundleName)
|
103 | return archive.finalize()
|
104 | }).then(() => {
|
105 | return check()
|
106 | }).then(isOutOfDate => {
|
107 | if (isOutOfDate) {
|
108 | const str = `You are using Envoy v${isOutOfDate.local}, v${isOutOfDate.remote} is available`
|
109 | console.log(blue(str))
|
110 | }
|
111 | if (NODE_ENV === 'production') {
|
112 | const filePath = path.join(paths.appZip, `${packageBundleName}.zip`)
|
113 | const bucket = 'mesh-package'
|
114 | const key = `${packageBundleName}.zip`
|
115 | const fingerprint = md5File.sync(filePath)
|
116 | return deploy(filePath, bucket, key, appConfig, fingerprint)
|
117 | }
|
118 | }).then(data => {
|
119 | if (data) {
|
120 | console.log(blue(data))
|
121 | }
|
122 | }).catch(e => {
|
123 | console.log(red(e))
|
124 | })
|
125 | }
|
126 |
|
127 |
|
128 | function write (dest, code) {
|
129 | return new Promise(function (resolve, reject) {
|
130 | fs.writeFile(dest, code, function (err) {
|
131 | if (err) return reject(err)
|
132 | console.log(blue(dest) + ' ' + getSize(code))
|
133 | resolve()
|
134 | })
|
135 | })
|
136 | }
|
137 |
|
138 | function getSize (code) {
|
139 | return (code.length / 1024).toFixed(2) + 'kb'
|
140 | }
|
141 |
|
142 | function blue (str) {
|
143 | return '\x1b[1m\x1b[34m' + str + '\x1b[39m\x1b[22m'
|
144 | }
|
145 |
|
146 | function red(str) {
|
147 | return "\x1b[31m\x1b[1m" + str + "\x1b[0m"
|
148 | }
|