Skip to content

Commit e0c1aa8

Browse files
committed
bruhh whyyyyyyyyy
1 parent 301111c commit e0c1aa8

File tree

4 files changed

+106
-104
lines changed

4 files changed

+106
-104
lines changed

.github/workflows/docs.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ name: Deploy GitHub Pages
22

33
on:
44
push:
5-
branches: [main, master]
5+
branches:
6+
- master
67
paths: ['docs/**']
78
workflow_dispatch:
89

src/cli/index.ts

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import * as fs from 'fs';
44
import * as path from 'path';
55
import { processDirectory, processFile } from './processor';
6-
import { LuatsConfig, loadConfig, defaultConfig } from './config';
6+
import { LuatsConfig, loadConfig } from './config';
7+
import { analyze } from '../index';
78

89
// CLI command types
910
type Command = 'convert' | 'validate' | 'help';
@@ -15,6 +16,7 @@ interface CliOptions {
1516
config?: string;
1617
watch?: boolean;
1718
verbose?: boolean;
19+
silent?: boolean;
1820
}
1921

2022
/**
@@ -80,6 +82,8 @@ function parseOptions(args: string[]): CliOptions {
8082
options.watch = true;
8183
} else if (arg === '--verbose' || arg === '-v') {
8284
options.verbose = true;
85+
} else if (arg === '--silent' || arg === '-s') {
86+
options.silent = true;
8387
}
8488
}
8589

@@ -135,11 +139,15 @@ async function convertCommand(options: CliOptions, config: LuatsConfig): Promise
135139
if (stats.isFile()) {
136140
// Process a single file
137141
await processFile(inputPath, outputPath, config);
138-
console.log(`Converted file: ${inputPath} -> ${outputPath}`);
142+
if (!options.silent) {
143+
console.log(`Converted file: ${inputPath} -> ${outputPath}`);
144+
}
139145
} else if (stats.isDirectory()) {
140146
// Process a directory
141147
await processDirectory(inputPath, outputPath, config);
142-
console.log(`Converted directory: ${inputPath} -> ${outputPath}`);
148+
if (!options.silent) {
149+
console.log(`Converted directory: ${inputPath} -> ${outputPath}`);
150+
}
143151
} else {
144152
throw new Error(`Invalid input: ${inputPath}`);
145153
}
@@ -156,24 +164,41 @@ async function validateCommand(options: CliOptions, config: LuatsConfig): Promis
156164
const inputPath = path.resolve(options.input);
157165

158166
if (!fs.existsSync(inputPath)) {
159-
throw new Error(`Input file does not exist: ${inputPath}`);
167+
throw new Error(`File not found: ${inputPath}`);
160168
}
161169

162-
if (fs.statSync(inputPath).isDirectory()) {
163-
throw new Error('Validation can only be performed on a single file');
170+
if (!fs.statSync(inputPath).isFile()) {
171+
throw new Error(`Not a file: ${inputPath}`);
164172
}
165173

166-
// Read the file
174+
if (!options.silent) {
175+
console.log(`Validating ${inputPath}`);
176+
}
177+
178+
// Read input file
167179
const luaCode = fs.readFileSync(inputPath, 'utf-8');
168180

169181
// Determine if it's Luau based on extension
170-
const isLuau = inputPath.endsWith('.luau');
182+
const isLuau = path.extname(inputPath) === '.luau';
171183

172-
console.log(`Validating file: ${inputPath}`);
184+
// Analyze the code
185+
const result = analyze(luaCode, isLuau);
173186

174-
// Here you would add the actual validation logic
175-
// For now, we'll just return success
176-
console.log('Validation successful!');
187+
if (result.errors.length === 0) {
188+
if (!options.silent) {
189+
console.log(`✓ ${inputPath} is valid`);
190+
}
191+
} else {
192+
console.error(`✗ ${inputPath} has ${result.errors.length} error(s)`);
193+
194+
if (options.verbose || !options.silent) {
195+
result.errors.forEach((error: Error, index: number) => {
196+
console.error(`Error ${index + 1}: ${error.message}`);
197+
});
198+
}
199+
200+
throw new Error('Validation failed');
201+
}
177202
}
178203

179204
/**
@@ -187,17 +212,19 @@ Usage:
187212
luats <command> [options]
188213
189214
Commands:
190-
convert Convert Lua/Luau files to TypeScript
191-
validate Validate Lua/Luau files
215+
convert Convert a Lua/Luau file or directory to TypeScript
216+
validate Validate a Lua/Luau file
192217
193218
Options:
194219
--input, -i Input file or directory
195220
--output, -o Output file or directory
196221
--config, -c Config file path
197222
--watch, -w Watch for file changes
198223
--verbose, -v Verbose output
224+
--silent, -s Suppress output messages
199225
200226
Examples:
227+
luats convert --input ./src/types.lua --output ./dist/types.d.ts
201228
luats convert --input ./src --output ./dist
202229
luats validate --input ./src/main.lua
203230
`);
@@ -210,33 +237,6 @@ if (require.main === module) {
210237
process.exit(1);
211238
});
212239
}
213-
214-
if (!fs.existsSync(inputPath)) {
215-
throw new Error(`File not found: ${inputPath}`);
216-
}
217-
218-
if (!fs.statSync(inputPath).isFile()) {
219-
throw new Error(`Not a file: ${inputPath}`);
220-
}
221-
222-
if (!options.silent) {
223-
console.log(`Validating ${inputPath}`);
224-
}
225-
226-
// Read input file
227-
const luaCode = fs.readFileSync(inputPath, 'utf-8');
228-
229-
// Determine if it's Luau based on extension
230-
const isLuau = path.extname(inputPath) === '.luau';
231-
232-
// Analyze the code
233-
const result = analyze(luaCode, isLuau);
234-
235-
if (result.errors.length === 0) {
236-
if (!options.silent) {
237-
console.log(`✓ ${inputPath} is valid`);
238-
}
239-
} else {
240240
console.error(`✗ ${inputPath} has ${result.errors.length} error(s)`);
241241

242242
if (options.verbose || !options.silent) {

src/cli/processor.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import * as path from 'path';
33
import * as glob from 'glob';
44
import { LuatsConfig } from './config';
55

6-
// Import your core modules
7-
import { Parser } from '../parsers/lua';
6+
// Import core modules
7+
import { LuaParser } from '../parsers/lua';
8+
import { LuauParser } from '../parsers/luau';
89
import { TypeGenerator } from '../generators/typescript';
910

1011
// Interface for plugin context
@@ -59,7 +60,8 @@ export async function processDirectory(
5960
): Promise<void> {
6061
try {
6162
// Get all matching files
62-
const files = glob.sync(config.include || ['**/*.{lua,luau}'], {
63+
const includePatterns = config.include || ['**/*.{lua,luau}'];
64+
const files = glob.sync(includePatterns.length === 1 ? includePatterns[0] : includePatterns, {
6365
cwd: inputDir,
6466
ignore: config.exclude || ['**/node_modules/**', '**/dist/**'],
6567
absolute: false
@@ -90,12 +92,9 @@ export async function processDirectory(
9092
*/
9193
function generateTypeScript(luaCode: string, isLuau: boolean, config: LuatsConfig): string {
9294
// Initialize parser based on whether it's Luau or Lua
93-
const parser = new Parser({
94-
luaVersion: isLuau ? 'luau' : (config.parserOptions?.luaVersion || '5.1'),
95-
locations: config.parserOptions?.locations,
96-
comments: config.parserOptions?.comments,
97-
scope: config.parserOptions?.scope
98-
});
95+
const parser = isLuau
96+
? new LuauParser()
97+
: new LuaParser();
9998

10099
// Initialize type generator
101100
const generator = new TypeGenerator(config.typeGeneratorOptions || {});
@@ -112,7 +111,9 @@ function generateTypeScript(luaCode: string, isLuau: boolean, config: LuatsConfi
112111
}
113112

114113
// Generate TypeScript code
115-
return generator.generate(ast);
114+
return isLuau
115+
? generator.generateFromLuauAST(ast)
116+
: generator.generateFromLuaAST(ast);
116117
}
117118

118119
/**

src/generators/typescript.ts

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,78 @@
1-
import * as AST from '../types.js';
1+
import * as AST from '../types';
22

3-
export interface TypeScriptInterface {
4-
name: string;
5-
properties: TypeScriptProperty[];
6-
extends?: string[];
7-
generics?: string[];
3+
export interface TypeGeneratorOptions {
4+
moduleType?: 'esm' | 'commonjs';
5+
inferTypes?: boolean;
6+
strictNullChecks?: boolean;
7+
robloxTypes?: boolean;
8+
preserveComments?: boolean;
9+
commentStyle?: 'jsdoc' | 'tsdoc' | 'inline';
810
}
911

1012
export interface TypeScriptProperty {
1113
name: string;
1214
type: string;
1315
optional: boolean;
14-
readonly?: boolean;
16+
description?: string;
1517
}
1618

17-
export interface TypeGeneratorOptions {
18-
useUnknown: boolean; // Use 'unknown' instead of 'any'
19-
exportTypes: boolean; // Export generated interfaces
20-
useReadonly: boolean; // Mark properties as readonly when possible
21-
generateComments: boolean; // Generate JSDoc comments
22-
arrayType: 'array' | 'record' | 'auto'; // How to convert table array types
23-
preserveTableIndexSignatures: boolean; // Preserve Lua table index signatures in TS
24-
functionStyle: 'arrow' | 'method' | 'interface'; // How to represent function types
25-
mergeInterfaces: boolean; // Merge interfaces with the same name
26-
useNamespaces: boolean; // Use TS namespaces for organization
27-
inferTypes: boolean; // Infer types for inline tables
28-
indentSpaces: number; // Number of spaces for indentation
29-
singleQuote: boolean; // Use single quotes instead of double quotes
30-
trailingComma: boolean; // Include trailing commas in object types
31-
extractEnums: boolean; // Extract string union types to enum declarations
32-
preserveOptionalTypes: boolean; // Keep optional types (foo?: string vs foo: string | undefined)
19+
export interface TypeScriptInterface {
20+
name: string;
21+
properties: TypeScriptProperty[];
22+
extends?: string[];
23+
description?: string;
3324
}
3425

3526
export const defaultTypeGeneratorOptions: TypeGeneratorOptions = {
36-
useUnknown: false,
37-
exportTypes: true,
38-
useReadonly: false,
39-
generateComments: true,
40-
arrayType: 'auto',
41-
preserveTableIndexSignatures: true,
42-
functionStyle: 'arrow',
43-
mergeInterfaces: true,
44-
useNamespaces: false,
45-
inferTypes: false,
46-
indentSpaces: 2,
47-
singleQuote: false,
48-
trailingComma: false,
49-
extractEnums: true,
50-
preserveOptionalTypes: true
27+
moduleType: 'esm',
28+
inferTypes: true,
29+
strictNullChecks: true,
30+
robloxTypes: false,
31+
preserveComments: true,
32+
commentStyle: 'jsdoc'
5133
};
5234

5335
export class TypeGenerator {
5436
private options: TypeGeneratorOptions;
55-
private interfaces: Map<string, TypeScriptInterface> = new Map();
5637

5738
constructor(options: Partial<TypeGeneratorOptions> = {}) {
5839
this.options = { ...defaultTypeGeneratorOptions, ...options };
5940
}
6041

61-
public generateFromLuauAST(ast: AST.Program): string {
62-
this.interfaces.clear();
63-
64-
// Extract comments if available and associate with declarations
65-
this.extractComments(ast);
66-
67-
for (const statement of ast.body) {
68-
if (statement.type === 'TypeAlias') {
69-
this.processTypeAlias(statement);
70-
}
71-
}
72-
73-
return this.generateTypeScriptCode();
42+
/**
43+
* Generate TypeScript code from a Lua AST
44+
*/
45+
public generateFromLuaAST(ast: AST.Program): string {
46+
// Placeholder implementation
47+
return `// TypeScript code generated from Lua AST
48+
// Options: ${JSON.stringify(this.options)}
49+
50+
/**
51+
* This is a placeholder implementation
52+
*/
53+
export interface LuaModule {
54+
// Add your generated types here
55+
}
56+
`;
7457
}
7558

59+
/**
60+
* Generate TypeScript code from a Luau AST
61+
*/
62+
public generateFromLuauAST(ast: AST.Program): string {
63+
// Placeholder implementation
64+
return `// TypeScript code generated from Luau AST
65+
// Options: ${JSON.stringify(this.options)}
66+
67+
/**
68+
* This is a placeholder implementation
69+
*/
70+
export interface LuauModule {
71+
// Add your generated types here
72+
}
73+
`;
74+
}
75+
}
7676
public generateFromTableType(tableName: string, tableType: AST.TableType): TypeScriptInterface {
7777
const properties: TypeScriptProperty[] = [];
7878

0 commit comments

Comments
 (0)