|
| 1 | +#!/usr/bin/env node |
| 2 | +import {request} from 'https' |
| 3 | +import {readFileSync, writeFileSync} from 'fs' |
| 4 | +const escapeMap = { |
| 5 | + '&': '&', |
| 6 | + '<': '<', |
| 7 | + '>': '>', |
| 8 | + "'": ''', |
| 9 | + '"': '&#quot;', |
| 10 | +} |
| 11 | +function escape(str) { |
| 12 | + let newStr = '' |
| 13 | + for(const char of str) newStr += char in escapeMap ? escapeMap[char] : char |
| 14 | + return newStr |
| 15 | +} |
| 16 | + |
| 17 | +function json(url) { |
| 18 | + return new Promise((resolve, reject) => { |
| 19 | + const req = request(url, { |
| 20 | + headers: { |
| 21 | + 'User-Agent': `nodejs ${process.version}`, |
| 22 | + 'Authorization': `Bearer ${process.env['GITHUB_TOKEN']}`, |
| 23 | + 'Accept': 'application/vnd.github.mercy-preview+json' |
| 24 | + } |
| 25 | + }, async res => { |
| 26 | + res.on('error', reject) |
| 27 | + let body = '' |
| 28 | + for await (const chunk of res) { |
| 29 | + body += chunk |
| 30 | + } |
| 31 | + resolve(JSON.parse(body)) |
| 32 | + }) |
| 33 | + req.on('error', reject) |
| 34 | + req.end() |
| 35 | + }) |
| 36 | +} |
| 37 | + |
| 38 | +async function *getRepos() { |
| 39 | + for(let page = 1; page < 1000; page += 1) { |
| 40 | + const repos = await json(`https://api.github.com/orgs/github/repos?type=public&per_page=100&page=${page}`) |
| 41 | + if (!repos.length) return |
| 42 | + for (const repo of repos) { |
| 43 | + if (!repo.topics) continue |
| 44 | + if (repo.private) continue |
| 45 | + if (repo.fork) continue |
| 46 | + if (!repo.topics.includes('web-components')) continue |
| 47 | + if (!repo.topics.includes('custom-elements')) continue |
| 48 | + yield repo |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +let readme = readFileSync('readme.head.md', 'utf-8') |
| 54 | +const bowerJson = JSON.parse(readFileSync('bower.json', 'utf-8')) |
| 55 | +bowerJson.dependencies = {} |
| 56 | +const packageJson = JSON.parse(readFileSync('package.json', 'utf-8')) |
| 57 | +packageJson.dependencies = {} |
| 58 | +let repos = [] |
| 59 | +for await (const repo of getRepos()) { |
| 60 | + if (repo.full_name === 'github/custom-element-boilerplate') continue |
| 61 | + repos.push(repo) |
| 62 | +} |
| 63 | +repos.sort((a, b) => a.full_name.localeCompare(b.full_name)) |
| 64 | +readme += ` |
| 65 | +We have ${repos.length} open source custom elements: |
| 66 | +` |
| 67 | +for (const repo of repos) { |
| 68 | + bowerJson.dependencies[repo.name] = repo.full_name |
| 69 | + packageJson.dependencies[`@${repo.full_name}`] = '*' |
| 70 | + readme += ` |
| 71 | +### [${escape(repo.full_name)}](${repo.html_url}) |
| 72 | +
|
| 73 | +${escape(repo.description)} |
| 74 | +
|
| 75 | +[Link](${repo.html_url}) |
| 76 | +` |
| 77 | +} |
| 78 | +readme += readFileSync('readme.tail.md', 'utf-8') |
| 79 | +writeFileSync('README.md', readme) |
| 80 | +writeFileSync('bower.json', JSON.stringify(bowerJson, null, 2)) |
| 81 | +writeFileSync('package.json', JSON.stringify(packageJson, null, 2)) |
| 82 | + |
0 commit comments