Search code examples
javascriptvisual-studio-codeterminalcommand-line-interfaceplaywright

Running terminal commands in VS code


Is there anyway to run terminal commands using playwright(javascript)? Can there be a way to access the terminal through VScode? What I am trying to achieve is instead of having steps 1-10 being login steps with username and password. I have a command and or code that can reach the terminal, login and access my Org? I attached the code of the command I am trying to use.

SFDX force:org:open -u "Test@test.com"

I tried a few scenarios but cant access the terminal through the code.

What bash/shell commands would be of use for this scenario? Cant run the SFDX command in vs code without it running in the terminal.. I want it to run the SFDX command first step 1 run the command and then run the rest of the script after.


Solution

  • Since you have a package.json I usually add a few extra scripts in it to help me in cases like that.

    So there's two ways to run those scripts using npm or yarn:

    1. add the item to the scripts section in the package.json like this:
    {
      "name": "stack-74869815",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "setup": "SFDX force:org:open -u 'Test@test.com' && command2 && comand3"
      },
      "author": "",
      "license": "ISC"
    }
    

    and then run :

    npm run setup
    

    or

    yarn setup
    

    The second one (which I recommend the most) is to create a script and then run that script from the package.json, like this:

    script file -> setup.sh (in this case is a shell script file but it could be a .bat for windows or powershell instead:

    #!/bin/bash
    
    echo 'Running setup...'
    
    SFDX force:org:open -u "Test@test.com"
    
    # do other scripts stuff
    
    echo 'Setup finished'
    
    

    and then your package.json:

    {
      "name": "stack-74869815",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "setup": "sh setup.sh"
      },
      "author": "",
      "license": "ISC"
    }
    

    This is a sample of a scripts section of one of my projects. As you can see I have both cases here:

      "scripts": {
        "ng": "ng",
        "start": "ng serve --port=4201",
        "build": "ng build --configuration=production",
        "test": "ng test",
        "lint": "ng lint",
        "e2e": "ng e2e",
        "resources": "npx capacitor-resources -p android,ios",
        "build-android": "sh build-android.sh",
        "build-ios": "sh build-ios.sh",
        "ios": "npx cap sync ios && npx ionic capacitor run ios --livereload --external",
        "android": "npx cap sync android && npx ionic capacitor run android --livereload --external",
        "postinstall": "echo '!!!WARNING - replacing CAPBridgeViewController.swift - Shake fix for iOS' && cp npm-hooks/CAPBridgeViewController.swift ./node_modules/@capacitor/ios/Capacitor/Capacitor/CAPBridgeViewController.swift"
      },
    

    There is also preinstall and postinstall scripts which would run before and after npm install respectfully.

    Hope that helps!