From 1389c46b9a4b409cef671554f05ed4902d7dfbb1 Mon Sep 17 00:00:00 2001 From: Edgar Pogosyan Date: Sun, 12 May 2024 20:35:40 +0300 Subject: [PATCH] fix: remove require in get-tray-bin-path --- dist/index.cjs | 4 ++-- dist/index.cjs.map | 2 +- dist/index.esm.js | 4 ++-- dist/index.esm.js.map | 2 +- dist/index.js | 4 ++-- dist/index.js.map | 2 +- dist/index.mjs | 4 ++-- dist/index.mjs.map | 2 +- src/get-tray-bin-path.ts | 6 +++--- types/get-tray-bin-path.d.ts.map | 2 +- 10 files changed, 16 insertions(+), 16 deletions(-) diff --git a/dist/index.cjs b/dist/index.cjs index 6bdee4a..dbe07c4 100644 --- a/dist/index.cjs +++ b/dist/index.cjs @@ -43,8 +43,6 @@ var path__namespace = /*#__PURE__*/_interopNamespace(path); var os__namespace = /*#__PURE__*/_interopNamespace(os); var fs__namespace = /*#__PURE__*/_interopNamespace(fs); -// eslint-disable-next-line @typescript-eslint/no-var-requires -const pkg = require('../package.json'); function getTrayBinPath(debug = false, copyDir = false) { const binName = { win32: `tray_windows${debug ? '' : '_release'}.exe`, @@ -56,6 +54,8 @@ function getTrayBinPath(debug = false, copyDir = false) { } const binPath = path__namespace.resolve(`${__dirname}/../traybin/${binName}`); if (copyDir) { + // eslint-disable-next-line @typescript-eslint/no-var-requires + const pkg = require('../package.json'); copyDir = path__namespace.join(typeof copyDir === 'string' ? copyDir : `${os__namespace.homedir()}/.cache/node-systray/`, pkg.version); diff --git a/dist/index.cjs.map b/dist/index.cjs.map index 9110276..3ee59e8 100644 --- a/dist/index.cjs.map +++ b/dist/index.cjs.map @@ -1 +1 @@ -{"version":3,"file":"index.cjs","sources":["../src/get-tray-bin-path.ts","../src/index.ts"],"sourcesContent":["import * as path from 'path';\nimport * as os from 'os';\nimport * as fs from 'fs-extra';\n\n// eslint-disable-next-line @typescript-eslint/no-var-requires\nconst pkg = require('../package.json');\n\nexport function getTrayBinPath(\n debug = false,\n copyDir: boolean | string = false,\n): string {\n const binName = (\n {\n win32: `tray_windows${debug ? '' : '_release'}.exe`,\n darwin: `tray_darwin${debug ? '' : '_release'}`,\n linux: `tray_linux${debug ? '' : '_release'}`,\n } as Record\n )[process.platform];\n\n if (!binName) {\n throw new Error(\n `node-systray-v2: unsupported platform ${process.platform}.`,\n );\n }\n\n const binPath = path.resolve(`${__dirname}/../traybin/${binName}`);\n\n if (copyDir) {\n copyDir = path.join(\n typeof copyDir === 'string'\n ? copyDir\n : `${os.homedir()}/.cache/node-systray/`,\n pkg.version,\n );\n\n const copyDistPath = path.join(copyDir, binName);\n if (!fs.existsSync(copyDistPath)) {\n fs.ensureDirSync(copyDir);\n fs.copySync(binPath, copyDistPath);\n }\n\n return copyDistPath;\n }\n\n return binPath;\n}\n","import * as child from 'child_process';\nimport { EventEmitter } from 'events';\nimport * as readline from 'readline';\nimport Debug from 'debug';\nimport { getTrayBinPath } from './get-tray-bin-path';\n\nconst debug = Debug('systray');\n\nexport type MenuItem = {\n title: string;\n tooltip: string;\n checked: boolean;\n enabled: boolean;\n};\n\nexport type Menu = {\n icon: string;\n title: string;\n tooltip: string;\n items: MenuItem[];\n};\n\nexport type ClickEvent = {\n type: 'clicked';\n item: MenuItem;\n seq_id: number;\n};\n\nexport type ReadyEvent = {\n type: 'ready';\n};\n\nexport type Event = ClickEvent | ReadyEvent;\n\nexport type UpdateItemAction = {\n type: 'update-item';\n item: MenuItem;\n seq_id: number;\n};\n\nexport type UpdateMenuAction = {\n type: 'update-menu';\n menu: Menu;\n seq_id: number;\n};\n\nexport type UpdateMenuAndItemAction = {\n type: 'update-menu-and-item';\n menu: Menu;\n item: MenuItem;\n seq_id: number;\n};\n\nexport type Action =\n | UpdateItemAction\n | UpdateMenuAction\n | UpdateMenuAndItemAction;\n\nexport type Conf = {\n menu: Menu;\n debug?: boolean;\n copyDir?: boolean | string;\n};\n\nconst CHECK_STR = ' (√)';\n\nfunction updateCheckedInLinux(item: MenuItem): MenuItem {\n if (process.platform !== 'linux') {\n return item;\n }\n if (item.checked) {\n item.title += CHECK_STR;\n } else {\n item.title = (item.title || '').replace(RegExp(CHECK_STR + '$'), '');\n }\n return item;\n}\n\nexport class SysTray extends EventEmitter {\n protected _conf: Conf;\n protected _process: child.ChildProcess;\n protected _rl: readline.ReadLine;\n protected _binPath: string;\n\n constructor(conf: Conf) {\n super();\n\n this._conf = conf;\n this._binPath = getTrayBinPath(conf.debug, conf.copyDir);\n this._process = child.spawn(this._binPath, [], {\n windowsHide: true,\n });\n\n this._rl = readline.createInterface({\n input: this._process.stdout!,\n });\n\n conf.menu.items = conf.menu.items.map(updateCheckedInLinux);\n this._rl.on('line', (data) => debug('onLine', data));\n this.onReady(() => this.writeLine(JSON.stringify(conf.menu)));\n }\n\n onReady(listener: () => void): this {\n this._rl.on('line', (line: string) => {\n const action: Event = JSON.parse(line);\n if (action.type === 'ready') {\n listener();\n debug('onReady', action);\n }\n });\n return this;\n }\n\n onClick(listener: (action: ClickEvent) => void): this {\n this._rl.on('line', (line: string) => {\n const action: ClickEvent = JSON.parse(line);\n if (action.type === 'clicked') {\n debug('onClick', action);\n listener(action);\n }\n });\n return this;\n }\n\n writeLine(line: string): this {\n if (line) {\n debug('writeLine', line + '\\n', '=====');\n this._process.stdin!.write(line.trim() + '\\n');\n }\n return this;\n }\n\n sendAction(action: Action): this {\n switch (action.type) {\n case 'update-item':\n action.item = updateCheckedInLinux(action.item);\n break;\n case 'update-menu':\n action.menu.items = action.menu.items.map(updateCheckedInLinux);\n break;\n case 'update-menu-and-item':\n action.menu.items = action.menu.items.map(updateCheckedInLinux);\n action.item = updateCheckedInLinux(action.item);\n break;\n }\n debug('sendAction', action);\n this.writeLine(JSON.stringify(action));\n return this;\n }\n /**\n * Kill the systray process.\n *\n * ## Change notes:\n * ### v2.0.0\n * Removed parameter `exitNode` that automatically killed nodejs process when systray exitted.\n */\n kill(): void {\n this._rl.close();\n this._process.kill();\n }\n\n onExit(listener: (code: number | null, signal: string | null) => void): void {\n this._process.on('exit', listener);\n }\n\n onError(listener: (err: Error) => void): void {\n this._process.on('error', (err) => {\n debug('onError', err, 'binPath', this.binPath);\n listener(err);\n });\n }\n\n get killed(): boolean {\n return this._process.killed;\n }\n\n get binPath(): string {\n return this._binPath;\n }\n}\n"],"names":["path","os","fs","Debug","EventEmitter","child","readline"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA;AACA,MAAM,GAAG,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAAC;AAEjC,SAAU,cAAc,CAC5B,KAAK,GAAG,KAAK,EACb,UAA4B,KAAK,EAAA;AAEjC,IAAA,MAAM,OAAO,GACX;QACE,KAAK,EAAE,CAAe,YAAA,EAAA,KAAK,GAAG,EAAE,GAAG,UAAU,CAAM,IAAA,CAAA;QACnD,MAAM,EAAE,CAAc,WAAA,EAAA,KAAK,GAAG,EAAE,GAAG,UAAU,CAAE,CAAA;QAC/C,KAAK,EAAE,CAAa,UAAA,EAAA,KAAK,GAAG,EAAE,GAAG,UAAU,CAAE,CAAA;AAEhD,KAAA,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IAEpB,IAAI,CAAC,OAAO,EAAE;QACZ,MAAM,IAAI,KAAK,CACb,CAAA,sCAAA,EAAyC,OAAO,CAAC,QAAQ,CAAG,CAAA,CAAA,CAC7D,CAAC;AACH,KAAA;AAED,IAAA,MAAM,OAAO,GAAGA,eAAI,CAAC,OAAO,CAAC,CAAG,EAAA,SAAS,CAAe,YAAA,EAAA,OAAO,CAAE,CAAA,CAAC,CAAC;AAEnE,IAAA,IAAI,OAAO,EAAE;QACX,OAAO,GAAGA,eAAI,CAAC,IAAI,CACjB,OAAO,OAAO,KAAK,QAAQ;AACzB,cAAE,OAAO;AACT,cAAE,CAAA,EAAGC,aAAE,CAAC,OAAO,EAAE,CAAuB,qBAAA,CAAA,EAC1C,GAAG,CAAC,OAAO,CACZ,CAAC;QAEF,MAAM,YAAY,GAAGD,eAAI,CAAC,IAAI,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B,QAAA,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC;KACtB;AAED,IAAA,MAAM,CAAC,QAA8D,EAAA;QACnE,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;KACpC;AAED,IAAA,OAAO,CAAC,QAA8B,EAAA;QACpC,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,GAAG,KAAI;YAChC,KAAK,CAAC,SAAS,EAAE,GAAG,EAAE,SAAS,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;YAC/C,QAAQ,CAAC,GAAG,CAAC,CAAC;AAChB,SAAC,CAAC,CAAC;KACJ;AAED,IAAA,IAAI,MAAM,GAAA;AACR,QAAA,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC;KAC7B;AAED,IAAA,IAAI,OAAO,GAAA;QACT,OAAO,IAAI,CAAC,QAAQ,CAAC;KACtB;AACF;;;;"} \ No newline at end of file +{"version":3,"file":"index.cjs","sources":["../src/get-tray-bin-path.ts","../src/index.ts"],"sourcesContent":["import * as path from 'path';\nimport * as os from 'os';\nimport * as fs from 'fs-extra';\n\nexport function getTrayBinPath(\n debug = false,\n copyDir: boolean | string = false,\n): string {\n const binName = (\n {\n win32: `tray_windows${debug ? '' : '_release'}.exe`,\n darwin: `tray_darwin${debug ? '' : '_release'}`,\n linux: `tray_linux${debug ? '' : '_release'}`,\n } as Record\n )[process.platform];\n\n if (!binName) {\n throw new Error(\n `node-systray-v2: unsupported platform ${process.platform}.`,\n );\n }\n\n const binPath = path.resolve(`${__dirname}/../traybin/${binName}`);\n\n if (copyDir) {\n // eslint-disable-next-line @typescript-eslint/no-var-requires\n const pkg = require('../package.json');\n\n copyDir = path.join(\n typeof copyDir === 'string'\n ? copyDir\n : `${os.homedir()}/.cache/node-systray/`,\n pkg.version,\n );\n\n const copyDistPath = path.join(copyDir, binName);\n if (!fs.existsSync(copyDistPath)) {\n fs.ensureDirSync(copyDir);\n fs.copySync(binPath, copyDistPath);\n }\n\n return copyDistPath;\n }\n\n return binPath;\n}\n","import * as child from 'child_process';\nimport { EventEmitter } from 'events';\nimport * as readline from 'readline';\nimport Debug from 'debug';\nimport { getTrayBinPath } from './get-tray-bin-path';\n\nconst debug = Debug('systray');\n\nexport type MenuItem = {\n title: string;\n tooltip: string;\n checked: boolean;\n enabled: boolean;\n};\n\nexport type Menu = {\n icon: string;\n title: string;\n tooltip: string;\n items: MenuItem[];\n};\n\nexport type ClickEvent = {\n type: 'clicked';\n item: MenuItem;\n seq_id: number;\n};\n\nexport type ReadyEvent = {\n type: 'ready';\n};\n\nexport type Event = ClickEvent | ReadyEvent;\n\nexport type UpdateItemAction = {\n type: 'update-item';\n item: MenuItem;\n seq_id: number;\n};\n\nexport type UpdateMenuAction = {\n type: 'update-menu';\n menu: Menu;\n seq_id: number;\n};\n\nexport type UpdateMenuAndItemAction = {\n type: 'update-menu-and-item';\n menu: Menu;\n item: MenuItem;\n seq_id: number;\n};\n\nexport type Action =\n | UpdateItemAction\n | UpdateMenuAction\n | UpdateMenuAndItemAction;\n\nexport type Conf = {\n menu: Menu;\n debug?: boolean;\n copyDir?: boolean | string;\n};\n\nconst CHECK_STR = ' (√)';\n\nfunction updateCheckedInLinux(item: MenuItem): MenuItem {\n if (process.platform !== 'linux') {\n return item;\n }\n if (item.checked) {\n item.title += CHECK_STR;\n } else {\n item.title = (item.title || '').replace(RegExp(CHECK_STR + '$'), '');\n }\n return item;\n}\n\nexport class SysTray extends EventEmitter {\n protected _conf: Conf;\n protected _process: child.ChildProcess;\n protected _rl: readline.ReadLine;\n protected _binPath: string;\n\n constructor(conf: Conf) {\n super();\n\n this._conf = conf;\n this._binPath = getTrayBinPath(conf.debug, conf.copyDir);\n this._process = child.spawn(this._binPath, [], {\n windowsHide: true,\n });\n\n this._rl = readline.createInterface({\n input: this._process.stdout!,\n });\n\n conf.menu.items = conf.menu.items.map(updateCheckedInLinux);\n this._rl.on('line', (data) => debug('onLine', data));\n this.onReady(() => this.writeLine(JSON.stringify(conf.menu)));\n }\n\n onReady(listener: () => void): this {\n this._rl.on('line', (line: string) => {\n const action: Event = JSON.parse(line);\n if (action.type === 'ready') {\n listener();\n debug('onReady', action);\n }\n });\n return this;\n }\n\n onClick(listener: (action: ClickEvent) => void): this {\n this._rl.on('line', (line: string) => {\n const action: ClickEvent = JSON.parse(line);\n if (action.type === 'clicked') {\n debug('onClick', action);\n listener(action);\n }\n });\n return this;\n }\n\n writeLine(line: string): this {\n if (line) {\n debug('writeLine', line + '\\n', '=====');\n this._process.stdin!.write(line.trim() + '\\n');\n }\n return this;\n }\n\n sendAction(action: Action): this {\n switch (action.type) {\n case 'update-item':\n action.item = updateCheckedInLinux(action.item);\n break;\n case 'update-menu':\n action.menu.items = action.menu.items.map(updateCheckedInLinux);\n break;\n case 'update-menu-and-item':\n action.menu.items = action.menu.items.map(updateCheckedInLinux);\n action.item = updateCheckedInLinux(action.item);\n break;\n }\n debug('sendAction', action);\n this.writeLine(JSON.stringify(action));\n return this;\n }\n /**\n * Kill the systray process.\n *\n * ## Change notes:\n * ### v2.0.0\n * Removed parameter `exitNode` that automatically killed nodejs process when systray exitted.\n */\n kill(): void {\n this._rl.close();\n this._process.kill();\n }\n\n onExit(listener: (code: number | null, signal: string | null) => void): void {\n this._process.on('exit', listener);\n }\n\n onError(listener: (err: Error) => void): void {\n this._process.on('error', (err) => {\n debug('onError', err, 'binPath', this.binPath);\n listener(err);\n });\n }\n\n get killed(): boolean {\n return this._process.killed;\n }\n\n get binPath(): string {\n return this._binPath;\n }\n}\n"],"names":["path","os","fs","Debug","EventEmitter","child","readline"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIM,SAAU,cAAc,CAC5B,KAAK,GAAG,KAAK,EACb,UAA4B,KAAK,EAAA;AAEjC,IAAA,MAAM,OAAO,GACX;QACE,KAAK,EAAE,CAAe,YAAA,EAAA,KAAK,GAAG,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,CAAC,CAAC;AAChB,SAAC,CAAC,CAAC;KACJ;AAED,IAAA,IAAI,MAAM,GAAA;AACR,QAAA,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC;KAC7B;AAED,IAAA,IAAI,OAAO,GAAA;QACT,OAAO,IAAI,CAAC,QAAQ,CAAC;KACtB;AACF;;;;"} \ No newline at end of file diff --git a/dist/index.esm.js b/dist/index.esm.js index 45f7a51..bf4c0e7 100644 --- a/dist/index.esm.js +++ b/dist/index.esm.js @@ -12,8 +12,6 @@ import * as path from 'path'; import * as os from 'os'; import * as fs from 'fs-extra'; -// eslint-disable-next-line @typescript-eslint/no-var-requires -const pkg = require('../package.json'); function getTrayBinPath(debug = false, copyDir = false) { const binName = { win32: `tray_windows${debug ? '' : '_release'}.exe`, @@ -25,6 +23,8 @@ function getTrayBinPath(debug = false, copyDir = false) { } const binPath = path.resolve(`${__dirname}/../traybin/${binName}`); if (copyDir) { + // eslint-disable-next-line @typescript-eslint/no-var-requires + const pkg = require('../package.json'); copyDir = path.join(typeof copyDir === 'string' ? copyDir : `${os.homedir()}/.cache/node-systray/`, pkg.version); diff --git a/dist/index.esm.js.map b/dist/index.esm.js.map index 0bd2826..5d129d4 100644 --- a/dist/index.esm.js.map +++ b/dist/index.esm.js.map @@ -1 +1 @@ -{"version":3,"file":"index.esm.js","sources":["../src/get-tray-bin-path.ts","../src/index.ts"],"sourcesContent":["import * as path from 'path';\nimport * as os from 'os';\nimport * as fs from 'fs-extra';\n\n// eslint-disable-next-line @typescript-eslint/no-var-requires\nconst pkg = require('../package.json');\n\nexport function getTrayBinPath(\n debug = false,\n copyDir: boolean | string = false,\n): string {\n const binName = (\n {\n win32: `tray_windows${debug ? '' : '_release'}.exe`,\n darwin: `tray_darwin${debug ? '' : '_release'}`,\n linux: `tray_linux${debug ? '' : '_release'}`,\n } as Record\n )[process.platform];\n\n if (!binName) {\n throw new Error(\n `node-systray-v2: unsupported platform ${process.platform}.`,\n );\n }\n\n const binPath = path.resolve(`${__dirname}/../traybin/${binName}`);\n\n if (copyDir) {\n copyDir = path.join(\n typeof copyDir === 'string'\n ? copyDir\n : `${os.homedir()}/.cache/node-systray/`,\n pkg.version,\n );\n\n const copyDistPath = path.join(copyDir, binName);\n if (!fs.existsSync(copyDistPath)) {\n fs.ensureDirSync(copyDir);\n fs.copySync(binPath, copyDistPath);\n }\n\n return copyDistPath;\n }\n\n return binPath;\n}\n","import * as child from 'child_process';\nimport { EventEmitter } from 'events';\nimport * as readline from 'readline';\nimport Debug from 'debug';\nimport { getTrayBinPath } from './get-tray-bin-path';\n\nconst debug = Debug('systray');\n\nexport type MenuItem = {\n title: string;\n tooltip: string;\n checked: boolean;\n enabled: boolean;\n};\n\nexport type Menu = {\n icon: string;\n title: string;\n tooltip: string;\n items: MenuItem[];\n};\n\nexport type ClickEvent = {\n type: 'clicked';\n item: MenuItem;\n seq_id: number;\n};\n\nexport type ReadyEvent = {\n type: 'ready';\n};\n\nexport type Event = ClickEvent | ReadyEvent;\n\nexport type UpdateItemAction = {\n type: 'update-item';\n item: MenuItem;\n seq_id: number;\n};\n\nexport type UpdateMenuAction = {\n type: 'update-menu';\n menu: Menu;\n seq_id: number;\n};\n\nexport type UpdateMenuAndItemAction = {\n type: 'update-menu-and-item';\n menu: Menu;\n item: MenuItem;\n seq_id: number;\n};\n\nexport type Action =\n | UpdateItemAction\n | UpdateMenuAction\n | UpdateMenuAndItemAction;\n\nexport type Conf = {\n menu: Menu;\n debug?: boolean;\n copyDir?: boolean | string;\n};\n\nconst CHECK_STR = ' (√)';\n\nfunction updateCheckedInLinux(item: MenuItem): MenuItem {\n if (process.platform !== 'linux') {\n return item;\n }\n if (item.checked) {\n item.title += CHECK_STR;\n } else {\n item.title = (item.title || '').replace(RegExp(CHECK_STR + '$'), '');\n }\n return item;\n}\n\nexport class SysTray extends EventEmitter {\n protected _conf: Conf;\n protected _process: child.ChildProcess;\n protected _rl: readline.ReadLine;\n protected _binPath: string;\n\n constructor(conf: Conf) {\n super();\n\n this._conf = conf;\n this._binPath = getTrayBinPath(conf.debug, conf.copyDir);\n this._process = child.spawn(this._binPath, [], {\n windowsHide: true,\n });\n\n this._rl = readline.createInterface({\n input: this._process.stdout!,\n });\n\n conf.menu.items = conf.menu.items.map(updateCheckedInLinux);\n this._rl.on('line', (data) => debug('onLine', data));\n this.onReady(() => this.writeLine(JSON.stringify(conf.menu)));\n }\n\n onReady(listener: () => void): this {\n this._rl.on('line', (line: string) => {\n const action: Event = JSON.parse(line);\n if (action.type === 'ready') {\n listener();\n debug('onReady', action);\n }\n });\n return this;\n }\n\n onClick(listener: (action: ClickEvent) => void): this {\n this._rl.on('line', (line: string) => {\n const action: ClickEvent = JSON.parse(line);\n if (action.type === 'clicked') {\n debug('onClick', action);\n listener(action);\n }\n });\n return this;\n }\n\n writeLine(line: string): this {\n if (line) {\n debug('writeLine', line + '\\n', '=====');\n this._process.stdin!.write(line.trim() + '\\n');\n }\n return this;\n }\n\n sendAction(action: Action): this {\n switch (action.type) {\n case 'update-item':\n action.item = updateCheckedInLinux(action.item);\n break;\n case 'update-menu':\n action.menu.items = action.menu.items.map(updateCheckedInLinux);\n break;\n case 'update-menu-and-item':\n action.menu.items = action.menu.items.map(updateCheckedInLinux);\n action.item = updateCheckedInLinux(action.item);\n break;\n }\n debug('sendAction', action);\n this.writeLine(JSON.stringify(action));\n return this;\n }\n /**\n * Kill the systray process.\n *\n * ## Change notes:\n * ### v2.0.0\n * Removed parameter `exitNode` that automatically killed nodejs process when systray exitted.\n */\n kill(): void {\n this._rl.close();\n this._process.kill();\n }\n\n onExit(listener: (code: number | null, signal: string | null) => void): void {\n this._process.on('exit', listener);\n }\n\n onError(listener: (err: Error) => void): void {\n this._process.on('error', (err) => {\n debug('onError', err, 'binPath', this.binPath);\n listener(err);\n });\n }\n\n get killed(): boolean {\n return this._process.killed;\n }\n\n get binPath(): string {\n return this._binPath;\n }\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;AAIA;AACA,MAAM,GA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C,KAAK,CAAC,SAAS,EAAE,GAAG,EAAE,SAAS,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;YAC/C,QAAQ,CAAC,GAAG,CAAC,CAAC;AAChB,SAAC,CAAC,CAAC;KACJ;AAED,IAAA,IAAI,MAAM,GAAA;AACR,QAAA,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC;KAC7B;AAED,IAAA,IAAI,OAAO,GAAA;QACT,OAAO,IAAI,CAAC,QAAQ,CAAC;KACtB;AACF;;;;"} \ No newline at end of file +{"version":3,"file":"index.esm.js","sources":["../src/get-tray-bin-path.ts","../src/index.ts"],"sourcesContent":["import * as path from 'path';\nimport * as os from 'os';\nimport * as fs from 'fs-extra';\n\nexport function getTrayBinPath(\n debug = false,\n copyDir: boolean | string = false,\n): string {\n const binName = (\n {\n win32: `tray_windows${debug ? '' : '_release'}.exe`,\n darwin: `tray_darwin${debug ? '' : '_release'}`,\n linux: `tray_linux${debug ? '' : '_release'}`,\n } as Record\n )[process.platform];\n\n if (!binName) {\n throw new Error(\n `node-systray-v2: unsupported platform ${process.platform}.`,\n );\n }\n\n const binPath = path.resolve(`${__dirname}/../traybin/${binName}`);\n\n if (copyDir) {\n // eslint-disable-next-line @typescript-eslint/no-var-requires\n const pkg = require('../package.json');\n\n copyDir = path.join(\n typeof copyDir === 'string'\n ? copyDir\n : `${os.homedir()}/.cache/node-systray/`,\n pkg.version,\n );\n\n const copyDistPath = path.join(copyDir, binName);\n if (!fs.existsSync(copyDistPath)) {\n fs.ensureDirSync(copyDir);\n fs.copySync(binPath, copyDistPath);\n }\n\n return copyDistPath;\n }\n\n return binPath;\n}\n","import * as child from 'child_process';\nimport { EventEmitter } from 'events';\nimport * as readline from 'readline';\nimport Debug from 'debug';\nimport { getTrayBinPath } from './get-tray-bin-path';\n\nconst debug = Debug('systray');\n\nexport type MenuItem = {\n title: string;\n tooltip: string;\n checked: boolean;\n enabled: boolean;\n};\n\nexport type Menu = {\n icon: string;\n title: string;\n tooltip: string;\n items: MenuItem[];\n};\n\nexport type ClickEvent = {\n type: 'clicked';\n item: MenuItem;\n seq_id: number;\n};\n\nexport type ReadyEvent = {\n type: 'ready';\n};\n\nexport type Event = ClickEvent | ReadyEvent;\n\nexport type UpdateItemAction = {\n type: 'update-item';\n item: MenuItem;\n seq_id: number;\n};\n\nexport type UpdateMenuAction = {\n type: 'update-menu';\n menu: Menu;\n seq_id: number;\n};\n\nexport type UpdateMenuAndItemAction = {\n type: 'update-menu-and-item';\n menu: Menu;\n item: MenuItem;\n seq_id: number;\n};\n\nexport type Action =\n | UpdateItemAction\n | UpdateMenuAction\n | UpdateMenuAndItemAction;\n\nexport type Conf = {\n menu: Menu;\n debug?: boolean;\n copyDir?: boolean | string;\n};\n\nconst CHECK_STR = ' (√)';\n\nfunction updateCheckedInLinux(item: MenuItem): MenuItem {\n if (process.platform !== 'linux') {\n return item;\n }\n if (item.checked) {\n item.title += CHECK_STR;\n } else {\n item.title = (item.title || '').replace(RegExp(CHECK_STR + '$'), '');\n }\n return item;\n}\n\nexport class SysTray extends EventEmitter {\n protected _conf: Conf;\n protected _process: child.ChildProcess;\n protected _rl: readline.ReadLine;\n protected _binPath: string;\n\n constructor(conf: Conf) {\n super();\n\n this._conf = conf;\n this._binPath = getTrayBinPath(conf.debug, conf.copyDir);\n this._process = child.spawn(this._binPath, [], {\n windowsHide: true,\n });\n\n this._rl = readline.createInterface({\n input: this._process.stdout!,\n });\n\n conf.menu.items = conf.menu.items.map(updateCheckedInLinux);\n this._rl.on('line', (data) => debug('onLine', data));\n this.onReady(() => this.writeLine(JSON.stringify(conf.menu)));\n }\n\n onReady(listener: () => void): this {\n this._rl.on('line', (line: string) => {\n const action: Event = JSON.parse(line);\n if (action.type === 'ready') {\n listener();\n debug('onReady', action);\n }\n });\n return this;\n }\n\n onClick(listener: (action: ClickEvent) => void): this {\n this._rl.on('line', (line: string) => {\n const action: ClickEvent = JSON.parse(line);\n if (action.type === 'clicked') {\n debug('onClick', action);\n listener(action);\n }\n });\n return this;\n }\n\n writeLine(line: string): this {\n if (line) {\n debug('writeLine', line + '\\n', '=====');\n this._process.stdin!.write(line.trim() + '\\n');\n }\n return this;\n }\n\n sendAction(action: Action): this {\n switch (action.type) {\n case 'update-item':\n action.item = updateCheckedInLinux(action.item);\n break;\n case 'update-menu':\n action.menu.items = action.menu.items.map(updateCheckedInLinux);\n break;\n case 'update-menu-and-item':\n action.menu.items = action.menu.items.map(updateCheckedInLinux);\n action.item = updateCheckedInLinux(action.item);\n break;\n }\n debug('sendAction', action);\n this.writeLine(JSON.stringify(action));\n return this;\n }\n /**\n * Kill the systray process.\n *\n * ## Change notes:\n * ### v2.0.0\n * Removed parameter `exitNode` that automatically killed nodejs process when systray exitted.\n */\n kill(): void {\n this._rl.close();\n this._process.kill();\n }\n\n onExit(listener: (code: number | null, signal: string | null) => void): void {\n this._process.on('exit', listener);\n }\n\n onError(listener: (err: Error) => void): void {\n this._process.on('error', (err) => {\n debug('onError', err, 'binPath', this.binPath);\n listener(err);\n });\n }\n\n get killed(): boolean {\n return this._process.killed;\n }\n\n get binPath(): string {\n return this._binPath;\n }\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;AAIM,SAAU,cAAc,CAC5B,KAAK,GAAG,KAAK,EACb,UAA4B,KAAK,EAAA;AAEjC,IAAA,MAAM,OAAO,GACX;QACE,KAAK,EAAE,CAAe,YAAA,EAAA,KAAK,GAAG,EAAE,GAAG,UAAU,CAAM,IAAA,CAAA;QACnD,MAAM,EAAE,CAAc,WAAA,EAAA,KAAK,GAAG,EAAE,GAAG,UAAU,CAAE,CAAA;QAC/C,KAAK,EAAE,CAAa,UAAA,EAAA,KAAK,GAAG,EAAE,GAAG,UAAU,CAAE,CAAA;AAEhD,KAAA,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IAEpB,IAAI,CAAC,OAAO,EAAE;QACZ,MAAM,IAAI,KAAK,CACb,CAAA,sCAAA,EAAyC,OAAO,CAAC,QAAQ,CAAG,C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B,gBAAA,MAAM,CAAC,IAAI,CAAC,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,oBAAoB,CAAC,CAAC;gBAChE,MAAM,CAAC,IAAI,GAAG,oBAAoB,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;gBAChD,MAAM;AACT,SAAA;AACD,QAAA,KAAK,CAAC,YAAY,EAAE,MAAM,CAAC,CAAC;QAC5B,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC;AACvC,QAAA,OAAO,IAAI,CAAC;KACb;AACD;;;;;;AAMG;IACH,IAAI,GAAA;AACF,QAAA,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC;AACjB,QAAA,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC;KACtB;AAED,IAAA,MAAM,CAAC,QAA8D,EAAA;QACnE,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;KACpC;AAED,IAAA,OAAO,CAAC,QAA8B,EAAA;QACpC,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,GAAG,KAAI;YAChC,KAAK,CAAC,SAAS,EAAE,GAAG,EAAE,SAAS,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;YAC/C,QAAQ,CAAC,GAAG,CAAC,CAAC;AAChB,SAAC,CAAC,CAAC;KACJ;AAED,IAAA,IAAI,MAAM,GAAA;AACR,QAAA,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC;KAC7B;AAED,IAAA,IAAI,OAAO,GAAA;QACT,OAAO,IAAI,CAAC,QAAQ,CAAC;KACtB;AACF;;;;"} \ No newline at end of file diff --git a/dist/index.js b/dist/index.js index 5121fdb..434aad8 100644 --- a/dist/index.js +++ b/dist/index.js @@ -43,8 +43,6 @@ var path__namespace = /*#__PURE__*/_interopNamespace(path); var os__namespace = /*#__PURE__*/_interopNamespace(os); var fs__namespace = /*#__PURE__*/_interopNamespace(fs); -// eslint-disable-next-line @typescript-eslint/no-var-requires -const pkg = require('../package.json'); function getTrayBinPath(debug = false, copyDir = false) { const binName = { win32: `tray_windows${debug ? '' : '_release'}.exe`, @@ -56,6 +54,8 @@ function getTrayBinPath(debug = false, copyDir = false) { } const binPath = path__namespace.resolve(`${__dirname}/../traybin/${binName}`); if (copyDir) { + // eslint-disable-next-line @typescript-eslint/no-var-requires + const pkg = require('../package.json'); copyDir = path__namespace.join(typeof copyDir === 'string' ? copyDir : `${os__namespace.homedir()}/.cache/node-systray/`, pkg.version); diff --git a/dist/index.js.map b/dist/index.js.map index 23b119e..082a95c 100644 --- a/dist/index.js.map +++ b/dist/index.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","sources":["../src/get-tray-bin-path.ts","../src/index.ts"],"sourcesContent":["import * as path from 'path';\nimport * as os from 'os';\nimport * as fs from 'fs-extra';\n\n// eslint-disable-next-line @typescript-eslint/no-var-requires\nconst pkg = require('../package.json');\n\nexport function getTrayBinPath(\n debug = false,\n copyDir: boolean | string = false,\n): string {\n const binName = (\n {\n win32: `tray_windows${debug ? '' : '_release'}.exe`,\n darwin: `tray_darwin${debug ? '' : '_release'}`,\n linux: `tray_linux${debug ? '' : '_release'}`,\n } as Record\n )[process.platform];\n\n if (!binName) {\n throw new Error(\n `node-systray-v2: unsupported platform ${process.platform}.`,\n );\n }\n\n const binPath = path.resolve(`${__dirname}/../traybin/${binName}`);\n\n if (copyDir) {\n copyDir = path.join(\n typeof copyDir === 'string'\n ? copyDir\n : `${os.homedir()}/.cache/node-systray/`,\n pkg.version,\n );\n\n const copyDistPath = path.join(copyDir, binName);\n if (!fs.existsSync(copyDistPath)) {\n fs.ensureDirSync(copyDir);\n fs.copySync(binPath, copyDistPath);\n }\n\n return copyDistPath;\n }\n\n return binPath;\n}\n","import * as child from 'child_process';\nimport { EventEmitter } from 'events';\nimport * as readline from 'readline';\nimport Debug from 'debug';\nimport { getTrayBinPath } from './get-tray-bin-path';\n\nconst debug = Debug('systray');\n\nexport type MenuItem = {\n title: string;\n tooltip: string;\n checked: boolean;\n enabled: boolean;\n};\n\nexport type Menu = {\n icon: string;\n title: string;\n tooltip: string;\n items: MenuItem[];\n};\n\nexport type ClickEvent = {\n type: 'clicked';\n item: MenuItem;\n seq_id: number;\n};\n\nexport type ReadyEvent = {\n type: 'ready';\n};\n\nexport type Event = ClickEvent | ReadyEvent;\n\nexport type UpdateItemAction = {\n type: 'update-item';\n item: MenuItem;\n seq_id: number;\n};\n\nexport type UpdateMenuAction = {\n type: 'update-menu';\n menu: Menu;\n seq_id: number;\n};\n\nexport type UpdateMenuAndItemAction = {\n type: 'update-menu-and-item';\n menu: Menu;\n item: MenuItem;\n seq_id: number;\n};\n\nexport type Action =\n | UpdateItemAction\n | UpdateMenuAction\n | UpdateMenuAndItemAction;\n\nexport type Conf = {\n menu: Menu;\n debug?: boolean;\n copyDir?: boolean | string;\n};\n\nconst CHECK_STR = ' (√)';\n\nfunction updateCheckedInLinux(item: MenuItem): MenuItem {\n if (process.platform !== 'linux') {\n return item;\n }\n if (item.checked) {\n item.title += CHECK_STR;\n } else {\n item.title = (item.title || '').replace(RegExp(CHECK_STR + '$'), '');\n }\n return item;\n}\n\nexport class SysTray extends EventEmitter {\n protected _conf: Conf;\n protected _process: child.ChildProcess;\n protected _rl: readline.ReadLine;\n protected _binPath: string;\n\n constructor(conf: Conf) {\n super();\n\n this._conf = conf;\n this._binPath = getTrayBinPath(conf.debug, conf.copyDir);\n this._process = child.spawn(this._binPath, [], {\n windowsHide: true,\n });\n\n this._rl = readline.createInterface({\n input: this._process.stdout!,\n });\n\n conf.menu.items = conf.menu.items.map(updateCheckedInLinux);\n this._rl.on('line', (data) => debug('onLine', data));\n this.onReady(() => this.writeLine(JSON.stringify(conf.menu)));\n }\n\n onReady(listener: () => void): this {\n this._rl.on('line', (line: string) => {\n const action: Event = JSON.parse(line);\n if (action.type === 'ready') {\n listener();\n debug('onReady', action);\n }\n });\n return this;\n }\n\n onClick(listener: (action: ClickEvent) => void): this {\n this._rl.on('line', (line: string) => {\n const action: ClickEvent = JSON.parse(line);\n if (action.type === 'clicked') {\n debug('onClick', action);\n listener(action);\n }\n });\n return this;\n }\n\n writeLine(line: string): this {\n if (line) {\n debug('writeLine', line + '\\n', '=====');\n this._process.stdin!.write(line.trim() + '\\n');\n }\n return this;\n }\n\n sendAction(action: Action): this {\n switch (action.type) {\n case 'update-item':\n action.item = updateCheckedInLinux(action.item);\n break;\n case 'update-menu':\n action.menu.items = action.menu.items.map(updateCheckedInLinux);\n break;\n case 'update-menu-and-item':\n action.menu.items = action.menu.items.map(updateCheckedInLinux);\n action.item = updateCheckedInLinux(action.item);\n break;\n }\n debug('sendAction', action);\n this.writeLine(JSON.stringify(action));\n return this;\n }\n /**\n * Kill the systray process.\n *\n * ## Change notes:\n * ### v2.0.0\n * Removed parameter `exitNode` that automatically killed nodejs process when systray exitted.\n */\n kill(): void {\n this._rl.close();\n this._process.kill();\n }\n\n onExit(listener: (code: number | null, signal: string | null) => void): void {\n this._process.on('exit', listener);\n }\n\n onError(listener: (err: Error) => void): void {\n this._process.on('error', (err) => {\n debug('onError', err, 'binPath', this.binPath);\n listener(err);\n });\n }\n\n get killed(): boolean {\n return this._process.killed;\n }\n\n get binPath(): string {\n return this._binPath;\n }\n}\n"],"names":["path","os","fs","Debug","EventEmitter","child","readline"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA;AACA,MAAM,GAAG,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAAC;AAEjC,SAAU,cAAc,CAC5B,KAAK,GAAG,KAAK,EACb,UAA4B,KAAK,EAAA;AAEjC,IAAA,MAAM,OAAO,GACX;QACE,KAAK,EAAE,CAAe,YAAA,EAAA,KAAK,GAAG,EAAE,GAAG,UAAU,CAAM,IAAA,CAAA;QACnD,MAAM,EAAE,CAAc,WAAA,EAAA,KAAK,GA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AI,OAAO,GAAA;QACT,OAAO,IAAI,CAAC,QAAQ,CAAC;KACtB;AACF;;;;"} \ No newline at end of file +{"version":3,"file":"index.js","sources":["../src/get-tray-bin-path.ts","../src/index.ts"],"sourcesContent":["import * as path from 'path';\nimport * as os from 'os';\nimport * as fs from 'fs-extra';\n\nexport function getTrayBinPath(\n debug = false,\n copyDir: boolean | string = false,\n): string {\n const binName = (\n {\n win32: `tray_windows${debug ? '' : '_release'}.exe`,\n darwin: `tray_darwin${debug ? '' : '_release'}`,\n linux: `tray_linux${debug ? '' : '_release'}`,\n } as Record\n )[process.platform];\n\n if (!binName) {\n throw new Error(\n `node-systray-v2: unsupported platform ${process.platform}.`,\n );\n }\n\n const binPath = path.resolve(`${__dirname}/../traybin/${binName}`);\n\n if (copyDir) {\n // eslint-disable-next-line @typescript-eslint/no-var-requires\n const pkg = require('../package.json');\n\n copyDir = path.join(\n typeof copyDir === 'string'\n ? copyDir\n : `${os.homedir()}/.cache/node-systray/`,\n pkg.version,\n );\n\n const copyDistPath = path.join(copyDir, binName);\n if (!fs.existsSync(copyDistPath)) {\n fs.ensureDirSync(copyDir);\n fs.copySync(binPath, copyDistPath);\n }\n\n return copyDistPath;\n }\n\n return binPath;\n}\n","import * as child from 'child_process';\nimport { EventEmitter } from 'events';\nimport * as readline from 'readline';\nimport Debug from 'debug';\nimport { getTrayBinPath } from './get-tray-bin-path';\n\nconst debug = Debug('systray');\n\nexport type MenuItem = {\n title: string;\n tooltip: string;\n checked: boolean;\n enabled: boolean;\n};\n\nexport type Menu = {\n icon: string;\n title: string;\n tooltip: string;\n items: MenuItem[];\n};\n\nexport type ClickEvent = {\n type: 'clicked';\n item: MenuItem;\n seq_id: number;\n};\n\nexport type ReadyEvent = {\n type: 'ready';\n};\n\nexport type Event = ClickEvent | ReadyEvent;\n\nexport type UpdateItemAction = {\n type: 'update-item';\n item: MenuItem;\n seq_id: number;\n};\n\nexport type UpdateMenuAction = {\n type: 'update-menu';\n menu: Menu;\n seq_id: number;\n};\n\nexport type UpdateMenuAndItemAction = {\n type: 'update-menu-and-item';\n menu: Menu;\n item: MenuItem;\n seq_id: number;\n};\n\nexport type Action =\n | UpdateItemAction\n | UpdateMenuAction\n | UpdateMenuAndItemAction;\n\nexport type Conf = {\n menu: Menu;\n debug?: boolean;\n copyDir?: boolean | string;\n};\n\nconst CHECK_STR = ' (√)';\n\nfunction updateCheckedInLinux(item: MenuItem): MenuItem {\n if (process.platform !== 'linux') {\n return item;\n }\n if (item.checked) {\n item.title += CHECK_STR;\n } else {\n item.title = (item.title || '').replace(RegExp(CHECK_STR + '$'), '');\n }\n return item;\n}\n\nexport class SysTray extends EventEmitter {\n protected _conf: Conf;\n protected _process: child.ChildProcess;\n protected _rl: readline.ReadLine;\n protected _binPath: string;\n\n constructor(conf: Conf) {\n super();\n\n this._conf = conf;\n this._binPath = getTrayBinPath(conf.debug, conf.copyDir);\n this._process = child.spawn(this._binPath, [], {\n windowsHide: true,\n });\n\n this._rl = readline.createInterface({\n input: this._process.stdout!,\n });\n\n conf.menu.items = conf.menu.items.map(updateCheckedInLinux);\n this._rl.on('line', (data) => debug('onLine', data));\n this.onReady(() => this.writeLine(JSON.stringify(conf.menu)));\n }\n\n onReady(listener: () => void): this {\n this._rl.on('line', (line: string) => {\n const action: Event = JSON.parse(line);\n if (action.type === 'ready') {\n listener();\n debug('onReady', action);\n }\n });\n return this;\n }\n\n onClick(listener: (action: ClickEvent) => void): this {\n this._rl.on('line', (line: string) => {\n const action: ClickEvent = JSON.parse(line);\n if (action.type === 'clicked') {\n debug('onClick', action);\n listener(action);\n }\n });\n return this;\n }\n\n writeLine(line: string): this {\n if (line) {\n debug('writeLine', line + '\\n', '=====');\n this._process.stdin!.write(line.trim() + '\\n');\n }\n return this;\n }\n\n sendAction(action: Action): this {\n switch (action.type) {\n case 'update-item':\n action.item = updateCheckedInLinux(action.item);\n break;\n case 'update-menu':\n action.menu.items = action.menu.items.map(updateCheckedInLinux);\n break;\n case 'update-menu-and-item':\n action.menu.items = action.menu.items.map(updateCheckedInLinux);\n action.item = updateCheckedInLinux(action.item);\n break;\n }\n debug('sendAction', action);\n this.writeLine(JSON.stringify(action));\n return this;\n }\n /**\n * Kill the systray process.\n *\n * ## Change notes:\n * ### v2.0.0\n * Removed parameter `exitNode` that automatically killed nodejs process when systray exitted.\n */\n kill(): void {\n this._rl.close();\n this._process.kill();\n }\n\n onExit(listener: (code: number | null, signal: string | null) => void): void {\n this._process.on('exit', listener);\n }\n\n onError(listener: (err: Error) => void): void {\n this._process.on('error', (err) => {\n debug('onError', err, 'binPath', this.binPath);\n listener(err);\n });\n }\n\n get killed(): boolean {\n return this._process.killed;\n }\n\n get binPath(): string {\n return this._binPath;\n }\n}\n"],"names":["path","os","fs","Debug","EventEmitter","child","readline"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIM,SAAU,cAAc,CAC5B,KAAK,GAAG,KAAK,EACb,UAA4B,KAAK,EAAA;AAEjC,IAAA,MAAM,OAAO,GACX;QACE,KAAK,EAAE,CAAe,YAAA,EAAA,KAAK,GAAG,EAAE,GAAG,UAAU,CAAM,IAAA,CAAA;QACnD,MAAM,EAAE,CAAc,WAAA,EAAA,KAAK,GAAG,EAAE,GAAG,UAAU,CAAE,CAAA;QAC/C,KAAK,EAAE,CAAa,UAAA,EAAA,KAAK,GAAG,EAAE,GAAG,UAAU,CAAE,CAAA;AAEhD,KAAA,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IAEpB,IAAI,CAAC,OAAO,EAAE;QACZ,MAAM,IAAI,KAAK,CACb,CAAA,sCAAA,EAAyC,OAAO,CAAC,QAAQ,CAAG,CAAA,CAAA,CAC7D,CAAC;AACH,KAAA;AAED,IAAA,MAAM,OAAO,GAAGA,eAAI,CAAC,OAAO,CAAC,CAAG,EAAA,SAAS,CAAe,YAAA,EAAA,OAAO,CAAE,CAAA,CAAC,CAAC;AAEnE,IAAA,IAAI,OAAO,EAAE;;AAE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I,CAAC,CAAC;gBAChD,MAAM;AACT,SAAA;AACD,QAAA,KAAK,CAAC,YAAY,EAAE,MAAM,CAAC,CAAC;QAC5B,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC;AACvC,QAAA,OAAO,IAAI,CAAC;KACb;AACD;;;;;;AAMG;IACH,IAAI,GAAA;AACF,QAAA,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC;AACjB,QAAA,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC;KACtB;AAED,IAAA,MAAM,CAAC,QAA8D,EAAA;QACnE,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;KACpC;AAED,IAAA,OAAO,CAAC,QAA8B,EAAA;QACpC,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,GAAG,KAAI;YAChC,KAAK,CAAC,SAAS,EAAE,GAAG,EAAE,SAAS,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;YAC/C,QAAQ,CAAC,GAAG,CAAC,CAAC;AAChB,SAAC,CAAC,CAAC;KACJ;AAED,IAAA,IAAI,MAAM,GAAA;AACR,QAAA,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC;KAC7B;AAED,IAAA,IAAI,OAAO,GAAA;QACT,OAAO,IAAI,CAAC,QAAQ,CAAC;KACtB;AACF;;;;"} \ No newline at end of file diff --git a/dist/index.mjs b/dist/index.mjs index 3d45a98..498497c 100644 --- a/dist/index.mjs +++ b/dist/index.mjs @@ -12,8 +12,6 @@ import * as path from 'path'; import * as os from 'os'; import * as fs from 'fs-extra'; -// eslint-disable-next-line @typescript-eslint/no-var-requires -const pkg = require('../package.json'); function getTrayBinPath(debug = false, copyDir = false) { const binName = { win32: `tray_windows${debug ? '' : '_release'}.exe`, @@ -25,6 +23,8 @@ function getTrayBinPath(debug = false, copyDir = false) { } const binPath = path.resolve(`${__dirname}/../traybin/${binName}`); if (copyDir) { + // eslint-disable-next-line @typescript-eslint/no-var-requires + const pkg = require('../package.json'); copyDir = path.join(typeof copyDir === 'string' ? copyDir : `${os.homedir()}/.cache/node-systray/`, pkg.version); diff --git a/dist/index.mjs.map b/dist/index.mjs.map index 43a3f45..14a228c 100644 --- a/dist/index.mjs.map +++ b/dist/index.mjs.map @@ -1 +1 @@ -{"version":3,"file":"index.mjs","sources":["../src/get-tray-bin-path.ts","../src/index.ts"],"sourcesContent":["import * as path from 'path';\nimport * as os from 'os';\nimport * as fs from 'fs-extra';\n\n// eslint-disable-next-line @typescript-eslint/no-var-requires\nconst pkg = require('../package.json');\n\nexport function getTrayBinPath(\n debug = false,\n copyDir: boolean | string = false,\n): string {\n const binName = (\n {\n win32: `tray_windows${debug ? '' : '_release'}.exe`,\n darwin: `tray_darwin${debug ? '' : '_release'}`,\n linux: `tray_linux${debug ? '' : '_release'}`,\n } as Record\n )[process.platform];\n\n if (!binName) {\n throw new Error(\n `node-systray-v2: unsupported platform ${process.platform}.`,\n );\n }\n\n const binPath = path.resolve(`${__dirname}/../traybin/${binName}`);\n\n if (copyDir) {\n copyDir = path.join(\n typeof copyDir === 'string'\n ? copyDir\n : `${os.homedir()}/.cache/node-systray/`,\n pkg.version,\n );\n\n const copyDistPath = path.join(copyDir, binName);\n if (!fs.existsSync(copyDistPath)) {\n fs.ensureDirSync(copyDir);\n fs.copySync(binPath, copyDistPath);\n }\n\n return copyDistPath;\n }\n\n return binPath;\n}\n","import * as child from 'child_process';\nimport { EventEmitter } from 'events';\nimport * as readline from 'readline';\nimport Debug from 'debug';\nimport { getTrayBinPath } from './get-tray-bin-path';\n\nconst debug = Debug('systray');\n\nexport type MenuItem = {\n title: string;\n tooltip: string;\n checked: boolean;\n enabled: boolean;\n};\n\nexport type Menu = {\n icon: string;\n title: string;\n tooltip: string;\n items: MenuItem[];\n};\n\nexport type ClickEvent = {\n type: 'clicked';\n item: MenuItem;\n seq_id: number;\n};\n\nexport type ReadyEvent = {\n type: 'ready';\n};\n\nexport type Event = ClickEvent | ReadyEvent;\n\nexport type UpdateItemAction = {\n type: 'update-item';\n item: MenuItem;\n seq_id: number;\n};\n\nexport type UpdateMenuAction = {\n type: 'update-menu';\n menu: Menu;\n seq_id: number;\n};\n\nexport type UpdateMenuAndItemAction = {\n type: 'update-menu-and-item';\n menu: Menu;\n item: MenuItem;\n seq_id: number;\n};\n\nexport type Action =\n | UpdateItemAction\n | UpdateMenuAction\n | UpdateMenuAndItemAction;\n\nexport type Conf = {\n menu: Menu;\n debug?: boolean;\n copyDir?: boolean | string;\n};\n\nconst CHECK_STR = ' (√)';\n\nfunction updateCheckedInLinux(item: MenuItem): MenuItem {\n if (process.platform !== 'linux') {\n return item;\n }\n if (item.checked) {\n item.title += CHECK_STR;\n } else {\n item.title = (item.title || '').replace(RegExp(CHECK_STR + '$'), '');\n }\n return item;\n}\n\nexport class SysTray extends EventEmitter {\n protected _conf: Conf;\n protected _process: child.ChildProcess;\n protected _rl: readline.ReadLine;\n protected _binPath: string;\n\n constructor(conf: Conf) {\n super();\n\n this._conf = conf;\n this._binPath = getTrayBinPath(conf.debug, conf.copyDir);\n this._process = child.spawn(this._binPath, [], {\n windowsHide: true,\n });\n\n this._rl = readline.createInterface({\n input: this._process.stdout!,\n });\n\n conf.menu.items = conf.menu.items.map(updateCheckedInLinux);\n this._rl.on('line', (data) => debug('onLine', data));\n this.onReady(() => this.writeLine(JSON.stringify(conf.menu)));\n }\n\n onReady(listener: () => void): this {\n this._rl.on('line', (line: string) => {\n const action: Event = JSON.parse(line);\n if (action.type === 'ready') {\n listener();\n debug('onReady', action);\n }\n });\n return this;\n }\n\n onClick(listener: (action: ClickEvent) => void): this {\n this._rl.on('line', (line: string) => {\n const action: ClickEvent = JSON.parse(line);\n if (action.type === 'clicked') {\n debug('onClick', action);\n listener(action);\n }\n });\n return this;\n }\n\n writeLine(line: string): this {\n if (line) {\n debug('writeLine', line + '\\n', '=====');\n this._process.stdin!.write(line.trim() + '\\n');\n }\n return this;\n }\n\n sendAction(action: Action): this {\n switch (action.type) {\n case 'update-item':\n action.item = updateCheckedInLinux(action.item);\n break;\n case 'update-menu':\n action.menu.items = action.menu.items.map(updateCheckedInLinux);\n break;\n case 'update-menu-and-item':\n action.menu.items = action.menu.items.map(updateCheckedInLinux);\n action.item = updateCheckedInLinux(action.item);\n break;\n }\n debug('sendAction', action);\n this.writeLine(JSON.stringify(action));\n return this;\n }\n /**\n * Kill the systray process.\n *\n * ## Change notes:\n * ### v2.0.0\n * Removed parameter `exitNode` that automatically killed nodejs process when systray exitted.\n */\n kill(): void {\n this._rl.close();\n this._process.kill();\n }\n\n onExit(listener: (code: number | null, signal: string | null) => void): void {\n this._process.on('exit', listener);\n }\n\n onError(listener: (err: Error) => void): void {\n this._process.on('error', (err) => {\n debug('onError', err, 'binPath', this.binPath);\n listener(err);\n });\n }\n\n get killed(): boolean {\n return this._process.killed;\n }\n\n get binPath(): string {\n return this._binPath;\n }\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;AAIA;AACA,MAAM,GAAG,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAAC;AAEjC,SAAU,cAAc,CAC5B,KAAK,GAAG,KAAK,EACb,UAA4B,KAAK,EAAA;AAEjC,IAAA,MAAM,OAAO,GACX;QACE,KAAK,EAAE,CAAe,YAAA,EAAA,KAAK,GAAG,EAAE,GAAG,UAAU,CAAM,IAAA,CAAA;QACnD,MAAM,EAAE,CAAc,WAAA,EAAA,KAAK,GAAG,EAAE,GAAG,UAAU,CAAE,CAAA;QAC/C,KAAK,EAAE,CAAa,UAAA,EAAA,KAAK,GAAG,EAAE,GAAG,UAAU,CAAE,CAAA;AAEhD,KAAA,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IAEpB,IAAI,CAAC,OAAO,EAAE;QACZ,MAAM,IAAI,KAAK,CACb,CAAA,sCAAA,EAAyC,OAAO,CAAC,QAAQ,CAAG,CAAA,CAAA,CAC7D,CA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,CAAC,oBAAoB,CAAC,CAAC;gBAChE,MAAM,CAAC,IAAI,GAAG,oBAAoB,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;gBAChD,MAAM;AACT,SAAA;AACD,QAAA,KAAK,CAAC,YAAY,EAAE,MAAM,CAAC,CAAC;QAC5B,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC;AACvC,QAAA,OAAO,IAAI,CAAC;KACb;AACD;;;;;;AAMG;IACH,IAAI,GAAA;AACF,QAAA,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC;AACjB,QAAA,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC;KACtB;AAED,IAAA,MAAM,CAAC,QAA8D,EAAA;QACnE,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;KACpC;AAED,IAAA,OAAO,CAAC,QAA8B,EAAA;QACpC,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,GAAG,KAAI;YAChC,KAAK,CAAC,SAAS,EAAE,GAAG,EAAE,SAAS,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;YAC/C,QAAQ,CAAC,GAAG,CAAC,CAAC;AAChB,SAAC,CAAC,CAAC;KACJ;AAED,IAAA,IAAI,MAAM,GAAA;AACR,QAAA,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC;KAC7B;AAED,IAAA,IAAI,OAAO,GAAA;QACT,OAAO,IAAI,CAAC,QAAQ,CAAC;KACtB;AACF;;;;"} \ No newline at end of file +{"version":3,"file":"index.mjs","sources":["../src/get-tray-bin-path.ts","../src/index.ts"],"sourcesContent":["import * as path from 'path';\nimport * as os from 'os';\nimport * as fs from 'fs-extra';\n\nexport function getTrayBinPath(\n debug = false,\n copyDir: boolean | string = false,\n): string {\n const binName = (\n {\n win32: `tray_windows${debug ? '' : '_release'}.exe`,\n darwin: `tray_darwin${debug ? '' : '_release'}`,\n linux: `tray_linux${debug ? '' : '_release'}`,\n } as Record\n )[process.platform];\n\n if (!binName) {\n throw new Error(\n `node-systray-v2: unsupported platform ${process.platform}.`,\n );\n }\n\n const binPath = path.resolve(`${__dirname}/../traybin/${binName}`);\n\n if (copyDir) {\n // eslint-disable-next-line @typescript-eslint/no-var-requires\n const pkg = require('../package.json');\n\n copyDir = path.join(\n typeof copyDir === 'string'\n ? copyDir\n : `${os.homedir()}/.cache/node-systray/`,\n pkg.version,\n );\n\n const copyDistPath = path.join(copyDir, binName);\n if (!fs.existsSync(copyDistPath)) {\n fs.ensureDirSync(copyDir);\n fs.copySync(binPath, copyDistPath);\n }\n\n return copyDistPath;\n }\n\n return binPath;\n}\n","import * as child from 'child_process';\nimport { EventEmitter } from 'events';\nimport * as readline from 'readline';\nimport Debug from 'debug';\nimport { getTrayBinPath } from './get-tray-bin-path';\n\nconst debug = Debug('systray');\n\nexport type MenuItem = {\n title: string;\n tooltip: string;\n checked: boolean;\n enabled: boolean;\n};\n\nexport type Menu = {\n icon: string;\n title: string;\n tooltip: string;\n items: MenuItem[];\n};\n\nexport type ClickEvent = {\n type: 'clicked';\n item: MenuItem;\n seq_id: number;\n};\n\nexport type ReadyEvent = {\n type: 'ready';\n};\n\nexport type Event = ClickEvent | ReadyEvent;\n\nexport type UpdateItemAction = {\n type: 'update-item';\n item: MenuItem;\n seq_id: number;\n};\n\nexport type UpdateMenuAction = {\n type: 'update-menu';\n menu: Menu;\n seq_id: number;\n};\n\nexport type UpdateMenuAndItemAction = {\n type: 'update-menu-and-item';\n menu: Menu;\n item: MenuItem;\n seq_id: number;\n};\n\nexport type Action =\n | UpdateItemAction\n | UpdateMenuAction\n | UpdateMenuAndItemAction;\n\nexport type Conf = {\n menu: Menu;\n debug?: boolean;\n copyDir?: boolean | string;\n};\n\nconst CHECK_STR = ' (√)';\n\nfunction updateCheckedInLinux(item: MenuItem): MenuItem {\n if (process.platform !== 'linux') {\n return item;\n }\n if (item.checked) {\n item.title += CHECK_STR;\n } else {\n item.title = (item.title || '').replace(RegExp(CHECK_STR + '$'), '');\n }\n return item;\n}\n\nexport class SysTray extends EventEmitter {\n protected _conf: Conf;\n protected _process: child.ChildProcess;\n protected _rl: readline.ReadLine;\n protected _binPath: string;\n\n constructor(conf: Conf) {\n super();\n\n this._conf = conf;\n this._binPath = getTrayBinPath(conf.debug, conf.copyDir);\n this._process = child.spawn(this._binPath, [], {\n windowsHide: true,\n });\n\n this._rl = readline.createInterface({\n input: this._process.stdout!,\n });\n\n conf.menu.items = conf.menu.items.map(updateCheckedInLinux);\n this._rl.on('line', (data) => debug('onLine', data));\n this.onReady(() => this.writeLine(JSON.stringify(conf.menu)));\n }\n\n onReady(listener: () => void): this {\n this._rl.on('line', (line: string) => {\n const action: Event = JSON.parse(line);\n if (action.type === 'ready') {\n listener();\n debug('onReady', action);\n }\n });\n return this;\n }\n\n onClick(listener: (action: ClickEvent) => void): this {\n this._rl.on('line', (line: string) => {\n const action: ClickEvent = JSON.parse(line);\n if (action.type === 'clicked') {\n debug('onClick', action);\n listener(action);\n }\n });\n return this;\n }\n\n writeLine(line: string): this {\n if (line) {\n debug('writeLine', line + '\\n', '=====');\n this._process.stdin!.write(line.trim() + '\\n');\n }\n return this;\n }\n\n sendAction(action: Action): this {\n switch (action.type) {\n case 'update-item':\n action.item = updateCheckedInLinux(action.item);\n break;\n case 'update-menu':\n action.menu.items = action.menu.items.map(updateCheckedInLinux);\n break;\n case 'update-menu-and-item':\n action.menu.items = action.menu.items.map(updateCheckedInLinux);\n action.item = updateCheckedInLinux(action.item);\n break;\n }\n debug('sendAction', action);\n this.writeLine(JSON.stringify(action));\n return this;\n }\n /**\n * Kill the systray process.\n *\n * ## Change notes:\n * ### v2.0.0\n * Removed parameter `exitNode` that automatically killed nodejs process when systray exitted.\n */\n kill(): void {\n this._rl.close();\n this._process.kill();\n }\n\n onExit(listener: (code: number | null, signal: string | null) => void): void {\n this._process.on('exit', listener);\n }\n\n onError(listener: (err: Error) => void): void {\n this._process.on('error', (err) => {\n debug('onError', err, 'binPath', this.binPath);\n listener(err);\n });\n }\n\n get killed(): boolean {\n return this._process.killed;\n }\n\n get binPath(): string {\n return this._binPath;\n }\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;AAIM,SAAU,cAAc,CAC5B,KAAK,GAAG,KAAK,EACb,UAA4B,KAAK,EAAA;AAEjC,IAAA,MAAM,OAAO,GACX;QACE,KAAK,EAAE,CAAe,YAAA,EAAA,KAAK,GAAG,EAAE,GAAG,UAAU,CAAM,IAAA,CAAA;QACnD,MAAM,EAAE,CAAc,WAAA,EAAA,KAAK,GAAG,EAAE,GAAG,UAAU,CAAE,CAAA;QAC/C,KAAK,EAAE,CAAa,UAAA,EAAA,KAAK,GAAG,EAAE,GAAG,UAAU,CAAE,CAAA;AAEhD,KAAA,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IAEpB,IAAI,CAAC,OAAO,EAAE;QACZ,MAAM,IAAI,KAAK,CACb,CAAA,sCAAA,EAAyC,OAAO,CAAC,QAAQ,CAAG,CAAA,CAAA,CAC7D,CAAC;AACH,KAAA;AAED,IAAA,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,CAAG,EAAA,SAAS,CAAe,YAAA,EAAA,OAAO,CAAE,CAAA,CAAC,CAAC;AAEnE,IAAA,IAAI,OAAO,EAAE;;AAEX,QAAA,MAAM,GAAG,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAAC;QAEvC,OAAO,GAAG,IAAI,CAAC,IAAI,CACjB,OAAO,OAAO,KAAK,QAAQ;AACzB,cAAE,OAAO;AACT,cAAE,CAAA,EAAG,EAAE,CAAC,OAAO,EAAE,CAAuB,qBAAA,CAAA,EAC1C,GAAG,CAAC,OAAO,CACZ,CAAC;QAEF,MAAM,YAAY,GAAG,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;AACjD,QAAA,IAAI,CAAC,EAAE,CAAC,UAAU,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AAA;QACnE,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;KACpC;AAED,IAAA,OAAO,CAAC,QAA8B,EAAA;QACpC,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,GAAG,KAAI;YAChC,KAAK,CAAC,SAAS,EAAE,GAAG,EAAE,SAAS,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;YAC/C,QAAQ,CAAC,GAAG,CAAC,CAAC;AAChB,SAAC,CAAC,CAAC;KACJ;AAED,IAAA,IAAI,MAAM,GAAA;AACR,QAAA,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC;KAC7B;AAED,IAAA,IAAI,OAAO,GAAA;QACT,OAAO,IAAI,CAAC,QAAQ,CAAC;KACtB;AACF;;;;"} \ No newline at end of file diff --git a/src/get-tray-bin-path.ts b/src/get-tray-bin-path.ts index 5d12631..1e6740d 100644 --- a/src/get-tray-bin-path.ts +++ b/src/get-tray-bin-path.ts @@ -2,9 +2,6 @@ import * as path from 'path'; import * as os from 'os'; import * as fs from 'fs-extra'; -// eslint-disable-next-line @typescript-eslint/no-var-requires -const pkg = require('../package.json'); - export function getTrayBinPath( debug = false, copyDir: boolean | string = false, @@ -26,6 +23,9 @@ export function getTrayBinPath( const binPath = path.resolve(`${__dirname}/../traybin/${binName}`); if (copyDir) { + // eslint-disable-next-line @typescript-eslint/no-var-requires + const pkg = require('../package.json'); + copyDir = path.join( typeof copyDir === 'string' ? copyDir diff --git a/types/get-tray-bin-path.d.ts.map b/types/get-tray-bin-path.d.ts.map index cd1f005..0681011 100644 --- a/types/get-tray-bin-path.d.ts.map +++ b/types/get-tray-bin-path.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"get-tray-bin-path.d.ts","sourceRoot":"","sources":["../src/get-tray-bin-path.ts"],"names":[],"mappings":"AAOA,wBAAgB,cAAc,CAC5B,KAAK,UAAQ,EACb,OAAO,GAAE,OAAO,GAAG,MAAc,GAChC,MAAM,CAmCR"} \ No newline at end of file +{"version":3,"file":"get-tray-bin-path.d.ts","sourceRoot":"","sources":["../src/get-tray-bin-path.ts"],"names":[],"mappings":"AAIA,wBAAgB,cAAc,CAC5B,KAAK,UAAQ,EACb,OAAO,GAAE,OAAO,GAAG,MAAc,GAChC,MAAM,CAsCR"} \ No newline at end of file