@vielhuber/wahelper 1.4.7 → 1.4.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/wahelper-daemon.js +32 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vielhuber/wahelper",
3
- "version": "1.4.7",
3
+ "version": "1.4.9",
4
4
  "description": "Lightweight whatsapp integration layer.",
5
5
  "main": "wahelper.js",
6
6
  "files": [
@@ -694,12 +694,44 @@ export default class wahelperDaemon {
694
694
  this.log('Daemon stopped');
695
695
  }
696
696
 
697
+ isAlreadyRunning() {
698
+ return new Promise(resolve => {
699
+ let socketPath = this.dirname + '/' + this.socketPath;
700
+ if (!fs.existsSync(socketPath)) {
701
+ resolve(false);
702
+ return;
703
+ }
704
+ let req = http.request({ socketPath, path: '/status', method: 'GET' }, res => {
705
+ resolve(res.statusCode === 200);
706
+ });
707
+ req.on('error', () => resolve(false));
708
+ req.setTimeout(2000, () => {
709
+ req.destroy();
710
+ resolve(false);
711
+ });
712
+ req.end();
713
+ });
714
+ }
715
+
697
716
  async init() {
698
717
  if (!this.args.device) {
699
718
  console.error('Error: --device argument is required');
700
719
  process.exit(1);
701
720
  }
702
721
 
722
+ if (await this.isAlreadyRunning()) {
723
+ console.error(
724
+ '⛔ Daemon already running for device ' +
725
+ this.device +
726
+ ' (socket: ' +
727
+ this.dirname +
728
+ '/' +
729
+ this.socketPath +
730
+ ')'
731
+ );
732
+ process.exit(1);
733
+ }
734
+
703
735
  this.log(
704
736
  'Daemon starting.. (device: ' + this.device + ', socket: ' + this.dirname + '/' + this.socketPath + ')'
705
737
  );