import path from 'path' import defaults from 'lodash/defaults' import merge from 'lodash/merge' import pick from 'lodash/pick' import { ConeConfig, ConeScaffoldProps, Obj, ProjectFileProcessor, } from '../type' import { loadJson } from '../json' import { formatJson } from '../prettier' import { pathExists, outputJsonWithSpaces } from '../fs' import log from '../log' import { DefaultPcTheme, DefaultConePlatformTheme, DefaultMobileTheme, } from './theme' export const CONE_CONFIG_FILE_NAME = 'cone.conf.json' // 确保 json key 的更新顺序 // 值要填满, 免得 ejs render 报错 const defaultConeConfigScaffold: Required = { gitGroup: '', name: '', title: '', description: '', version: '0.1.0', port: '7200', i18nCheck: true, i18nCheckReportError: false, ts: false, mobile: false, oneCode: true, lowcode: true, useLowcodeConfigs: false, routerMode: 'browser', useArms: true, armsPid: 'your-arms-project-id', useAem: false, aemPid: 'your-aem-project-id', pandaPid: 'your-panda-project-id', pandaVersion: '1', useDemo: true, layoutPackageName: '', themePackageName: DefaultPcTheme, buildPluginPackageName: '', coneCliPluginPackageName: '', coneMaterialTemplatePackageName: '', templateProcessorPackageName: '', } export async function checkIsConeProject({ dir = process.cwd(), throwWhenNotValid = false, }: { dir?: string throwWhenNotValid?: boolean } = {}) { const exists = await pathExists(path.join(dir, CONE_CONFIG_FILE_NAME)) if (!exists && throwWhenNotValid) { const msg = `找不到 ${CONE_CONFIG_FILE_NAME}, 请确认目录 ${dir} 是合法的 cone 应用` log.error('cone', msg) process.exit(-1) } return exists } export async function loadConeConfig(dir: string): Promise { return loadJson(dir, CONE_CONFIG_FILE_NAME) as unknown as ConeConfig } export async function writeConeConfig( dir: string, coneConfig: ConeConfig, ): Promise { await outputJsonWithSpaces(path.join(dir, CONE_CONFIG_FILE_NAME), coneConfig) } export async function getProjectScaffoldConfig( dir: string, ): Promise { const coneConfig = await loadConeConfig(dir) return merge({}, defaultConeConfigScaffold, coneConfig.scaffold || {}) } export function getConeAssetPkgs(props: ConeScaffoldProps) { const { coneMaterialTemplatePackageName } = props if (!coneMaterialTemplatePackageName) { return [] } const pkgs = Array.isArray(coneMaterialTemplatePackageName) ? coneMaterialTemplatePackageName : coneMaterialTemplatePackageName.split(',') return pkgs.filter((item) => item) } export const updateConeConfig: ProjectFileProcessor = async ({ obj, props, }) => { const { mobile, oneCode } = props const coneConfig = merge({}, { scaffold: defaultConeConfigScaffold }, obj, { // 只存已知的 key, 防止脏数据污染 scaffold: pick(props, Object.keys(defaultConeConfigScaffold)), }) // 更新移动端的默认主题 if (mobile && !oneCode) { if ( coneConfig.scaffold.themePackageName === DefaultPcTheme || coneConfig.scaffold.themePackageName === DefaultConePlatformTheme ) { coneConfig.scaffold.themePackageName = DefaultMobileTheme } } return coneConfig }