Skip to content

Commit

Permalink
chore(frontend-icons): change icon scripts to be synchronous
Browse files Browse the repository at this point in the history
This is necessary for running the scripts sequentially without
having the next script start before the callbacks are finished
on the previous script (unless all scripts are made asynchronous).
  • Loading branch information
davidmason committed May 15, 2017
1 parent ed65a62 commit 08682aa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 36 deletions.
34 changes: 14 additions & 20 deletions server/zanata-frontend/src/frontend/scripts/createIconsComponent.js
@@ -1,29 +1,23 @@
/* eslint-disable no-console */
var fs = require('fs')
// build spritesheet used in this script (could change to return sheet)
require('./build-icon-spritesheet')

var svgFile = './app/components/Icons/icons.svg'
var componentFileSrc = './app/components/Icons/index.jsx.src'
var componentFile = './app/components/Icons/index.jsx'

function getSVG (cb) {
fs.readFile(svgFile, 'utf8', function (err, data) {
var result
if (err) return console.log(err)
result = data.replace(/ style="position:absolute"/g, '')
cb(result)
})
function getSVG () {
const data = fs.readFileSync(svgFile, 'utf8')
return data.replace(/ style="position:absolute"/g, '')
}

function generateComponent (cb) {
fs.readFile(componentFileSrc, 'utf8', function (err, data) {
if (err) return console.log(err)
getSVG(function (svg) {
var component = data.replace(/{{svgFile: 'icons.svg'}}/, '\'' + svg + '\'')
cb(component)
})
})
function generateComponent () {
const data = fs.readFileSync(componentFileSrc, 'utf8')
const svg = getSVG()
return data.replace(/{{svgFile: 'icons.svg'}}/, '\'' + svg + '\'')
}

generateComponent(function (component) {
fs.writeFile(componentFile, component, 'utf8', function (err) {
if (err) return console.log(err)
})
})
process.stdout.write('Generating Icons component with embedded SVG')
fs.writeFileSync(componentFile, generateComponent(), 'utf8')
console.log(' ... Done')
27 changes: 11 additions & 16 deletions server/zanata-frontend/src/frontend/scripts/generateIconList.js
@@ -1,21 +1,16 @@
/* eslint-disable no-console */
var fs = require('fs')
var iconsSrc = './app/components/Icons/svgs'
var iconsFileName = './app/components/Icon/list.js'
var svgFileRegex = /Icon-(.*).svg/

fs.readdir(iconsSrc, function (err, files) {
if (err) {
console.error(err)
return
}
const fileNames = files.map(file => {
const fileName = file.match(svgFileRegex)
return fileName ? fileName[1] : undefined
}).filter(file => file)
const iconsFile =
`module.exports = [${fileNames.map(file => `'${file}'`)}]\r\n`
fs.writeFile(iconsFileName, iconsFile, (err) => {
if (err) throw err
console.log('Icon file list saved')
})
})
process.stdout.write('Generating list of icon names in Icon/list.js')
const files = fs.readdirSync(iconsSrc)
const fileNames = files.map(file => {
const fileName = file.match(svgFileRegex)
return fileName ? fileName[1] : undefined
}).filter(file => file)
const iconsFile =
`module.exports = [${fileNames.map(file => `'${file}'`)}]\r\n`
fs.writeFileSync(iconsFileName, iconsFile)
console.log(' ... Done')

0 comments on commit 08682aa

Please sign in to comment.