Programmatic access to ForSure functionality through APIs and SDKs.
ForSure provides multiple ways to integrate with your applications and workflows:
Get started with the ForSure API in minutes:
// Install the ForSure package
npm install @forsure/cli
// Import in your application
import { ForSure } from '@forsure/cli'
// Create a new ForSure instance
const forsure = new ForSure()
// Parse a ForSure file
const project = await forsure.parse('project.forsure')
// Generate the file structure
await forsure.generate(project, { output: './dist' })Parse and validate ForSure files:
import { Parser } from '@forsure/parser'
const parser = new Parser()
// Parse a ForSure file
const ast = await parser.parseFile('project.forsure')
// Validate the AST
const validation = parser.validate(ast)
if (!validation.valid) {
console.error('Validation errors:', validation.errors)
}
// Get file tree
const fileTree = parser.buildFileTree(ast)
console.log(fileTree)Generate file structures from parsed ForSure files:
import { Generator } from '@forsure/generator'
const generator = new Generator({
outputDir: './output',
overwrite: false,
dryRun: false
})
// Generate files
const result = await generator.generate(fileTree)
console.log(`Generated ${result.files.length} files`)
console.log(`Created ${result.directories.length} directories`)Use ForSure's template system for code generation:
import { TemplateEngine } from '@forsure/templates'
const templates = new TemplateEngine({
templateDir: './templates',
variables: {
author: 'Your Name',
license: 'MIT'
}
})
// Render a template
const component = await templates.render('react-component.tsx', {
name: 'MyButton',
props: ['onClick', 'children'],
typescript: true
})
// Write to file
await templates.writeFile('MyButton.tsx', component)Configure the API to match your needs:
import { ForSure } from '@forsure/cli'
const forsure = new ForSure({
// Global configuration
config: './.forsurerc.json',
// Generator options
generator: {
outputDir: './dist',
overwrite: true,
dryRun: false,
templates: './templates'
},
// Parser options
parser: {
strict: true,
allowComments: true,
validateImports: true
},
// Template variables
variables: {
author: 'Your Name',
version: '1.0.0',
license: 'MIT'
}
})Proper error handling for robust applications:
import { ForSure, ParseError, GenerateError } from '@forsure/cli'
async function processProject(filePath) {
try {
const forsure = new ForSure()
// Parse the file
const ast = await forsure.parse(filePath)
// Validate
const validation = forsure.validate(ast)
if (!validation.valid) {
throw new Error(`Validation failed: ${validation.errors.join(', ')}`)
}
// Generate
await forsure.generate(ast, { output: './output' })
console.log('Project generated successfully')
} catch (error) {
if (error instanceof ParseError) {
console.error('Parse error:', error.message)
console.error('Location:', error.line, error.column)
} else if (error instanceof GenerateError) {
console.error('Generation error:', error.message)
console.error('File:', error.file)
} else {
console.error('Unexpected error:', error)
}
}
}Listen to events and hook into the generation process:
import { ForSure } from '@forsure/cli'
const forsure = new ForSure()
// Listen to parse events
forsure.on('parse:start', (file) => {
console.log(`Parsing ${file}...`)
})
forsure.on('parse:complete', (ast) => {
console.log('Parsing complete')
})
// Listen to generation events
forsure.on('generate:file:start', (file) => {
console.log(`Generating ${file.path}...`)
})
forsure.on('generate:file:complete', (file) => {
console.log(`Generated ${file.path}`)
})
forsure.on('generate:complete', (result) => {
console.log(`Generated ${result.files.length} files`)
})
// Custom hooks
forsure.hook('before:generate', async (context) => {
// Custom logic before generation
console.log('Preparing for generation...')
})
forsure.hook('after:generate', async (context) => {
// Custom logic after generation
console.log('Cleaning up after generation...')
})Full TypeScript support with type definitions:
import { ForSure, FileTree, GeneratorOptions } from '@forsure/cli'
interface CustomOptions extends GeneratorOptions {
customFeature: boolean
metadata?: Record<string, any>
}
async function generateProject(
filePath: string,
options: CustomOptions
): Promise<void> {
const forsure = new ForSure()
const ast: FileTree = await forsure.parse(filePath)
await forsure.generate(ast, {
outputDir: options.outputDir,
customFeature: options.customFeature,
metadata: options.metadata
})
}
// Usage with type safety
generateProject('project.forsure', {
outputDir: './dist',
customFeature: true,
metadata: { version: '1.0.0' }
})Explore specific API documentation: