Refactor icon validation logic by consolidating checks into 'validate-icons' script and removing obsolete 'validate-pr' script. The new implementation checks for invalid metadata, naming conventions, and improper attributes in added icons from the main branch.
This commit is contained in:
parent
691971020b
commit
e4fc15e2b3
|
|
@ -22,6 +22,28 @@ const getIconName = (icon) => {
|
|||
return icon.split('/').slice(-2).join('/')
|
||||
}
|
||||
|
||||
function getAddedIconsFromMain() {
|
||||
try {
|
||||
const output = execSync('git diff origin/main...HEAD --name-status', { encoding: 'utf-8' })
|
||||
const addedIcons = []
|
||||
|
||||
output.split('\n').forEach(line => {
|
||||
if (line.startsWith('A\t')) {
|
||||
const filePath = line.substring(2)
|
||||
// Filter only SVG files from icons/outline/ or icons/filled/ directories
|
||||
if (filePath.match(/^icons\/((outline|filled)\/.+\.svg)$/)) {
|
||||
// add icon without icons/ prefix
|
||||
addedIcons.push(filePath.replace(/^icons\//, ''))
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return addedIcons
|
||||
} catch (error) {
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
types.forEach(type => {
|
||||
const icons = globSync(join(ICONS_SRC_DIR, type, '*.svg')).sort()
|
||||
|
||||
|
|
@ -170,6 +192,45 @@ Object.entries(aliases).forEach(([type, replacers]) => {
|
|||
})
|
||||
})
|
||||
|
||||
const addedIcons = getAddedIconsFromMain()
|
||||
for (const icon of addedIcons) {
|
||||
const iconPath = join(ICONS_SRC_DIR, icon)
|
||||
|
||||
try {
|
||||
const { data, content } = parseMatter(iconPath)
|
||||
|
||||
if (data.unicode) {
|
||||
console.log(`⛔️ Icon \`${icon}\` has unicode, but should not have it`)
|
||||
error = true
|
||||
}
|
||||
|
||||
if (data.version) {
|
||||
console.log(`⛔️ New icon \`${icon}\` has version, but should not have it`)
|
||||
error = true
|
||||
}
|
||||
|
||||
if (!icon.match(/^[a-z0-9-]+$/)) {
|
||||
console.log(`⛔️ New icon \`${icon}\` has invalid name`)
|
||||
error = true
|
||||
}
|
||||
|
||||
// check if filled icon hasnt category
|
||||
if (icon.match(/^filled\//) && data.category) {
|
||||
console.log(`⛔️ New icon \`${icon}\` has category, but should not have it`)
|
||||
error = true
|
||||
}
|
||||
|
||||
// check if filled icon has tags
|
||||
if (icon.match(/^filled\//) && data.tags) {
|
||||
console.log(`⛔️ New icon \`${icon}\` has tags, but should not have it`)
|
||||
error = true
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(`⛔️ New icon \`${icon}\` has invalid metadata`)
|
||||
error = true
|
||||
}
|
||||
}
|
||||
|
||||
if (error) {
|
||||
process.exit(1)
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -1,71 +0,0 @@
|
|||
import { execSync } from 'child_process'
|
||||
import { basename } from 'path'
|
||||
import { parseMatter, ICONS_SRC_DIR } from './helpers.mjs'
|
||||
import { join } from 'path'
|
||||
|
||||
// Check icon files added relative to main branch (for PR)
|
||||
function getAddedIconsFromMain() {
|
||||
try {
|
||||
const output = execSync('git diff origin/main...HEAD --name-status', { encoding: 'utf-8' })
|
||||
const addedIcons = []
|
||||
|
||||
output.split('\n').forEach(line => {
|
||||
if (line.startsWith('A\t')) {
|
||||
const filePath = line.substring(2)
|
||||
// Filter only SVG files from icons/outline/ or icons/filled/ directories
|
||||
if (filePath.match(/^icons\/((outline|filled)\/.+\.svg)$/)) {
|
||||
// add icon without icons/ prefix
|
||||
addedIcons.push(filePath.replace(/^icons\//, ''))
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return addedIcons
|
||||
} catch (error) {
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
function validateIcons(icons) {
|
||||
|
||||
for (const icon of icons) {
|
||||
const iconPath = join(ICONS_SRC_DIR, icon)
|
||||
|
||||
try {
|
||||
const { data, content } = parseMatter(iconPath)
|
||||
|
||||
if (data.unicode) {
|
||||
console.log(`⛔️ Icon \`${icon}\` has unicode, but should not have it`)
|
||||
}
|
||||
|
||||
if (data.version) {
|
||||
console.log(`⛔️ Icon \`${icon}\` has version, but should not have it`)
|
||||
}
|
||||
|
||||
if (!icon.match(/^[a-z0-9-]+$/)) {
|
||||
console.log(`⛔️ Icon \`${icon}\` has invalid name`)
|
||||
}
|
||||
|
||||
// check if filled icon hasnt category
|
||||
if (icon.match(/^filled\//) && data.category) {
|
||||
console.log(`⛔️ Icon \`${icon}\` has category, but should not have it`)
|
||||
}
|
||||
|
||||
// check if filled icon has tags
|
||||
if (icon.match(/^filled\//) && data.tags) {
|
||||
console.log(`⛔️ Icon \`${icon}\` has tags, but should not have it`)
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(`⛔️ Icon \`${icon}\` has invalid metadata`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const addedIcons = getAddedIconsFromMain()
|
||||
|
||||
if (addedIcons.length > 0) {
|
||||
validateIcons(addedIcons)
|
||||
process.exit(0)
|
||||
} else {
|
||||
process.exit(0)
|
||||
}
|
||||
|
|
@ -51,27 +51,4 @@ jobs:
|
|||
with:
|
||||
filePath: ./comment-markup.md
|
||||
comment_tag: validate
|
||||
mode: recreate
|
||||
|
||||
- name: Validate PR
|
||||
id: validate-pr
|
||||
run: pnpm run --silent validate-pr > ./comment-markup-pr.md || true
|
||||
continue-on-error: true
|
||||
|
||||
- name: Check if icons were added
|
||||
id: check-icons
|
||||
run: |
|
||||
if [ -s ./comment-markup-pr.md ]; then
|
||||
echo "has_icons=true" >> $GITHUB_OUTPUT
|
||||
else
|
||||
echo "has_icons=false" >> $GITHUB_OUTPUT
|
||||
fi
|
||||
|
||||
- name: Comment PR
|
||||
if: steps.check-icons.outputs.has_icons == 'true'
|
||||
uses: thollander/actions-comment-pull-request@v2
|
||||
with:
|
||||
filePath: ./comment-markup-pr.md
|
||||
comment_tag: validate-pr
|
||||
mode: recreate
|
||||
|
||||
mode: recreate
|
||||
Loading…
Reference in New Issue