在父级和子级Node.js进程中均未定义“ process.send”(使用了“ childProcess.spawn”变体)

时间:2019-09-05 08:23:11

标签: node.js typescript electron ipc child-process

我想提供我的主应用程序与子电子应用程序之间的数据交换(GUI可以从控制台启动)。

MainProcess(但不是电子“主过程”)

下面的源代码是TypeScript。

while(h):
        ch= raw_input("""####################################
1.Search
2.Add
3.Edit 5.Exit
####################################
""")
        print(ch)
        if "1" in ch:
                a=''
                ser1 = raw_input ("Enter Server Name Which You Want To Search:")
                print(ser1)
                a=('/opt/Projects/avo_user_error/Inv/'+ ser1 +'.txt')
                print(a)
                c=os.path.exists(a)
                print(c)
                if q == c:
                        aa=('/opt/Projects/avo_user_error/Inv/'+ ser1 +'.txt')
                        #print(aa)
                        file=open(aa,"r")
                        re=file.read()
                        print(re)
                else:
                        print("Server Not Found Kindly Check...")
        elif "2" in ch:
                ser = raw_input ("Enter Server Name :")
                print(ser)
                x=( ser +'.txt')
                print(x)
                v=os.path.exists(x)
                print(v)
                if q != v:
                        d3 = ('/opt/Projects/avo_user_error/Inv/'+ ser +'.txt')
                        fi = open(d3, "w+")
                        fi.write('Server Name :' +ser +'\n')
                        app = raw_input ("Enter Server Is Blowing To Which Application :")
                        print(app)
                        fi.write('Server Is Blowing To Which Application :' + app +'\n')
                        ser_os = raw_input ("Enter Server is in Unix/Windows :")
                        print(ser_os)
                        fi.write('Server is in Unix/Windows :' + ser_os +'\n')
                        ser_db = raw_input ("Enter The DB Server :")
                        print(ser_db)
                        fi.write('DB Server :' + ser_db +'\n')
                        db_type = raw_input ("Enter The Type Of DB :")
                        print(db_type)
                        fi.write('Type Of DB :' + db_type +'\n')
                        os = raw_input ("Enter The OS :")
                        print(os)
                        fi.write('OS :' + os +'\n')
                        sup = raw_input ("Enter The Support / Functional Team/SPOC :")
                        print(sup)
                        fi.write('The Support / Functional Team/SPOC :' + sup +'\n')
                        ven = raw_input ("Enter The Vendor Support(Yes/No) :")
                        print(ven)
                        fi.write('The Vendor Support(Yes/No) :' + ven +'\n')
                        ven_det = raw_input ("Enter The Vendor Details :")
                        print(ven_det)
                        fi.write('The Vendor Details :' + ven_det +'\n')
                        fi.close()
                        print("Server Added Successfully...")
                else:
                        print("Server Already Present Kindly Check...")
        elif "3" in ch:
                print("Edit")
        elif "4" in ch:
                ser2 = raw_input ("Enter Server Name Which You Want To Retire:")
                print(ser2)
                y=( ser2 +'.txt')
                print(y)
                g=os.path.exists(y)
                print(g)
                if q == g:
                        d3 = ('/opt/Projects/avo_user_error/Inv/'+ ser2 +'.txt')
                        fi = open(d3, "w+")
                        r_comm = raw_input ("Enter The Comment For Retirement :")
                        print(r_comm)
                        fi.write('Comment For Retirement :' + r_comm +'\n')
                        fi.close()
                        cmd1 = 'mv /opt/Projects/avo_user_error/Inv/'+ ser2 +'.txt /opt/Projects/avo_user_error/Inv/Retire/'
                        #print(cmd1)
                        os.system(cmd1)
                        print("Server Retirement Completed...")
                else:
                        print("Server Not Found Kindly Check...")
        elif"5" in ch:
                exit()
                print("Thany You...")
        else:
                print("Please Choose Correct Option...")

JavaScript错误消息是:

import Path from "path";
import ChildProcess, { ChildProcess as ChildProcess__type } from "child_process";
import Chalk from "chalk";


export default abstract class ProjectInitializer {

  private static readonly ELECTRON_EXECUTE_FILE_ABSOLUTE_PATH: string = Path.resolve(
      __dirname, "node_modules", ".bin", "electron.cmd"
  );
  private static readonly ELECTRON_MAIN_PROCESS_FILE_ABSOLUTE_PATH: string = Path.resolve(
      __dirname, "ProjectInitializer__ElectronMainProcess.js"
  );


  public static startInitializeProjectGUI(): void {

    console.log(Chalk.cyanBright("GUI is starting ..."));

    const electronApplicationChildProcess: ChildProcess__type = ChildProcess.spawn(
        ProjectInitializer.ELECTRON_EXECUTE_FILE_ABSOLUTE_PATH,
        [
          ProjectInitializer.ELECTRON_MAIN_PROCESS_FILE_ABSOLUTE_PATH,
          "--color", "always"
        ]
    );

    // No TypeScript error but error occurs in compiled JavaScript
    electronApplicationChildProcess.send({ foo: "alpha" });

    electronApplicationChildProcess.on("message", (message: unknown) => {
      console.log(message);
    });
  }
}

子过程(但在电子术语中是“电子主过程”)

TypeError: electronApplicationChildProcess.send is not a function
    at Function.ProjectInitializer.startInitializeProjectGUI (webpack:///./ProjectInitializer/ProjectInitializer.ts?:24:41)
    at Function.HikariAutomation.interpretConsoleCommandAndExecute

这是import URL from "url"; import Path from "path"; import { app as electronApplication, App as ElectronApplication, BrowserWindow, } from "electron"; import enableElectronDebug from "electron-debug"; class ProjectInitializerGUI { private electronApplication: ElectronApplication; private mainWindow: BrowserWindow | null = null; private static MAIN_WINDOW_MARKUP__FILE_NAME_WITH_EXTENSION: string = 'ProjectInitializer.html'; private constructor() { this.electronApplication = electronApplication; enableElectronDebug(); this.electronApplication.on('ready', this.onApplicationReadyEventHandler.bind(this)); } public static start(): void { new ProjectInitializerGUI(); } private onApplicationReadyEventHandler(): void { this.mainWindow = new BrowserWindow({ width: 1280, height: 800, useContentSize: true, center: true, resizable: false, fullscreenable: false, titleBarStyle: 'hidden', webPreferences: { nodeIntegration: true } }); this.mainWindow.loadURL(URL.format({ pathname: Path.join( __dirname, ProjectInitializerGUI.MAIN_WINDOW_MARKUP__FILE_NAME_WITH_EXTENSION ), protocol: 'file:', slashes: true })); this.mainWindow.setMenu(null); } } (function executeApplication(): void { ProjectInitializerGUI.start(); process.on("message", (message: unknown) => { console.log(message); }); // TypeScript forces to check send method on existence ... if (process.send) { // but because it's undefined, code in this scope in unreachable process.send("Just message"); process.send({ foo: "alpha", bar: "bravo" }) } })(); 的类型定义。如您所见,它是可选属性:

process.send

interface Process extends EventEmitter { // ... send?(message: any, sendHandle?: any, options?: { swallowErrors?: boolean}, callback?: (error: Error | null) => void): boolean; // ... 的定义不同。尽管我的案例是实验,但结果证明了child_process方法始终存在于子进程的假设,但在send的类型定义中,child_process属性是必需的:

send

0 个答案:

没有答案