Skip to content
This repository has been archived by the owner on Apr 24, 2023. It is now read-only.

Commit

Permalink
feat(feed): add compact code
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope committed May 31, 2022
1 parent 3bb666e commit 1d66e6c
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
36 changes: 36 additions & 0 deletions packages/feed/src/node/compact/convert.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { droppedLogger } from "./utils";

import type { FeedOptions } from "../../types";

/** @deprecated */
export const covertOptions = (
options: FeedOptions & Record<string, unknown>
): void => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
// eslint-disable-next-line
options.atom = options["output"]?.atom?.enable ?? true;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
// eslint-disable-next-line
options.json = options["output"]?.json?.enable ?? true;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
// eslint-disable-next-line
options.rss = options["output"]?.rss?.enable ?? true;

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
// eslint-disable-next-line
options.atomOutputFilename = options["output"]?.atom?.path ?? "atom.xml";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
// eslint-disable-next-line
options.jsonOutputFilename = options["output"]?.json?.path ?? "feed.json";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
// eslint-disable-next-line
options.jsonOutputFilename = options["output"]?.rss?.path ?? "rss.xml";

droppedLogger(options, "output");
};
1 change: 1 addition & 0 deletions packages/feed/src/node/compact/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./convert";
57 changes: 57 additions & 0 deletions packages/feed/src/node/compact/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
export interface DeprecatedLoggerOptions {
options: Record<string, unknown>;
deprecatedOption: string;
newOption: string;
msg?: string;
scope?: string;
}

export const deprecatedLogger = ({
options,
deprecatedOption,
newOption,
msg = "",
}: DeprecatedLoggerOptions): void => {
if (deprecatedOption in options) {
console.warn(
`"${deprecatedOption}" is deprecate in feed plugin, please use "${newOption}" instead.${
msg ? `\n${msg}` : ""
}`
);

if (newOption.includes(".")) {
const keys = newOption.split(".");
let temp = options;

keys.forEach((key, index) => {
if (index !== keys.length - 1) {
// ensure level exists
temp[key] = temp[key] || {};

temp = temp[key] as Record<string, unknown>;
} else temp[key] = options[deprecatedOption];
});
} else options[newOption] = options[deprecatedOption];

delete options[deprecatedOption];
}
};

export const droppedLogger = (
options: Record<string, unknown>,
droppedOption: string,
hint = "",
newOption = ""
): void => {
if (droppedOption in options) {
console.error(
`"${droppedOption}" is removed${
newOption
? `, please use ${newOption} instead.`
: " and no longer supported"
}${hint ? `\n${hint}` : ""}`
);

if (!newOption) delete options[droppedOption];
}
};
5 changes: 4 additions & 1 deletion packages/feed/src/node/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import chalk from "chalk";
import { checkOutput, ensureHostName, getFeedOptions } from "./options";
import { covertOptions } from "./compact";
import { injectLinkstoHead } from "./injectHead";
import { FeedGenerator } from "./generator";
import { checkOutput, ensureHostName, getFeedOptions } from "./options";

import type { Plugin, PluginOptionAPI } from "@mr-hope/vuepress-types";
import type { FeedOptions } from "../types";
Expand All @@ -12,6 +13,8 @@ const feedPlugin: Plugin<FeedOptions> = (options, context) => {
name: "vuepress-plugin-feed2",
};

covertOptions(options as FeedOptions & Record<string, unknown>);

if (!ensureHostName(options)) {
error(`Option ${chalk.magenta("hostname")} is required!`);

Expand Down

0 comments on commit 1d66e6c

Please sign in to comment.