|
| 1 | +// node ./active-script/update-readme.js |
| 2 | +const { readFileSync, writeFileSync } = require('fs'); |
| 3 | +const axios = require('axios'); |
| 4 | +const { format } = require('prettier'); |
| 5 | +const { stripIndent } = require('common-tags'); |
| 6 | + |
| 7 | +const url = 'https://raw.githubusercontent.com/zoo-js/zoo-data/main/json/organizations.json'; |
| 8 | +var organizations = []; |
| 9 | + |
| 10 | +function main() { |
| 11 | + const baseReadme = readFileSync('./base/README.base.md', { encoding:'utf8', flag:'r' }); |
| 12 | + const baseReadmeEn = readFileSync('./base/README.en-US.base.md', { encoding:'utf8', flag:'r' }); |
| 13 | + if (organizations.length == 0) { |
| 14 | + console.log(`❌ Get organizations error`); |
| 15 | + return false; |
| 16 | + } |
| 17 | + let content = ''; |
| 18 | + let row = organizations.length / 6; |
| 19 | + const lastNo = organizations.length % 6; |
| 20 | + if (lastNo != 0) row += 1; |
| 21 | + for (var j = 1; j <= row; j++) { |
| 22 | + let data = ''; |
| 23 | + data = stripIndent` |
| 24 | + <tr> |
| 25 | + <td align="center">${getFullName(organizations[(j-1)*6])}</td> |
| 26 | + <td align="center">${getFullName(organizations[(j-1)*6+1])}</td> |
| 27 | + <td align="center">${getFullName(organizations[(j-1)*6+2])}</td> |
| 28 | + <td align="center">${getFullName(organizations[(j-1)*6+3])}</td> |
| 29 | + <td align="center">${getFullName(organizations[(j-1)*6+4])}</td> |
| 30 | + <td align="center">${getFullName(organizations[(j-1)*6+5])}</td> |
| 31 | + </tr> |
| 32 | + <tr> |
| 33 | + <td width="160" align="center">${getCnName(organizations[(j-1)*6])}</td> |
| 34 | + <td width="160" align="center">${getCnName(organizations[(j-1)*6+1])}</td> |
| 35 | + <td width="160" align="center">${getCnName(organizations[(j-1)*6+2])}</td> |
| 36 | + <td width="160" align="center">${getCnName(organizations[(j-1)*6+3])}</td> |
| 37 | + <td width="160" align="center">${getCnName(organizations[(j-1)*6+4])}</td> |
| 38 | + <td width="160" align="center">${getCnName(organizations[(j-1)*6+5])}</td> |
| 39 | + </tr> |
| 40 | + `; |
| 41 | + content += data |
| 42 | + } |
| 43 | + |
| 44 | + let newReadme = baseReadme.replace('UPDATE_README_NUMBER', organizations.length); |
| 45 | + newReadme = newReadme.replace('UPDATE_README_TABLE', `<table>${content}</table>`); |
| 46 | + const newReadmeFormatted = format(newReadme, { |
| 47 | + parser: 'markdown', |
| 48 | + }); |
| 49 | + writeFileSync('./README.md', newReadmeFormatted); |
| 50 | + console.log(`🎉 Done readme`); |
| 51 | + |
| 52 | + let newReadmeEn = baseReadmeEn.replace('UPDATE_README_NUMBER', organizations.length); |
| 53 | + newReadmeEn = newReadmeEn.replace('UPDATE_README_TABLE', `<table>${content}</table>`); |
| 54 | + const newReadmeEnFormatted = format(newReadmeEn, { |
| 55 | + parser: 'markdown', |
| 56 | + }); |
| 57 | + writeFileSync('./README.en-US.md', newReadmeEnFormatted); |
| 58 | + console.log(`🎉 Done readme-us`); |
| 59 | +}; |
| 60 | + |
| 61 | +function getFullName(pet) { |
| 62 | + if (pet) { |
| 63 | + return `<a href="https://github.com/${pet.fullName}" target="_blank"><img src="https://avatars0.githubusercontent.com/u/${pet.code}?s=200&v=4" width="50" /></a>`; |
| 64 | + } |
| 65 | + return ''; |
| 66 | +}; |
| 67 | + |
| 68 | +function getCnName(pet) { |
| 69 | + if (pet) { |
| 70 | + return `${pet.name}<br />${pet.cnName}`; |
| 71 | + } |
| 72 | + return ''; |
| 73 | +}; |
| 74 | + |
| 75 | +async function getOrganizations() { |
| 76 | + try { |
| 77 | + const res = await axios.get(url); |
| 78 | + organizations = res.data.data; |
| 79 | + } catch(err) { |
| 80 | + console.log(err); |
| 81 | + } |
| 82 | +}; |
| 83 | + |
| 84 | +(async () => { |
| 85 | + await getOrganizations(); |
| 86 | + main(); |
| 87 | +})(); |
0 commit comments