UNPKG

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