Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
const { spawnSync, spawn } = require('child_process')
const { existsSync, writeFileSync } = require('fs')
const path = require('path')
const SESSION_ID = 'updateThis' // Edit this line only, don't remove ' <- this symbol
let nodeRestartCount = 0
const maxNodeRestarts = 5
const restartWindow = 30000 // 30 seconds
let lastRestartTime = Date.now()
function startNode() {
const child = spawn('node', ['index.js'], { cwd: 'levanter', stdio: 'inherit' })
child.on('exit', (code) => {
if (code !== 0) {
const currentTime = Date.now()
if (currentTime - lastRestartTime > restartWindow) {
nodeRestartCount = 0
}
lastRestartTime = currentTime
nodeRestartCount++
})
}
function startPm2() {
const pm2 = spawn('yarn', ['pm2', 'start', 'index.js', '--name', 'levanter', '--attach'], {
cwd: 'levanter',
stdio: ['pipe', 'pipe', 'pipe'],
})
let restartCount = 0
const maxRestarts = 5 // Adjust this value as needed
pm2.on('exit', (code) => {
if (code !== 0) {
// console.log('yarn pm2 failed to start, falling back to node...')
startNode()
}
})
pm2.on('error', (error) => {
console.error(
yarn pm2 error: ${error.message})startNode()
})
// Check for infinite restarts
if (pm2.stderr) {
pm2.stderr.on('data', (data) => {
const output = data.toString()
if (output.includes('restart')) {
restartCount++
if (restartCount > maxRestarts) {
// console.log('yarn pm2 is restarting indefinitely, stopping yarn pm2 and starting node...')
spawnSync('yarn', ['pm2', 'delete', 'levanter'], { cwd: 'levanter', stdio: 'inherit' })
startNode()
}
}
})
}
if (pm2.stdout) {
pm2.stdout.on('data', (data) => {
const output = data.toString()
console.log(output)
if (output.includes('Connecting')) {
// console.log('Application is online.')
restartCount = 0
}
})
}
}
function installDependencies() {
// console.log('Installing dependencies...')
const installResult = spawnSync(
'yarn',
['install', '--force', '--non-interactive', '--network-concurrency', '3'],
{
cwd: 'levanter',
stdio: 'inherit',
env: { ...process.env, CI: 'true' }, // Ensure non-interactive environment
}
)
if (installResult.error || installResult.status !== 0) {
console.error(
Failed to install dependencies: ${ installResult.error ? installResult.error.message : 'Unknown error' })
process.exit(1) // Exit the process if installation fails
}
}
function checkDependencies() {
if (!existsSync(path.resolve('levanter/package.json'))) {
console.error('package.json not found!')
process.exit(1)
}
const result = spawnSync('yarn', ['check', '--verify-tree'], {
cwd: 'levanter',
stdio: 'inherit',
})
// Check the exit code to determine if there was an error
if (result.status !== 0) {
console.log('Some dependencies are missing or incorrectly installed.')
installDependencies()
} else {
// console.log('All dependencies are installed properly.')
}
}
function cloneRepository() {
// console.log('Cloning the repository...')
const cloneResult = spawnSync(
'git',
['clone', 'https://github.com/lyfe00011/levanter.git', 'levanter'],
{
stdio: 'inherit',
}
)
if (cloneResult.error) {
throw new Error(
Failed to clone the repository: ${cloneResult.error.message})}
const configPath = 'levanter/config.env'
try {
// console.log('Writing to config.env...')
writeFileSync(configPath,
VPS=true\nSESSION_ID=${SESSION_ID})} catch (err) {
throw new Error(
Failed to write to config.env: ${err.message})}
installDependencies()
}
if (!existsSync('levanter')) {
cloneRepository()
checkDependencies()
} else {
checkDependencies()
}
startPm2()const { spawnSync, spawn } = require('child_process')
const { existsSync, writeFileSync } = require('fs')
const path = require('path')
const SESSION_ID = 'updateThis' // Edit this line only, don't remove ' <- this symbol
let nodeRestartCount = 0
const maxNodeRestarts = 5
const restartWindow = 30000 // 30 seconds
let lastRestartTime = Date.now()
function startNode() {
const child = spawn('node', ['index.js'], { cwd: 'levanter', stdio: 'inherit' })
child.on('exit', (code) => {
if (code !== 0) {
const currentTime = Date.now()
if (currentTime - lastRestartTime > restartWindow) {
nodeRestartCount = 0
}
lastRestartTime = currentTime
nodeRestartCount++
})
}
function startPm2() {
const pm2 = spawn('yarn', ['pm2', 'start', 'index.js', '--name', 'levanter', '--attach'], {
cwd: 'levanter',
stdio: ['pipe', 'pipe', 'pipe'],
})
let restartCount = 0
const maxRestarts = 5 // Adjust this value as needed
pm2.on('exit', (code) => {
if (code !== 0) {
// console.log('yarn pm2 failed to start, falling back to node...')
startNode()
}
})
pm2.on('error', (error) => {
console.error(
yarn pm2 error: ${error.message})startNode()
})
// Check for infinite restarts
if (pm2.stderr) {
pm2.stderr.on('data', (data) => {
const output = data.toString()
if (output.includes('restart')) {
restartCount++
if (restartCount > maxRestarts) {
// console.log('yarn pm2 is restarting indefinitely, stopping yarn pm2 and starting node...')
spawnSync('yarn', ['pm2', 'delete', 'levanter'], { cwd: 'levanter', stdio: 'inherit' })
startNode()
}
}
})
}
if (pm2.stdout) {
pm2.stdout.on('data', (data) => {
const output = data.toString()
console.log(output)
if (output.includes('Connecting')) {
// console.log('Application is online.')
restartCount = 0
}
})
}
}
function installDependencies() {
// console.log('Installing dependencies...')
const installResult = spawnSync(
'yarn',
['install', '--force', '--non-interactive', '--network-concurrency', '3'],
{
cwd: 'levanter',
stdio: 'inherit',
env: { ...process.env, CI: 'true' }, // Ensure non-interactive environment
}
)
if (installResult.error || installResult.status !== 0) {
console.error(
Failed to install dependencies: ${ installResult.error ? installResult.error.message : 'Unknown error' })
process.exit(1) // Exit the process if installation fails
}
}
function checkDependencies() {
if (!existsSync(path.resolve('levanter/package.json'))) {
console.error('package.json not found!')
process.exit(1)
}
const result = spawnSync('yarn', ['check', '--verify-tree'], {
cwd: 'levanter',
stdio: 'inherit',
})
// Check the exit code to determine if there was an error
if (result.status !== 0) {
console.log('Some dependencies are missing or incorrectly installed.')
installDependencies()
} else {
// console.log('All dependencies are installed properly.')
}
}
function cloneRepository() {
// console.log('Cloning the repository...')
const cloneResult = spawnSync(
'git',
['clone', 'https://github.com/lyfe00011/levanter.git', 'levanter'],
{
stdio: 'inherit',
}
)
if (cloneResult.error) {
throw new Error(
Failed to clone the repository: ${cloneResult.error.message})}
const configPath = 'levanter/config.env'
try {
// console.log('Writing to config.env...')
writeFileSync(configPath,
VPS=true\nSESSION_ID=${SESSION_ID})} catch (err) {
throw new Error(
Failed to write to config.env: ${err.message})}
installDependencies()
}
if (!existsSync('levanter')) {
cloneRepository()
checkDependencies()
} else {
checkDependencies()
}
startPm2()