-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(ts): Make deep file structures
Separate files by their roles, and merge tests.
- Loading branch information
Showing
20 changed files
with
233 additions
and
218 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,12 @@ | ||
{ | ||
"$schema": "https://deno.land/x/deno/cli/schemas/config-file.v1.json", | ||
"fmt": { "exclude": ["LICENSE", ".github/**/*.md"] }, | ||
"test": { "include": ["src/", "test/"] }, | ||
"tasks": { | ||
"gen": "deno run --allow-env --allow-read --allow-write ./src/main.ts", | ||
"test": "deno test --allow-env --allow-read --allow-write --parallel --shuffle", | ||
"cov": "deno task test --coverage && deno coverage --lcov > coverage.lcov" | ||
}, | ||
"imports": { | ||
"@5ouma/opml-generator/libs": "./src/libs/mod.ts", | ||
"@5ouma/opml-generator/types": "./src/types/mod.ts", | ||
"@libs/xml": "jsr:@libs/[email protected]", | ||
"@std/assert": "jsr:@std/[email protected]", | ||
"@std/cli": "jsr:@std/[email protected]", | ||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 +1,3 @@ | ||
export * from "./io.ts"; | ||
export * from "./toml/io.ts"; | ||
export * from "./xml/io.ts"; | ||
export type { Lists } from "./types.ts"; |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { parse } from "@std/toml"; | ||
import { transcodeXmlUrl } from "../utils/sites.ts"; | ||
import type { Feed, List, Lists } from "../types.ts"; | ||
|
||
export function convert(data: string): Lists { | ||
const lists: Lists = parse(data) as Lists; | ||
lists.lists.map((list: List) => { | ||
list.feeds.map((feed: Feed) => { | ||
feed.xmlUrl = feed.xmlUrl | ||
? new URL(feed.xmlUrl) | ||
: transcodeXmlUrl(feed.title, feed.type, feed.id); | ||
}); | ||
}); | ||
return lists; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { assertEquals } from "@std/assert"; | ||
import { convert } from "./convert.ts"; | ||
import type { Lists } from "../types.ts"; | ||
|
||
Deno.test("Parse TOML", async (t: Deno.TestContext) => { | ||
await t.step("rss", () => { | ||
const toml = ` | ||
[[lists]] | ||
name = "list name" | ||
[[lists.feeds]] | ||
title = "feed title" | ||
xmlUrl = "https://example.com/feed" | ||
`; | ||
|
||
const feeds: Lists = { | ||
lists: [{ | ||
name: "list name", | ||
feeds: [{ | ||
title: "feed title", | ||
xmlUrl: new URL("https://example.com/feed"), | ||
}], | ||
}], | ||
}; | ||
|
||
assertEquals(convert(toml), feeds); | ||
}); | ||
|
||
await t.step("site", () => { | ||
const toml = ` | ||
[[lists]] | ||
name = "list name" | ||
[[lists.feeds]] | ||
title = "feed title" | ||
type = "bluesky" | ||
id = "username" | ||
`; | ||
|
||
const feeds: Lists = { | ||
lists: [{ | ||
name: "list name", | ||
feeds: [{ | ||
title: "feed title", | ||
type: "bluesky", | ||
id: "username", | ||
xmlUrl: new URL("https://bsky.app/profile/username/rss"), | ||
}], | ||
}], | ||
}; | ||
|
||
assertEquals(convert(toml), feeds); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { convert } from "./convert.ts"; | ||
import type { Lists } from "../types.ts"; | ||
|
||
export async function readTOML(file: string): Promise<Lists> { | ||
try { | ||
const data: string = await Deno.readTextFile(file); | ||
return convert(data); | ||
} catch (error) { | ||
if (error instanceof Deno.errors.NotFound) { | ||
throw new Error(`file not found: "${file}"`); | ||
} else if (error instanceof Deno.errors.PermissionDenied) { | ||
throw new Error(`permission denied: "${file}"`); | ||
} else throw error; | ||
} | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { stringify } from "@libs/xml"; | ||
import { transcodeXmlUrl } from "../utils/sites.ts"; | ||
import type { Feed, List, OPMLOutline } from "../types.ts"; | ||
|
||
export function convert(list: List): string { | ||
const body = { | ||
outline: list.feeds.map((feed: Feed): OPMLOutline => { | ||
return { | ||
"@title": feed.title, | ||
"@text": feed.title, | ||
"@xmlUrl": feed.xmlUrl | ||
? new URL(feed.xmlUrl) | ||
: transcodeXmlUrl(feed.title, feed.type, feed.id), | ||
"@type": "rss", | ||
}; | ||
}), | ||
}; | ||
|
||
return `<?xml version="1.0" encoding="UTF-8"?> | ||
${stringify({ opml: { "@version": "2.0", body: [body] } })} | ||
`; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { assertEquals } from "@std/assert"; | ||
import { convert } from "./convert.ts"; | ||
import type { List } from "../types.ts"; | ||
Deno.test("Convert Lists to OPML", async (t: Deno.TestContext) => { | ||
await t.step("rss", () => { | ||
const xml = `\ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<opml version="2.0"> | ||
<body> | ||
<outline title="feed title" text="feed title" xmlUrl="https://example.com/feed" type="rss"/> | ||
</body> | ||
</opml> | ||
`; | ||
|
||
const list: List = { | ||
name: "list name", | ||
feeds: [{ | ||
title: "feed title", | ||
xmlUrl: new URL("https://example.com/feed"), | ||
}], | ||
}; | ||
|
||
assertEquals(convert(list), xml); | ||
}); | ||
|
||
await t.step("site", () => { | ||
const xml = `\ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<opml version="2.0"> | ||
<body> | ||
<outline title="feed title" text="feed title" xmlUrl="https://bsky.app/profile/username/rss" type="rss"/> | ||
</body> | ||
</opml> | ||
`; | ||
|
||
const list: List = { | ||
name: "list name", | ||
feeds: [{ | ||
title: "feed title", | ||
type: "bluesky", | ||
id: "username", | ||
}], | ||
}; | ||
|
||
assertEquals(convert(list), xml); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { paramCase } from "@wok/case"; | ||
import { format } from "@std/path"; | ||
import { convert } from "./convert.ts"; | ||
import type { List, Lists } from "../types.ts"; | ||
|
||
export async function writeXML(feeds: Lists, dir: string): Promise<void> { | ||
await Deno.mkdir(dir, { recursive: true }); | ||
feeds.lists.map(async (list: List) => { | ||
const file: string = format({ | ||
dir: dir, | ||
name: paramCase(list.name), | ||
ext: ".xml", | ||
}); | ||
await Deno.writeTextFile(file, convert(list)); | ||
}); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { assertEquals } from "@std/assert"; | ||
import { join } from "@std/path"; | ||
import { writeXML } from "./io.ts"; | ||
import type { Lists } from "../types.ts"; | ||
|
||
Deno.test("Write XML", async () => { | ||
const lists: Lists = { | ||
lists: [{ | ||
name: "list name", | ||
feeds: [{ | ||
title: "feed title", | ||
xmlUrl: new URL("https://example.com/feed"), | ||
}], | ||
}], | ||
}; | ||
|
||
const xml = `\ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<opml version="2.0"> | ||
<body> | ||
<outline title="feed title" text="feed title" xmlUrl="https://example.com/feed" type="rss"/> | ||
</body> | ||
</opml> | ||
`; | ||
|
||
const dir: string = await Deno.makeTempDir(); | ||
await writeXML(lists, dir); | ||
|
||
assertEquals(await Deno.readTextFile(join(dir, "list-name.xml")), xml); | ||
}); |
Oops, something went wrong.