Skip to content

Commit

Permalink
feat: add --init-emojis option to suggest emojis for configs
Browse files Browse the repository at this point in the history
  • Loading branch information
bmish committed Mar 19, 2023
1 parent 0dce68f commit 917ce3c
Show file tree
Hide file tree
Showing 11 changed files with 374 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ There's also a `postprocess` option that's only available via a [config file](#c
| `--config-format` | The format to use for config names. See choices in below [table](#--config-format). | `name` |
| `--ignore-config` | Config to ignore from being displayed. Often used for an `all` config. Option can be repeated. | |
| `--ignore-deprecated-rules` | Whether to ignore deprecated rules from being checked, displayed, or updated. | `false` |
| `--init-emojis` | Whether to suggest an emoji for each config. Use `--config-emoji` option to utilize a suggestion. | `false` |
| `--init-rule-docs` | Whether to create rule doc files if they don't yet exist. | `false` |
| `--path-rule-doc` | Path to markdown file for each rule doc. Use `{name}` placeholder for the rule name. | `docs/rules/{name}.md` |
| `--path-rule-list` | Path to markdown file where the rules table list should live. Option can be repeated. | `README.md` |
Expand Down
8 changes: 8 additions & 0 deletions lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ async function loadConfigFileOptions(): Promise<GenerateOptions> {
configFormat: { type: 'string' },
ignoreConfig: schemaStringArray,
ignoreDeprecatedRules: { type: 'boolean' },
initEmojis: { type: 'boolean' },
initRuleDocs: { type: 'boolean' },
pathRuleDoc: { type: 'string' },
pathRuleList: { anyOf: [{ type: 'string' }, schemaStringArray] },
Expand Down Expand Up @@ -217,6 +218,13 @@ export async function run(
)})`,
parseBoolean
)
.option(
'--init-emojis [boolean]',
`(optional) Whether to suggest an emoji for each config. Use \`--config-emoji\` option to utilize a suggestion. (default: ${String(
OPTION_DEFAULTS[OPTION_TYPE.INIT_EMOJIS]
)})`,
parseBoolean
)
.option(
'--init-rule-docs [boolean]',
`(optional) Whether to create rule doc files if they don't yet exist. (default: ${String(
Expand Down
20 changes: 20 additions & 0 deletions lib/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import { diff } from 'jest-diff';
import type { GenerateOptions } from './types.js';
import { OPTION_TYPE, RuleModule } from './types.js';
import { replaceRulePlaceholder } from './rule-link.js';
import { getEmojis } from 'gpt-emoji';
import { table } from 'table';

function stringOrArrayWithFallback<T extends string | readonly string[]>(
stringOrArray: undefined | T,
Expand Down Expand Up @@ -75,6 +77,8 @@ export async function generate(path: string, options?: GenerateOptions) {
const ignoreDeprecatedRules =
options?.ignoreDeprecatedRules ??
OPTION_DEFAULTS[OPTION_TYPE.IGNORE_DEPRECATED_RULES];
const initEmojis =
options?.initEmojis ?? OPTION_DEFAULTS[OPTION_TYPE.INIT_EMOJIS];
const initRuleDocs =
options?.initRuleDocs ?? OPTION_DEFAULTS[OPTION_TYPE.INIT_RULE_DOCS];
const pathRuleDoc =
Expand Down Expand Up @@ -113,6 +117,22 @@ export async function generate(path: string, options?: GenerateOptions) {
const urlRuleDoc =
options?.urlRuleDoc ?? OPTION_DEFAULTS[OPTION_TYPE.URL_RULE_DOC];

if (initEmojis) {
if (!process.env.OPENAI_API_KEY) {
throw new Error('Missing OPENAI_API_KEY environment variable.');
}
const configNames = Object.keys(plugin.configs || {});
const result = await getEmojis(configNames, {
count: 1,
});
const data = [
['Config', 'Emoji'],
...configNames.map((configName, i) => [configName, result[i]]),
];
console.log(table(data));
return;
}

// Gather normalized list of rules.
const ruleNamesAndRules = Object.entries(plugin.rules)
.map(([name, ruleModule]) => {
Expand Down
1 change: 1 addition & 0 deletions lib/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export const OPTION_DEFAULTS = {
[OPTION_TYPE.CONFIG_FORMAT]: DEFAULT_CONFIG_FORMAT,
[OPTION_TYPE.IGNORE_CONFIG]: [],
[OPTION_TYPE.IGNORE_DEPRECATED_RULES]: false,
[OPTION_TYPE.INIT_EMOJIS]: false,
[OPTION_TYPE.INIT_RULE_DOCS]: false,
[OPTION_TYPE.PATH_RULE_DOC]: join('docs', 'rules', '{name}.md'),
[OPTION_TYPE.PATH_RULE_LIST]: ['README.md'],
Expand Down
2 changes: 0 additions & 2 deletions lib/package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ export async function loadPlugin(path: string): Promise<Plugin> {
const propertiesToCheck: readonly (keyof PackageJson.ExportConditions)[] =
['.', 'node', 'import', 'require', 'default'];
for (const prop of propertiesToCheck) {
// @ts-expect-error -- The union type for the object is causing trouble.
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const value = exports[prop];
if (typeof value === 'string') {
pluginEntryPoint = value;
Expand Down
3 changes: 3 additions & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export enum OPTION_TYPE {
CONFIG_FORMAT = 'configFormat',
IGNORE_CONFIG = 'ignoreConfig',
IGNORE_DEPRECATED_RULES = 'ignoreDeprecatedRules',
INIT_EMOJIS = 'initEmojis',
INIT_RULE_DOCS = 'initRuleDocs',
PATH_RULE_DOC = 'pathRuleDoc',
PATH_RULE_LIST = 'pathRuleList',
Expand Down Expand Up @@ -164,6 +165,8 @@ export type GenerateOptions = {
readonly ignoreConfig?: readonly string[];
/** Whether to ignore deprecated rules from being checked, displayed, or updated. Default: `false`. */
readonly ignoreDeprecatedRules?: boolean;
/** Whether to suggest an emoji for each config. Use `--config-emoji` option to utilize a suggestion. Default: `false`. */
readonly initEmojis?: boolean;
/** Whether to create rule doc files if they don't yet exist. Default: `false`. */
readonly initRuleDocs?: boolean;
/** Path to markdown file for each rule doc. Use `{name}` placeholder for the rule name. Default: `docs/rules/{name}.md`. */
Expand Down
Loading

0 comments on commit 917ce3c

Please sign in to comment.