-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update:add validation functions to validate user input at init.ts scr…
…ipt (#18) Co-authored-by: sinyo-matu <sinyo_matu@icloud.com>
- Loading branch information
1 parent
afb27ea
commit e2d542a
Showing
2 changed files
with
91 additions
and
56 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,57 +1,3 @@ | ||
import { ensureFile, join, resolve } from "./src/deps.ts"; | ||
import { init } from "./src/initFile.ts"; | ||
|
||
export async function createSitemap(url: string | null) { | ||
const routesDirectory = resolve("./routes"); | ||
|
||
const destination = join(routesDirectory, "sitemap.xml.ts"); | ||
|
||
await ensureFile(destination); | ||
|
||
const stub = `import { Handlers } from "$fresh/server.ts"; | ||
import manifest from "../fresh.gen.ts"; | ||
import { SitemapContext } from "https://deno.land/x/fresh_seo@0.1.1/mod.ts"; | ||
export const handler: Handlers = { | ||
GET(req, ctx) { | ||
const sitemap = new SitemapContext("${ | ||
url ?? "http://exmaple.com" | ||
}", manifest); | ||
// You can add additional page here | ||
return sitemap.render(); | ||
}, | ||
}; | ||
`; | ||
|
||
return Deno.writeTextFile(destination, stub); | ||
} | ||
|
||
export async function createRobotTxt( | ||
url: string | null, | ||
staticPath: string | null, | ||
) { | ||
const directory = resolve(staticPath ?? "./static"); | ||
|
||
const destination = join(directory, "robots.txt"); | ||
|
||
await ensureFile(destination); | ||
|
||
const stub = `User-agent: * | ||
Allow: / | ||
Disallow: /api/* | ||
Sitemap: ${url ?? "http://example.com"}/sitemap.xml | ||
`; | ||
|
||
return Deno.writeTextFile(destination, stub); | ||
} | ||
|
||
const url = prompt( | ||
"Please input your site's url (skip by enter):", | ||
"http://example.com", | ||
); | ||
|
||
const staticPath = prompt( | ||
"Please input your site's static folder path (skip by enter):", | ||
"./static", | ||
); | ||
|
||
await createSitemap(url); | ||
await createRobotTxt(url, staticPath); | ||
init(); |
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,89 @@ | ||
import { ensureFile, join, resolve } from "./deps.ts"; | ||
|
||
function isUrl(input: string): boolean { | ||
try { | ||
new URL(input); | ||
} catch (_) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
function isValidUrl(inputUrl: string): boolean { | ||
if (!isUrl(inputUrl.trim())) return false; | ||
return true; | ||
} | ||
|
||
function isValidDir(inputDir: string): boolean { | ||
if (inputDir.trim().length === 0) return false; | ||
return true; | ||
} | ||
|
||
async function createSitemap(url: string) { | ||
const routesDirectory = resolve("./routes"); | ||
|
||
const destination = join(routesDirectory, "sitemap.xml.ts"); | ||
|
||
await ensureFile(destination); | ||
|
||
const stub = `import { Handlers } from "$fresh/server.ts"; | ||
import manifest from "../fresh.gen.ts"; | ||
import { SitemapContext } from "https://deno.land/x/fresh_seo@0.1.1/mod.ts"; | ||
export const handler: Handlers = { | ||
GET(req, ctx) { | ||
const sitemap = new SitemapContext("${url}", manifest); | ||
// You can add additional page here | ||
return sitemap.render(); | ||
}, | ||
}; | ||
`; | ||
|
||
return Deno.writeTextFile(destination, stub); | ||
} | ||
|
||
async function createRobotTxt( | ||
url: string, | ||
staticPath: string, | ||
) { | ||
const directory = resolve(staticPath); | ||
|
||
const destination = join(directory, "robots.txt"); | ||
|
||
await ensureFile(destination); | ||
|
||
const stub = `User-agent: * | ||
Allow: / | ||
Disallow: /api/* | ||
Sitemap: ${url}/sitemap.xml | ||
`; | ||
|
||
return Deno.writeTextFile(destination, stub); | ||
} | ||
|
||
export async function init() { | ||
let url = prompt( | ||
"Please input your site's url (skip by enter):", | ||
"http://example.com", | ||
); | ||
|
||
if (!url || !isValidUrl(url)) { | ||
console.log( | ||
"Invalid url input! Setting to default value:'http://example.com'", | ||
); | ||
url = "http://example.com"; | ||
} | ||
|
||
let staticPath = prompt( | ||
"Please input your site's static folder path (skip by enter):", | ||
"./static", | ||
); | ||
|
||
if (!staticPath || !isValidDir(staticPath)) { | ||
console.log("Invalid folder input! Setting to default value:'./static'"); | ||
staticPath = "./static"; | ||
} | ||
|
||
await createSitemap(url); | ||
|
||
await createRobotTxt(url, staticPath); | ||
} |