Skip to content

Commit

Permalink
config style json to js, secret loader removed
Browse files Browse the repository at this point in the history
  • Loading branch information
darsan-in committed Nov 26, 2024
1 parent 9e85726 commit 9145052
Show file tree
Hide file tree
Showing 16 changed files with 164 additions and 217 deletions.
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
node_modules
dist
sample-op
credentials
robot.txt
sitemap.xml
.hawk.lrs

gserv.json
secrets.json
4 changes: 2 additions & 2 deletions CONTRIBUTING
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Contributing to HAWK JS
# Contributing to Hawk.js

First off, thank you for considering contributing to HAWK JS! We welcome contributions from the community.
First off, thank you for considering contributing to Hawk.js! We welcome contributions from the community.

## License Agreement

Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<div align="center">

# Hawk JS - Advanced Sitemap Generator and Deployer for SEO
# Hawk.js - Advanced Sitemap Generator and Deployer for SEO

<p id="intro">Hawk JS is an advanced sitemap generator and deployer designed to enhance your website's search engine optimization (SEO). It automates the creation of comprehensive sitemaps and simplifies submission to major search engines like Google, Bing, Yahoo, Yandex, and more, ensuring your site is indexed accurately and quickly.</p>
<p id="intro">Hawk.js is an advanced sitemap generator and deployer designed to enhance your website's search engine optimization (SEO). It automates the creation of comprehensive sitemaps and simplifies submission to major search engines like Google, Bing, Yahoo, Yandex, and more, ensuring your site is indexed accurately and quickly.</p>

### Supported Platforms

Expand Down Expand Up @@ -153,12 +153,12 @@ Thank you for being part of this journey. Your support means the world to us.
## Installation - Step-by-Step Guide 🪜
Follow Below link:

[Getting Started with Hawk JS](https://hawkjs.cresteem.com/getting-started-with-hawk-js)
[Getting Started with Hawk.js](https://hawkjs.cresteem.com/getting-started-with-hawk-js)


## Usage

Everything from top to bottom of Hawk JS available here - https://hawkjs.cresteem.com/
Everything from top to bottom of Hawk.js available here - https://hawkjs.cresteem.com/

## License ©️

Expand Down Expand Up @@ -186,7 +186,7 @@ For any questions, please reach out via connect@cresteem.com

## Credits 🙏🏻

Hawk JS is developed and maintained by [DARSAN](https://darsan.in/) at [CRESTEEM](https://cresteem.com/).
Hawk.js is developed and maintained by [DARSAN](https://darsan.in/) at [CRESTEEM](https://cresteem.com/).

---

Expand Down Expand Up @@ -269,5 +269,5 @@ Hawk JS is developed and maintained by [DARSAN](https://darsan.in/) at [CRESTEEM
<li>content management</li>
<li>digital marketing</li>
<li>cresteem</li>
<li>hawk js</li>
<li>Hawk.js</li>
</ul>
4 changes: 0 additions & 4 deletions bin/CLIDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import yargs from "yargs";
import { hawk, hawkStrategy } from "../hawk";
import { makeSitemap } from "../lib/utils";
import { secretLoadWindows } from "./secretsLoader";

async function _genMapHandler(argv: any): Promise<void> {
if (argv.commit) {
Expand Down Expand Up @@ -69,11 +68,8 @@ async function main(): Promise<void> {
.help().argv;

const isGenMap: boolean = argv._.includes("genmap");
const isSecret: boolean = argv._.includes("secret");
if (isGenMap) {
_genMapHandler(argv);
} else if (isSecret) {
secretLoadWindows();
} else {
_mainHandler(argv);
}
Expand Down
37 changes: 0 additions & 37 deletions bin/secretsLoader.ts

This file was deleted.

62 changes: 24 additions & 38 deletions configLoader.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,33 @@
import { existsSync, readFileSync } from "fs";
import { join } from "path";
import { ConfigurationOptions, ftpCredentialOptions } from "./lib/options";
import { existsSync } from "node:fs";
import { join } from "node:path";
import { ConfigurationOptions } from "./lib/types";

const CONFIG_FILE_NAME = "hawk.config.json";
export default function loadConfig(): ConfigurationOptions {
const CONFIG_FILE_NAME = "hawk.config";

const projectConfigFile = join(process.cwd(), CONFIG_FILE_NAME);
const projectHasConfig = existsSync(projectConfigFile);
const projectConfigFile = join(process.cwd(), `${CONFIG_FILE_NAME}.js`);
const projectHasConfig = existsSync(projectConfigFile);

let projectConfig: ConfigurationOptions = {} as ConfigurationOptions;
let defaultConfig: ConfigurationOptions = {} as ConfigurationOptions;
let projectConfig: ConfigurationOptions = {} as ConfigurationOptions;
let defaultConfig: ConfigurationOptions = {} as ConfigurationOptions;

if (projectHasConfig) {
//load project config
try {
projectConfig = JSON.parse(
readFileSync(projectConfigFile, { encoding: "utf8" }),
);
} catch (err) {
if (err instanceof SyntaxError) {
console.log(
"Error: Check configuration file if there any syntax mistake",
);
} else {
console.log("Unexpected Error while loading settings");
if (projectHasConfig) {
//load project config
try {
projectConfig = require(projectConfigFile).default;
} catch (err) {
console.log("Error while loading settings\n", err);
process.exit(1);
}
process.exit(1);
}
}
//load default configuration
defaultConfig = JSON.parse(
readFileSync(join(__dirname, CONFIG_FILE_NAME), { encoding: "utf8" }),
);

const ftpCredential: ftpCredentialOptions = {
hostname: process.env.FTPHOST ?? "",
username: process.env.FTPUSER ?? "",
password: process.env.FTPPASS ?? "",
};
//load default configuration
defaultConfig = require(join(__dirname, CONFIG_FILE_NAME)).default;

const configurations: ConfigurationOptions = {
...defaultConfig,
...projectConfig,
...{ ftpCredential: ftpCredential },
};
const configurations: ConfigurationOptions = {
...defaultConfig,
...projectConfig,
};

export default configurations;
return configurations;
}
14 changes: 0 additions & 14 deletions hawk.config.json

This file was deleted.

14 changes: 14 additions & 0 deletions hawk.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ConfigurationOptions } from "./lib/types";

const config: ConfigurationOptions = {
lookupPatterns: ["**/*.html", "**/*.htm"],
ignorePattern: ["node_modules/**"],
timeZone: "Asia/Kolkata",
domainName: "www.cresteem.com",
sitemapPath: "sitemap.xml",
robotPath: "robot.txt",
serviceAccountFile: "gserv.json",
ftpCredential: {} as any,
};

export default config;
9 changes: 4 additions & 5 deletions hawk.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { suppotredStrategies, sitemapMetaOptions } from "./lib/options";
import {
googleIndex,
lastSubmissionStatusGAPI,
submitSitemapGAPI,
} from "./lib/gindex";
import { indexNow } from "./lib/indexnow";
import { SitemapMeta, SuppotredStrategies } from "./lib/types";
import {
getLastRunTimeStamp,
getUpdatedRoutesPath,
Expand Down Expand Up @@ -39,14 +39,13 @@ export const hawkStrategy = {
await submitSitemapGAPI();

/* check status */
const statusMeta: sitemapMetaOptions =
await lastSubmissionStatusGAPI();
const statusMeta: SitemapMeta = await lastSubmissionStatusGAPI();
console.log(statusMeta);
},
};

export async function hawk(
strategy: suppotredStrategies,
strategy: SuppotredStrategies,
lookupPatterns: string[] = [],
ignorePattern: string[] = [],
prettify: boolean = true,
Expand Down Expand Up @@ -89,7 +88,7 @@ async function _makeSitemapRobot(
}

async function strategyHandler(
strategy: suppotredStrategies,
strategy: SuppotredStrategies,
stateChangedRoutes: string[],
prettify: boolean = true,
lookupPatterns: string[] = [],
Expand Down
41 changes: 20 additions & 21 deletions lib/gindex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@ import { google } from "googleapis";
import { request } from "https";

import {
constants,
googleIndexResponseOptions,
googleIndexStatusCode,
sitemapMetaOptions,
} from "./options";
GoogleIndexResponseOptions,
GoogleIndexStatusCode,
SitemapMeta,
} from "./types";

import config from "../configLoader";
import {
convertTimeinCTZone,
lastStateReader,
lastStateWriter,
} from "./utils";
const { domainName, sitemapPath } = config;
const { domainName, sitemapPath, serviceAccountFile } = config();

function _callIndexingAPI(
accessToken: string,
updatedRoute: string,
): Promise<googleIndexResponseOptions> {
): Promise<GoogleIndexResponseOptions> {
return new Promise((resolve, reject) => {
const postData: string = JSON.stringify({
url: updatedRoute,
Expand All @@ -42,10 +41,10 @@ function _callIndexingAPI(
responseBody += data;
});
res.on("end", () => {
const response: googleIndexResponseOptions = {
const response: GoogleIndexResponseOptions = {
url: updatedRoute,
body: JSON.parse(responseBody),
statusCode: (res.statusCode ?? 0) as googleIndexStatusCode,
statusCode: (res.statusCode ?? 0) as GoogleIndexStatusCode,
};
resolve(response);
});
Expand All @@ -61,10 +60,10 @@ function _callIndexingAPI(
}

export async function googleIndex(stateChangedRoutes: string[]) {
const callPromises: Promise<googleIndexResponseOptions>[] = [];
const callPromises: Promise<GoogleIndexResponseOptions>[] = [];

const jwtClient = new google.auth.JWT({
keyFile: constants.serviceAccountFile,
keyFile: serviceAccountFile,
scopes: ["https://www.googleapis.com/auth/indexing"],
});

Expand All @@ -84,14 +83,14 @@ export async function googleIndex(stateChangedRoutes: string[]) {
);
});

const apiResponses: googleIndexResponseOptions[] = await Promise.all(
const apiResponses: GoogleIndexResponseOptions[] = await Promise.all(
callPromises,
);

/* Grouping api responses */
const statusGroups: Record<
googleIndexStatusCode,
googleIndexResponseOptions[]
GoogleIndexStatusCode,
GoogleIndexResponseOptions[]
> = {
204: [], //dummy
400: [],
Expand Down Expand Up @@ -166,7 +165,7 @@ export async function googleIndex(stateChangedRoutes: string[]) {
}

function _sitemapGAPIResponseHandler(
response: googleIndexResponseOptions,
response: GoogleIndexResponseOptions,
) {
switch (response.statusCode) {
case 200:
Expand Down Expand Up @@ -194,7 +193,7 @@ export function submitSitemapGAPI(): Promise<void> {
const sitemapURL: string = `https://${domainName}/${sitemapPath}`;

const jwtClient = new google.auth.JWT({
keyFile: constants.serviceAccountFile,
keyFile: serviceAccountFile,
scopes: ["https://www.googleapis.com/auth/webmasters"],
});

Expand Down Expand Up @@ -236,10 +235,10 @@ export function submitSitemapGAPI(): Promise<void> {
});

res.on("end", () => {
const response: googleIndexResponseOptions = {
const response: GoogleIndexResponseOptions = {
url: sitemapURL,
body: responseBody ? JSON.parse(responseBody) : "",
statusCode: res.statusCode as googleIndexStatusCode,
statusCode: res.statusCode as GoogleIndexStatusCode,
};
_sitemapGAPIResponseHandler(response);

Expand All @@ -258,7 +257,7 @@ export function submitSitemapGAPI(): Promise<void> {
});
}

export function lastSubmissionStatusGAPI(): Promise<sitemapMetaOptions> {
export function lastSubmissionStatusGAPI(): Promise<SitemapMeta> {
const lastSubmittedURL: string = lastStateReader(
"submittedSitemap",
) as string;
Expand All @@ -269,7 +268,7 @@ export function lastSubmissionStatusGAPI(): Promise<sitemapMetaOptions> {
}

const jwtClient = new google.auth.JWT({
keyFile: constants.serviceAccountFile,
keyFile: serviceAccountFile,
scopes: ["https://www.googleapis.com/auth/webmasters"],
});

Expand Down Expand Up @@ -304,7 +303,7 @@ export function lastSubmissionStatusGAPI(): Promise<sitemapMetaOptions> {
reject("😕 Last submittion status not found");
}

const sitemapMeta: sitemapMetaOptions = {
const sitemapMeta: SitemapMeta = {
pageCounts: targetedMeta?.contents
? parseInt(targetedMeta?.contents[0].submitted ?? "0")
: 0,
Expand Down
Loading

0 comments on commit 9145052

Please sign in to comment.