var fs = require('fs')
var version = require('../vue/package.json').version
var themeconfPath = 'themes/vue/_config.yml'
var installPath = 'src/v2/guide/installation.md'
var themeconfig = fs.readFileSync(themeconfPath, 'utf-8')
var installation = fs.readFileSync(installPath, 'utf-8')

fs.writeFileSync(
  themeconfPath,
  themeconfig.replace(/vue_version: .*/, 'vue_version: ' + version)
)

var sizes = {
  dev: 'vue.js',
  min: 'vue.min.js',
  gz: 'vue.min.js.gz',
  ro_gz: 'vue.runtime.min.js.gz'
}

for (var file in sizes) {
  var filesize = fs.statSync('../vue/dist/' + sizes[file], 'utf-8').size
  sizes[file] = (filesize / 1024).toFixed(2)
}

fs.writeFileSync(
  installPath,
  installation
    .replace(/vue_version: .*/, 'vue_version: ' + version)
    .replace(/(\w+)_size:.*/g, function (m, p1) {
      return p1 + '_size: "' + sizes[p1] + '"'
    })
)

@@ -16,16 +16,43 @@
 e('fs')%0A
+var zlib = require('zlib')%0A
 var vers
@@ -481,19 +481,16 @@
 e.min.js
-.gz
 ',%0A  ro_
@@ -516,39 +516,64 @@
 n.js
-.gz
 '%0A%7D%0A%0A
-for (var file in sizes)
+var pending = %5B%5D%0AObject.keys(sizes).forEach(file =%3E
  %7B%0A 
@@ -644,16 +644,45 @@
 ').size%0A
+  if (!/gz$/.test(file)) %7B%0A  
   sizes%5B
@@ -718,19 +718,403 @@
 ixed(2)%0A
-%7D%0A%0A
+  %7D else %7B%0A    pending.push(new Promise((resolve, reject) =%3E %7B%0A      fs.readFile('../vue/dist/' + sizes%5Bfile%5D, (err, buf) =%3E %7B%0A        if (err) return reject(err)%0A        zlib.gzip(buf, (err, buf) =%3E %7B%0A          if (err) return reject(err)%0A          sizes%5Bfile%5D = (buf.length / 1024).toFixed(2)%0A          resolve()%0A        %7D)%0A      %7D)%0A    %7D))%0A  %7D%0A%7D)%0A%0APromise.all(pending).then(() =%3E %7B%0A  
 fs.write
@@ -1123,16 +1123,18 @@
 leSync(%0A
+  
   instal
@@ -1140,16 +1140,18 @@
 llPath,%0A
+  
   instal
@@ -1157,16 +1157,18 @@
 llation%0A
+  
     .rep
@@ -1218,24 +1218,26 @@
 ersion)%0A    
+  
 .replace(/(%5C
@@ -1266,24 +1266,26 @@
 n (m, p1) %7B%0A
+  
       return
@@ -1323,13 +1323,80 @@
 '%22'%0A    
+  
 %7D)%0A
+  )%0A  console.log(sizes)%0A%7D).catch(err =%3E %7B%0A  console.error(err)%0A%7D
 )%0A

update update.js