UNPKG

@ahmic/autoit-js

Version:
64 lines (61 loc) 2.11 kB
import { arch, platform } from 'node:os'; import { dirname, resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; import koffi from 'koffi'; import { Logger } from '../util/logger.js'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); class AutoIt { path; logger; lib = null; functionCache = {}; constructor() { const archSuffix = arch() === 'x64' ? '_x64' : ''; this.path = resolve(`${__dirname}/AutoItX3${archSuffix}.dll`); this.logger = new Logger(this.constructor.name); if (platform() !== 'win32') { this.logger.warn('AutoIt is only supported on Windows. AutoIt JS will not function as expected!'); } } load() { if (this.lib) { this.logger.warn('AutoIt is already loaded'); } else { this.logger.debug(`Loading AutoIt from ${this.path}`); this.lib = koffi.load(this.path); this.logger.debug('AutoIt loaded'); } } unload() { if (this.lib) { this.logger.debug(`Unloading AutoIt from ${this.path}`); this.lib.unload(); this.logger.debug('AutoIt unloaded'); this.lib = null; } else { this.logger.warn('AutoIt is already unloaded'); } } get isLoaded() { return this.lib !== null; } invoke(functionName, functionReturnType, functionArgumentTypes, functionArguments) { if (!this.lib) { throw new Error('You must call load() before invoking functions'); } let func = this.functionCache[functionName]; if (!func) { func = this.lib.func('__stdcall', functionName, functionReturnType, functionArgumentTypes); this.functionCache[functionName] = func; } const output = func(...functionArguments); this.logger.logFunctionCall(functionName, functionArguments, output); return output; } } const autoit = new AutoIt(); export { AutoIt, autoit }; //# sourceMappingURL=autoit.js.map