create-dacosta-proj 1.0.21 → 1.0.22

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-dacosta-proj",
3
- "version": "1.0.21",
3
+ "version": "1.0.22",
4
4
  "bin": {
5
5
  "create-dacosta-proj": "./index.js"
6
6
  }
@@ -12,7 +12,7 @@ const rootDir = path.resolve(__dirname, '..');
12
12
 
13
13
  // Get All Servers
14
14
 
15
- let servers = Object.keys(process.env).filter(key => key.startsWith('DEPLOY_')).length / 3;
15
+ let servers = Object.keys(process.env).filter(key => key.startsWith('DEPLOY_') && key.endsWith('_IP')).length;
16
16
  servers = new Array(servers).fill().map((_,server) => {
17
17
  return {
18
18
  ip: process.env[`DEPLOY_${server+1}_IP`],
@@ -0,0 +1,102 @@
1
+ // Global Tools
2
+
3
+ require('dotenv').config({ quiet: true });
4
+
5
+ // Packages / Helpers
6
+
7
+ const { NodeSSH } = require('node-ssh');
8
+
9
+ // Get All Servers
10
+
11
+ const servers = Object.keys(process.env)
12
+ .filter(key => /^DEPLOY_\d+_IP$/.test(key))
13
+ .length;
14
+
15
+ const targets = new Array(servers).fill().map((_, server) => {
16
+ const pm2 = (process.env[`DEPLOY_${server+1}_PM2_NAME`] || '')
17
+ .split(';')
18
+ .map(p => p.trim())
19
+ .filter(Boolean);
20
+ return {
21
+ ip: process.env[`DEPLOY_${server+1}_IP`],
22
+ password: process.env[`DEPLOY_${server+1}_PASSWORD`],
23
+ path: process.env[`DEPLOY_${server+1}_PATH`],
24
+ src: process.env[`DEPLOY_${server+1}_PM2_SRC`],
25
+ args: process.env[`DEPLOY_${server+1}_PM2_ARGS`],
26
+ pm2,
27
+ };
28
+ });
29
+
30
+ // Build a shell command that loads nvm first
31
+
32
+ function withNvm(cmd) {
33
+ return `. ~/.nvm/nvm.sh && ${cmd}`;
34
+ }
35
+
36
+ // Restart on Server
37
+
38
+ async function restartOn(server) {
39
+
40
+ console.log(`\n→ ${server.ip}`);
41
+
42
+ if (!server.pm2.length) {
43
+ console.warn(' ⚠ no pm2 processes configured, skipping');
44
+ return;
45
+ }
46
+
47
+ const ssh = new NodeSSH();
48
+ await ssh.connect({
49
+ host: server.ip,
50
+ username: 'root',
51
+ password: server.password,
52
+ });
53
+
54
+ const install = await ssh.execCommand(withNvm(`cd ${server.path} && npm install --no-audit --no-fund`));
55
+ if (install.stdout) console.log(install.stdout.split('\n').map(l => ` ${l}`).join('\n'));
56
+ if (install.code === 0) {
57
+ console.log(' ✓ npm install');
58
+ } else {
59
+ console.error(` ✗ npm install — ${install.stderr || install.stdout}`);
60
+ }
61
+
62
+ for (const name of server.pm2) {
63
+
64
+ const exists = await ssh.execCommand(withNvm(`pm2 describe ${name} > /dev/null 2>&1`));
65
+
66
+ if (exists.code === 0) {
67
+ const result = await ssh.execCommand(withNvm(`pm2 restart ${name}`));
68
+ if (result.code === 0) {
69
+ console.log(` ✓ ${name} (restarted)`);
70
+ } else {
71
+ console.error(` ✗ ${name} — ${result.stderr || result.stdout}`);
72
+ }
73
+ } else {
74
+ const result = await ssh.execCommand(
75
+ withNvm(`${server.args ? `${server.args} ` : ''}pm2 start ${server.src} --name "${name}" --log-date-format "YYYY-MM-DD HH:mm"`),
76
+ { cwd: server.path }
77
+ );
78
+ if (result.code === 0) {
79
+ console.log(` ✓ ${name} (created)`);
80
+ } else {
81
+ console.error(` ✗ ${name} — ${result.stderr || result.stdout}`);
82
+ }
83
+ }
84
+
85
+ }
86
+
87
+ ssh.dispose();
88
+
89
+ }
90
+
91
+ // Start Restart
92
+
93
+ (async () => {
94
+
95
+ for (const server of targets) {
96
+ await restartOn(server);
97
+ }
98
+
99
+ console.log('\nRestart complete!');
100
+ process.exit();
101
+
102
+ })();
@@ -7,6 +7,12 @@ PROJECT_ID=
7
7
  DEPLOY_1_IP=
8
8
  DEPLOY_1_PASSWORD=
9
9
  DEPLOY_1_PATH=
10
+ DEPLOY_1_PM2_NAME=
11
+ # EXAMPLE 1: DEPLOY_1_PM2_NAME=nm-bot
12
+ # EXAMPLE 2: DEPLOY_1_PM2_NAME=nm-tiktok-video-1;nm-tiktok-live-1
13
+ DEPLOY_1_PM2_SRC=src/index.js
14
+ DEPLOY_1_PM2_ARGS= # Optional
15
+ # EXAMPLE: DEPLOY_1_PM2_ARGS=PROJECT_INSTANCE_ID=1 PROJECT_INSTANCE_TYPE=video
10
16
 
11
17
  # Supabase
12
18
 
@@ -5,7 +5,8 @@
5
5
  "scripts": {
6
6
  "dev": "nodemon src/index.js",
7
7
  "start": "node src/index.js",
8
- "deploy": "node .deploy/deploy.js"
8
+ "deploy": "node .deploy/deploy.js",
9
+ "restart": "node .deploy/restart.js"
9
10
  },
10
11
  "dependencies": {
11
12
  "@supabase/supabase-js": "^2.101.1",