-
-
Notifications
You must be signed in to change notification settings - Fork 242
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: remove userstyles from metadata
Instead, we're grabbing it from https://github.com/catppuccin/userstyles. Co-authored-by: rubyowo <perhaps-you-know@what-is.ml>
- Loading branch information
Showing
7 changed files
with
163 additions
and
262 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,110 +1,145 @@ | ||
#!/usr/bin/env -S deno run --allow-env --allow-read --allow-write | ||
|
||
import { Ajv, parseYaml, path, schema } from "./deps.ts"; | ||
import type { Categories, Port, Ports, Showcases } from "./types.d.ts"; | ||
#!/usr/bin/env -S deno run --allow-env --allow-read --allow-write --allow-net | ||
|
||
import { | ||
Ajv, | ||
parseYaml, | ||
path, | ||
portsSchema, | ||
userstylesSchema, | ||
userstylesYaml, | ||
Userstyle, | ||
Userstyles, | ||
} from './deps.ts' | ||
import type { Categories, Port, Ports, Showcases } from './types.d.ts' | ||
|
||
const root = new URL('.', import.meta.url).pathname | ||
|
||
type PortsMetadata = { | ||
categories: Categories | ||
ports: Ports | ||
showcases: Showcases | ||
} | ||
|
||
const root = new URL(".", import.meta.url).pathname; | ||
type UserstylesMetadata = { | ||
userstyles: Userstyles | ||
} | ||
|
||
type Metadata = { | ||
categories: Categories; | ||
ports: Ports; | ||
showcases: Showcases; | ||
}; | ||
const ajv = new Ajv() | ||
const validatePorts = ajv.compile<PortsMetadata>(portsSchema) | ||
const validateUserstyles = ajv.compile<PortsMetadata>(userstylesSchema) | ||
|
||
const ajv = new Ajv(); | ||
const validate = ajv.compile<Metadata>(schema); | ||
const portsYaml = Deno.readTextFileSync(path.join(root, '../ports.yml')) | ||
const portsData = parseYaml(portsYaml) as PortsMetadata | ||
|
||
const portsYaml = Deno.readTextFileSync(path.join(root, "../ports.yml")); | ||
const data = parseYaml(portsYaml) as Metadata; | ||
const userstylesData = parseYaml( | ||
await userstylesYaml.text() | ||
) as UserstylesMetadata | ||
|
||
// throw error if the YAML is invalid | ||
if (!validate(data)) { | ||
console.log(validate.errors); | ||
Deno.exit(1); | ||
if (!validatePorts(portsData)) { | ||
console.log(validatePorts.errors) | ||
Deno.exit(1) | ||
} | ||
|
||
if (!validateUserstyles(userstylesData)) { | ||
console.log(validateUserstyles.errors) | ||
Deno.exit(1) | ||
} | ||
|
||
export type MappedPort = Port & { html_url: string }; | ||
|
||
const categorized = Object.entries(data.ports).reduce((acc, [slug, port]) => { | ||
!acc[port.category] && (acc[port.category] = []); | ||
acc[port.category].push({ | ||
html_url: `https://github.com/catppuccin/${slug}`, | ||
...port, | ||
}); | ||
acc[port.category].sort((a, b) => a.name.localeCompare(b.name)); | ||
return acc; | ||
}, {} as Record<string, MappedPort[]>); | ||
|
||
const portListData = data.categories.map((category) => { | ||
return { | ||
meta: category, | ||
ports: categorized[category.key], | ||
}; | ||
}); | ||
const ports = Object.assign(portsData.ports, userstylesData.userstyles) | ||
|
||
export type MappedPort = (Port | Userstyle) & { html_url: string } | ||
|
||
const categorized = Object.entries(ports).reduce( | ||
(acc, [slug, port]: [string, MappedPort]) => { | ||
!acc[port.category] && (acc[port.category] = []) | ||
acc[port.category].push({ | ||
html_url: `https://github.com/catppuccin/${ | ||
port.readme ? `userstyles/tree/main/styles/${slug}` : slug | ||
}`, | ||
...port, | ||
name: [port.name].flat().join(', '), | ||
}) | ||
acc[port.category].sort((a, b) => a.name.localeCompare(b.name)) | ||
return acc | ||
}, | ||
{} as Record<string, MappedPort[]> | ||
) | ||
|
||
const portListData = portsData.categories.map((category) => { | ||
return { | ||
meta: category, | ||
ports: categorized[category.key], | ||
} | ||
}) | ||
|
||
const updateReadme = ({ | ||
readme, | ||
section, | ||
newContent, | ||
readme, | ||
section, | ||
newContent, | ||
}: { | ||
readme: string; | ||
section: string; | ||
newContent: string; | ||
readme: string | ||
section: string | ||
newContent: string | ||
}): string => { | ||
const preamble = | ||
"<!-- the following section is auto-generated, do not edit -->"; | ||
const markers = { | ||
start: `<!-- AUTOGEN:${section.toUpperCase()} START -->`, | ||
end: `<!-- AUTOGEN:${section.toUpperCase()} END -->`, | ||
}; | ||
|
||
const wrapped = markers.start + "\n" + preamble + "\n" + newContent + "\n" + | ||
markers.end; | ||
|
||
if ( | ||
!( | ||
readmeContent.includes(markers.start) && | ||
readmeContent.includes(markers.end) | ||
) | ||
) { | ||
throw new Error("Markers not found in README.md"); | ||
} | ||
|
||
const pre = readme.split(markers.start)[0]; | ||
const end = readme.split(markers.end)[1]; | ||
|
||
return pre + wrapped + end; | ||
}; | ||
|
||
const readmePath = path.join(root, "../../README.md"); | ||
let readmeContent = Deno.readTextFileSync(readmePath); | ||
|
||
const portContent = portListData.map((data) => { | ||
return `<details open> | ||
const preamble = | ||
'<!-- the following section is auto-generated, do not edit -->' | ||
const markers = { | ||
start: `<!-- AUTOGEN:${section.toUpperCase()} START -->`, | ||
end: `<!-- AUTOGEN:${section.toUpperCase()} END -->`, | ||
} | ||
|
||
const wrapped = | ||
markers.start + '\n' + preamble + '\n' + newContent + '\n' + markers.end | ||
|
||
if ( | ||
!( | ||
readmeContent.includes(markers.start) && | ||
readmeContent.includes(markers.end) | ||
) | ||
) { | ||
throw new Error('Markers not found in README.md') | ||
} | ||
|
||
const pre = readme.split(markers.start)[0] | ||
const end = readme.split(markers.end)[1] | ||
|
||
return pre + wrapped + end | ||
} | ||
|
||
const readmePath = path.join(root, '../../README.md') | ||
let readmeContent = Deno.readTextFileSync(readmePath) | ||
|
||
const portContent = portListData | ||
.map((data) => { | ||
return `<details open> | ||
<summary>${data.meta.emoji} ${data.meta.name}</summary> | ||
${data.ports.map((port) => `- [${port.name}](${port.html_url})`).join("\n")} | ||
${data.ports.map((port) => `- [${port.name}](${port.html_url})`).join('\n')} | ||
</details>`; | ||
}).join("\n"); | ||
</details>` | ||
}) | ||
.join('\n') | ||
|
||
const showcaseContent = data.showcases.map((showcase) => { | ||
return `- [${showcase.title}](${showcase.link}) - ${showcase.description}`; | ||
}).join("\n"); | ||
const showcaseContent = portsData.showcases | ||
.map((showcase) => { | ||
return `- [${showcase.title}](${showcase.link}) - ${showcase.description}` | ||
}) | ||
.join('\n') | ||
|
||
try { | ||
readmeContent = updateReadme({ | ||
readme: readmeContent, | ||
section: "portlist", | ||
newContent: portContent, | ||
}); | ||
readmeContent = updateReadme({ | ||
readme: readmeContent, | ||
section: "showcase", | ||
newContent: showcaseContent, | ||
}); | ||
readmeContent = updateReadme({ | ||
readme: readmeContent, | ||
section: 'portlist', | ||
newContent: portContent, | ||
}) | ||
readmeContent = updateReadme({ | ||
readme: readmeContent, | ||
section: 'showcase', | ||
newContent: showcaseContent, | ||
}) | ||
} catch (e) { | ||
console.log("Failed to update the README:", e); | ||
console.log('Failed to update the README:', e) | ||
} finally { | ||
Deno.writeTextFileSync(readmePath, readmeContent); | ||
Deno.writeTextFileSync(readmePath, readmeContent) | ||
} |
Oops, something went wrong.