2016-06-13 14:28:16 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2016-06-14 18:22:47 +00:00
|
|
|
/* generate-octicons
|
|
|
|
*
|
|
|
|
* Utility script for generating a strongly typed representation of all
|
|
|
|
* octicons currently in the master branch of primer/octicons. Automatically
|
|
|
|
* downloads the latest version of the `sprite.octicons.svg` file.
|
|
|
|
*/
|
|
|
|
|
2016-06-13 14:28:16 +00:00
|
|
|
'use strict'
|
|
|
|
|
2017-07-13 15:11:04 +00:00
|
|
|
const fs = require('fs')
|
|
|
|
const process = require('process')
|
|
|
|
const xml2js = require('xml2js')
|
|
|
|
const path = require('path')
|
|
|
|
const toCamelCase = require('to-camel-case')
|
2016-06-13 14:28:16 +00:00
|
|
|
|
2017-08-18 04:37:45 +00:00
|
|
|
const filePath = path.resolve(
|
|
|
|
__dirname,
|
|
|
|
'..',
|
|
|
|
'node_modules',
|
|
|
|
'octicons',
|
|
|
|
'build',
|
|
|
|
'sprite.octicons.svg'
|
|
|
|
)
|
2016-06-13 14:28:16 +00:00
|
|
|
|
2017-08-18 04:37:45 +00:00
|
|
|
const file = fs.readFileSync(filePath)
|
2016-06-13 14:28:16 +00:00
|
|
|
|
2017-08-18 04:37:45 +00:00
|
|
|
xml2js.parseString(file, function(err, result) {
|
|
|
|
const viewBoxRe = /0 0 (\d+) (\d+)/
|
|
|
|
const out = fs.createWriteStream(
|
|
|
|
path.resolve(__dirname, '../app/src/ui/octicons/octicons.generated.ts')
|
|
|
|
)
|
2016-06-15 17:47:16 +00:00
|
|
|
|
2017-08-18 04:37:45 +00:00
|
|
|
out.write('/*\n')
|
|
|
|
out.write(
|
|
|
|
' * This file is automatically generated by the generate-octicons tool.\n'
|
|
|
|
)
|
|
|
|
out.write(' * Manually changing this file will only lead to sadness.\n')
|
|
|
|
out.write(' */\n\n')
|
2016-06-13 14:28:16 +00:00
|
|
|
|
2017-08-18 04:37:45 +00:00
|
|
|
out.write('export class OcticonSymbol {\n')
|
2016-06-13 18:40:13 +00:00
|
|
|
|
2017-08-18 04:37:45 +00:00
|
|
|
out.write(
|
|
|
|
'\n public constructor(public w: number, public h: number, public d: string) { }\n\n'
|
|
|
|
)
|
|
|
|
let count = 0
|
|
|
|
result.svg.symbol.forEach(function(symbol) {
|
|
|
|
count++
|
2016-06-13 14:28:16 +00:00
|
|
|
|
2017-08-18 04:37:45 +00:00
|
|
|
const id = symbol.$.id
|
|
|
|
const viewBox = symbol.$.viewBox
|
|
|
|
const pathData = symbol.path[0].$.d
|
2016-06-13 14:28:16 +00:00
|
|
|
|
2017-08-18 04:37:45 +00:00
|
|
|
const viewBoxMatch = viewBoxRe.exec(viewBox)
|
2016-06-13 14:28:16 +00:00
|
|
|
|
2017-08-18 04:37:45 +00:00
|
|
|
if (!viewBoxMatch) {
|
|
|
|
console.error(`Unexpected viewBox format for ${id}`)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
2016-06-13 14:28:16 +00:00
|
|
|
|
2017-08-18 04:37:45 +00:00
|
|
|
const [, w, h] = viewBoxMatch
|
|
|
|
const jsFriendlyName = toCamelCase(id)
|
2016-06-13 14:28:16 +00:00
|
|
|
|
2017-08-18 04:37:45 +00:00
|
|
|
out.write(
|
|
|
|
` public static get ${jsFriendlyName}() { return new OcticonSymbol(${w}, ${h}, '${pathData}') }\n`
|
|
|
|
)
|
2016-06-14 18:20:11 +00:00
|
|
|
})
|
2017-08-18 04:37:45 +00:00
|
|
|
|
|
|
|
out.write('}\n')
|
|
|
|
out.end()
|
|
|
|
|
|
|
|
console.log(`Wrote ${count} octicons`)
|
|
|
|
})
|