const fs = require('fs'); const path = require('path'); const cp = require('child_process'); const { getProcessArgs } = require('./util'); const envs = getProcessArgs(); const { prefix } = envs; const run = (params) => { const { urlPrefix, homeDir, category } = params; const metaFiles = `${cp.execSync( `cd plugins/${category} && find . -name "plugin-meta.json"` )}`.split('\n').filter(Boolean) .sort(); console.log('files', metaFiles); const metas = metaFiles.map((metaFile) => { try { const metaFilePath = path.join(homeDir,category, metaFile); const metaDirPath = path.dirname(metaFilePath); const metaDirRelativePath = path.relative(homeDir, metaDirPath); const meta = require(metaFilePath); const { logo, locales } = meta; // 判断 readme 、logo是否存在不该在gather里做。如果不存在报错的话整体不可用。 meta.readme = urlPrefix + path.join(metaDirRelativePath, meta.readme || 'README.md'); if (locales) { Object.keys(locales).forEach(lang => { locales[lang].readme = urlPrefix + path.join(metaDirRelativePath, locales[lang].readme); }) } return { // 如果插件市场一个category下有重名name,这里可以考虑加个区别的内容 id: metaDirRelativePath, name: meta.name, description: meta.description, tags: meta.tags, readme: meta.readme, logo: logo ? `${urlPrefix}${metaDirRelativePath}/${logo}` : undefined, source: meta.source, bugs: meta.bugs, images: meta.images, locales: meta.locales, // 方便有问题追溯 generate_from: 'cnb/plugins/cnbcool/market', }; } catch (e) { console.error(`parse meta file ${metaFile} error`, e); } }); console.log('metas', metas); fs.writeFileSync(path.join(homeDir, category, 'plugins.json'), JSON.stringify(metas, null, 2)); }; const main = async () => { await run({ urlPrefix: prefix, homeDir: path.join(__dirname, '../plugins'), category: 'cnbcool', }); await run({ urlPrefix: prefix, homeDir: path.join(__dirname, '../plugins'), category: 'tencentcom', }); }; try { main(); } catch (e) { console.error(e); process.exit(-1); }