|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +'use strict'; |
| 7 | + |
| 8 | +var path = require('path'); |
| 9 | +var fs = require('fs'); |
| 10 | +var https = require('https'); |
| 11 | +var url = require('url'); |
| 12 | + |
| 13 | +function getCommitSha(repoId, repoPath) { |
| 14 | + var commitInfo = 'https://api.github.com/repos/' + repoId + '/commits?path=' + repoPath; |
| 15 | + return download(commitInfo).then(function (content) { |
| 16 | + try { |
| 17 | + let lastCommit = JSON.parse(content)[0]; |
| 18 | + return Promise.resolve({ |
| 19 | + commitSha: lastCommit.sha, |
| 20 | + commitDate: lastCommit.commit.author.date |
| 21 | + }); |
| 22 | + } catch (e) { |
| 23 | + return Promise.resolve(null); |
| 24 | + } |
| 25 | + }, function () { |
| 26 | + console.err('Failed loading ' + commitInfo); |
| 27 | + return Promise.resolve(null); |
| 28 | + }); |
| 29 | +} |
| 30 | + |
| 31 | +function download(urlString) { |
| 32 | + return new Promise((c, e) => { |
| 33 | + var _url = url.parse(urlString); |
| 34 | + var options = { host: _url.host, port: _url.port, path: _url.path, headers: { 'User-Agent': 'NodeJS' }}; |
| 35 | + var content = ''; |
| 36 | + var request = https.get(options, function (response) { |
| 37 | + response.on('data', function (data) { |
| 38 | + content += data.toString(); |
| 39 | + }).on('end', function () { |
| 40 | + c(content); |
| 41 | + }); |
| 42 | + }).on('error', function (err) { |
| 43 | + e(err.message); |
| 44 | + }); |
| 45 | + }); |
| 46 | +} |
| 47 | + |
| 48 | +function invertColor(color) { |
| 49 | + var res = '#' |
| 50 | + for (var i = 1; i < 7; i+=2) { |
| 51 | + var newVal = 255 - parseInt('0x' + color.substr(i, 2), 16); |
| 52 | + res += newVal.toString(16); |
| 53 | + } |
| 54 | + return res; |
| 55 | +} |
| 56 | + |
| 57 | + |
| 58 | +exports.update = function () { |
| 59 | + var fontMappings = 'https://raw.githubusercontent.com/jesseweed/seti-ui/master/styles/_fonts/seti.less'; |
| 60 | + console.log('Reading from ' + fontMappings); |
| 61 | + var def2Content = {}; |
| 62 | + var ext2Def = {}; |
| 63 | + var fileName2Def = {}; |
| 64 | + var def2ColorId = {}; |
| 65 | + var colorId2Value = {}; |
| 66 | + |
| 67 | + function writeFileIconContent(info) { |
| 68 | + var iconDefinitions = {}; |
| 69 | + |
| 70 | + for (var def in def2Content) { |
| 71 | + var entry = { fontCharacter: def2Content[def] }; |
| 72 | + var colorId = def2ColorId[def]; |
| 73 | + if (colorId) { |
| 74 | + var colorValue = colorId2Value[colorId]; |
| 75 | + if (colorValue) { |
| 76 | + entry.fontColor = colorValue; |
| 77 | + |
| 78 | + var entryInverse = { fontCharacter: entry.fontCharacter, fontColor: invertColor(colorValue) }; |
| 79 | + iconDefinitions[def + '_light'] = entryInverse; |
| 80 | + } |
| 81 | + } |
| 82 | + iconDefinitions[def] = entry; |
| 83 | + } |
| 84 | + |
| 85 | + function getInvertSet(input) { |
| 86 | + var result = {}; |
| 87 | + for (var assoc in input) { |
| 88 | + let invertDef = input[assoc] + '_light';; |
| 89 | + if (iconDefinitions[invertDef]) { |
| 90 | + result[assoc] = invertDef; |
| 91 | + } |
| 92 | + } |
| 93 | + return result; |
| 94 | + } |
| 95 | + |
| 96 | + var res = { |
| 97 | + fonts: [{ |
| 98 | + id: "seti", |
| 99 | + src: [{ "path": "./seti.woff", "format": "woff" }], |
| 100 | + weight: "normal", |
| 101 | + style: "normal", |
| 102 | + size: "150%" |
| 103 | + }], |
| 104 | + iconDefinitions: iconDefinitions, |
| 105 | + // folder: "_folder", |
| 106 | + file: "_default", |
| 107 | + fileExtensions: ext2Def, |
| 108 | + fileNames: fileName2Def, |
| 109 | + light: { |
| 110 | + file: "_default_light", |
| 111 | + fileExtensions: getInvertSet(ext2Def), |
| 112 | + fileNames: getInvertSet(fileName2Def) |
| 113 | + }, |
| 114 | + version: 'https://github.com/jesseweed/seti-ui/commit/' + info.commitSha, |
| 115 | + }; |
| 116 | + fs.writeFileSync('./icons/seti-icon-theme.json', JSON.stringify(res, null, '\t')); |
| 117 | + |
| 118 | + } |
| 119 | + |
| 120 | + |
| 121 | + var match; |
| 122 | + |
| 123 | + return download(fontMappings).then(function (content) { |
| 124 | + var regex = /@([\w-]+):\s*'(\\E[0-9A-F]+)';/g; |
| 125 | + while ((match = regex.exec(content)) !== null) { |
| 126 | + def2Content['_' + match[1]] = match[2]; |
| 127 | + } |
| 128 | + |
| 129 | + var mappings = 'https://raw.githubusercontent.com/jesseweed/seti-ui/master/styles/icons/mapping.less'; |
| 130 | + return download(mappings).then(function (content) { |
| 131 | + var regex2 = /\.icon-(?:set|partial)\('([\w-\.]+)',\s*'([\w-]+)',\s*(@[\w-]+)\)/g; |
| 132 | + while ((match = regex2.exec(content)) !== null) { |
| 133 | + let pattern = match[1]; |
| 134 | + let def = '_' + match[2]; |
| 135 | + let colorId = match[3]; |
| 136 | + if (pattern[0] === '.') { |
| 137 | + ext2Def[pattern.substr(1)] = def; |
| 138 | + } else { |
| 139 | + fileName2Def[pattern] = def; |
| 140 | + } |
| 141 | + def2ColorId[def] = colorId; |
| 142 | + } |
| 143 | + var colors = 'https://raw.githubusercontent.com/jesseweed/seti-ui/master/styles/ui-variables.less'; |
| 144 | + return download(colors).then(function (content) { |
| 145 | + var regex3 = /(@[\w-]+):\s*(#[0-9a-z]+)/g; |
| 146 | + while ((match = regex3.exec(content)) !== null) { |
| 147 | + colorId2Value[match[1]] = match[2]; |
| 148 | + } |
| 149 | + return getCommitSha('jesseweed/seti-ui', 'styles/_fonts/seti.less').then(function (info) { |
| 150 | + try { |
| 151 | + writeFileIconContent(info); |
| 152 | + if (info) { |
| 153 | + console.log('Updated to jesseweed/seti-ui@' + info.commitSha.substr(0, 7) + ' (' + info.commitDate.substr(0, 10) + ')'); |
| 154 | + } |
| 155 | + } catch (e) { |
| 156 | + console.error(e); |
| 157 | + } |
| 158 | + }); |
| 159 | + }); |
| 160 | + }); |
| 161 | + }, console.error); |
| 162 | +} |
| 163 | + |
| 164 | +if (path.basename(process.argv[1]) === 'update-icon-theme.js') { |
| 165 | + exports.update(); |
| 166 | +} |
| 167 | + |
| 168 | + |
| 169 | + |
0 commit comments