ASK HN: How to engineer a JavaScript to Python migration?
Context: I was tasked with migrating a legacy workflow system (Broadcom CA Workflow Automation) to Airflow.
There are some jobs that contain rather simple JavaScript snippets, and I was trying to design a first prototype that simply takes the JS parts and runs them in a transpiler.
In this respect, I found a couple of packages that could be leveraged: - js2py: https://github.com/PiotrDabkowski/Js2Py - mini-racer: https://github.com/bpcreech/PyMiniRacer Yet, both seem to be abandoned packages that might not be suitable for usage in production.
Therefore, I was thinking about parsing and translating Javascript's abstract syntax trees to Python. Whereas a colleague suggested I bring up an LLM pipeline.
How much of an overkill that might be? Has anyone else ever dealt with a JavaScript-to-Python migration and could share heads-ups on strategies or pitfalls to avoid?
Working on something similar but in reverse (Python to JavaScript). A few tips:
- If the scripts are primarily by the same author, it's likely they copy-pasted a lot of their functions throughout their scripts. Do something like a "Find All" in the repository holding the scripts for the function name. Regex will help a lot in this, and help you see if a function ever has variations on number of arguments, slight name changes or misspells, etc.
- Refine an AI prompt as you convert scripts over and over.
- AI (ChatGPT, Copilot, etc.) is going to be the best automation you can get for this, because often times conversions don't match up one-to-one in a language. Especially if your scripts use npm, you might not find one-to-one matches in PyPI. AI refactors will also force you to examine the conversions.
- Understand what tech debt could be introduced in a one-to-one conversion. There may be some language workarounds that make a lot more sense to introduce over exact conversions of workaround functions for the language. I've had to work with several legacy Python scripts that called functions that can be better written in your target language. For example, one of our functions was conditionally_get_keys(level1, level2, level3) to kind of recursively get a nested value. This didn't need to be a function when I rewrote it in JS, rather I just wrote the variable as something like `city = user.location?.city`. No need to one-to-one convert a function that can be better written with a JS language feature. You'll probably encounter this when you can more succinctly write list manipulations with slicing (e.g. `steppedList = fooList[::2]` instead of converting a function necessary in JS to do the same thing).
Sorry if you have a time-crunch with converting things, but I would recommend a more hands-on conversion strategy, leveraging AI to do the basic conversion and then manually testing, debugging the solutions.
> How much of an overkill that might be?
It sounds like a complete waste of time. If you are talking about small code snippets then simply write new original Python to replace them.
Yep, I thought about that... Still, there's a few hundreds of workflows to migrate, so I was looking for a systematic approach
Hi, is this a monolith or a series of scripts? if the latter, isolate each script enough that it could be ran with python and then do it for all the scripts. if it is a monolith, consider splitting it and working on the pieces one by one.
an old package on prod is not an issue if it is isolated and you have a plan for change, so if you have to transpile, do it and then work from the bottom.
It's a bunch of scripts, a few lines long. But again, I haven't seen the totality of them and there might be edge cases with longer scripts.
Thanks for your hint!
if we are talking small scripts, just rewrite in python and make sure that they are called with the right interpreter. use ai if you are not acquainted with the language. ask your managers if they might want to use another language which could be compiled and save on compute time. though not popular, nim is python like, and should do the job admirably.