Skip to content

Commit

Permalink
feat(config): load config file properly (cjs)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartholomej committed Jan 18, 2022
1 parent c10272c commit dde7b5b
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 35 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Sometimes it can be useful to call the script directly from JavaScript or TypeSc
```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`
Expand Down
27 changes: 21 additions & 6 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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), {
Expand Down Expand Up @@ -90,7 +92,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,
Expand All @@ -101,5 +103,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));
}
}
2 changes: 1 addition & 1 deletion src/helpers/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { loadFile } from './file';

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

export const defaultConfig: OptionsSvelteSitemap = {
Expand Down
2 changes: 0 additions & 2 deletions src/helpers/file.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
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);
}

Expand Down
23 changes: 2 additions & 21 deletions src/helpers/global.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,8 @@ 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 { APP_NAME, OUT_DIR } from '../vars';
import { changeFreq, ChangeFreq, Options, PagesJson } from '../interfaces/global.interface';
import { OUT_DIR } from '../vars';
import { cliColors, errorMsg, successMsg } from './vars.helper';

const getUrl = (url: string, domain: string, options: Options) => {
Expand All @@ -29,8 +23,6 @@ const getUrl = (url: string, domain: string, options: Options) => {
};

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

const ignore = prepareIgnored(options?.ignore, options?.outDir);
const changeFreq = prepareChangeFreq(options);
const pages: string[] = await fg(`${options?.outDir ?? OUT_DIR}/**/*.html`, { ignore });
Expand Down Expand Up @@ -104,14 +96,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;
};
2 changes: 1 addition & 1 deletion src/helpers/vars.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 errorMsg = (outDir: string) =>
` × Make sure you are using this script as 'postbuild' so '${outDir}' folder was successfully created before running this script. See https://github.com/bartholomej/svelte-sitemap#readme`;
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const createSitemap = async (options: OptionsSvelteSitemap): Promise<void
if (options?.debug) {
console.log('OPTIONS', options);
}
console.log('OPTIONS', options);

const json = await prepareData(options.domain, options);

if (options?.debug) {
Expand Down
2 changes: 1 addition & 1 deletion src/vars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const DOMAIN = 'https://example.com';

export const OUT_DIR = 'build';

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

// export const OPTIONS: Options = {
// domain: DOMAIN,
Expand Down
File renamed without changes.
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", "svelte-sitemap.js"],
"include": ["src", "index.ts", "svelte-sitemap.cjs"],
"exclude": ["dist/**/*", "*/tests/**/*"]
}

0 comments on commit dde7b5b

Please sign in to comment.