This repository has been archived by the owner on Jun 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
icons.js
executable file
·138 lines (121 loc) · 4.39 KB
/
icons.js
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env node
const fs = require('fs');
const glob = require('glob');
const request = require('request-promise-native');
const prompt = require('prompt');
const xmldoc = require('xmldoc');
const PATH_TO_SEARCH = '../icons/';
const SERVER = 'http://localhost:5000';
const folderToAuthor = {
lorc: 'Lorc, http://lorcblog.blogspot.com',
delapouite: 'Delapouite, http://delapouite.com',
'john-colburn': 'John Colburn, http://ninmunanmu.com',
felbrigg: 'Felbrigg, http://blackdogofdoom.blogspot.co.uk',
'john-redman': 'John Redman, http://www.uniquedicetowers.com',
'carl-olsen': 'Carl Olsen, https://twitter.com/unstoppableCarl',
sbed: 'Sbed, http://opengameart.org/content/95-game-icons',
willdabeast: 'Willdabeast, http://wjbstories.blogspot.com',
'viscious-speed': 'Viscious Speed, http://viscious-speed.deviantart.com - CC0',
'lord-berandas': 'Lord Berandas, http://berandas.deviantart.com',
irongamer: 'Irongamer, http://ecesisllc.wix.com/home',
'heavenly-dog': 'HeavenlyDog, http://www.gnomosygoblins.blogspot.com',
lucasms: 'Lucas',
faithtoken: 'Faithtoken, http://fungustoken.deviantart.com',
skoll: 'Skoll',
andymeneely: 'Andy Meneely, http://www.se.rit.edu/~andy/',
cathelineau: 'Cathelineau',
'kier-heyl': 'Kier Heyl',
aussiesim: 'Aussiesim',
sparker: 'Sparker, http://citizenparker.com',
zeromancer: 'Zeromancer - CC0',
rihlsul: 'Rihlsul',
quoting: 'Quoting',
guard13007: 'Guard13007, https://guard13007.com',
darkzaitzev: 'DarkZaitzev, http://darkzaitzev.deviantart.com',
spencerdub: 'SpencerDub',
generalace135: 'GeneralAce135',
zajkonur: 'Zajkonur',
};
prompt.override = require('yargs').argv;
prompt.start();
var schema = {
properties: {
name: {
pattern: /^[a-zA-Z\s\-]+$/,
message: 'Name must be only letters, spaces, or dashes',
required: true,
default: 'dalius',
},
password: {
hidden: true,
},
server: {
message: 'Server to upload icons to',
required: true,
default: SERVER,
},
},
};
prompt.get(schema, function(err, result) {
uploadImages(result.name, result.password, result.server);
});
const uploadImages = async (username, password, server) => {
const svgs = glob.sync(PATH_TO_SEARCH + '**/*.svg');
let accessToken = null;
try {
const form = {
username,
password,
};
const resp = await request.post({ url: `${server}/api/tokens`, form, json: true });
accessToken = resp['accessToken'];
} catch (err) {
console.error('Upload failed:', err);
return;
}
if (accessToken === undefined) {
console.error('Token is undefined.');
return;
}
for (const image of svgs) {
let name = image.substring(PATH_TO_SEARCH.length).replace(/\//g, ' ');
name = name.replace('.svg', '');
name = name.replace(' svg ', ' ');
name = name.replace(' originals ', ' ');
console.log(`Processing ${name}.`);
let author = name.split(' ')[0];
let attribution = undefined;
if (author in folderToAuthor) {
attribution = folderToAuthor[author];
console.log(`Attribution ${attribution}`);
}
const svg_data = fs.readFileSync(image, 'utf8');
let doc = new xmldoc.XmlDocument(svg_data);
doc.children = doc.children.filter(el => el.name !== 'path' || 'fill' in el.attr);
for (let child of doc.children) {
if (child.name === 'path' && 'fill' in child.attr) {
child.attr['fill'] = '#000';
}
}
try {
const formData = {
global: 'true',
name,
metadata: JSON.stringify({ source: 'game-icons', attribution }),
image: {
value: doc.toString({ compressed: true }),
options: {
filename: 'name' + '.svg',
},
},
};
const headers = {
Authorization: `Bearer ${accessToken}`,
};
const resp = await request.post({ url: `${server}/api/images`, headers, formData });
console.log(resp);
} catch (err) {
console.error('Upload failed:', err.statusCode);
}
}
};