a collection of lightweight TypeScript packages for AT Protocol, the protocol powering Bluesky
atproto bluesky typescript npm
101
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor(lex-cli): lazily import commands

Mary e879572f 49e88723

+76 -86
+5
.changeset/icy-donkeys-like.md
··· 1 + --- 2 + '@atcute/lex-cli': patch 3 + --- 4 + 5 + lazily import comamnds
+65 -7
packages/lexicons/lex-cli/src/cli.ts
··· 1 - import { or } from '@optique/core/constructs'; 1 + import { merge, object, or } from '@optique/core/constructs'; 2 + import { message } from '@optique/core/message'; 3 + import { optional } from '@optique/core/modifiers'; 4 + import { type InferValue } from '@optique/core/parser'; 5 + import { command, constant, option } from '@optique/core/primitives'; 2 6 import { run } from '@optique/run'; 7 + import { path as pathParser } from '@optique/run/valueparser'; 3 8 4 - import { exportCommandSchema, runExport } from './commands/export.ts'; 5 - import { generateCommandSchema, runGenerate } from './commands/generate.ts'; 6 - import { pullCommandSchema, runPull } from './commands/pull.ts'; 9 + const sharedOptions = object(`Global options`, { 10 + config: optional( 11 + option('-c', '--config', pathParser({ metavar: 'CONFIG' }), { 12 + description: message`path to the lexicon configuration file. defaults to searching for lex.config.js or lex.config.ts in the current directory.`, 13 + }), 14 + ), 15 + }); 16 + 17 + const generateCommandSchema = command( 18 + 'generate', 19 + merge( 20 + object({ 21 + type: constant('generate'), 22 + }), 23 + sharedOptions, 24 + ), 25 + { 26 + brief: message`generate type definitions from lexicon documents`, 27 + description: message`reads lexicon documents from the configured files and generates TypeScript type definitions and runtime validators.`, 28 + }, 29 + ); 30 + 31 + const pullCommandSchema = command( 32 + 'pull', 33 + merge( 34 + object({ 35 + type: constant('pull'), 36 + }), 37 + sharedOptions, 38 + ), 39 + { 40 + brief: message`pull lexicon documents from configured sources`, 41 + description: message`fetches lexicon documents from configured git repositories and writes them to the output directory.`, 42 + }, 43 + ); 44 + 45 + const exportCommandSchema = command( 46 + 'export', 47 + merge( 48 + object({ 49 + type: constant('export'), 50 + }), 51 + sharedOptions, 52 + ), 53 + { 54 + brief: message`export lexicon documents as JSON files`, 55 + description: message`exports lexicon documents (from JSON or builder files) to JSON format for publishing or distribution.`, 56 + }, 57 + ); 58 + 59 + export type GenerateCommand = InferValue<typeof generateCommandSchema>; 60 + export type PullCommand = InferValue<typeof pullCommandSchema>; 61 + export type ExportCommand = InferValue<typeof exportCommandSchema>; 7 62 8 63 const parser = or(generateCommandSchema, pullCommandSchema, exportCommandSchema); 9 64 10 65 const result = run(parser, { programName: 'lex-cli', help: 'both' }); 11 66 12 67 if (result.type === 'generate') { 13 - await runGenerate(result); 68 + const { handler } = await import('./commands/generate.ts'); 69 + await handler(result); 14 70 } else if (result.type === 'pull') { 15 - await runPull(result); 71 + const { handler } = await import('./commands/pull.ts'); 72 + await handler(result); 16 73 } else if (result.type === 'export') { 17 - await runExport(result); 74 + const { handler } = await import('./commands/export.ts'); 75 + await handler(result); 18 76 }
+2 -22
packages/lexicons/lex-cli/src/commands/export.ts
··· 3 3 4 4 import type { LexiconDoc } from '@atcute/lexicon-doc'; 5 5 6 - import { merge, object } from '@optique/core/constructs'; 7 - import { message } from '@optique/core/message'; 8 - import { type InferValue } from '@optique/core/parser'; 9 - import { command, constant } from '@optique/core/primitives'; 10 6 import pc from 'picocolors'; 11 7 8 + import type { ExportCommand } from '../cli.ts'; 12 9 import { loadConfig, type ExportConfig, type NormalizedConfig } from '../config.ts'; 13 10 import { createFormatter, type Formatter } from '../formatter.ts'; 14 11 import { loadLexicons } from '../lexicon-loader.ts'; 15 - import { sharedOptions } from '../shared-options.ts'; 16 - 17 - export const exportCommandSchema = command( 18 - 'export', 19 - merge( 20 - object({ 21 - type: constant('export'), 22 - }), 23 - sharedOptions, 24 - ), 25 - { 26 - brief: message`export lexicon documents as JSON files`, 27 - description: message`exports lexicon documents (from JSON or builder files) to JSON format for publishing or distribution.`, 28 - }, 29 - ); 30 - 31 - export type ExportCommand = InferValue<typeof exportCommandSchema>; 32 12 33 13 /** 34 14 * ensures export configuration is present ··· 64 44 * runs the export command to write lexicon documents as JSON files 65 45 * @param args parsed command arguments 66 46 */ 67 - export const runExport = async (args: ExportCommand): Promise<void> => { 47 + export const handler = async (args: ExportCommand): Promise<void> => { 68 48 const config = await loadConfig(args.config); 69 49 const exportConfig = ensureExportConfig(config); 70 50
+2 -22
packages/lexicons/lex-cli/src/commands/generate.ts
··· 2 2 import * as module from 'node:module'; 3 3 import * as path from 'node:path'; 4 4 5 - import { merge, object } from '@optique/core/constructs'; 6 - import { message } from '@optique/core/message'; 7 - import { type InferValue } from '@optique/core/parser'; 8 - import { command, constant } from '@optique/core/primitives'; 9 5 import pc from 'picocolors'; 10 6 7 + import type { GenerateCommand } from '../cli.ts'; 11 8 import { generateLexiconApi, type ImportMapping } from '../codegen.ts'; 12 9 import { loadConfig, type GenerateConfig, type NormalizedConfig } from '../config.ts'; 13 10 import { createFormatter } from '../formatter.ts'; 14 11 import { loadLexicons } from '../lexicon-loader.ts'; 15 12 import { packageJsonSchema } from '../lexicon-metadata.ts'; 16 - import { sharedOptions } from '../shared-options.ts'; 17 13 18 14 /** 19 15 * resolves package imports to ImportMapping[] ··· 129 125 return mappings; 130 126 }; 131 127 132 - export const generateCommandSchema = command( 133 - 'generate', 134 - merge( 135 - object({ 136 - type: constant('generate'), 137 - }), 138 - sharedOptions, 139 - ), 140 - { 141 - brief: message`generate type definitions from lexicon documents`, 142 - description: message`reads lexicon documents from the configured files and generates TypeScript type definitions and runtime validators.`, 143 - }, 144 - ); 145 - 146 - export type GenerateCommand = InferValue<typeof generateCommandSchema>; 147 - 148 128 type ResolvedGenerateConfig = GenerateConfig & { outdir: string; files: string[] }; 149 129 150 130 const ensureGenerateConfig = (config: NormalizedConfig): ResolvedGenerateConfig => { ··· 172 152 * runs the generate command to create type definitions from lexicon documents 173 153 * @param args parsed command arguments 174 154 */ 175 - export const runGenerate = async (args: GenerateCommand): Promise<void> => { 155 + export const handler = async (args: GenerateCommand): Promise<void> => { 176 156 const config = await loadConfig(args.config); 177 157 const generateConfig = ensureGenerateConfig(config); 178 158
+2 -22
packages/lexicons/lex-cli/src/commands/pull.ts
··· 3 3 4 4 import { lexiconDoc, refineLexiconDoc, type LexiconDoc } from '@atcute/lexicon-doc'; 5 5 6 - import { merge, object } from '@optique/core/constructs'; 7 - import { message } from '@optique/core/message'; 8 - import { type InferValue } from '@optique/core/parser'; 9 - import { command, constant } from '@optique/core/primitives'; 10 6 import pc from 'picocolors'; 11 7 8 + import type { PullCommand } from '../cli.ts'; 12 9 import { loadConfig, type NormalizedConfig, type PullConfig, type SourceConfig } from '../config.ts'; 13 10 import { createFormatter, type Formatter } from '../formatter.ts'; 14 11 import { pullAtprotoSource } from '../pull-sources/atproto.ts'; 15 12 import { pullGitSource } from '../pull-sources/git.ts'; 16 13 import type { PullResult, PulledLexicon, SourceLocation } from '../pull-sources/types.ts'; 17 - import { sharedOptions } from '../shared-options.ts'; 18 - 19 - export const pullCommandSchema = command( 20 - 'pull', 21 - merge( 22 - object({ 23 - type: constant('pull'), 24 - }), 25 - sharedOptions, 26 - ), 27 - { 28 - brief: message`pull lexicon documents from configured sources`, 29 - description: message`fetches lexicon documents from configured git repositories and writes them to the output directory.`, 30 - }, 31 - ); 32 - 33 - export type PullCommand = InferValue<typeof pullCommandSchema>; 34 14 35 15 interface SourceRevision { 36 16 source: SourceConfig; ··· 180 160 * runs the pull command to fetch lexicon documents from configured sources 181 161 * @param args parsed command arguments 182 162 */ 183 - export const runPull = async (args: PullCommand): Promise<void> => { 163 + export const handler = async (args: PullCommand): Promise<void> => { 184 164 const config = await loadConfig(args.config); 185 165 const pullConfig = ensurePullConfig(config); 186 166
-13
packages/lexicons/lex-cli/src/shared-options.ts
··· 1 - import { object } from '@optique/core/constructs'; 2 - import { message } from '@optique/core/message'; 3 - import { optional } from '@optique/core/modifiers'; 4 - import { option } from '@optique/core/primitives'; 5 - import { path as pathParser } from '@optique/run/valueparser'; 6 - 7 - export const sharedOptions = object(`Global options`, { 8 - config: optional( 9 - option('-c', '--config', pathParser({ metavar: 'CONFIG' }), { 10 - description: message`path to the lexicon configuration file. defaults to searching for lex.config.js or lex.config.ts in the current directory.`, 11 - }), 12 - ), 13 - });