diff --git a/README.md b/README.md index 0e7123a..1c0fdf7 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ File `my-script.js`: ```typescript import { createSitemap } from 'svelte-sitemap/src/index.js'; -createSitemap('https://example.com', { debug: true }); +createSitemap({ domain: 'https://example.com', debug: true }); ``` And now you can run your script like this: `node my-script.js` diff --git a/index.ts b/index.ts index fc5d42c..58470d7 100644 --- a/index.ts +++ b/index.ts @@ -2,17 +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 { loadConfig, withDefaultConfig } from './src/helpers/config'; +import { cliColors } from './src/helpers/vars.helper'; import { createSitemap } from './src/index'; import { ChangeFreq, OptionsSvelteSitemap } from './src/interfaces/global.interface'; -import { CONFIG_FILE } from './src/vars'; +import { APP_NAME, CONFIG_FILE } from './src/vars'; + +console.log(cliColors.cyanAndBold, `> Using ${APP_NAME}`); const REPO_URL = 'https://github.com/bartholomej/svelte-sitemap'; let stop = false; -// Load svelte-sitemap.js +// Load svelte-sitemap.cjs const config = loadConfig(CONFIG_FILE); const args = minimist(process.argv.slice(2), { @@ -92,7 +94,7 @@ if (args.help || args.version === '' || args.version === true) { const attribution: boolean = args['attribution'] === '' || args['attribution'] === false ? false : true; - const options: OptionsSvelteSitemap = { + const optionsCli: OptionsSvelteSitemap = { debug, resetTime, changeFreq, @@ -103,5 +105,18 @@ if (args.help || args.version === '' || args.version === true) { trailingSlashes }; - createSitemap(mergeOptions(options, config)); + // Config file is preferred + if (config && Object.keys(config).length === 0) { + console.log( + cliColors.cyanAndBold, + ` ✔ Using CLI options. Config file ${CONFIG_FILE} not found.` + ); + createSitemap(optionsCli); + } else { + console.log( + cliColors.green, + ` ✔ Loading config from ${CONFIG_FILE}. CLI options are ignored now.` + ); + createSitemap(withDefaultConfig(config)); + } } diff --git a/src/helpers/config.ts b/src/helpers/config.ts index 17ed7da..ee1eb95 100644 --- a/src/helpers/config.ts +++ b/src/helpers/config.ts @@ -4,7 +4,7 @@ import { loadFile } from './file'; export const loadConfig = (path: string): OptionsSvelteSitemap => { const baseConfig = loadFile(path); - return withDefaultConfig(baseConfig!); + return baseConfig!; }; export const defaultConfig: OptionsSvelteSitemap = { diff --git a/src/helpers/file.ts b/src/helpers/file.ts index ceabf0c..3b2fe89 100644 --- a/src/helpers/file.ts +++ b/src/helpers/file.ts @@ -1,12 +1,10 @@ import { existsSync } from 'fs'; import { resolve } from 'path'; -import { cliColors } from './vars.helper'; export const loadFile = (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); } diff --git a/src/helpers/global.helper.ts b/src/helpers/global.helper.ts index a023d3b..7a9bc67 100644 --- a/src/helpers/global.helper.ts +++ b/src/helpers/global.helper.ts @@ -2,13 +2,7 @@ import fg from 'fast-glob'; import fs from 'fs'; import { create } from 'xmlbuilder2'; import { version } from '../../package.json'; -import { - changeFreq, - ChangeFreq, - Options, - OptionsSvelteSitemap, - PagesJson -} from '../interfaces/global.interface'; +import { changeFreq, ChangeFreq, Options, PagesJson } from '../interfaces/global.interface'; import { OUT_DIR } from '../vars'; import { cliColors, @@ -135,14 +129,3 @@ 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; -}; diff --git a/src/helpers/vars.helper.ts b/src/helpers/vars.helper.ts index 3efd1e0..f4c9b36 100644 --- a/src/helpers/vars.helper.ts +++ b/src/helpers/vars.helper.ts @@ -5,7 +5,7 @@ export const cliColors = { }; export const successMsg = (outDir: string) => - ` ✔ done. Check your new sitemap here: ./${outDir}/sitemap.xml`; + ` ✔ Done! Check your new sitemap here: ./${outDir}/sitemap.xml`; export const errorMsgWrite = (outDir: string) => ` × File '${outDir}/sitemap.xml' could not be created.`; diff --git a/src/index.ts b/src/index.ts index d65de47..6036e50 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,7 +7,7 @@ export const createSitemap = async (options: OptionsSvelteSitemap): Promise