|
| 1 | +import { readFileSync, writeFileSync } from 'node:fs' |
| 2 | +import { resolve } from 'node:path' |
| 3 | +import type { FeatureName } from '../constants/config.js' |
| 4 | +import type { InstallationType } from '../types/types.js' |
| 5 | +import { isFeatureSelected } from '../utils/utils.js' |
| 6 | +import { exec } from './exec.js' |
| 7 | + |
| 8 | +function patchPackageJson(projectFolder: string, features: FeatureName[]): void { |
| 9 | + const packageJsonPath = resolve(projectFolder, 'package.json') |
| 10 | + const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8')) |
| 11 | + |
| 12 | + if (!isFeatureSelected('subgraph', features)) { |
| 13 | + packageJson.scripts['subgraph-codegen'] = undefined |
| 14 | + } |
| 15 | + |
| 16 | + if (!isFeatureSelected('typedoc', features)) { |
| 17 | + packageJson.scripts['typedoc:build'] = undefined |
| 18 | + } |
| 19 | + |
| 20 | + if (!isFeatureSelected('vocs', features)) { |
| 21 | + packageJson.scripts['docs:build'] = undefined |
| 22 | + packageJson.scripts['docs:dev'] = undefined |
| 23 | + packageJson.scripts['docs:preview'] = undefined |
| 24 | + } |
| 25 | + |
| 26 | + if (!isFeatureSelected('husky', features)) { |
| 27 | + packageJson.scripts.prepare = undefined |
| 28 | + } |
| 29 | + |
| 30 | + writeFileSync(packageJsonPath, `${JSON.stringify(packageJson, null, 2)}\n`) |
| 31 | +} |
| 32 | + |
| 33 | +async function cleanupDemo(projectFolder: string): Promise<void> { |
| 34 | + await exec('rm -rf src/components/pageComponents/home', { cwd: projectFolder }) |
| 35 | + await exec('mkdir -p src/components/pageComponents/home', { cwd: projectFolder }) |
| 36 | + await exec('cp .install-files/home/index.tsx src/components/pageComponents/home/', { |
| 37 | + cwd: projectFolder, |
| 38 | + }) |
| 39 | +} |
| 40 | + |
| 41 | +async function cleanupSubgraph(projectFolder: string, features: FeatureName[]): Promise<void> { |
| 42 | + await exec('rm -rf src/subgraphs', { cwd: projectFolder }) |
| 43 | + |
| 44 | + if (isFeatureSelected('demo', features)) { |
| 45 | + const homeFolder = 'src/components/pageComponents/home' |
| 46 | + |
| 47 | + await exec(`rm -rf ${homeFolder}/Examples/demos/subgraphs`, { cwd: projectFolder }) |
| 48 | + await exec(`rm ${homeFolder}/Examples/index.tsx`, { cwd: projectFolder }) |
| 49 | + await exec(`cp .install-files/home/Examples/index.tsx ${homeFolder}/Examples/index.tsx`, { |
| 50 | + cwd: projectFolder, |
| 51 | + }) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +async function cleanupTypedoc(projectFolder: string): Promise<void> { |
| 56 | + await exec('rm typedoc.json', { cwd: projectFolder }) |
| 57 | +} |
| 58 | + |
| 59 | +async function cleanupVocs(projectFolder: string): Promise<void> { |
| 60 | + await exec('rm vocs.config.ts', { cwd: projectFolder }) |
| 61 | + await exec('rm -rf docs', { cwd: projectFolder }) |
| 62 | +} |
| 63 | + |
| 64 | +async function cleanupHusky(projectFolder: string): Promise<void> { |
| 65 | + await exec('rm -rf .husky', { cwd: projectFolder }) |
| 66 | + await exec('rm .lintstagedrc.mjs', { cwd: projectFolder }) |
| 67 | + await exec('rm commitlint.config.js', { cwd: projectFolder }) |
| 68 | +} |
| 69 | + |
| 70 | +export async function cleanupFiles( |
| 71 | + projectFolder: string, |
| 72 | + mode: InstallationType, |
| 73 | + features: FeatureName[] = [], |
| 74 | +): Promise<void> { |
| 75 | + if (mode === 'custom') { |
| 76 | + if (!isFeatureSelected('demo', features)) { |
| 77 | + await cleanupDemo(projectFolder) |
| 78 | + } |
| 79 | + |
| 80 | + if (!isFeatureSelected('subgraph', features)) { |
| 81 | + await cleanupSubgraph(projectFolder, features) |
| 82 | + } |
| 83 | + |
| 84 | + if (!isFeatureSelected('typedoc', features)) { |
| 85 | + await cleanupTypedoc(projectFolder) |
| 86 | + } |
| 87 | + |
| 88 | + if (!isFeatureSelected('vocs', features)) { |
| 89 | + await cleanupVocs(projectFolder) |
| 90 | + } |
| 91 | + |
| 92 | + if (!isFeatureSelected('husky', features)) { |
| 93 | + await cleanupHusky(projectFolder) |
| 94 | + } |
| 95 | + |
| 96 | + patchPackageJson(projectFolder, features) |
| 97 | + } |
| 98 | + |
| 99 | + await exec('rm -rf .install-files', { cwd: projectFolder }) |
| 100 | +} |
0 commit comments