public-main.mjs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { existsSync } from 'node:fs'
  2. import { readdir, stat } from 'node:fs/promises'
  3. import { join } from 'node:path'
  4. export const openSourceMainForbiddenTopLevel = [
  5. '.env',
  6. '.env.local',
  7. '.eslintrc.cjs',
  8. '.prettierrc.json',
  9. '.vscode',
  10. 'build.sh',
  11. 'env.d.ts',
  12. 'index.html',
  13. 'pnpm-lock.yaml',
  14. 'public',
  15. 'scripts',
  16. 'src',
  17. 'yarn.lock'
  18. ]
  19. export const openSourceMainDefaultRoots = [
  20. 'README.md',
  21. 'README.en.md',
  22. 'BRANCHES.md',
  23. 'WRAPPER_ECOSYSTEM.md',
  24. 'LICENSE',
  25. 'package.json',
  26. 'pnpm-workspace.yaml',
  27. '.github',
  28. 'apps',
  29. 'packages',
  30. 'dist',
  31. 'demo',
  32. 'component-demo',
  33. 'docs',
  34. 'docs-dist',
  35. 'example',
  36. 'artifacts'
  37. ]
  38. export async function assertDirectory(path, label = path) {
  39. if (!existsSync(path)) {
  40. throw new Error(`Missing directory: ${label}`)
  41. }
  42. const info = await stat(path)
  43. if (!info.isDirectory()) {
  44. throw new Error(`Not a directory: ${label}`)
  45. }
  46. }
  47. export async function assertFile(path, label = path) {
  48. if (!existsSync(path)) {
  49. throw new Error(`Missing file: ${label}`)
  50. }
  51. const info = await stat(path)
  52. if (!info.isFile()) {
  53. throw new Error(`Not a file: ${label}`)
  54. }
  55. }
  56. export async function assertOpenSourceMainRepoLayout(repoDir, options = {}) {
  57. for (const entry of openSourceMainForbiddenTopLevel) {
  58. if (existsSync(join(repoDir, entry))) {
  59. throw new Error(`Forbidden private workspace entry found in open-source main repo: ${entry}`)
  60. }
  61. }
  62. if (existsSync(join(repoDir, 'packages', 'runtime'))) {
  63. throw new Error('Forbidden removed runtime package found in open-source main repo: packages/runtime')
  64. }
  65. if (!options.allowedRoots) {
  66. return
  67. }
  68. const allowedRoots = new Set([
  69. '.git',
  70. ...(Array.isArray(options.allowedRoots) ? options.allowedRoots : openSourceMainDefaultRoots)
  71. ])
  72. for (const entry of await readdir(repoDir)) {
  73. if (!allowedRoots.has(entry)) {
  74. throw new Error(`Unexpected top-level entry in open-source main repo: ${entry}`)
  75. }
  76. }
  77. }