@vielhuber/wahelper 1.4.6 → 1.4.8

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.
package/README.MD CHANGED
@@ -107,7 +107,7 @@ $wahelper->sendGroup(
107
107
  npm install -g pm2
108
108
 
109
109
  # start daemon
110
- pm2 start npx --name wahelper-xxxxxxxxxxxx -- wahelper-daemon --device xxxxxxxxxxxx
110
+ pm2 start npx --name wahelper-xxxxxxxxxxxx --cwd /var/www/wahelper -- wahelper-daemon --device xxxxxxxxxxxx
111
111
 
112
112
  # optional: autostart
113
113
  ## save config
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vielhuber/wahelper",
3
- "version": "1.4.6",
3
+ "version": "1.4.8",
4
4
  "description": "Lightweight whatsapp integration layer.",
5
5
  "main": "wahelper.js",
6
6
  "files": [
@@ -46,6 +46,9 @@ export default class wahelperDaemon {
46
46
  currentDir = dirname(fileURLToPath(import.meta.url));
47
47
  if (currentDir.includes('node_modules')) {
48
48
  projectRoot = dirname(dirname(dirname(currentDir)));
49
+ if (!fs.existsSync(projectRoot + '/package.json')) {
50
+ projectRoot = process.cwd();
51
+ }
49
52
  } else if (currentDir.includes('vendor')) {
50
53
  projectRoot = dirname(dirname(dirname(currentDir)));
51
54
  } else {
@@ -691,14 +694,50 @@ export default class wahelperDaemon {
691
694
  this.log('Daemon stopped');
692
695
  }
693
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
+
694
716
  async init() {
695
717
  if (!this.args.device) {
696
718
  console.error('Error: --device argument is required');
697
719
  process.exit(1);
698
720
  }
699
721
 
700
- this.log('Daemon starting.. (device: ' + this.device + ')');
701
- console.log('Daemon starting... (device: ' + this.device + ')');
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
+
735
+ this.log(
736
+ 'Daemon starting.. (device: ' + this.device + ', socket: ' + this.dirname + '/' + this.socketPath + ')'
737
+ );
738
+ console.log(
739
+ 'Daemon starting... (device: ' + this.device + ', socket: ' + this.dirname + '/' + this.socketPath + ')'
740
+ );
702
741
 
703
742
  this.initDatabase();
704
743
  this.initExitHooks();
package/wahelper.js CHANGED
@@ -33,6 +33,9 @@ export default class wahelper {
33
33
  currentDir = dirname(fileURLToPath(import.meta.url));
34
34
  if (currentDir.includes('node_modules')) {
35
35
  projectRoot = dirname(dirname(dirname(currentDir)));
36
+ if (!fs.existsSync(projectRoot + '/package.json')) {
37
+ projectRoot = process.cwd();
38
+ }
36
39
  } else if (currentDir.includes('vendor')) {
37
40
  projectRoot = dirname(dirname(dirname(currentDir)));
38
41
  } else {