shoelace/scripts/make-vscode-data.js

76 wiersze
1.7 KiB
JavaScript
Czysty Zwykły widok Historia

//
// This script generates vscode.html-custom-data.json (for IntelliSense).
//
// You must generate dist/custom-elements.json before running this script.
//
import chalk from 'chalk';
import fs from 'fs';
const metadata = JSON.parse(fs.readFileSync('./dist/custom-elements.json', 'utf8'));
2021-06-28 13:31:22 +00:00
function getAllComponents() {
const allComponents = [];
metadata.modules.map(module => {
2021-07-13 20:43:18 +00:00
module.declarations?.map(declaration => {
if (declaration.customElement) {
const component = declaration;
2021-06-28 12:34:37 +00:00
2021-06-28 12:53:31 +00:00
if (component) {
2021-07-13 20:43:18 +00:00
allComponents.push(component);
2021-06-28 12:53:31 +00:00
}
}
});
});
return allComponents;
}
2021-06-28 13:31:22 +00:00
console.log('Generating custom data for VS Code');
const components = getAllComponents();
const vscode = { tags: [] };
components.map(component => {
const name = component.tag;
const attributes = component.attributes?.map(attr => {
const type = attr.type?.text;
let values = [];
if (type) {
type.split('|').map(val => {
val = val.trim();
// Only accept values that are strings and numbers
const isString = val.startsWith(`'`);
const isNumber = Number(val).toString() === val;
if (isString) {
// Remove quotes
val = val.replace(/^'/, '').replace(/'$/, '');
}
if (isNumber || isString) {
values.push({ name: val });
}
});
}
if (values.length === 0) {
values = undefined;
}
return {
name: attr.name,
description: attr.description,
values
};
});
vscode.tags.push({ name, attributes });
});
fs.writeFileSync('./dist/vscode.html-custom-data.json', JSON.stringify(vscode, null, 2), 'utf8');
2021-06-28 12:34:37 +00:00
console.log(chalk.cyan(`Successfully generated custom data for VS Code 🔮\n`));