import path from 'path' import ejs from 'ejs' import { ConeScaffoldProps, PkgJsonPatch, Obj, ProjectFileProcessor, } from '../type' import { formatJson, formatCode } from '../prettier' import { isExtJs, isExtTs } from '../js' import { processCss, isExtCss } from '../css' import { processJs } from './js' import { PKG_JSON_FILE_NAME } from '../package-json' import { updateLowcodeRoutes } from './lowcode-routes' import { updateLowcodeBlocks } from './lowcode-blocks' import { CONE_CONFIG_FILE_NAME, updateConeConfig } from './cone-config' import { BUILD_JSON_FILE_NAME, updateBuildJson } from './build-json' import { updatePackageJson } from './package-json' import { updateConfigsRoutes } from './configsRoutes' import { updateLayout } from './project-layout' import { updateCssVar } from './css-var' export const processorMap: Record = { [CONE_CONFIG_FILE_NAME]: updateConeConfig, [BUILD_JSON_FILE_NAME]: updateBuildJson, [PKG_JSON_FILE_NAME]: updatePackageJson, 'src/configs/lowcode/routes.json': updateLowcodeRoutes, 'src/configs/lowcode/blocks.json': updateLowcodeBlocks, 'src/configs/routes.ts': updateConfigsRoutes, 'src/configs/routes.js': updateConfigsRoutes, 'src/pages/_layout.jsx': updateLayout, 'src/pages/_layout.tsx': updateLayout, 'src/styles/vars/index.scss': updateCssVar, } export async function postProcessFile({ srcDir, destDir, filepath, str, props, pkgJsonPatch, }: { srcDir: string destDir: string filepath: string str: string | Obj props: ConeScaffoldProps pkgJsonPatch?: PkgJsonPatch }): Promise { const absFilePath = path.resolve(srcDir, filepath) if (typeof str === 'object') { str = JSON.stringify(str, null, 2) + '\n' } // ejs render try { str = ejs.render(str, props, { filename: filepath }) } catch (err) { // ignore render err } // extra process const fn = processorMap[filepath] if (fn) { let obj = {} const isJson = filepath.endsWith('.json') if (isJson) { obj = JSON.parse(str) as Obj } const rtn = await fn({ srcDir, destDir, filepath, str, obj, props, pkgJsonPatch, }) if (typeof rtn === 'string') { str = rtn } else { str = await formatJson({ filepath: absFilePath, obj: rtn }) } } // 处理 js if (isExtJs(filepath) || isExtTs(filepath)) { str = processJs({ str, filepath, props, }) str = await formatCode({ filepath: absFilePath, str }) } // 处理 css if (isExtCss(filepath)) { str = processCss({ str, filepath, props }) } return str }