primevue-mirror/apps/showcase/api-generator/build-api.js

176 lines
4.4 KiB
JavaScript
Raw Normal View History

2022-09-14 14:26:41 +00:00
const fs = require('fs');
const path = require('path');
2022-09-14 14:26:41 +00:00
const rootDir = path.resolve(__dirname, '../');
const distDir = path.resolve(rootDir, 'dist');
const componentPath = path.join(__dirname, './components');
// Import project `package.json`
2022-09-14 14:26:41 +00:00
const pkg = require(path.resolve(rootDir, 'package.json'));
const libraryName = 'PrimeVue';
const libraryVersion = pkg.version;
2023-03-07 14:43:46 +00:00
const showcaseURL = 'https://primevue.org/';
const fileModules = {},
veturTags = {},
veturAttributes = {};
const files = fs.readdirSync(componentPath);
2022-12-20 12:32:32 +00:00
2022-09-14 14:26:41 +00:00
files.forEach((file) => {
const { name } = path.parse(file);
fileModules[name] = require(`./components//${name}`);
});
const webTypes = {
2022-09-14 14:26:41 +00:00
$schema: 'https://raw.githubusercontent.com/JetBrains/web-types/master/schema/web-types.json',
framework: 'vue',
name: libraryName,
version: libraryVersion,
contributions: {
html: {
2022-09-14 14:26:41 +00:00
'types-syntax': 'typescript',
'description-markup': 'markdown',
tags: [],
attributes: []
}
}
};
const addURL = (attr, url, value) => {
const newArray = [];
2022-09-14 14:26:41 +00:00
attr.forEach((att) => {
const newObj = {};
2022-09-14 14:26:41 +00:00
Object.keys(att).forEach((key) => {
newObj[key] = att[key];
2022-09-14 14:26:41 +00:00
if (key === value) newObj['doc-url'] = url;
});
newArray.push(newObj);
});
return newArray;
};
2022-09-14 14:26:41 +00:00
const createWebTypes = (component) => {
const url = showcaseURL + `${component['doc-url'] ? component['doc-url'] : component.name.toLowerCase()}`;
// components
const tag = {
name: component.name,
source: {
module: libraryName,
symbol: component.name
},
2022-09-14 14:26:41 +00:00
'doc-url': url,
description: component.description
};
// directives
const attribute = {
name: component.name,
source: {
module: libraryName,
symbol: component.name
},
description: component.description,
2022-09-14 14:26:41 +00:00
'doc-url': url,
value: {
2022-09-14 14:26:41 +00:00
kind: 'expression',
type: 'function'
}
};
if (component.props) {
2022-09-14 14:26:41 +00:00
tag.attributes = addURL(component.props, url, 'default');
tag.attributes.forEach((k) => {
k['value'] = {
kind: 'expression',
type: k['type']
};
2022-09-14 14:26:41 +00:00
delete k['type'];
});
}
if (component.events) {
2022-09-14 14:26:41 +00:00
tag.events = addURL(component.events, url, 'name');
tag.events.forEach((k) => {
if (k.arguments) {
2022-09-14 14:26:41 +00:00
k.arguments = addURL(k.arguments, url, 'name');
}
});
}
if (component.slots) {
2022-09-14 14:26:41 +00:00
tag.slots = addURL(component.slots, url, 'name');
}
2022-09-14 14:26:41 +00:00
if (component['vue-modifiers']) {
attribute.required = false;
2022-09-14 14:26:41 +00:00
attribute['vue-modifiers'] = addURL(component['vue-modifiers'], url, 'name');
2022-09-14 14:26:41 +00:00
if (attribute['vue-modifiers'].length < 1) delete attribute['vue-modifiers'];
webTypes.contributions.html.attributes.push(attribute);
}
webTypes.contributions.html.tags.push(tag);
};
2022-09-14 14:26:41 +00:00
const createVeturTags = (component) => {
const attributes = [];
2022-12-20 12:32:32 +00:00
if (component.props) {
2022-09-14 14:26:41 +00:00
component.props.forEach((comp) => {
attributes.push(comp.name);
});
}
2022-12-20 12:32:32 +00:00
if (attributes.length > 0) {
veturTags[component.name] = {
description: component.description,
attributes
};
}
};
2022-09-14 14:26:41 +00:00
const createVeturAttributes = (component) => {
if (component.props) {
2022-09-14 14:26:41 +00:00
component.props.forEach((comp) => {
veturAttributes[component.name.toLowerCase() + `/${comp.name}`] = {
description: comp.description,
type: comp.type
};
});
}
};
2022-09-14 14:26:41 +00:00
Object.keys(fileModules).forEach((p) => {
2024-04-29 20:38:45 +00:00
const component = fileModules[p][p];
if (component) {
createWebTypes(component);
createVeturTags(component);
createVeturAttributes(component);
}
});
!fs.existsSync(distDir) && fs.mkdirSync(distDir);
const webTypesJson = JSON.stringify(webTypes, null, 2);
2022-12-20 12:32:32 +00:00
2022-09-14 14:26:41 +00:00
fs.writeFileSync(path.resolve(distDir, 'web-types.json'), webTypesJson);
const veturTagsJson = JSON.stringify(veturTags, null, 2);
2022-12-20 12:32:32 +00:00
2022-09-14 14:26:41 +00:00
fs.writeFileSync(path.resolve(distDir, 'vetur-tags.json'), veturTagsJson);
const veturAttributesJson = JSON.stringify(veturAttributes, null, 2);
2022-12-20 12:32:32 +00:00
2022-09-14 14:26:41 +00:00
fs.writeFileSync(path.resolve(distDir, 'vetur-attributes.json'), veturAttributesJson);