Skip to content

Commit

Permalink
add setStability() helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
tasshi-me committed Oct 28, 2024
1 parent 96c0995 commit 3312a0f
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 27 deletions.
21 changes: 12 additions & 9 deletions src/cli/plugin/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
import type { CommandModule } from "yargs";
import type yargs from "yargs";
import { packCommand } from "./pack";
import { emitExperimentalWarning } from "../../utils/stability";
import { infoCommand } from "./info";
import { setStability } from "../stability";

const command = "plugin";

const describe = "[Experimental] Commands for kintone plugin";
const describe = "Commands for kintone plugin";

const builder = (args: yargs.Argv) =>
args.command(infoCommand).command(packCommand).demandCommand();

const handler = () => {
emitExperimentalWarning("This feature is under early development");
/** noop **/
};

export const pluginCommand: CommandModule = {
command,
describe,
builder,
handler,
};
export const pluginCommand: CommandModule = setStability(
{
command,
describe,
builder,
handler,
},
"experimental",
"This feature is under early development",
);
21 changes: 12 additions & 9 deletions src/cli/plugin/info.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type yargs from "yargs";
import type { CommandModule } from "yargs";
import { emitExperimentalWarning } from "../../utils/stability";
import type { OutputFormat } from "../../plugin/info/";
import { run } from "../../plugin/info/";
import { logger } from "../../utils/log";
import { RunError } from "../../record/error";
import { setStability } from "../stability";

const command = "info";

const describe = "[Experimental] Show information from plugin file";
const describe = "Show information from plugin file";

const outputFormats: OutputFormat[] = ["plain", "json"];

Expand All @@ -33,7 +33,6 @@ type Args = yargs.Arguments<

const handler = async (args: Args) => {
try {
emitExperimentalWarning("This feature is under early development");
await run(args.input, args.format);
} catch (error) {
logger.error(new RunError(error));
Expand All @@ -42,9 +41,13 @@ const handler = async (args: Args) => {
}
};

export const infoCommand: CommandModule<{}, Args> = {
command,
describe,
builder,
handler,
};
export const infoCommand: CommandModule<{}, Args> = setStability(
{
command,
describe,
builder,
handler,
},
"experimental",
"This feature is under early development",
);
21 changes: 12 additions & 9 deletions src/cli/plugin/pack.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type yargs from "yargs";
import type { CommandModule } from "yargs";
import { emitExperimentalWarning } from "../../utils/stability";
import { run } from "../../plugin/packer/";
import { logger } from "../../utils/log";
import { RunError } from "../../record/error";
import { setStability } from "../stability";

const command = "pack";

const describe = "[Experimental] Packaging plugin project to a zip file";
const describe = "Packaging plugin project to a zip file";

const builder = (args: yargs.Argv) =>
args
Expand Down Expand Up @@ -41,7 +41,6 @@ type Args = yargs.Arguments<

const handler = async (args: Args) => {
try {
emitExperimentalWarning("This feature is under early development");
const flags = {
ppk: args["private-key"],
output: args.output,
Expand All @@ -59,9 +58,13 @@ const handler = async (args: Args) => {
}
};

export const packCommand: CommandModule<{}, Args> = {
command,
describe,
builder,
handler,
};
export const packCommand: CommandModule<{}, Args> = setStability(
{
command,
describe,
builder,
handler,
},
"experimental",
"This feature is under early development",
);
69 changes: 69 additions & 0 deletions src/cli/stability.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import type { ArgumentsCamelCase, CommandModule } from "yargs";
import {
emitDeprecationWarning,
emitExperimentalWarning,
} from "../utils/stability";

/**
* Set stability index to a command.
* - Show stability on help message
* - Emit warning on execution
* @param cmd Command module
* @param stability "experimental" or "deprecated"
* @param message additional information
*/
export const setStability = <T = {}, U = {}>(
cmd: CommandModule<T, U>,
stability: "experimental" | "deprecated",
message: string,
): CommandModule<T, U> => {
const { describe, handler, ...restCmd } = cmd;
const newDescribe = buildDescriptionWithStability(
describe,
message,
stability,
);

const newHandler = async (args: ArgumentsCamelCase<U>) => {
switch (stability) {
case "experimental":
emitExperimentalWarning(message);
break;
case "deprecated":
emitDeprecationWarning(message);
break;
}
await handler(args);
};

return {
...restCmd,
describe: newDescribe,
handler: newHandler,
};
};

const buildDescriptionWithStability = (
description: string | false | undefined,
message: string,
stability: "experimental" | "deprecated",
): string => {
const labels = {
experimental: "Experimental",
deprecated: "Deprecated",
};
const label = labels[stability];
const msgLines = message.split("\n");

let output = "";
if (description) {
output += description + "\n\n";
}

output += `[${label}: ${msgLines[0]}]`;
if (msgLines.length > 1) {
output += `${"\n" + msgLines.slice(1)}`;
}

return output;
};

0 comments on commit 3312a0f

Please sign in to comment.