@prism-engineer/router
Version:
Type-safe Express.js router with automatic client generation
187 lines (181 loc) ⢠6.71 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = require("path");
const router_1 = require("../router");
async function loadConfig() {
const configPaths = [
'config.prism.router.js',
'prism.config.js'
];
for (const configPath of configPaths) {
try {
const fullPath = (0, path_1.resolve)(process.cwd(), configPath);
// For .ts files, we need to handle them differently
if (configPath.endsWith('.ts')) {
// Try to import using dynamic import for TypeScript files
try {
const config = await Promise.resolve(`${fullPath}`).then(s => __importStar(require(s)));
return config.default || config;
}
catch (tsError) {
console.log('tsError', tsError);
// If TypeScript import fails, try requiring it (in case it's been compiled)
try {
delete require.cache[fullPath];
const config = require(fullPath);
return config.default || config;
}
catch (requireError) {
continue; // Try next config file
}
}
}
else {
// For .js files, use require
try {
delete require.cache[fullPath];
const config = require(fullPath);
return config.default || config;
}
catch (jsError) {
continue; // Try next config file
}
}
}
catch (error) {
continue; // Try next config file
}
}
throw new Error(`No configuration file found. Please create one of:\n` +
configPaths.map(p => ` - ${p}`).join('\n') +
`\n\nExample config.prism.router.ts:\n` +
`export default {\n` +
` outputDir: './generated',\n` +
` name: 'ApiClient',\n` +
` baseUrl: 'http://localhost:3000',\n` +
` routes: {\n` +
` directory: './api',\n` +
` pattern: /.*\\.ts$/\n` +
` }\n` +
`} as const;`);
}
async function runCompile() {
try {
console.log('š Loading configuration...');
const config = await loadConfig();
console.log(`š Output directory: ${config.outputDir}`);
console.log(`š·ļø Client name: ${config.name}`);
console.log(`š Base URL: ${config.baseUrl}`);
for (const routeConfig of config.routes) {
console.log(`š Routes directory: ${routeConfig.directory}`);
console.log(`š Pattern: ${routeConfig.pattern}`);
if (routeConfig.options?.prefix) {
console.log(`š Prefix: ${routeConfig.options.prefix}`);
}
}
console.log('\nā” Compiling API client...');
await router_1.router.compile(config);
console.log(`\nā
API client generated successfully!`);
console.log(`š Generated file: ${config.outputDir}/${config.name}.generated.ts`);
console.log(`\nš” Usage:`);
console.log(`import { create${config.name} } from './${config.outputDir}/${config.name}.generated';`);
console.log(`const client = create${config.name}('${config.baseUrl}');`);
}
catch (error) {
console.error('\nā Compilation failed:');
console.error(error instanceof Error ? error.message : String(error));
process.exit(1);
}
}
async function runHelp() {
console.log(`
š @prism-engineer/router CLI
USAGE:
npx @prism-engineer/router <command>
npx prism-router <command>
COMMANDS:
compile Generate API client from route definitions
help Show this help message
EXAMPLES:
npx @prism-engineer/router compile
npx prism-router compile
npx prism-router help
CONFIGURATION:
Create a config file in your project root:
config.prism.router.ts:
export default {
outputDir: './generated',
name: 'ApiClient',
baseUrl: 'http://localhost:3000',
routes: {
directory: './api',
pattern: /.*\\.ts$/
}
} as const;
For more information, visit: https://github.com/prism-engineer/router
`);
}
async function main() {
const args = process.argv.slice(2);
const command = args[0];
switch (command) {
case 'compile':
await runCompile();
break;
case 'help':
case '--help':
case '-h':
await runHelp();
break;
case undefined:
console.log('ā No command specified. Use "compile" or "help".');
console.log('Run "npx @prism-engineer/router help" for usage information.');
process.exit(1);
break;
default:
console.log(`ā Unknown command: ${command}`);
console.log('Available commands: compile, help');
console.log('Run "npx @prism-engineer/router help" for usage information.');
process.exit(1);
}
}
// Run the CLI
main().catch((error) => {
console.error('ā Unexpected error:', error);
process.exit(1);
});
//# sourceMappingURL=index.js.map