|
| 1 | +#!/usr/bin/env node |
| 2 | +'use strict' |
| 3 | + |
| 4 | +const fs = require('fs') |
| 5 | +const path = require('path') |
| 6 | + |
| 7 | +const ROOT = path.resolve(__dirname, '..') |
| 8 | +const pkg = JSON.parse(fs.readFileSync(path.join(ROOT, 'package.json'), 'utf8')) |
| 9 | +const plugin = JSON.parse(fs.readFileSync(path.join(ROOT, 'plugin.json'), 'utf8')) |
| 10 | +const readme = fs.readFileSync(path.join(ROOT, 'README.md'), 'utf8') |
| 11 | + |
| 12 | +const errors = [] |
| 13 | + |
| 14 | +function expect(condition, message) { |
| 15 | + if (!condition) errors.push(message) |
| 16 | +} |
| 17 | + |
| 18 | +function nonEmptyString(value) { |
| 19 | + return typeof value === 'string' && value.trim().length > 0 |
| 20 | +} |
| 21 | + |
| 22 | +function readUrl(field) { |
| 23 | + if (typeof field === 'string') return field |
| 24 | + if (field && typeof field === 'object' && typeof field.url === 'string') return field.url |
| 25 | + return '' |
| 26 | +} |
| 27 | + |
| 28 | +const keywords = Array.isArray(pkg.keywords) |
| 29 | + ? pkg.keywords.filter(item => nonEmptyString(item)) |
| 30 | + : [] |
| 31 | +const files = Array.isArray(pkg.files) |
| 32 | + ? pkg.files.filter(item => nonEmptyString(item)) |
| 33 | + : [] |
| 34 | +const pluginKeywords = Array.isArray(plugin.keywords) |
| 35 | + ? plugin.keywords.filter(item => nonEmptyString(item)) |
| 36 | + : [] |
| 37 | +const pluginCategories = Array.isArray(plugin.categories) |
| 38 | + ? plugin.categories.filter(item => nonEmptyString(item)) |
| 39 | + : [] |
| 40 | + |
| 41 | +expect(nonEmptyString(pkg.name), 'package.json name 不能为空') |
| 42 | +expect(nonEmptyString(pkg.version), 'package.json version 不能为空') |
| 43 | +expect(nonEmptyString(pkg.description), 'package.json description 不能为空') |
| 44 | +expect(keywords.length >= 3, 'package.json keywords 至少需要 3 个非空条目') |
| 45 | +expect(nonEmptyString(pkg.license), 'package.json license 不能为空') |
| 46 | +expect(nonEmptyString(readUrl(pkg.repository)), 'package.json repository.url 不能为空') |
| 47 | +expect(nonEmptyString(readUrl(pkg.bugs)), 'package.json bugs.url 不能为空') |
| 48 | +expect(nonEmptyString(pkg.homepage), 'package.json homepage 不能为空') |
| 49 | +expect(nonEmptyString(pkg.engines && pkg.engines.node), 'package.json engines.node 不能为空') |
| 50 | +expect(nonEmptyString(pkg.publishConfig && pkg.publishConfig.registry), 'package.json publishConfig.registry 不能为空') |
| 51 | +expect(nonEmptyString(pkg.publishConfig && pkg.publishConfig.access), 'package.json publishConfig.access 不能为空') |
| 52 | +expect(files.length > 0, 'package.json files 不能为空') |
| 53 | +expect( |
| 54 | + nonEmptyString(pkg.main) || |
| 55 | + (pkg.exports && Object.keys(pkg.exports).length > 0) || |
| 56 | + (pkg.bin && Object.keys(pkg.bin).length > 0), |
| 57 | + 'package.json 至少需要 main / exports / bin 之一' |
| 58 | +) |
| 59 | + |
| 60 | +expect(nonEmptyString(plugin.version), 'plugin.json version 不能为空') |
| 61 | +expect(plugin.version === pkg.version, 'plugin.json version 必须与 package.json version 一致') |
| 62 | +expect(nonEmptyString(plugin.description), 'plugin.json description 不能为空') |
| 63 | +expect(nonEmptyString(plugin.repository), 'plugin.json repository 不能为空') |
| 64 | +expect(nonEmptyString(plugin.homepage), 'plugin.json homepage 不能为空') |
| 65 | +expect(nonEmptyString(plugin.license), 'plugin.json license 不能为空') |
| 66 | +expect(pluginKeywords.length >= 3, 'plugin.json keywords 至少需要 3 个非空条目') |
| 67 | +expect(pluginCategories.length > 0, 'plugin.json categories 不能为空') |
| 68 | + |
| 69 | +if ((pkg.publishConfig.registry || '').includes('npm.pkg.github.com') || pkg.publishConfig.access === 'restricted') { |
| 70 | + for (const needle of ['GitHub Packages', 'npm.pkg.github.com', 'NODE_AUTH_TOKEN']) { |
| 71 | + expect(readme.includes(needle), `README 必须显式说明 GitHub Packages 安装边界:${needle}`) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +if (errors.length) { |
| 76 | + console.error('\x1b[31m✗ Release metadata checks failed:\x1b[0m') |
| 77 | + for (const message of errors) { |
| 78 | + console.error(` - ${message}`) |
| 79 | + } |
| 80 | + process.exit(1) |
| 81 | +} |
| 82 | + |
| 83 | +console.log('\x1b[32m✓ Release metadata checks passed\x1b[0m') |
0 commit comments