The AutoRest Core processing engine orchestrates the entire code generation pipeline, managing configuration, extensions, and event coordination.
Main class that controls the AutoRest code generation process, extending Node.js EventEmitter for comprehensive event handling.
/**
* Main AutoRest generator instance that orchestrates code generation pipeline
* Extends EventEmitter to provide progress tracking and artifact notifications
*/
class AutoRest extends EventEmitter {
constructor(
loggerSink: LoggerSink,
fileSystem?: IFileSystem,
configFileOrFolderUri?: string
);
/** Add configuration to the current AutoRest instance */
AddConfiguration(configuration: any): void;
/** Reset all configuration to default state */
ResetConfiguration(): Promise<void>;
/** Start the code generation process */
Process(): { finish: Promise<boolean | Error>; cancel(): void };
/** Regenerate the configuration view */
RegenerateView(includeDefault?: boolean): Promise<AutorestContext>;
/** Invalidate the current configuration view, forcing regeneration */
Invalidate(): void;
/** Get the current configuration view (async property) */
readonly view: Promise<AutorestContext>;
}
/** Function signature for logging message handler */
type LoggerSink = (message: Message) => void;Usage Examples:
import { AutoRest } from "@autorest/core";
import { RealFileSystem } from "@azure-tools/datastore";
// Basic setup
const autorest = new AutoRest(
(message) => {
console.log(`[${message.Channel}] ${message.Text}`);
},
new RealFileSystem()
);
// Add configuration for C# generation
autorest.AddConfiguration({
"input-file": "https://petstore.swagger.io/v2/swagger.json",
"output-folder": "./generated",
"csharp": {
"namespace": "PetStore.Client",
"client-name": "PetStoreClient"
}
});
// Subscribe to events
autorest.GeneratedFile.Subscribe((sender, artifact) => {
console.log(`Generated: ${artifact.uri}`);
});
autorest.Finished.Subscribe((sender, result) => {
if (result instanceof Error) {
console.error("Generation failed:", result);
} else {
console.log("Generation completed successfully");
}
});
// Start processing
const { finish, cancel } = autorest.Process();
// Wait for completion or cancel if needed
try {
const success = await finish;
console.log("Final result:", success);
} catch (error) {
console.error("Process error:", error);
}Event system for tracking processing progress and handling generated artifacts.
interface AutoRest {
/** Emitted when the Process() method completes */
Finished: IEvent<AutoRest, boolean | Error>;
/** Emitted when a file is generated during processing */
GeneratedFile: IEvent<AutoRest, Artifact>;
/** Emitted when a file should be protected from deletion */
ProtectFile: IEvent<AutoRest, string>;
/** Emitted when a folder should be cleared */
ClearFolder: IEvent<AutoRest, string>;
}
interface IEvent<TSender, TArgs> {
/** Subscribe to the event with a callback function */
Subscribe(fn: (sender: TSender, args: TArgs) => void): () => void;
/** Unsubscribe a specific callback from the event */
Unsubscribe(fn: (sender: TSender, args: TArgs) => void): void;
/** Manually dispatch the event (typically internal use) */
Dispatch(args: TArgs): void;
}Methods for controlling the AutoRest processing lifecycle.
interface ProcessResult {
/** Promise that resolves when processing completes */
finish: Promise<boolean | Error>;
/** Function to cancel the current processing operation */
cancel(): void;
}Configuration Management:
// Multiple configuration sources
autorest.AddConfiguration({
"input-file": ["swagger1.json", "swagger2.json"],
"output-folder": "./client",
"generate-metadata": true
});
// Override previous settings
autorest.AddConfiguration({
"output-folder": "./updated-client",
"clear-output-folder": true
});
// Reset to start fresh
await autorest.ResetConfiguration();Utility function for shutting down all active AutoRest processes.
/**
* Shut down active AutoRest extension processes
* Call this when your application is terminating to clean up resources
*/
async function Shutdown(): Promise<void>;Usage:
import { Shutdown } from "@autorest/core";
// During application shutdown
process.on('SIGINT', async () => {
console.log('Shutting down AutoRest processes...');
await Shutdown();
process.exit(0);
});Core document processing functions exported from the main module.
/**
* Processes a document (yaml, markdown or JSON) and returns as JSON
* @param content - Document content to process
* @returns Promise resolving to JSON string representation
*/
async function LiterateToJson(content: string): Promise<string>;
/**
* Determines document type based on content analysis
* @param content - Document content to analyze
* @returns Promise resolving to DocumentType
*/
async function IdentifyDocument(content: string): Promise<DocumentType>;
/**
* Checks if document content represents an OpenAPI specification
* @param content - Document content to evaluate
* @returns Promise resolving to true if OpenAPI document
*/
async function IsOpenApiDocument(content: string): Promise<boolean>;
/**
* Checks if file extension is supported for OpenAPI documents
* @param extension - File extension (without dot)
* @returns Promise resolving to true if supported OpenAPI extension
*/
async function IsOpenApiExtension(extension: string): Promise<boolean>;
/**
* Checks if file extension is for configuration documents
* @param extension - File extension (without dot)
* @returns Promise resolving to true if supported config extension
*/
async function IsConfigurationExtension(extension: string): Promise<boolean>;