clean.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import fs from 'node:fs'
  2. import path from 'node:path'
  3. import { fileURLToPath } from 'node:url'
  4. const __filename = fileURLToPath(import.meta.url)
  5. const __dirname = path.dirname(__filename)
  6. /**
  7. * Recursively find the project root by looking for package.json
  8. * @param {string} dir - Current directory to check
  9. * @returns {string} - Path to the project root
  10. */
  11. function findProjectRoot(dir) {
  12. if (fs.existsSync(path.join(dir, 'package.json'))) {
  13. return dir
  14. }
  15. const parentDir = path.dirname(dir)
  16. if (parentDir === dir) {
  17. throw new Error('Could not find project root (package.json not found)')
  18. }
  19. return findProjectRoot(parentDir)
  20. }
  21. const projectRoot = findProjectRoot(__dirname)
  22. console.log(`Cleaning project at: ${projectRoot}`)
  23. // 1. Remove Directories and Files
  24. const pathsToRemove = [
  25. 'docs',
  26. 'src/subAsyncEcharts',
  27. 'src/subEcharts',
  28. 'src/subPages',
  29. 'src/pages.json',
  30. 'pnpm-workspace.yaml',
  31. ]
  32. pathsToRemove.forEach((p) => {
  33. const fullPath = path.join(projectRoot, p)
  34. if (fs.existsSync(fullPath)) {
  35. console.log(`Removing: ${p}`)
  36. fs.rmSync(fullPath, { recursive: true, force: true })
  37. }
  38. else {
  39. console.log(`Skipping (not found): ${p}`)
  40. }
  41. })
  42. // 2. Modify vite.config.ts
  43. const viteConfigPath = path.join(projectRoot, 'vite.config.ts')
  44. if (fs.existsSync(viteConfigPath)) {
  45. console.log('Modifying vite.config.ts...')
  46. let content = fs.readFileSync(viteConfigPath, 'utf-8')
  47. // Remove from UniHelperPages configuration
  48. // Matches subPackages: [ ... ] structure
  49. const subPackagesRegex = /(subPackages:\s*\[)([\s\S]*?)(\])/
  50. const match = content.match(subPackagesRegex)
  51. if (match) {
  52. let subPackagesContent = match[2]
  53. // Remove lines containing subEcharts or subAsyncEcharts
  54. subPackagesContent = subPackagesContent.replace(/.*'src\/subEcharts',\n?/g, '')
  55. subPackagesContent = subPackagesContent.replace(/.*'src\/subAsyncEcharts',\n?/g, '')
  56. content = content.replace(subPackagesRegex, `$1${subPackagesContent}$3`)
  57. fs.writeFileSync(viteConfigPath, content, 'utf-8')
  58. }
  59. }
  60. // 3. Modify package.json
  61. const packageJsonPath = path.join(projectRoot, 'package.json')
  62. if (fs.existsSync(packageJsonPath)) {
  63. console.log('Modifying package.json...')
  64. const pkg = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'))
  65. // Remove docs scripts
  66. let scriptsChanged = false
  67. if (pkg.scripts) {
  68. Object.keys(pkg.scripts).forEach((key) => {
  69. if (key.startsWith('docs:')) {
  70. delete pkg.scripts[key]
  71. scriptsChanged = true
  72. }
  73. })
  74. }
  75. if (scriptsChanged) {
  76. fs.writeFileSync(packageJsonPath, `${JSON.stringify(pkg, null, 2)}\n`, 'utf-8')
  77. }
  78. }
  79. console.log('Cleanup complete!')