aboutsummaryrefslogtreecommitdiff
path: root/scripts/updateVersion.js
blob: 218ffa9a120ce0db1557223a6dfebe6cb8ade412 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import p from '../package.json'
import cp from 'child_process'
import fs from 'fs'
import { basename } from 'path'

const setVersionToIcons = (version, files) => {
  for (const i in files) {
    const file = files[i]

    if (fs.existsSync(`src/_icons/${file}.svg`)) {
      let svgFile = fs.readFileSync(`src/_icons/${file}.svg`).toString()

      if (!svgFile.match(/version: ([0-9.]+)/i)) {
        svgFile = svgFile.replace(/---\n<svg>/i, function(m) {
          return `version: "${version}"\n${m}`
        })

        console.log(`Set version to ${version} in "${basename(file)}"`);
        fs.writeFileSync(`src/_icons/${file}.svg`, svgFile)
      } else {
        console.log(`File ${file} already has version`)
      }
    } else {
      console.log(`File ${file} doesn't exists`)
    }
  }
}

const updateIconsVersion = (version) => {

  if (version) {
    cp.exec(`grep -RiL "version: " ./src/_icons/*.svg`, function(err, ret) {

      let newIcons = []

      ret.replace(/src\/_icons\/([a-z0-9-]+)\.svg/g, function(m, fileName) {
        newIcons.push(fileName)
      })

      if (newIcons.length) {
        setVersionToIcons(version.replace(/\.0$/, ''), newIcons)
      }
    })
  }
}

updateIconsVersion(p.version)