// nodejs.ts
import * as net from 'net';
import * as fs from 'fs';

const ipcFunc = (messageFilePath: string) => {
    return new Promise<string>((resolve, reject) => {
        const server = net.createServer((client) => {
            console.log('Node.js received IPC message from C#: SolidWorks operation completed');
            client.end();
            server.close();
            resolve('SolidWorks operation completed');
        });

        server.listen({ host: '127.0.0.1', port: 3000 }, () => {
            const socket = net.connect({ host: '127.0.0.1', port: 3000 }, () => {
                // Read the message from the file
                const message = fs.readFileSync(messageFilePath, 'utf-8');
                socket.write(message, 'utf-8', () => {
                    socket.end();
                });
            });
        });
    });
};

// Example usage
const pipeName = 'YourPipeName';
const logFilePath = 'path/to/solidworks.log';

// Watch for changes in the log file
fs.watchFile(logFilePath, { persistent: false }, () => {
    ipcFunc(logFilePath)
        .then((result) => console.log(result))
        .catch((error) => console.error(error));
});
