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, } = params; const metaFiles = `${cp.execSync( `cd plugins && find . -name "plugin-meta.json"` )}`.split('\n').filter(Boolean) .sort(); // console.log('files', metaFiles); const metas = metaFiles.map((metaFile) => { console.log('metaFile', metaFile); try { const metaFilePath = path.join(homeDir, 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 { id: metaDirRelativePath, name: meta.name, description: meta.description, mark: meta.mark, tags: meta.tags, readme: meta.readme, logo: logo ? `${urlPrefix}${metaDirRelativePath}/${logo}` : undefined, source: meta.source, bugs: meta.bugs, images: meta.images, locales: meta.locales, }; } catch (e) { console.error(`parse meta file ${metaFile} error`, e); } }); return metas; }; const main = async () => { const plugins = await run({ urlPrefix: prefix, homeDir: path.join(__dirname, '../plugins'), }); console.log(plugins); fs.writeFileSync(path.join(__dirname, '../plugins', 'plugins.json'), JSON.stringify(plugins, null, 2)); }; main().catch(e => { console.error(e); process.exit(-1); });