Skip to content

Commit

Permalink
update:add validation functions to validate user input at init.ts scr…
Browse files Browse the repository at this point in the history
…ipt (#18)

Co-authored-by: sinyo-matu <sinyo_matu@icloud.com>
  • Loading branch information
sinyo-matu and sinyo-matu committed Oct 12, 2022
1 parent afb27ea commit e2d542a
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 56 deletions.
58 changes: 2 additions & 56 deletions init.ts
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();
89 changes: 89 additions & 0 deletions src/initFile.ts
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);
}

0 comments on commit e2d542a

Please sign in to comment.