Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update:add validation functions to validate user input at init.ts script #18

Merged
merged 3 commits into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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://exmaple.com";
sinyo-matu marked this conversation as resolved.
Show resolved Hide resolved
}

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";
sinyo-matu marked this conversation as resolved.
Show resolved Hide resolved
}

await createSitemap(url);

await createRobotTxt(url, staticPath);
}