Skip to content

Commit

Permalink
feat: add init metarials cli to yunti-ui-lowcode-materials
Browse files Browse the repository at this point in the history
  • Loading branch information
Jandiasnow committed Apr 23, 2024
1 parent 16103c7 commit 42cb851
Show file tree
Hide file tree
Showing 6 changed files with 808 additions and 71 deletions.
135 changes: 135 additions & 0 deletions packages/yunti-ui-lowcode-materials/cli/files.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import fse from 'fs-extra'
import path from 'path'
import { formatComponentSchema } from './utils.mjs'

export const mkfileView = ({ rootDir, lowcodeDir }) => {

const filePath = path.resolve(rootDir,`${lowcodeDir}/view.tsx`)

if(fse.existsSync(filePath)) return

const fileContent = `export * from '@yuntijs/ui';`

fse.outputFileSync(filePath, fileContent);

}

export const mkfileUtils= ({ rootDir, lowcodeDir }) => {

const filePath = path.resolve(rootDir,`${lowcodeDir}/utils.ts`)

if(fse.existsSync(filePath)) return

const fileContent = `import { IPublicTypeFieldConfig } from '@alilc/lowcode-types';
// 获取组件优先级
export const getPriority = (componentName: string) => {
const priorities = {};
const SortArr = ['Tree'];
for (const [i, component] of SortArr.entries()) {
priorities[component] = SortArr.length - i;
}
return priorities[componentName];
};
export const COMMON_CONFIGURE_PROPS: IPublicTypeFieldConfig[] = [
{
title: '通用配置',
display: 'block',
type: 'group',
items: [
{
name: 'id',
title: { label: '唯一 id', tip: 'id' },
setter: [{ componentName: 'StringSetter' }],
},
{
name: 'className',
title: { label: '类名', tip: '自定义样式类名' },
setter: [{ componentName: 'StringSetter' }],
},
],
},
];`

fse.outputFileSync(filePath, fileContent);

}

export const mkfileMeta = ({ rootDir, lowcodeDir, componentsMetaArr = [] }) => {

const filePath = path.resolve(rootDir,`${lowcodeDir}/meta.ts`)

const fileContent = `import pkgJson from '../package.json';
${
componentsMetaArr.map(item => `import { ${item.name} } from '${item.path}';
`).join('')
}
const components = [${componentsMetaArr.map(item => item.name).join(', ')}].map(c => {
if (c.npm) {
c.npm.version = pkgJson.version;
}
if (!c.group) {
c.group = 'YuntiUI 组件';
}
return c;
});
// 注意不要使用 default 导出
export { components };
`

fse.outputFileSync(filePath, fileContent);

}

export const mkfilesComponent = ({ rootDir, lowcodeDir, metaDevSubfix, metaFormat, component, packageInfo, componentNameFolder }) => {

const getComponentFilePath = (name) => `${lowcodeDir}/${componentNameFolder}/${name}${metaDevSubfix}.${metaFormat || 'ts'}`
const filePath = path.resolve(rootDir, getComponentFilePath('meta'))

if(fse.existsSync(filePath)) return

const { componentName } = component || {}

const schema = formatComponentSchema(component);
if (schema.title === packageInfo.name) {
schema.title = schema.componentName;
}
const { snippets } = schema;
const componentDescription = schema;
delete componentDescription.snippets;

const fileContent = `
import { IPublicTypeComponentMetadata } from '@alilc/lowcode-types';
import { COMMON_CONFIGURE_PROPS } from '../utils';
import { ${componentName}Snippets } from './snippets';
const ${componentName}MetaInfo: IPublicTypeComponentMetadata = ${JSON.stringify(
componentDescription,
null,
2,
).replace('"props": [\n', '"props": [\n ...COMMON_CONFIGURE_PROPS,\n')
};
export const ${componentName}Meta = {
...${componentName}MetaInfo,
snippets: ${componentName}Snippets
}
`

fse.outputFileSync(filePath, fileContent);


const snippetsFilePath = path.resolve(rootDir, getComponentFilePath('snippets'))
const snippetsFileContent = `
import { IPublicTypeSnippet } from '@alilc/lowcode-types';
export const ${componentName}Snippets: IPublicTypeSnippet[] = ${JSON.stringify(snippets, null, 2)};
`
fse.outputFileSync(snippetsFilePath, snippetsFileContent);

}

79 changes: 79 additions & 0 deletions packages/yunti-ui-lowcode-materials/cli/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@

import { getEntry, formatComponentSchema, resolvePkgJson } from './utils.mjs'
import parser from '@alilc/lowcode-material-parser'
import { mkfileMeta, mkfileUtils, mkfileView, mkfilesComponent } from './files.mjs'

const init = async ({
entryPath = "src/index.tsx",
rootDir = '',
devAlias = undefined,
components,
metaFormat = undefined,
lowcodeDir = 'lowcode',
skipComponents,
}) => {
const entry = getEntry(rootDir, entryPath);
let result = await parser.default({ accesser: 'local', entry, npmClient: 'npm' });

const packageInfo = await resolvePkgJson()

if (!result) {
// If the result is not parsed, the result is generated by default
result = [
formatComponentSchema({
componentName: PARSED_NPM_NAME.uniqueName,
npm: {
package: packageInfo.name,
version: packageInfo.version || "{{version}}",
exportName: 'default',
main: 'lib/index.js',
destructuring: false,
subName: '',
},
}),
];
} else if (result.length === 1 && result[0].componentName === 'default') {
result[0].componentName = PARSED_NPM_NAME.uniqueName;
if (result[0].title === 'default') {
result[0].title = PARSED_NPM_NAME.uniqueName;
}
}

const componentsMetaArr = [];

const metaDevSubfix = devAlias ? `.${devAlias}` : '';
const filteredComponents = result.filter((item) => {
return !skipComponents.includes(item.componentName) && (components ? components.includes(item.componentName) : true)
});

filteredComponents.forEach((component) => {

const componentNameFolder = component.componentName;

componentsMetaArr.push({
path: `./${componentNameFolder}/meta${metaDevSubfix}`,
name: `${component.componentName}Meta`,
})

mkfilesComponent({
rootDir,
lowcodeDir,
metaDevSubfix,
metaFormat,
component,
packageInfo,
componentNameFolder
})

});

mkfileView({rootDir, lowcodeDir})
mkfileUtils({rootDir, lowcodeDir})
mkfileMeta({rootDir, lowcodeDir, componentsMetaArr})

}

init({
components: undefined,
skipComponents: ['TreeNode', 'DirectoryTree', 'Item', 'MonacoDiffEditor', 'BaseMonacoEditor'],
})
Loading

0 comments on commit 42cb851

Please sign in to comment.