import re with open("/opt/amos/services/ecosystem.config.cjs") as f: content = f.read() if "aamos-bank-import" in content: print("ALREADY_EXISTS") else: insert = """ // ── AAMOS Finance — Bank CSV Import ───────────────────────────────────────── { name: 'aamos-bank-import', script: '/opt/amos/services/aamos-bank-import/index.mjs', env: { PORT: 3252, DATABASE_URL: DB, NODE_ENV: 'production', SERVICE_NAME: 'aamos-bank-import', LEDGER_URL: 'http://127.0.0.1:3250', } }, """ # Insert before the closing ] # Find the last ] followed by newline }; new_content = re.sub(r'(\n \]\n\};)', insert + r'\1', content, count=1) if new_content == content: # Try alternative closing pattern new_content = content.replace("\n ]\n};", insert + "\n ]\n};", 1) if new_content == content: print("PATTERN_NOT_FOUND - trying line-by-line") lines = content.split('\n') # Find last line that is just "]" or " ]" for i in range(len(lines)-1, -1, -1): stripped = lines[i].strip() if stripped == ']': lines.insert(i, insert) new_content = '\n'.join(lines) break with open("/opt/amos/services/ecosystem.config.cjs", "w") as f: f.write(new_content) print("INSERTED")