0

I'm writing a script for a webapp, which I can only copy my code manually to its platform and not push it from my machine. In the code there are parts that use its API and parts that don't. I cannot split it into multiple modules as well. Everything should be on one file.

To be able to debug it on local, I need to change the parts using its API with my own code. However when it's time to upload it then I need to comment my parts and uncomment the API parts. Then undo that to debug again. Is there a way to skip these steps?

I use VS Code. The language is JS. The platform is Fibery.

Ooker
  • 174
  • 9

2 Answers2

4
  • Everything should be in one file: You can use a tool like esbuild to combine ("bundle") many source files (using import to refer to each other) into a single file ready to copy and paste. You could even follow it up with a tool that reads the file and puts it into your clipboard for easy pasting (see e.g. https://stackoverflow.com/q/749544/1428461).

  • I need to change the parts using its API with my own code: Most such tools also support "aliases" or "resolutions" that will let you change which files are being imported (e.g. based on an environment variable) without actually changing the source code. Isolate the code that should change into their own file(s) and choose which one(s) get imported based on whether you are testing or deploying. You could also use a different "main" file for each version and then import the common parts in both.

  • I can only copy my code manually to its platform and not push it from my machine: It may not be worth the trouble, but you could probably use a browser automation tool like Puppeteer to do this on your behalf. It's also possible that you could curl to whatever endpoint the UI eventually submits your code to.

All tools I mentioned have alternatives and these are just the first examples that came to mind.

Jacob Raihle
  • 1,692
  • 13
  • 14
  • if I can always bundle 3rd party modules into one single file, then what would be the reason for restricting using 3rd party modules at the beginning? I guess it's for security, but then I can always bypassing it? – Ooker Jul 10 '23 at 06:48
  • @Ooker I don't think it is a security feature, but simply the _lack_ of a "multi file" feature. – Jacob Raihle Jul 10 '23 at 08:49
  • is there any benefit for that? – Ooker Jul 10 '23 at 11:37
  • 1
    @Ooker it's easier for the platform to let you upload / replace one file than to manage multiple – Jacob Raihle Jul 10 '23 at 12:01
1

Another way is to wrap it in a if that checks for dev/prod environment:

try {
    //calling an object or function that is only available in the product
} catch (error) {
    var isOnIDE = true
}
if (!isOnIDE ) {
    //product code

If you want to control this manually, try:

function main(mode='production') {
    if (mode === 'debug' || mode === 1) {
        // debug code
    } else {
        // product code
    }
} 

main('debug')

While you still have to edit the mode variable when pasting to the platform, at least you don't have to comment and uncomment repeatedly.

Ooker
  • 174
  • 9