-
-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add autogenerate component theme types (#775)
* refactor volar type generation * add component theme type generation * refactor component type definitions * update components class props descriptions
- Loading branch information
Showing
95 changed files
with
2,128 additions
and
1,954 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// The file is not designed to run directly. `cwd` should be project root. | ||
import fs from 'fs-extra' | ||
import path from 'path' | ||
import process from 'process' | ||
|
||
import { createComponentMetaChecker } from '../packages/oruga-next/node_modules/vue-component-meta/out/index.js'; | ||
|
||
import { componentDirectory, getFolders, getComponents, exist } from "./utils.mjs"; | ||
|
||
const __dirname = process.cwd() | ||
|
||
if(!exist(path.resolve(__dirname, componentDirectory))) | ||
throw new Error("Path not exist: " + componentDirectory); | ||
|
||
// create component meta checker | ||
const checker = createComponentMetaChecker( | ||
path.resolve(__dirname, './tsconfig.json'), | ||
{ | ||
forceUseTs: true, | ||
printer: { newLine: 1 }, | ||
// schema: { ignore: ['MyIgnoredNestedProps'] }, | ||
}, | ||
); | ||
|
||
// get all component folders | ||
const component_folders = getFolders(componentDirectory); | ||
|
||
const components = component_folders.map(folder => { | ||
const name = folder.toLowerCase(); | ||
const folderPath = path.resolve(__dirname, componentDirectory, folder); | ||
|
||
// get all components in component folder | ||
const components = getComponents(folderPath) | ||
|
||
// get all configruable props from all components in folder | ||
const props = components.flatMap(comp => { | ||
const file = comp.toLocaleLowerCase()+".vue"; | ||
const componentPath = path.resolve(__dirname, componentDirectory, name, file); | ||
const meta = checker.getComponentMeta(componentPath); | ||
|
||
return meta.props.filter(prop => { | ||
// filter only class props and configurable props | ||
if(prop.name.includes("Class")) return true; | ||
if(prop.default?.includes("getOption")) { | ||
const path = prop.default.match(/"(.*?)"/)[0]; | ||
return path.includes("."); | ||
} | ||
return false; | ||
}).map(prop => { | ||
// change type for class props | ||
if(prop.type === "ComponentClass") | ||
prop.type = "ClassDefinition"; | ||
|
||
if(prop.name.includes("Classes")) return prop; | ||
|
||
// change property name based on config path | ||
if(prop.default && prop.default?.includes("getOption")) { | ||
let name = prop.default.match(/"(.*?)"/)[0]; | ||
name = name.substring(1, name.length-1); | ||
const split = name.split("."); | ||
name = split.length == 2 ? split[1] : split[0]; | ||
if(prop.name !== name) | ||
prop.name = name; | ||
} | ||
return prop; | ||
}) | ||
}) | ||
// filter duplicates | ||
.filter((item, idx, self) => | ||
idx === self.findIndex(p => p.name === item.name) | ||
); | ||
|
||
return { name, props }; | ||
}); | ||
|
||
|
||
const code = `import type { | ||
ClassDefinition, | ||
ComponentConfigBase, | ||
DynamicComponent, | ||
} from "@/types"; | ||
// Auto generated component theme config definition | ||
declare module "../index" { | ||
interface OrugaOptions { | ||
${components.map(({name, props}) => | ||
`${name.toLowerCase()}?: ComponentConfigBase & | ||
Partial<{${ | ||
props.map(prop =>` | ||
/** ${prop.description} */ | ||
${prop.name}: ${prop.type};` | ||
).join("") | ||
} | ||
}>;` | ||
).join(` | ||
`)} | ||
} | ||
} | ||
`; | ||
|
||
const file = path.resolve(__dirname, componentDirectory, "types.ts"); | ||
fs.writeFileSync(path.resolve(__dirname, file), code, 'utf-8') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// The file is not designed to run directly. `cwd` should be project root. | ||
import fs from 'fs-extra'; | ||
import path from 'path'; | ||
|
||
// Components to be ignored | ||
const IGNORE = [ | ||
"FieldBody", | ||
"SliderThumb", | ||
"TableMobileSort", | ||
"TablePagination", | ||
"PaginationButton", | ||
"DatepickerTable", | ||
"DatepickerTableRow", | ||
"DatepickerMonth", | ||
"NotificationNotice", | ||
]; | ||
|
||
export const componentDirectory = './src/components'; | ||
|
||
export function exist (path) { | ||
return fs.existsSync(path) | ||
} | ||
|
||
export function getFolders(dir) { | ||
const folders = fs.readdirSync(dir) | ||
// remove test and util files | ||
.filter(f => !f.includes("tests") && !f.includes("utils") && !f.includes(".ts")); | ||
return folders; | ||
} | ||
|
||
export function getComponents(dir) { | ||
const files = fs.readdirSync(dir, { recursive: true }); | ||
return files | ||
// filter only vue files and remove test and util files | ||
.filter(f => f.includes(".vue") && !f.includes("tests") && !f.includes("utils")) | ||
// remove path | ||
.map(f => path.basename(f)) | ||
// remove .vue suffix | ||
.map(f => f.substring(0, f.indexOf(".vue"))) | ||
// filter blacklist | ||
.filter((key) => !IGNORE.includes(key)) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.