|
| 1 | +const sketch = require('sketch') |
| 2 | + |
| 3 | +export default function() { |
| 4 | + const document = sketch.getSelectedDocument() |
| 5 | + const selection = document.selectedLayers.layers |
| 6 | + const Settings = sketch.Settings |
| 7 | + |
| 8 | + const ciToken = Settings.documentSettingForKey(document, 'ci-token') |
| 9 | + const ciUrl = Settings.documentSettingForKey(document, 'ci-url') |
| 10 | + |
| 11 | + function postIcon(iconCatalog, iconName, iconCode) { |
| 12 | + console.log(iconCatalog) |
| 13 | + console.log(iconName) |
| 14 | + console.log(iconCode) |
| 15 | + let requestOptions = { |
| 16 | + method: 'POST', |
| 17 | + headers: { |
| 18 | + "Authorization": `Basic ${ciToken}`, |
| 19 | + "Content-Type": "application/json" |
| 20 | + }, |
| 21 | + body: JSON.stringify({ |
| 22 | + "ref": "master", |
| 23 | + "envs": [ |
| 24 | + { |
| 25 | + "name": "ICON_CATALOG", |
| 26 | + "value": iconCatalog, |
| 27 | + "sensitive": 0 |
| 28 | + }, |
| 29 | + { |
| 30 | + "name": "ICON_NAME", |
| 31 | + "value": iconName, |
| 32 | + "sensitive": 0 |
| 33 | + }, |
| 34 | + { |
| 35 | + "name": "ICON_CODE", |
| 36 | + "value": iconCode.replace(/\\"/g, '\\\\"'), |
| 37 | + "sensitive": 0 |
| 38 | + } |
| 39 | + ] |
| 40 | + }) |
| 41 | + } |
| 42 | + fetch(ciUrl, requestOptions) |
| 43 | + .then(response => response.text()) |
| 44 | + .then(result => console.log(result)) |
| 45 | + .then(() => sketch.UI.alert('🎉 Well Done!', 'The Merge Requset for new icon will be created in 1min. Please check it in the CodingUIKit project. \n \nThanks for your contribution. 你是 CODING 的骄傲!')) |
| 46 | + .catch(error => console.log('error', error) |
| 47 | + ) |
| 48 | + } |
| 49 | + |
| 50 | + if (ciToken) { |
| 51 | + let iconCatalog = [] |
| 52 | + let iconName = [] |
| 53 | + let iconCode = [] |
| 54 | + selection.map(layer => { |
| 55 | + console.log(layer.name) |
| 56 | + const options = { formats: 'svg', output: false, compact: true} |
| 57 | + const sketchSVG = sketch.export(layer, options) |
| 58 | + console.log(sketchSVG.toString()) |
| 59 | + iconCatalog.push(layer.name.split('/')[0]) |
| 60 | + iconName.push(layer.name.split('/')[1]) |
| 61 | + iconCode.push(sketchSVG.toString()) |
| 62 | + }) |
| 63 | + postIcon(JSON.stringify(iconCatalog).toString(), JSON.stringify(iconName).toString(), JSON.stringify(iconCode).toString()) |
| 64 | + } else { |
| 65 | + console.log('The ci-token setting not found in this document! Make sure your current docmuent is Coding-Icons Library.') |
| 66 | + sketch.UI.alert('⛔️ Wrong File!', 'The ci-token setting not found in this document! \nPlease make sure current docmuent is the Coding-Icons Library.') |
| 67 | + } |
| 68 | +} |
0 commit comments