|
| 1 | +import fse from 'fs-extra' |
| 2 | +import path from 'path' |
| 3 | + |
| 4 | +import { toCamelCase, toPascalCase } from '../utils/casing.js' |
| 5 | + |
| 6 | +/** |
| 7 | + * Configures a plugin project by updating all package name placeholders to projectName |
| 8 | + */ |
| 9 | +export const configurePluginProject = ({ |
| 10 | + projectDirPath, |
| 11 | + projectName, |
| 12 | +}: { |
| 13 | + projectDirPath: string |
| 14 | + projectName: string |
| 15 | +}) => { |
| 16 | + const devPayloadConfigPath = path.resolve(projectDirPath, './dev/payload.config.ts') |
| 17 | + const devTsConfigPath = path.resolve(projectDirPath, './dev/tsconfig.json') |
| 18 | + const indexTsPath = path.resolve(projectDirPath, './src/index.ts') |
| 19 | + |
| 20 | + const devPayloadConfig = fse.readFileSync(devPayloadConfigPath, 'utf8') |
| 21 | + const devTsConfig = fse.readFileSync(devTsConfigPath, 'utf8') |
| 22 | + const indexTs = fse.readFileSync(indexTsPath, 'utf-8') |
| 23 | + |
| 24 | + const updatedTsConfig = devTsConfig.replaceAll('plugin-package-name-placeholder', projectName) |
| 25 | + let updatedIndexTs = indexTs.replaceAll('plugin-package-name-placeholder', projectName) |
| 26 | + |
| 27 | + const pluginExportVariableName = toCamelCase(projectName) |
| 28 | + |
| 29 | + updatedIndexTs = updatedIndexTs.replace( |
| 30 | + 'export const myPlugin', |
| 31 | + `export const ${pluginExportVariableName}`, |
| 32 | + ) |
| 33 | + |
| 34 | + updatedIndexTs = updatedIndexTs.replaceAll('MyPluginConfig', `${toPascalCase(projectName)}Config`) |
| 35 | + |
| 36 | + let updatedPayloadConfig = devPayloadConfig.replace( |
| 37 | + 'plugin-package-name-placeholder', |
| 38 | + projectName, |
| 39 | + ) |
| 40 | + |
| 41 | + updatedPayloadConfig = updatedPayloadConfig.replaceAll('myPlugin', pluginExportVariableName) |
| 42 | + |
| 43 | + fse.writeFileSync(devPayloadConfigPath, updatedPayloadConfig) |
| 44 | + fse.writeFileSync(devTsConfigPath, updatedTsConfig) |
| 45 | + fse.writeFileSync(indexTsPath, updatedIndexTs) |
| 46 | +} |
0 commit comments