forked from desktop/desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-octicons
65 lines (50 loc) · 2.03 KB
/
generate-octicons
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env node
/* 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.
*/
'use strict'
const fs = require('fs');
const process = require('process');
const xml2js = require('xml2js');
const got = require('got');
const path = require('path');
const toCamelCase = require('to-camel-case');
console.log('Downloading latest version of octicon sprite svg');
got('https://github.com/primer/octicons/raw/master/build/sprite.octicons.svg')
.then((res) => {
xml2js.parseString(res.body, function (err, result) {
const viewBoxRe = /0 0 (\d+) (\d+)/
const out = fs.createWriteStream(path.resolve(__dirname, '../app/src/ui/octicons/octicons.generated.ts'));
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")
out.write("export class OcticonSymbol {\n")
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++;
const id = symbol.$.id;
const viewBox = symbol.$.viewBox;
const pathData = symbol.path[0].$.d;
const viewBoxMatch = viewBoxRe.exec(viewBox);
if(!viewBoxMatch) {
console.error(`Unexpected viewBox format for ${id}`);
process.exit(1);
}
const [, w, h] = viewBoxMatch;
const jsFriendlyName = toCamelCase(id);
out.write(` public static get ${jsFriendlyName}() { return new OcticonSymbol(${w}, ${h}, '${pathData}') }\n`);
});
out.write("}\n");
out.end();
console.log(`Wrote ${count} octicons`);
});
})
.catch((err) => {
console.log(`Failed downloading octicon sprite svg: ${err.message}`);
process.exit(1);
})