Skip to content

Commit 917ce3c

Browse files
committed
feat: add --init-emojis option to suggest emojis for configs
1 parent 0dce68f commit 917ce3c

File tree

11 files changed

+374
-10
lines changed

11 files changed

+374
-10
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ There's also a `postprocess` option that's only available via a [config file](#c
136136
| `--config-format` | The format to use for config names. See choices in below [table](#--config-format). | `name` |
137137
| `--ignore-config` | Config to ignore from being displayed. Often used for an `all` config. Option can be repeated. | |
138138
| `--ignore-deprecated-rules` | Whether to ignore deprecated rules from being checked, displayed, or updated. | `false` |
139+
| `--init-emojis` | Whether to suggest an emoji for each config. Use `--config-emoji` option to utilize a suggestion. | `false` |
139140
| `--init-rule-docs` | Whether to create rule doc files if they don't yet exist. | `false` |
140141
| `--path-rule-doc` | Path to markdown file for each rule doc. Use `{name}` placeholder for the rule name. | `docs/rules/{name}.md` |
141142
| `--path-rule-list` | Path to markdown file where the rules table list should live. Option can be repeated. | `README.md` |

lib/cli.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ async function loadConfigFileOptions(): Promise<GenerateOptions> {
9595
configFormat: { type: 'string' },
9696
ignoreConfig: schemaStringArray,
9797
ignoreDeprecatedRules: { type: 'boolean' },
98+
initEmojis: { type: 'boolean' },
9899
initRuleDocs: { type: 'boolean' },
99100
pathRuleDoc: { type: 'string' },
100101
pathRuleList: { anyOf: [{ type: 'string' }, schemaStringArray] },
@@ -217,6 +218,13 @@ export async function run(
217218
)})`,
218219
parseBoolean
219220
)
221+
.option(
222+
'--init-emojis [boolean]',
223+
`(optional) Whether to suggest an emoji for each config. Use \`--config-emoji\` option to utilize a suggestion. (default: ${String(
224+
OPTION_DEFAULTS[OPTION_TYPE.INIT_EMOJIS]
225+
)})`,
226+
parseBoolean
227+
)
220228
.option(
221229
'--init-rule-docs [boolean]',
222230
`(optional) Whether to create rule doc files if they don't yet exist. (default: ${String(

lib/generator.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import { diff } from 'jest-diff';
2626
import type { GenerateOptions } from './types.js';
2727
import { OPTION_TYPE, RuleModule } from './types.js';
2828
import { replaceRulePlaceholder } from './rule-link.js';
29+
import { getEmojis } from 'gpt-emoji';
30+
import { table } from 'table';
2931

3032
function stringOrArrayWithFallback<T extends string | readonly string[]>(
3133
stringOrArray: undefined | T,
@@ -75,6 +77,8 @@ export async function generate(path: string, options?: GenerateOptions) {
7577
const ignoreDeprecatedRules =
7678
options?.ignoreDeprecatedRules ??
7779
OPTION_DEFAULTS[OPTION_TYPE.IGNORE_DEPRECATED_RULES];
80+
const initEmojis =
81+
options?.initEmojis ?? OPTION_DEFAULTS[OPTION_TYPE.INIT_EMOJIS];
7882
const initRuleDocs =
7983
options?.initRuleDocs ?? OPTION_DEFAULTS[OPTION_TYPE.INIT_RULE_DOCS];
8084
const pathRuleDoc =
@@ -113,6 +117,22 @@ export async function generate(path: string, options?: GenerateOptions) {
113117
const urlRuleDoc =
114118
options?.urlRuleDoc ?? OPTION_DEFAULTS[OPTION_TYPE.URL_RULE_DOC];
115119

120+
if (initEmojis) {
121+
if (!process.env.OPENAI_API_KEY) {
122+
throw new Error('Missing OPENAI_API_KEY environment variable.');
123+
}
124+
const configNames = Object.keys(plugin.configs || {});
125+
const result = await getEmojis(configNames, {
126+
count: 1,
127+
});
128+
const data = [
129+
['Config', 'Emoji'],
130+
...configNames.map((configName, i) => [configName, result[i]]),
131+
];
132+
console.log(table(data));
133+
return;
134+
}
135+
116136
// Gather normalized list of rules.
117137
const ruleNamesAndRules = Object.entries(plugin.rules)
118138
.map(([name, ruleModule]) => {

lib/options.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export const OPTION_DEFAULTS = {
4949
[OPTION_TYPE.CONFIG_FORMAT]: DEFAULT_CONFIG_FORMAT,
5050
[OPTION_TYPE.IGNORE_CONFIG]: [],
5151
[OPTION_TYPE.IGNORE_DEPRECATED_RULES]: false,
52+
[OPTION_TYPE.INIT_EMOJIS]: false,
5253
[OPTION_TYPE.INIT_RULE_DOCS]: false,
5354
[OPTION_TYPE.PATH_RULE_DOC]: join('docs', 'rules', '{name}.md'),
5455
[OPTION_TYPE.PATH_RULE_LIST]: ['README.md'],

lib/package-json.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ export async function loadPlugin(path: string): Promise<Plugin> {
5353
const propertiesToCheck: readonly (keyof PackageJson.ExportConditions)[] =
5454
['.', 'node', 'import', 'require', 'default'];
5555
for (const prop of propertiesToCheck) {
56-
// @ts-expect-error -- The union type for the object is causing trouble.
57-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
5856
const value = exports[prop];
5957
if (typeof value === 'string') {
6058
pluginEntryPoint = value;

lib/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ export enum OPTION_TYPE {
100100
CONFIG_FORMAT = 'configFormat',
101101
IGNORE_CONFIG = 'ignoreConfig',
102102
IGNORE_DEPRECATED_RULES = 'ignoreDeprecatedRules',
103+
INIT_EMOJIS = 'initEmojis',
103104
INIT_RULE_DOCS = 'initRuleDocs',
104105
PATH_RULE_DOC = 'pathRuleDoc',
105106
PATH_RULE_LIST = 'pathRuleList',
@@ -164,6 +165,8 @@ export type GenerateOptions = {
164165
readonly ignoreConfig?: readonly string[];
165166
/** Whether to ignore deprecated rules from being checked, displayed, or updated. Default: `false`. */
166167
readonly ignoreDeprecatedRules?: boolean;
168+
/** Whether to suggest an emoji for each config. Use `--config-emoji` option to utilize a suggestion. Default: `false`. */
169+
readonly initEmojis?: boolean;
167170
/** Whether to create rule doc files if they don't yet exist. Default: `false`. */
168171
readonly initRuleDocs?: boolean;
169172
/** Path to markdown file for each rule doc. Use `{name}` placeholder for the rule name. Default: `docs/rules/{name}.md`. */

0 commit comments

Comments
 (0)