Skip to content

Commit

Permalink
cleanup: make scripts more lightweight
Browse files Browse the repository at this point in the history
I’ve managed to make both of the scripts to require just one package. I’ve also updated the feed generator script to follow the new config of the feed package 1.1.0. And other small improvments, that were mentioned by the Deno Lint
  • Loading branch information
GabsEdits committed Nov 2, 2024
1 parent 5ae09b4 commit cb09758
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
26 changes: 13 additions & 13 deletions scripts/combine.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { walk } from "jsr:@std/fs";
import { parse } from "jsr:@std/yaml";

interface Stack {
Expand Down Expand Up @@ -31,27 +30,28 @@ async function parseYamlFile(filePath: string): Promise<unknown> {
return parse(fileContent);
}

async function collectData<T>(
async function collectData<T, D>(
dir: string,
transform: (data: all) => T,
transform: (data: D) => T,
): Promise<T[]> {
const items: T[] = [];

for await (
const entry of walk(dir, {
exts: [".yaml", ".yml"],
includeDirs: false,
})
) {
const data = await parseYamlFile(entry.path);
items.push(transform(data));
for await (const entry of Deno.readDir(dir)) {
// Only process files with .yaml or .yml extensions
if (
entry.isFile &&
(entry.name.endsWith(".yaml") || entry.name.endsWith(".yml"))
) {
const data = await parseYamlFile(`${dir}/${entry.name}`) as D;
items.push(transform(data));
}
}

return items;
}

function collectStacks(stacksDir: string): Promise<Stack[]> {
return collectData(stacksDir, (data) => ({
return collectData<Stack, Stack>(stacksDir, (data) => ({
name: data.name,
base: data.base,
packages: data.packages,
Expand All @@ -61,7 +61,7 @@ function collectStacks(stacksDir: string): Promise<Stack[]> {
}

function collectPkgManagers(pkgManagersDir: string): Promise<PkgManager[]> {
return collectData(pkgManagersDir, (data) => ({
return collectData<PkgManager, PkgManager>(pkgManagersDir, (data) => ({
name: data.name,
model: data.model,
needsudo: data.needsudo,
Expand Down
11 changes: 5 additions & 6 deletions scripts/feed.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Atom } from "jsr:@feed/feed";
import { dirname, join } from "jsr:@std/path";

interface Stack {
name: string;
Expand Down Expand Up @@ -31,10 +30,8 @@ interface UpdateData {
pkgManagers?: pkgManager[];
}

const __dirname = dirname(new URL(import.meta.url).pathname);

const data: UpdateData = JSON.parse(
await Deno.readTextFile(join(__dirname, "../_index.json")),
await Deno.readTextFile(new URL("../_index.json", import.meta.url).pathname),
);

const feed = new Atom({
Expand All @@ -44,7 +41,6 @@ const feed = new Atom({
id: "https://apx.vanillaos.org/",
link: "https://apx.vanillaos.org/",
language: "en",
updated: new Date(),
feedLinks: {
atom: "https://apx-community.vanillaos.org/feed.xml",
},
Expand Down Expand Up @@ -108,6 +104,9 @@ pkgManagers.forEach((pkgManager: pkgManager) => {
});
});

await Deno.writeTextFile(join(__dirname, "../feed.xml"), feed.build());
await Deno.writeTextFile(
new URL("../feed.xml", import.meta.url).pathname,
feed.build(),
);

console.log("Atom feed generated.");

0 comments on commit cb09758

Please sign in to comment.