verify-package-output.mjs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import fs from 'node:fs'
  2. import path from 'node:path'
  3. const root = process.cwd()
  4. const distDir = path.join(root, 'dist')
  5. const fail = message => {
  6. console.error(`[package-output] ${message}`)
  7. process.exit(1)
  8. }
  9. const assertFile = file => {
  10. const fullPath = path.join(root, file)
  11. if (!fs.existsSync(fullPath) || !fs.statSync(fullPath).isFile()) {
  12. fail(`Missing required package file: ${file}`)
  13. }
  14. }
  15. const assertMissing = file => {
  16. if (fs.existsSync(path.join(root, file))) {
  17. fail(`Unexpected demo artifact in library package output: ${file}`)
  18. }
  19. }
  20. assertFile('dist/index.mjs')
  21. assertFile('dist/src/package/index.d.ts')
  22. assertFile('dist/style.css')
  23. assertFile('dist/wasm/cad/dwg-worker.js')
  24. assertMissing('dist/index.html')
  25. assertMissing('dist/compare.html')
  26. const packageJson = JSON.parse(fs.readFileSync(path.join(root, 'package.json'), 'utf8'))
  27. const typeEntry = packageJson.exports?.['.']?.types || packageJson.types
  28. if (!typeEntry) {
  29. fail('package.json does not expose a type entry.')
  30. }
  31. const normalizedTypeEntry = typeEntry.replace(/^\.\//, '')
  32. if (!fs.existsSync(path.join(root, normalizedTypeEntry))) {
  33. fail(`package.json type entry does not exist: ${typeEntry}`)
  34. }
  35. console.log(`[package-output] Verified library package output in ${distDir}`)