diff --git a/package.json b/package.json index b0faf374a47b..6035681f37b1 100644 --- a/package.json +++ b/package.json @@ -50,6 +50,7 @@ "jest": "^27.5.1", "lerna": "^4.0.0", "lint-staged": "^12.3.4", + "matcher": "^5.0.0", "only-allow": "^1.1.0", "prettier": "^2.5.1", "prettier-plugin-organize-imports": "^2.3.4", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b0a8219410a4..479f40ec6b55 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -25,6 +25,7 @@ importers: jest: ^27.5.1 lerna: ^4.0.0 lint-staged: ^12.3.4 + matcher: ^5.0.0 only-allow: ^1.1.0 prettier: ^2.5.1 prettier-plugin-organize-imports: ^2.3.4 @@ -61,6 +62,7 @@ importers: jest: 27.5.1_ts-node@10.6.0 lerna: 4.0.0 lint-staged: 12.3.4 + matcher: 5.0.0 only-allow: 1.1.0 prettier: 2.5.1 prettier-plugin-organize-imports: 2.3.4_prettier@2.5.1+typescript@4.6.2 @@ -10263,6 +10265,11 @@ packages: engines: {node: '>=10'} dev: false + /escape-string-regexp/5.0.0: + resolution: {integrity: sha512-/veY75JbMK4j1yjvuUxuVsiS/hr/4iHs9FTT6cgTexxdE0Ly/glccBAkloH/DofkjRbZU3bnoj38mOmhkZ0lHw==} + engines: {node: '>=12'} + dev: true + /escodegen/2.0.0: resolution: {integrity: sha512-mmHKys/C8BFUGI+MAWNcSYoORYLMdPzjrknd2Vc+bUsjN5bXcr8EhrNB+UTqfL1y3I9c4fw2ihgtMPQLBRiQxw==} engines: {node: '>=6.0'} @@ -13657,6 +13664,13 @@ packages: remove-accents: 0.4.2 dev: false + /matcher/5.0.0: + resolution: {integrity: sha512-s2EMBOWtXFc8dgqvoAzKJXxNHibcdJMV0gwqKUaw9E2JBJuGUK7DrNKrA6g/i+v72TT16+6sVm5mS3thaMLQUw==} + engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0} + dependencies: + escape-string-regexp: 5.0.0 + dev: true + /material-colors/1.2.6: resolution: {integrity: sha512-6qE4B9deFBIa9YSpOc9O0Sgc43zTeVYbgDT5veRKSlB2+ZuHNoVVxA1L/ckMUayV9Ay9y7Z/SZCLcGteW9i7bg==} dev: false diff --git a/scripts/checkPackageFiles.ts b/scripts/checkPackageFiles.ts index d1a5dab54b14..8b69151404c4 100644 --- a/scripts/checkPackageFiles.ts +++ b/scripts/checkPackageFiles.ts @@ -1,11 +1,38 @@ +import * as logger from '@umijs/utils/src/logger'; +import { readdirSync } from 'fs'; +import { isMatch } from 'matcher'; import path from 'path'; import { eachPkg, getPkgs } from './utils'; +const COMMON_IGNORES = [ + 'src', + '*.md', + 'tsconfig*.json', + 'fixtures', + 'node_modules', + 'package.json', +]; + const cwd = process.cwd(); +let missingDetected = false; eachPkg(getPkgs(), (opts) => { - console.log('Checking package:', opts.pkg); const pkg = require(path.join(cwd, 'packages', opts.pkg, 'package.json')); - pkg.files; - // TODO - // 检测 pkg.files 是否包含必要的和当前 pkg 下的文件和目录 + const base = path.join(cwd, 'packages', opts.pkg); + + const files = readdirSync(base).filter((f) => !isMatch(f, COMMON_IGNORES)); + const missingAddFiles = files.filter((f) => !isMatch(f, pkg.files)); + + if (missingAddFiles.length > 0) { + logger.error('Checking package:', opts.pkg); + logger.error( + ` "${missingAddFiles.join( + ', ', + )}" missing in the package.json files field`, + ); + missingDetected = true; + } }); + +if (missingDetected) { + process.exit(1); +}