import * as fs from 'fs'; import * as path from 'path'; import * as util from 'util';…
- User
import * as fs from 'fs'; import * as path from 'path'; import * as util from 'util'; import * as glob from 'glob';
// Promisified versions of fs functions for easier async/await usage. const readFile = util.promisify(fs.readFile); const writeFile = util.promisify(fs.writeFile); const mkdir = util.promisify(fs.mkdir); const access = util.promisify(fs.access);
/**
- Interface for piconfig.json */ interface PiConfig { [sourceDir: string]: string; }
/**
- Reads and parses the piconfig.json file.
- @returns The parsed configuration object.
*/
async function readConfig(): Promise {
const configPath = path.resolve('piconfig.json');
try {
const data = await readFile(configPath, 'utf8');
const config: PiConfig = JSON.parse(data);
console.log(
Loaded configuration from ${configPath}:, config); return config; } catch (err) { console.error(Error reading or parsing piconfig.json:, err); process.exit(1); } }
/**
-
Translates .pi content to Python syntax by replacing curly brackets with colons
-
and managing indentation.
-
@param content The content of the .pi file.
-
@returns The translated Python code. */ function translatePiToPy(content: string): string { const lines = content.split(/\r?\n/); const outputLines: string[] = []; const indentUnit = ' '; // 4 spaces let currentIndentLevel = 0;
for (let line of lines) { let trimmedLine = line.trim();
// Skip empty lines if (trimmedLine.length === 0) { outputLines.push(''); continue; } // Handle closing brace if (trimmedLine.startsWith('}')) { currentIndentLevel = Math.max(currentIndentLevel - 1, 0); trimmedLine = trimmedLine.substring(1).trim(); // If there's code after }, process it if (trimmedLine.length > 0) { // Fall through to process the remaining code } else { // No more code on this line outputLines.push(''); continue; } } // Handle opening brace let hasOpeningBrace = false; if (trimmedLine.endsWith('{')) { hasOpeningBrace = true; trimmedLine = trimmedLine.substring(0, trimmedLine.length - 1).trim() + ':'; } // Apply indentation const indent = indentUnit.repeat(currentIndentLevel); outputLines.push(indent + trimmedLine); // If there was an opening brace, increase the indent if (hasOpeningBrace) { currentIndentLevel += 1; }}
return outputLines.join('\n'); }
/**
- Ensures that the directory exists. If not, creates it recursively.
- @param dirPath The directory path to ensure. */ async function ensureDirectory(dirPath: string) { try { await access(dirPath, fs.constants.F_OK); // Directory exists } catch { // Directory does not exist, create it await mkdir(dirPath, { recursive: true }); console.log(); } }
- Assistant