49 lines
1.4 KiB
TypeScript
Executable File
49 lines
1.4 KiB
TypeScript
Executable File
#!/usr/bin/env bun
|
|
|
|
import { Command } from "commander";
|
|
import { readFileSync, writeFileSync } from "fs";
|
|
import Papa from "papaparse";
|
|
import prettier from "prettier";
|
|
|
|
type pm2Config = {
|
|
name: string;
|
|
script: string;
|
|
args: string;
|
|
cwd: string;
|
|
interpreter: string;
|
|
env: string;
|
|
autorestart: "1" | "0";
|
|
watch: "1" | "0";
|
|
disabled: "1" | "0";
|
|
}
|
|
|
|
const pm2edit = new Command("pm2edit")
|
|
.description("Convert PM2 CSV to JS")
|
|
.argument("<csvFile>", "The CSV file to convert")
|
|
.action(async (csvFile) => {
|
|
// read and parse csv file
|
|
const csv = readFileSync(csvFile, "utf8");
|
|
const parsedData = Papa.parse<pm2Config>(csv, { header: true })
|
|
|
|
let data = parsedData.data
|
|
.filter((row) => row.disabled === "0")
|
|
.map((row) => {
|
|
return {
|
|
...row,
|
|
env: row.env ? Object.fromEntries(row.env.split(",").map(item => item.split("="))) : undefined,
|
|
out_file: `/home/pi/logs/${row.name}.log`,
|
|
error_file: `/home/pi/logs/${row.name}.log`,
|
|
merge_logs: true,
|
|
watch: row.watch === "1",
|
|
autorestart: row.autorestart === "1",
|
|
}
|
|
})
|
|
|
|
const fileContent = `module.exports = {apps: ${JSON.stringify(data, null, 2)}}`
|
|
|
|
const formattedContent = await prettier.format(fileContent, { parser: "typescript" });
|
|
|
|
writeFileSync("ecosystem.config.js", formattedContent);
|
|
});
|
|
|
|
pm2edit.parseAsync(process.argv); |