diff --git a/scripts/discover-sources.js b/scripts/discover-sources.js new file mode 100644 index 0000000..88cb61d --- /dev/null +++ b/scripts/discover-sources.js @@ -0,0 +1,175 @@ +#!/usr/bin/env node +/** + * 画个标 · 素材源发现器 + * + * 扫描 Iconify 官方集合清单,与本地已收录源对比,输出: + * - 可直接新增的独立源(协议合规、非别名) + * - 别名/子集(同库不同 id,避免重复收录) + * - 协议不合规(BY-NC / BY-SA / BY-ND / GPL) + * - 需人工判断(品牌/商标风险、协议不明) + * + * 用法: + * node scripts/discover-sources.js + * node scripts/discover-sources.js --json # 只输出 JSON + * + * 输出文件:output/source-candidates.json(不入库) + */ +'use strict'; + +const fs = require('fs'); +const path = require('path'); +const https = require('https'); + +const ROOT = path.resolve(__dirname, '..'); +const OUT_DIR = path.join(ROOT, 'output'); +const SOURCES_FILE = path.join(ROOT, 'js', 'data', 'icon-sources.js'); + +/** 已知别名 / 子集:这些 Iconify id 与本地已有源是同一套图标,不应重复收录 */ +const ALIAS = { + ph: 'phosphor', + bi: 'bootstrap', + bx: 'boxicons', + ri: 'remix', + 'ant-design': 'antd', + 'radix-icons': 'radix', + 'game-icons': 'gameicons', + 'heroicons-outline': 'heroicons', + 'heroicons-solid': 'heroicons', + 'tabler-flags': 'tabler', + fa: 'fontawesome', + 'fa-solid': 'fontawesome', + 'fa-regular': 'fontawesome', + 'fa-brands': 'fontawesome', + 'fa6-solid': 'fontawesome', + 'fa6-regular': 'fontawesome', + 'fa6-brands': 'fontawesome' +}; + +/** 已知品牌/商标风险集合(可收录但需标注,不建议用于 LOGO) */ +const BRAND_RISK = new Set(['arcticons', 'wordpress', 'brandico', 'entypo-social', 'fa6-brands']); + +function fetchJson(url) { + return new Promise((resolve, reject) => { + https.get(url, { headers: { 'User-Agent': 'drawlogo-source-discovery' } }, res => { + let data = ''; + res.on('data', c => { data += c; }); + res.on('end', () => { + try { resolve(JSON.parse(data)); } + catch (e) { reject(new Error('解析失败: ' + url)); } + }); + }).on('error', reject); + }); +} + +function readExistingIds() { + const text = fs.readFileSync(SOURCES_FILE, 'utf8'); + return new Set([...text.matchAll(/\bid:\s*'([^']+)'/g)].map(m => m[1])); +} + +/** 协议是否允许商用(与项目规则一致) */ +function isCommercial(spdx) { + const s = String(spdx || ''); + if (!s) return false; + return !/(^|[\s-])(NC|SA|ND)([\s-]|$)|NonCommercial|GPL/i.test(s); +} + +function pad(s, n) { + s = String(s); + // 中文按 2 字符宽估算 + let width = 0; + for (const ch of s) width += /[\u4e00-\u9fa5]/.test(ch) ? 2 : 1; + return s + ' '.repeat(Math.max(0, n - width)); +} + +async function main() { + console.log('画个标 · 素材源发现器\n'); + + const existing = readExistingIds(); + console.log(`本地已收录源: ${existing.size}`); + + const collections = await fetchJson('https://api.iconify.design/collections'); + const allIds = Object.keys(collections); + console.log(`Iconify 官方集合: ${allIds.length}\n`); + + const covered = []; + const candidates = []; + const aliases = []; + const nonCommercial = []; + + for (const id of allIds) { + const info = collections[id] || {}; + const item = { + id, + name: info.name || id, + total: info.total || 0, + license: (info.license && info.license.spdx) || (info.license && info.license.title) || '未知', + author: info.author && info.author.name ? info.author.name : '', + home: info.author && info.author.url ? info.author.url : '', + brandRisk: BRAND_RISK.has(id) + }; + + if (existing.has(id)) { covered.push(item); continue; } + if (ALIAS[id]) { aliases.push({ ...item, aliasOf: ALIAS[id] }); continue; } + if (!isCommercial(item.license)) { nonCommercial.push(item); continue; } + + // 预判拉取方式:Iconify 集合统一走 npm @iconify-json/ + format:'iconify' + scaleMode:'body' + item.suggestConfig = { + format: 'iconify', + npm: '@iconify-json/' + id, + scaleMode: 'body', + type: item.brandRisk ? 'brand' : 'icon' + }; + candidates.push(item); + } + + candidates.sort((a, b) => b.total - a.total); + + if (process.argv.includes('--json')) { + console.log(JSON.stringify({ candidates, aliases, nonCommercial }, null, 2)); + return; + } + + console.log('=== 可直接新增(协议合规、非别名)==='); + console.log(pad('ID', 22) + pad('图标数', 9) + pad('协议', 18) + pad('品牌风险', 10) + '名称'); + console.log('-'.repeat(100)); + for (const c of candidates) { + console.log( + pad(c.id, 22) + + pad(c.total.toLocaleString('zh-CN'), 9) + + pad(c.license, 18) + + pad(c.brandRisk ? '是' : '-', 10) + + c.name + ); + } + + console.log('\n=== 别名 / 子集(跳过,避免重复)==='); + console.log(aliases.map(a => `${a.id} → ${a.aliasOf}`).join(', ') || '无'); + + console.log('\n=== 协议不合规(跳过)==='); + console.log(nonCommercial.map(a => `${a.id}(${a.license})`).join(', ') || '无'); + + const totalIcons = candidates.reduce((a, b) => a + b.total, 0); + console.log('\n=== 汇总 ==='); + console.log(`已覆盖: ${covered.length}`); + console.log(`可新增独立源: ${candidates.length} 个,约 ${totalIcons.toLocaleString('zh-CN')} 个图标`); + console.log(`其中品牌风险: ${candidates.filter(c => c.brandRisk).length} 个`); + console.log(`别名/子集: ${aliases.length} 个`); + console.log(`协议不合规: ${nonCommercial.length} 个`); + + fs.mkdirSync(OUT_DIR, { recursive: true }); + const outFile = path.join(OUT_DIR, 'source-candidates.json'); + fs.writeFileSync(outFile, JSON.stringify({ + scannedAt: new Date().toISOString(), + iconifyTotal: allIds.length, + covered: covered.length, + candidates, + aliases, + nonCommercial + }, null, 2)); + console.log(`\n候选清单已写入: ${path.relative(ROOT, outFile)}`); +} + +main().catch(e => { + console.error('发现失败:', e.message); + process.exit(1); +});