Skip to content

Instantly share code, notes, and snippets.

@julianoBRL
Last active April 10, 2024 20:35
Show Gist options
  • Save julianoBRL/88aa113f254ca36b81bd41cd1fe94451 to your computer and use it in GitHub Desktop.
Save julianoBRL/88aa113f254ca36b81bd41cd1fe94451 to your computer and use it in GitHub Desktop.
PM2 Ecosystem Autoscan
/*
Title: PM2 Ecosystem Autoscan
Author: Juliano Lira(Shintaro)
Date: 10/04/2024 17:30
*/
const fs = require('node:fs');
const path = require('node:path');
const apps = []
const scanServices = () => {
const servicesFolders = fs.readdirSync(__dirname);
for (const folder of servicesFolders) {
const servicePath = path.join(__dirname, folder);
if(fs.lstatSync(servicePath).isDirectory()){
const serviceStartup = fs.readdirSync(servicePath).filter(file => file.match("PM2_Startup.js"));
const serviceStartupPath = path.join(servicePath, serviceStartup[0]);
const startup = require(serviceStartupPath);
if ('startup' in startup) {
apps.push(startup.startup);
} else {
console.log(`[WARNING] The command at ${serviceStartup} is missing a required [startup] property.`);
}
}
}
return apps;
}
module.exports = {
apps : scanServices()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment