How to forward command into run-commands using NX CLI? - nrwl-nx

Given I have entries in angular.json that define various commands using the serverless CLI (as an example)
"serverless-deploy": {
"builder": "#nrwl/workspace:run-commands",
"options": {
"command": "npx serverless deploy",
"cwd": "apps/my-app"
}
},
"serverless-remove": {
"builder": "#nrwl/workspace:run-commands",
"options": {
"command": "npx serverless remove",
"cwd": "apps/my-app"
}
}
How can I improve this by reducing duplication and combining it into one entry so that I can forward the command in?
E.G something like this:
"sls": {
"builder": "#nrwl/workspace:run-commands",
"options": {
"command": "npx serverless",
"cwd": "apps/my-app"
}
}
And then call it with nx run my-app:sls MYCOMMAND (E.G 'deploy' or 'remove')?
Unfortunately the above doesn't work but would love to know if this is possible.

"sls": {
"builder": "#nrwl/workspace:run-commands",
"options": {
"commands": [
"npx sls {args.cmd}"
],
"cwd": "apps/api",
"parallel": false
}
},
nx run api:sls --cmd=deploy

Related

Unable to read appsettings.json when debugging .net core webapi in vscode

When debugging in vscode I'm unable to read the appsettings.json in my Startup.cs file of my webapi project. They all return as empty/null.
My launch.json configuration for the webapi was generated by adding the .NET Core Launch (console) configuration. I also tried adding the .NET Core Launch (web) configuration but the same problem occurs.
The only thing I then had to change was the "program" setting to point to the correct dll in my project folder.
Here is my launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/SATestUtils.API/bin/Debug/netcoreapp3.1/SATestUtils.API.dll",
"args": [],
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
"console": "internalConsole"
}
]
}
And here is my tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/SATestUtils.Api/SATestUtils.API.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/SATestUtils.Api/SATestUtils.API.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"${workspaceFolder}/SATestUtils.Api/SATestUtils.API.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
}
]
}
Here is my appsettings.json and appsettings.Development.json file...
{
"ConnectionStrings": {
"DefaultConnection": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=SaTestUtils"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"StorageAccountName": "devstoreaccount1",
"AzureAD": {
"Instance": "https://login.microsoftonline.com",
"Domain": "[Domain]",
"TenantId": "[TenantId]",
"ClientId": "[ClientId]",
"Scope": "[Scope]"
},
"AllowedHosts": "*"
}
When I attempt to debug the following code in my services method in Startup.cs
var connectionString = Configuration["ConnectionStrings:DefaultConnection"];
Console.WriteLine($"Connection string = {connectionString}");
I simply get the following logged to the debug terminal
Connection string =
All the other settings in appsettings.json are also not returning. The correct settings are returned when using dotnet run.
What could be the issue here?
Note that I have a solution file in my working directory, then I have a folder called SATestUtils.API that contains the SATestUtils.API.csproj file for my webapi project.
I experienced the same case, for me, what worked was what has been commented before:
Change the value of 'cwd' property in launch.json to the value of the project directory.
{
...
"cwd": "${workspaceFolder}"
}
to
{
...
"cwd": "${workspaceFolder}/SATestUtils.API"
}
All the credits to Bemm...
Did you try to specify the asp net environment at the launch.json?
Something like this:

how to execute nx build before nx serve?

Curious what the correct way is to execute a nx build command as a prerequisite of nx serve?
So for example, in this example from my workspace.json:
"foo": {
"root": "apps/foo",
"sourceRoot": "apps/foo/src",
"projectType": "application",
"prefix": "foo",
"schematics": {},
"architect": {
"build": {
"builder": "./tools/builders/foo:build",
"options": {}
},
"serve": {
"builder": "./tools/builders/foo:serve",
"options": {}
}
}
},
when i run nx serve foo I want it to automatically call nx build foo --with-deps
what's the 'nx way' to do this?
To achieve what you want you could define a new target:
"architect": {
"build": {
"builder": "./tools/builders/foo:build",
"options": {}
},
"base-serve": {
"builder": "./tools/builders/foo:serve",
"options": {}
},
"serve": {
"builder": "#nrwl/workspace:run-commands",
"options": {
"commands": [
"nx build foo --with-deps",
"nx base-serve foo"
],
"parallel": false
}
}
}
Run commands allows you to invoke any number of commands or shell scripts, in parallel or in order. You can block on certain output to appear, etc..
In your case, it looks like you have a custom builder. So you could also extend the builder to invoke the build target before starting serving. If you use the same serve builder many times, that might be preferable.
Nx doesn't have an "aspect-oriented" way of decorating targets. The main reason why is that it sort of works for basic scenarios, but doesn't work for anything sophisticated. For instance, in you case, you may want to watch the files and rebuild all deps of the project. Like this: https://github.com/nrwl/nx-incremental-large-repo/blob/master/tools/scripts/watch.js
So may want to have a long running process performing rebuilds.
You should be able to do this with waitUntilTargets and a run command abstracting the build call
"build-foo": {
"executor": "#nrwl/workspace:run-commands",
"options": {
"commands": [
{
"command": "nx build foo --with-deps"
}
],
"readyWhen": "Put text here you will see when build is complete"
}
},
"serve": {
"executor": "#nrwl/node:execute",
"options": {
"buildTarget": "app:build",
"waitUntilTargets": ["app:build-foo"]
}
},
It's unclear to me if this is a new syntax as the setup you have for declaring the targets is not what I'm seeing now. I have these targets set up in the app project.json files.

Is it possible to open a new terminal inside VSCode from inside a script?

I want to start 3 servers from a single command.
I have package.json scripts like so:
"serve_auth": "cd dev/mock/auth && nodemon --exec babel-node ./server.js --presets #babel/env",
"serve_db": "cd dev/mock/db && nodemon --exec babel-node ./server.js --presets #babel/env",
"start": "react-scripts start",
"develop": "./launch_script.sh"
and I have a script launch_script.sh like so:
#!/bin/bash
( yarn serve_db ) & ( yarn serve_auth ) & ( yarn start )
but this opens them all in a single terminal window, and they end up tripping all over each other.
I know you can open new terminals from the VSCode GUI, but is it possible to open a new terminal from within one? Or to tell VSCode to open 3 terminals each with a separate command ?
I think this could be something for compound tasks
{
"version": "2.0.0",
"tasks": [
{
"label": "Client Build",
"command": "gulp",
"args": ["build"],
"options": {
"cwd": "${workspaceRoot}/client"
}
},
{
"label": "Server Build",
"command": "gulp",
"args": ["build"],
"options": {
"cwd": "${workspaceRoot}/server"
}
},
{
"label": "Build",
"dependsOn": ["Client Build", "Server Build"]
}
]
}
Compound tasks
You can also compose tasks out of simpler tasks with
the dependsOn property. For example, if you have a workspace with a
client and server folder and both contain a build script, you can
create a task that starts both build scripts in separate terminals. If
you list more than one task in the dependsOn property, they are
executed in parallel by default.
Also compound launch configurations may be interesting to you as it seems like your scripts are for starting a frontend and backend app.
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Server",
"program": "${workspaceFolder}/server.js",
"cwd": "${workspaceFolder}"
},
{
"type": "node",
"request": "launch",
"name": "Client",
"program": "${workspaceFolder}/client.js",
"cwd": "${workspaceFolder}"
}
],
"compounds": [
{
"name": "Server/Client",
"configurations": ["Server", "Client"]
}
]
}
Both are examples from the corresponding docs page but adjusting them to your scripts should be straightforward.

How to launch specific task from input variable in VS Code?

I'm trying to create a launch configuration where an environment variable is dynamically determined by a shell script. Even though a command variable can start a task via workbench.action.tasks.runTask, it doesn't seem to be possible to specify which task to run. Input variables seem to be a little more flexible in that regard, but I can't seem to get it to work. Here is what I got:
launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/main.go",
"env": {
"XXX": "${input:foo}"
},
"args": []
}
],
"inputs": [
{
"type": "command",
"id": "foo",
"command": "workbench.action.tasks.runTask",
"args": {
"args": "bar",
}
}
]
}
tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "bar",
"type": "shell",
"command": "find /dev -name 'myspecialdevice*' -maxdepth 1"
}
]
}
The issue is that the user is still queried for which task to run. I'm most insecure about the inputs.args section of the launch.json. I don't really know what the key value should be. Perhaps the implementation helps to figure this out?
This answer not really relates to make use of a vscode task, but your introducting sentence offers the motivation/what is intended to be solved.
I was faced to the same question and was wondering about vscode's input type:command. It offers a way to embed a (custom) vscode command -- which looks like a powerfull mechanism to embed a (custom) extension here. But I didn't found a builtin command that simply executes a shell script and returns it stdout. Thus an extension to capture the output of a shell command is imho required todo the trick.
E.g. https://marketplace.visualstudio.com/items?itemName=augustocdias.tasks-shell-input
provides a command shellCommand.execute doing exactly this.
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/main.go",
"env": {
"XXX": "${input:foo}"
},
"args": []
}
],
"inputs": [
{
"id": "foo",
"type": "command",
"command": "shellCommand.execute",
"args": {
"command": "find /dev -name 'myspecialdevice*' -maxdepth 1",
"cwd": "${workspaceFolder}",
/* To prevent user from selecting output (if there is just
one line printed by command), un-comment next line */
//"useSingleResult": true
}
}
]
}
(Inspired by https://stackoverflow.com/a/58930746/1903441)
In your launch.json, try replacing
"args": {
"args": "bar",
}
with
"args": "bar"
It seems that in vscode, you cannot pass a return or even an environment variable from task.json to launch.json. But you can use a file as a intermediate.
For example, in task.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "bar",
"type": "shell",
"command": "find /dev -name 'myspecialdevice*' -maxdepth 1 > ${workspaceFolder}/.vscode/temp"
}
]
}
In launch.json you set bar as preLaunchTask, and later access the file using inputs, like this:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/main.go",
"env": {
"XXX": "${input:foo}"
},
"args": [],
"preLaunchTask": "bar",
}
],
"inputs": [
{
"id": "foo",
"type": "command",
"command": "extension.commandvariable.file.content",
"args": {
"fileName": "${workspaceFolder}/.vscode/temp",
}
}
]
}
To get the comment working, just install this extension: https://marketplace.visualstudio.com/items?itemName=rioj7.command-variable

Chain pre-launch tasks in VS Code

Is it possible to invoke more than one pre-launch task using VS Code?
I try to restore packages then build then run but I can only get to configure build.
My launch.json:
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceRoot}/src/myProject/bin/Debug/netcoreapp1.0/myProject.dll",
"args": [],
"cwd": "${workspaceRoot}/src/myProject",
"stopAtEntry": false,
"externalConsole": false
},
My tasks.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "dotnet",
"isShellCommand": true,
"args": [],
"tasks": [
{
"taskName": "build",
"args": [
"./**/project.json"
],
"isBuildCommand": true,
"showOutput": "always",
"problemMatcher": "$msCompile"
}
]
}
so I tried to specify the dotnet restore command however it does not work.
I know this is long over due. However, I think I figured out the solution. Steps I did.
Create a workspace to include all the projects in the workspace.
Go to Run and Debug, and click on "Add Config (workspace)
the format is the following:
{
"folders": [
{
"path": "project1"
},
{
"path": "project2"
},
],
"launch": {
"version": "0.2.0",
"compounds": [{
"name": "Chain Stars",
"configurations": [
"ConfigurationName1",
"ConfigurationName2",
]
}]
}
}
ConfigurationName1, and ConfigurationName2 is the profile name you would like to put them in sequence to launch your website.
4. Save the profile. In this case. "Chain Stars" is going to show up in the profile name for you to run it. Let me know if you have any questions.