feat: add configuration file strategy
This commit is contained in:
24
generation-config.json
Normal file
24
generation-config.json
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
{
|
||||||
|
"input": "example-swagger.yaml",
|
||||||
|
"output": "./src/app",
|
||||||
|
"skipLint": false,
|
||||||
|
"skipInstall": false,
|
||||||
|
"tags": {
|
||||||
|
"User": {
|
||||||
|
"baseUrl": "apiUrl",
|
||||||
|
"endpoints": [
|
||||||
|
"getUsers",
|
||||||
|
"createUser",
|
||||||
|
"getUserById",
|
||||||
|
"deleteUser"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"Product": {
|
||||||
|
"baseUrl": "apiUrl",
|
||||||
|
"endpoints": [
|
||||||
|
"getProducts"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"templates": "/Users/bsantome/Downloads/openapi-clean-arch-generator/templates"
|
||||||
|
}
|
||||||
104
main.ts
104
main.ts
@@ -18,6 +18,13 @@ import { generateReport } from './src/generators/report.generator';
|
|||||||
import { lintGeneratedFiles } from './src/generators/lint.generator';
|
import { lintGeneratedFiles } from './src/generators/lint.generator';
|
||||||
import { findEnvironmentFile, parseApiKeys } from './src/utils/environment-finder';
|
import { findEnvironmentFile, parseApiKeys } from './src/utils/environment-finder';
|
||||||
import { askApiKeysForTags, askSelectionFilter } from './src/utils/prompt';
|
import { askApiKeysForTags, askSelectionFilter } from './src/utils/prompt';
|
||||||
|
import {
|
||||||
|
loadConfig,
|
||||||
|
generateDefaultConfig,
|
||||||
|
writeConfig,
|
||||||
|
deriveSelectionFilter,
|
||||||
|
deriveTagApiKeyMap
|
||||||
|
} from './src/utils/config';
|
||||||
import type { SelectionFilter } from './src/types';
|
import type { SelectionFilter } from './src/types';
|
||||||
import type { CliOptions } from './src/types';
|
import type { CliOptions } from './src/types';
|
||||||
import packageJson from './package.json';
|
import packageJson from './package.json';
|
||||||
@@ -40,6 +47,8 @@ program
|
|||||||
.option('--dry-run', 'Simulate without generating files')
|
.option('--dry-run', 'Simulate without generating files')
|
||||||
.option('--skip-lint', 'Skip post-generation linting and formatting')
|
.option('--skip-lint', 'Skip post-generation linting and formatting')
|
||||||
.option('-s, --select-endpoints', 'Interactively select which tags and endpoints to generate')
|
.option('-s, --select-endpoints', 'Interactively select which tags and endpoints to generate')
|
||||||
|
.option('-c, --config <file>', 'Use a JSON configuration file (skips interactive prompts)')
|
||||||
|
.option('--init-config [file]', 'Generate a JSON configuration file instead of generating code')
|
||||||
.parse(process.argv);
|
.parse(process.argv);
|
||||||
|
|
||||||
const options = program.opts<CliOptions>();
|
const options = program.opts<CliOptions>();
|
||||||
@@ -52,6 +61,20 @@ async function main(): Promise<void> {
|
|||||||
log(' Angular + Clean Architecture Code Generator', 'cyan');
|
log(' Angular + Clean Architecture Code Generator', 'cyan');
|
||||||
console.log('='.repeat(60) + '\n');
|
console.log('='.repeat(60) + '\n');
|
||||||
|
|
||||||
|
// ── CONFIG FILE: override CLI defaults with config values ─────────────────
|
||||||
|
const configFile = options.config;
|
||||||
|
const generationConfig = configFile ? loadConfig(configFile) : undefined;
|
||||||
|
|
||||||
|
if (generationConfig) {
|
||||||
|
if (generationConfig.input) options.input = generationConfig.input;
|
||||||
|
if (generationConfig.output) options.output = generationConfig.output;
|
||||||
|
if (generationConfig.templates) options.templates = generationConfig.templates;
|
||||||
|
if (generationConfig.skipInstall !== undefined)
|
||||||
|
options.skipInstall = generationConfig.skipInstall;
|
||||||
|
if (generationConfig.skipLint !== undefined) options.skipLint = generationConfig.skipLint;
|
||||||
|
logInfo(`Using configuration file: ${configFile}`);
|
||||||
|
}
|
||||||
|
|
||||||
if (!fs.existsSync(options.input)) {
|
if (!fs.existsSync(options.input)) {
|
||||||
logError(`File not found: ${options.input}`);
|
logError(`File not found: ${options.input}`);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
@@ -80,6 +103,26 @@ async function main(): Promise<void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const analysis = analyzeSwagger(options.input);
|
const analysis = analyzeSwagger(options.input);
|
||||||
|
const tagSummaries = extractTagsWithOperations(analysis);
|
||||||
|
|
||||||
|
// ── INIT CONFIG MODE: generate config file and exit ───────────────────────
|
||||||
|
if (options.initConfig !== undefined) {
|
||||||
|
const envFile = findEnvironmentFile(process.cwd());
|
||||||
|
let apiKeys: ReturnType<typeof parseApiKeys> = [];
|
||||||
|
if (envFile) {
|
||||||
|
const envContent = fs.readFileSync(envFile, 'utf8');
|
||||||
|
apiKeys = parseApiKeys(envContent);
|
||||||
|
}
|
||||||
|
|
||||||
|
const defaultConfig = generateDefaultConfig(analysis, tagSummaries, options, apiKeys);
|
||||||
|
const outputFile =
|
||||||
|
typeof options.initConfig === 'string' ? options.initConfig : 'generation-config.json';
|
||||||
|
|
||||||
|
writeConfig(defaultConfig, outputFile);
|
||||||
|
logSuccess(`Configuration file generated: ${outputFile}`);
|
||||||
|
logInfo('Edit the file to customise tags, endpoints and baseUrls, then run with --config');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (options.dryRun) {
|
if (options.dryRun) {
|
||||||
logInfo('Finishing in DRY RUN mode');
|
logInfo('Finishing in DRY RUN mode');
|
||||||
@@ -89,37 +132,46 @@ async function main(): Promise<void> {
|
|||||||
createDirectoryStructure(options.output);
|
createDirectoryStructure(options.output);
|
||||||
|
|
||||||
// ── SELECTION: tags and endpoints ─────────────────────────────────────────
|
// ── SELECTION: tags and endpoints ─────────────────────────────────────────
|
||||||
const tagSummaries = extractTagsWithOperations(analysis);
|
|
||||||
let selectionFilter: SelectionFilter = {};
|
let selectionFilter: SelectionFilter = {};
|
||||||
|
let tagApiKeyMap: Record<string, string>;
|
||||||
|
|
||||||
if (options.selectEndpoints) {
|
if (generationConfig) {
|
||||||
selectionFilter = await askSelectionFilter(tagSummaries);
|
// Config-driven: derive everything from the JSON file
|
||||||
}
|
selectionFilter = deriveSelectionFilter(generationConfig);
|
||||||
|
tagApiKeyMap = deriveTagApiKeyMap(generationConfig);
|
||||||
const selectedTags = options.selectEndpoints
|
logInfo(`Tags from config: ${Object.keys(generationConfig.tags).join(', ')}`);
|
||||||
? Object.keys(selectionFilter)
|
|
||||||
: tagSummaries.map((t) => t.tag);
|
|
||||||
|
|
||||||
// ── ENVIRONMENT API KEY SELECTION ──────────────────────────────────────────
|
|
||||||
const envFile = findEnvironmentFile(process.cwd());
|
|
||||||
let apiKeys: ReturnType<typeof parseApiKeys> = [];
|
|
||||||
|
|
||||||
if (envFile) {
|
|
||||||
const envContent = fs.readFileSync(envFile, 'utf8');
|
|
||||||
apiKeys = parseApiKeys(envContent);
|
|
||||||
logSuccess(
|
|
||||||
`environment.ts found: ${colors.cyan}${path.relative(process.cwd(), envFile)}${colors.reset}`
|
|
||||||
);
|
|
||||||
if (apiKeys.length > 0) {
|
|
||||||
logInfo(`Detected API keys: ${apiKeys.map((k) => k.key).join(', ')}`);
|
|
||||||
} else {
|
|
||||||
logWarning('No keys containing "api" found in environment.ts. Will be requested manually.');
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
logWarning('No environment.ts found. The key will be requested manually per repository.');
|
// Interactive mode (original behaviour)
|
||||||
|
if (options.selectEndpoints) {
|
||||||
|
selectionFilter = await askSelectionFilter(tagSummaries);
|
||||||
|
}
|
||||||
|
|
||||||
|
const selectedTags = options.selectEndpoints
|
||||||
|
? Object.keys(selectionFilter)
|
||||||
|
: tagSummaries.map((t) => t.tag);
|
||||||
|
|
||||||
|
// ── ENVIRONMENT API KEY SELECTION ────────────────────────────────────────
|
||||||
|
const envFile = findEnvironmentFile(process.cwd());
|
||||||
|
let apiKeys: ReturnType<typeof parseApiKeys> = [];
|
||||||
|
|
||||||
|
if (envFile) {
|
||||||
|
const envContent = fs.readFileSync(envFile, 'utf8');
|
||||||
|
apiKeys = parseApiKeys(envContent);
|
||||||
|
logSuccess(
|
||||||
|
`environment.ts found: ${colors.cyan}${path.relative(process.cwd(), envFile)}${colors.reset}`
|
||||||
|
);
|
||||||
|
if (apiKeys.length > 0) {
|
||||||
|
logInfo(`Detected API keys: ${apiKeys.map((k) => k.key).join(', ')}`);
|
||||||
|
} else {
|
||||||
|
logWarning('No keys containing "api" found in environment.ts. Will be requested manually.');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
logWarning('No environment.ts found. The key will be requested manually per repository.');
|
||||||
|
}
|
||||||
|
|
||||||
|
tagApiKeyMap = await askApiKeysForTags(selectedTags, apiKeys);
|
||||||
}
|
}
|
||||||
|
|
||||||
const tagApiKeyMap = await askApiKeysForTags(selectedTags, apiKeys);
|
|
||||||
// ──────────────────────────────────────────────────────────────────────────
|
// ──────────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
const tempDir = generateCode(options.input, options.templates);
|
const tempDir = generateCode(options.input, options.templates);
|
||||||
|
|||||||
@@ -10,4 +10,27 @@ export interface CliOptions {
|
|||||||
dryRun?: boolean;
|
dryRun?: boolean;
|
||||||
selectEndpoints?: boolean;
|
selectEndpoints?: boolean;
|
||||||
skipLint?: boolean;
|
skipLint?: boolean;
|
||||||
|
config?: string;
|
||||||
|
initConfig?: string | boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Per-tag configuration inside the generation config file.
|
||||||
|
*/
|
||||||
|
export interface TagConfig {
|
||||||
|
baseUrl: string;
|
||||||
|
endpoints: string[];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JSON configuration file schema.
|
||||||
|
* Allows full non-interactive control of the generation process.
|
||||||
|
*/
|
||||||
|
export interface GenerationConfig {
|
||||||
|
input: string;
|
||||||
|
output: string;
|
||||||
|
templates?: string;
|
||||||
|
skipInstall?: boolean;
|
||||||
|
skipLint?: boolean;
|
||||||
|
tags: Record<string, TagConfig>;
|
||||||
}
|
}
|
||||||
|
|||||||
118
src/utils/config.ts
Normal file
118
src/utils/config.ts
Normal file
@@ -0,0 +1,118 @@
|
|||||||
|
import fs from 'fs-extra';
|
||||||
|
import { logInfo, logError } from './logger';
|
||||||
|
import type { GenerationConfig, TagConfig } from '../types';
|
||||||
|
import type { SwaggerAnalysis } from '../types';
|
||||||
|
import type { TagSummary } from '../types';
|
||||||
|
import type { ApiKeyInfo } from './environment-finder';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads and validates a GenerationConfig from a JSON file.
|
||||||
|
*/
|
||||||
|
export function loadConfig(filePath: string): GenerationConfig {
|
||||||
|
if (!fs.existsSync(filePath)) {
|
||||||
|
logError(`Configuration file not found: ${filePath}`);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
const raw = fs.readFileSync(filePath, 'utf8');
|
||||||
|
let config: GenerationConfig;
|
||||||
|
|
||||||
|
try {
|
||||||
|
config = JSON.parse(raw) as GenerationConfig;
|
||||||
|
} catch {
|
||||||
|
logError(`Invalid JSON in configuration file: ${filePath}`);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!config.tags || typeof config.tags !== 'object') {
|
||||||
|
logError('Configuration file must contain a "tags" object');
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const [tag, tagConfig] of Object.entries(config.tags)) {
|
||||||
|
if (!tagConfig.baseUrl || typeof tagConfig.baseUrl !== 'string') {
|
||||||
|
logError(`Tag "${tag}" must have a "baseUrl" string`);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
if (!Array.isArray(tagConfig.endpoints) || tagConfig.endpoints.length === 0) {
|
||||||
|
logError(`Tag "${tag}" must have a non-empty "endpoints" array`);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds a default GenerationConfig from a swagger analysis, including all tags
|
||||||
|
* and all endpoints. Useful for --init-config to scaffold a config template.
|
||||||
|
*/
|
||||||
|
export function generateDefaultConfig(
|
||||||
|
analysis: SwaggerAnalysis,
|
||||||
|
tagSummaries: TagSummary[],
|
||||||
|
cliOptions: {
|
||||||
|
input: string;
|
||||||
|
output: string;
|
||||||
|
templates?: string;
|
||||||
|
skipLint?: boolean;
|
||||||
|
skipInstall?: boolean;
|
||||||
|
},
|
||||||
|
apiKeys: ApiKeyInfo[]
|
||||||
|
): GenerationConfig {
|
||||||
|
const tags: Record<string, TagConfig> = {};
|
||||||
|
|
||||||
|
for (const summary of tagSummaries) {
|
||||||
|
const matchingKey = apiKeys.find((k) =>
|
||||||
|
k.key.toLowerCase().includes(summary.tag.toLowerCase())
|
||||||
|
);
|
||||||
|
|
||||||
|
tags[summary.tag] = {
|
||||||
|
baseUrl: matchingKey?.key || 'apiUrl',
|
||||||
|
endpoints: summary.operations.map((op) => op.nickname)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const config: GenerationConfig = {
|
||||||
|
input: cliOptions.input,
|
||||||
|
output: cliOptions.output,
|
||||||
|
skipLint: cliOptions.skipLint ?? false,
|
||||||
|
skipInstall: cliOptions.skipInstall ?? false,
|
||||||
|
tags
|
||||||
|
};
|
||||||
|
|
||||||
|
if (cliOptions.templates) {
|
||||||
|
config.templates = cliOptions.templates;
|
||||||
|
}
|
||||||
|
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes a GenerationConfig to a JSON file.
|
||||||
|
*/
|
||||||
|
export function writeConfig(config: GenerationConfig, filePath: string): void {
|
||||||
|
fs.writeFileSync(filePath, JSON.stringify(config, null, 2) + '\n', 'utf8');
|
||||||
|
logInfo(`Configuration file written to: ${filePath}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Derives the selectionFilter (tag → endpoint nicknames) from a GenerationConfig.
|
||||||
|
*/
|
||||||
|
export function deriveSelectionFilter(config: GenerationConfig): Record<string, string[]> {
|
||||||
|
const filter: Record<string, string[]> = {};
|
||||||
|
for (const [tag, tagConfig] of Object.entries(config.tags)) {
|
||||||
|
filter[tag] = [...tagConfig.endpoints];
|
||||||
|
}
|
||||||
|
return filter;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Derives the tagApiKeyMap (tag → baseUrl key) from a GenerationConfig.
|
||||||
|
*/
|
||||||
|
export function deriveTagApiKeyMap(config: GenerationConfig): Record<string, string> {
|
||||||
|
const map: Record<string, string> = {};
|
||||||
|
for (const [tag, tagConfig] of Object.entries(config.tags)) {
|
||||||
|
map[tag] = tagConfig.baseUrl;
|
||||||
|
}
|
||||||
|
return map;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user