Skip to content

Commit

Permalink
replace events with callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
idleberg committed Mar 4, 2024
1 parent 0a3d83b commit a0f451f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 20 deletions.
5 changes: 0 additions & 5 deletions src/events.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/makensis.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export { eventEmitter as events } from './events';

export { commandHelp, compile, headerInfo, license, nsisDir, version } from './commands.js';
15 changes: 6 additions & 9 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { eventEmitter } from './events.js';
import { input as inputCharsets, output as outputCharsets } from './charsets.js';
import { platform } from 'node:os';
import { spawn } from 'node:child_process';
Expand Down Expand Up @@ -344,11 +343,11 @@ function spawnMakensis(cmd: string, args: Array<string>, compilerOptions: makens
outFile = detectOutfile(line);
}

if (!compilerOptions.events) {
if (typeof compilerOptions.onData !== 'function') {
return;
}

eventEmitter.emit('stdout', {
compilerOptions.onData({
line,
outFile,
hasWarning: Boolean(warnings),
Expand All @@ -359,13 +358,11 @@ function spawnMakensis(cmd: string, args: Array<string>, compilerOptions: makens
const line = data.toString();
stream.stderr += line;

if (!compilerOptions.events) {
if (typeof compilerOptions.onError !== 'function') {
return;
}

eventEmitter.emit('stderr', {
line,
});
compilerOptions.onError(line);
});

child.on('error', (errorMessage: string) => {
Expand All @@ -387,8 +384,8 @@ function spawnMakensis(cmd: string, args: Array<string>, compilerOptions: makens
output['outFile'] = outFile;
}

if (compilerOptions.events) {
eventEmitter.emit('exit', output);
if (typeof compilerOptions.onClose === 'function') {
compilerOptions.onClose(output);
}

if (code === 0 || (streamFormatted.stderr && !hasErrorCode(streamFormatted.stderr))) {
Expand Down
14 changes: 10 additions & 4 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { EventEmitter } from 'node:events';
import type { SpawnOptions } from 'node:child_process';

export declare namespace makensis {
Expand All @@ -9,21 +8,28 @@ export declare namespace makensis {
function nsisDir(compilerOptions: CompilerOptions): Promise<string | JSON>;
function version(compilerOptions: CompilerOptions, spawnOptions: SpawnOptions): Promise<CompilerOutput>;

const events: EventEmitter;

interface CommandHelpOptions {
[key: string]: string;
}

interface CompilerData {
line: string;
outFile: string;
hasWarning: boolean;
}

interface CompilerOptions {
define?: DefineOptions;
env?: boolean;
env?: Record<string, string>;
events?: boolean;
inputCharset?: string;
json?: boolean;
noCD?: boolean;
noConfig?: boolean;
outputCharset?: string;
onData?: (data: CompilerData) => void;
onError?: (line: string) => void;
onClose?: (data: CompilerOutput) => void;
pause?: boolean;
postExecute?: string | string[];
preExecute?: string | string[];
Expand Down

0 comments on commit a0f451f

Please sign in to comment.