Skip to content

Commit

Permalink
feat(config): add config file [WIP]
Browse files Browse the repository at this point in the history
  • Loading branch information
bartholomej committed Aug 16, 2022
1 parent e16a4f0 commit 193177d
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 21 deletions.
2 changes: 1 addition & 1 deletion demo.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { createSitemap } from './src/index';

createSitemap('https://example.com/', { debug: false, resetTime: true });
createSitemap({ domain: 'https://bartweb.cz', debug: false, resetTime: true, outDir: 'build' });
22 changes: 17 additions & 5 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@

import minimist from 'minimist';
import { version } from './package.json';
import { loadConfig } from './src/helpers/config';
import { mergeOptions } from './src/helpers/global.helper';
import { createSitemap } from './src/index';
import { ChangeFreq, Options } from './src/interfaces/global.interface';
import { ChangeFreq, OptionsSvelteSitemap } from './src/interfaces/global.interface';
import { CONFIG_FILE } from './src/vars';

const REPO_URL = 'https://github.com/bartholomej/svelte-sitemap';

let stop = false;

// Load svelte-sitemap.js
const config = loadConfig(CONFIG_FILE);

const args = minimist(process.argv.slice(2), {
string: ['domain', 'out-dir', 'ignore', 'change-freq'],
boolean: ['attribution', 'reset-time', 'trailing-slashes', 'debug', 'version'],
Expand Down Expand Up @@ -57,12 +63,16 @@ if (args.help || args.version === '' || args.version === true) {
log(' --debug Debug mode');
log(' ');
process.exit(args.help ? 0 : 1);
} else if (!args.domain) {
} else if (!config.domain && !args.domain) {
console.log(
`⚠ svelte-sitemap: --domain argument is required.\n\nSee instructions: ${REPO_URL}\n\nExample:\n\n svelte-sitemap --domain https://mydomain.com\n`
);
process.exit(0);
} else if (!args.domain.includes('http')) {
} else if (
// (config.domain || args.domain) &&
!config.domain?.includes('http') &&
!args.domain?.includes('http')
) {
console.log(
`⚠ svelte-sitemap: --domain argument must starts with https://\n\nSee instructions: ${REPO_URL}\n\nExample:\n\n svelte-sitemap --domain https://mydomain.com\n`
);
Expand All @@ -81,15 +91,17 @@ if (args.help || args.version === '' || args.version === true) {
const ignore: string = args['ignore'];
const attribution: boolean =
args['attribution'] === '' || args['attribution'] === false ? false : true;
const options: Options = {

const options: OptionsSvelteSitemap = {
debug,
resetTime,
changeFreq,
outDir,
domain,
attribution,
ignore,
trailingSlashes
};

createSitemap(domain, options);
createSitemap(mergeOptions(options, config));
}
30 changes: 30 additions & 0 deletions src/helpers/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { OptionsSvelteSitemap } from '../interfaces/global.interface';
import { OUT_DIR } from './../vars';
import { loadFile } from './file';

export const loadConfig = (path: string): OptionsSvelteSitemap => {
const baseConfig = loadFile<OptionsSvelteSitemap>(path);
return withDefaultConfig(baseConfig!);
};

export const defaultConfig: OptionsSvelteSitemap = {
debug: false,
changeFreq: null,
resetTime: false,
outDir: OUT_DIR,
attribution: true,
ignore: null,
trailingSlashes: false,
domain: null
};

export const updateConfig = (
currConfig: OptionsSvelteSitemap,
newConfig: OptionsSvelteSitemap
): OptionsSvelteSitemap => {
return { ...currConfig, ...newConfig };
};

export const withDefaultConfig = (config: OptionsSvelteSitemap): OptionsSvelteSitemap => {
return updateConfig(defaultConfig, config);
};
17 changes: 17 additions & 0 deletions src/helpers/file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { existsSync } from 'fs';
import { resolve } from 'path';
import { cliColors } from './vars.helper';

export const loadFile = <T>(fileName: string, throwError = true): T => {
const filePath = resolve(resolve(process.cwd(), fileName));

if (existsSync(filePath)) {
console.log(cliColors.cyanAndBold, `> Loading config from ${fileName}`);
return require(filePath);
}

if (throwError) {
new Error(`${filePath} does not exist.`);
}
return null;
};
25 changes: 20 additions & 5 deletions src/helpers/global.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ import fg from 'fast-glob';
import fs from 'fs';
import { create } from 'xmlbuilder2';
import { version } from '../../package.json';
import { changeFreq, ChangeFreq, Options, PagesJson } from '../interfaces/global.interface';
import { APP_NAME, OUT_DIR } from '../vars';
import {
changeFreq,
ChangeFreq,
Options,
OptionsSvelteSitemap,
PagesJson
} from '../interfaces/global.interface';
import { OUT_DIR } from '../vars';
import {
cliColors,
errorMsgFolder,
Expand Down Expand Up @@ -40,15 +46,13 @@ export const removeHtml = (fileName: string) => {
};

export async function prepareData(domain: string, options?: Options): Promise<PagesJson[]> {
console.log(cliColors.cyanAndBold, `> Using ${APP_NAME}`);

const FOLDER = options?.outDir ?? OUT_DIR;

const ignore = prepareIgnored(options?.ignore, options?.outDir);
const changeFreq = prepareChangeFreq(options);
const pages: string[] = await fg(`${FOLDER}/**/*.html`, { ignore });

const results = pages.map((page) => {
const results: PagesJson[] = pages.map((page) => {
return {
page: getUrl(page, domain, options),
changeFreq: changeFreq,
Expand Down Expand Up @@ -131,3 +135,14 @@ const prepareChangeFreq = (options: Options): ChangeFreq => {
}
return result;
};

export const mergeOptions = (obj1: any, obj2: any): OptionsSvelteSitemap => {
const answer: any = {};
for (const key in obj1) {
if (answer[key] === undefined || answer[key] === null) answer[key] = obj1[key];
}
for (const key in obj2) {
if (answer[key] === undefined || answer[key] === null) answer[key] = obj2[key];
}
return answer;
};
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { prepareData, writeSitemap } from './helpers/global.helper';
import { cliColors, errorMsgWrite } from './helpers/vars.helper';
import { Options } from './interfaces/global.interface';
import { DOMAIN, OUT_DIR } from './vars';
import { OptionsSvelteSitemap } from './interfaces/global.interface';
import { OUT_DIR } from './vars';

export const createSitemap = async (domain: string = DOMAIN, options?: Options): Promise<void> => {
export const createSitemap = async (options: OptionsSvelteSitemap): Promise<void> => {
if (options?.debug) {
console.log('OPTIONS', options);
}

const json = await prepareData(domain, options);
console.log('OPTIONS', options);
const json = await prepareData(options.domain, options);

if (options?.debug) {
console.log('RESULT', json);
Expand Down
4 changes: 4 additions & 0 deletions src/interfaces/global.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export interface Options {
trailingSlashes?: boolean;
}

export interface OptionsSvelteSitemap extends Options {
domain: string;
}

export interface PagesJson {
page: string;
changeFreq?: ChangeFreq;
Expand Down
13 changes: 9 additions & 4 deletions src/vars.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { Options } from './interfaces/global.interface';

export const APP_NAME = 'svelte-sitemap';

export const DOMAIN = 'https://example.com';

export const OPTIONS: Options = { resetTime: false, debug: false, changeFreq: 'weekly' };

export const OUT_DIR = 'build';

export const CONFIG_FILE = 'svelte-sitemap.js';

// export const OPTIONS: Options = {
// domain: DOMAIN,
// resetTime: false,
// debug: false,
// changeFreq: 'weekly'
// };
4 changes: 4 additions & 0 deletions svelte-sitemap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
domain: 'https://www.example.com',
debug: true
};
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
"noImplicitAny": true,
"noImplicitReturns": true
},
"include": ["src", "index.ts"],
"include": ["src", "index.ts", "svelte-sitemap.js"],
"exclude": ["dist/**/*", "*/tests/**/*"]
}

0 comments on commit 193177d

Please sign in to comment.