VSCode Tasks, user input for npm script - visual-studio-code

I am trying to create a task for the npm script configured in package.json. So, here are the files:
package.json
....
scripts : {
"migrate": "node_modules/.bin/migrate-mongo",
"migrate:create": "npm run migrate create" // --> this command needs input (file name)
}
....
Now when I have to run the migrate:create command I have to go to shell and run like this:
$> npm run migrate:craete filename
And it works fine.
Now I want to create a task for the same. For that I did some research and taking the idea from:
https://stackoverflow.com/a/53846572/1227940 created the tasks.json as:
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "Create a db migration script.",
"command": "npm run migrate create ${input:migrationName}",
}
],
"inputs": [
{
"type": "promptString",
"id": "migrationName",
"description": "Name your migration script?",
"default": "create"
}
]
}
But somehow it is not giving me a text box to input anything and directly going inside and firing a command: npm run migrate:create which is failing since filename is missed.
Can anyone please shed some light, what I lacked and what I missed, please let me know?
Thanks in advance,
happy coding :)

So, finally I did some research and found the solution and here it is:
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "Create Migration Script",
"command": "npm run migrate:create ${input:migrationName}",
"problemMatcher": []
}
],
"inputs": [
{
"type": "promptString",
"id": "migrationName",
"description": "Name your migration script (prefix with create-, update-, delete-, insert-)?"
}
]
}
Here the command is the actual script I configured in pacakage.json as:
package.json
....
scripts : {
"migrate": "node_modules/.bin/migrate-mongo",
"migrate:create": "npm run migrate create",
}
....
In the command of tasks.json I mentioned the npm run script which is configured in package.json.
Thanks and happy coding :)

Related

Is it possible to specify user specific pre commands in VS Code?

We have a slew of folks doing development through the same GitLab repo. We are using VS Code tasks to execute internal commands. The main command is the same for everyone: internal_command on Windows and internalCommand on Linux.
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label" : "do it",
"type" : "shell",
"windows": {
"command": "internal_command"
},
"linux": {
"command": "internalCommand"
}
}
]
}
This works as expected.
Some users need/want to run a specific command before the main command. For example, one use wants to rename a file, another user wants to change an environment variable, etc...
We don't want to have multiple versions of .vscode/tasks.json cause that is a mess when pushing things to GitLab.
So I am wondering if there is a way to specify user specific tasks in the project's .vscode/tasks.json file?
You can with the help of the extension Command Variable it allows you to use the content of a file as a command in the terminal. The file can also contain Key-Value pairs or be a JSON file.
Say you store this userTask.txt or userTask.json file in the .vscode folder and add the file to the .gitignore file.
With the current version of the extension the file userTask.txt has to exist, I will add an option to supply alternative text in case the file does not exist. You can fill the file with a dummy command like echo No User Task
Set up your task.json like
{
"version": "2.0.0",
"tasks": [
{
"label" : "do it",
"type" : "shell",
"windows": {
"command": "internal_command"
},
"linux": {
"command": "internalCommand"
},
"dependsOrder": "sequence",
"dependsOn": ["userTask"]
},
{
"label" : "userTask",
"type" : "shell",
"command": "${input:getUserTask}"
}
],
"inputs": [
{
"id": "getUserTask",
"type": "command",
"command": "extension.commandvariable.file.content",
"args": {
"fileName": "${workspaceFolder}/.vscode/userTask.txt"
}
}
]
}

In flutter web is there a equivalent for reactjs package.json "prebuild"

We have a single package which is built for different sites, which needs some prebuild steps to be executed before actual build steps.
In flutter android, these steps are executed in "preBuild.dependsOn" (in android/app/build.gradle)
Edit (additional info):
MyFlutterMob/android/app/build.gradle
Following prebuild steps are run :
def option = System.getenv('APK_FOR')
setConfig()
{
if(option == "DEMO"){
copy{
from "../../DemoAppAsset/AppLogo.png"
into "../../assets/images"
}
}
else if(option == "PROD"){
copy{
from "../../ProdAppAsset/AppLogo.png"
into "../../assets/images"
}
}
}
preBuild.dependsOn setConfig
Is same possible in flutter web build (Not able to find gradle script for same )
If you are using VSCode, I think you can set a prebuild in launch.json, as is explained here
Basically you can setup your .vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Flutter",
"request": "launch",
"type": "dart",
}
]
}
then, you should create a file tasks.json in the same folder, and add a script that will do what you want (for example a npm script)
{
"version": "2.0.0",
"tasks": [
{
"type": "npm",
"script": "copyClientConfiguration",
"problemMatcher": []
}
]
}
and lastly you can add this task as preLaunchTask in launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Flutter",
"request": "launch",
"type": "dart",
"preLaunchTask": "npm: copyClientConfiguration"
}
]
}
Wrote shall scrip which takes environment name(PROD1/PROD2/UAT/... ) as input and copies the required "images to white label" /"theme colour list files"/"strings list configs" into respective folders before running "flutter build web". Although it restricts all release to happen from linux / maintain equivalent bat file for windows, broadly it works out.

VS Code tasks cannot be found by launch config when they are located in workspace config

In tradition, we put launch.json and tasks.json in .vscode folder in order to make debugger works. Meanwhile, VS Code also support developer to put these 2 configurations into workspace by setting them in *.vscode-workspace. Here are the steps to reproduce:
Create new js project by npm init with all default parameters.
Create new index.js with 1 line of code: console.log('done');
Create new ts-sample.code-workspace file within the same root folder, the content should be like following:
{
"launch": {
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/index.js",
"preLaunchTask": "nodeversion"
}
]
},
"tasks": {
"version": "2.0.0",
"tasks": [
{
"label": "nodeversion",
"type": "npm",
"script": "nodeversion",
}
]
},
"folders": [
{
"path": "."
}
]
}
Open the workspace
Open index.js and press F5
Error dialog prompted our with the following error message: Could not find the task 'nodeversion'
Open OUTPUT Panel and Error message is shown as following:
Error: The npm task detection didn't contribute a task for the following configuration:
{
"label": "nodeversion",
"type": "npm",
"script": "nodeversion"
}
The task will be ignored.
For easy demo please clone the sample project from https://github.com/mannok/WorkspaceLaunchTaskDemo
Is this a bug of VS Code or something I have missed?

Using a shell command as VSCode task variable value

I am trying to define a VSCode task in tasks.json that would adapt to the specific architecture where VSCode runs. To do this, I want to get the architecture as uname --m (e.g. "aarch64" or "amd64"). My goal is to interpolate the output of uname into an environment variable like so
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "cmake",
"args": [
"-DMYLIB_INCLUDE_DIR=$MYLIB/include",
"-DMYLIB_LIBRARY=$MYLIB/lib"
],
"options": {
"env": {
"MYLIB": "${workspaceFolder}/mylib/${command:get_arch}"
}
},
}
]
In my case, I will have architecture-specific versions of mylib under mylib/aarch64, mylib/amd64, etc.
My attempt so far as been to define a second get_arch task used in the environment definition of MYLIB, that simply runs uname.
{
"label": "get_arch",
"type": "shell",
"command": "uname --m"
}
Of course, this task is not a proper command and so it isn't detected by VSCode and my build task fails. I checked out the documentation on variable substition, but they don't mention if it's possible to substitute a shell command. I guess this would be possible from within an extension, but I want to keep things as simple as possible.
This extension provides a way to launch arbitrary shell commands as a VS Code command:
"tasks": [
{
"label": "test_arch",
"type": "shell",
"command": "echo",
"args": [
"${MYARCH}"
],
"options": {
"env": {
"MYARCH": "${input:get_arch}"
}
},
"problemMatcher": []
},
],
"inputs": [
{
"id": "get_arch",
"type": "command",
"command": "shellCommand.execute",
"args": {
"command": "uname -m"
}
}
]
One disadvantage I discovered is that you have to press Enter once more when the result of the command is prompted in picker. Aside of this, this is the straightest way to implement what you want, and yet it can be utilized in many similar situations.
Alternatively, you can add pickString input with arch picker or create an extension that adds only single command GetArch.
If you don't want to press enter every time, you can add the option useFirstResult: true in the args section.

Why can't I see any output when executing a windows batch file with VS code tasks

I'm trying VS code tasks for the first time, I have created a test.bat file with
echo hello
and this tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "test",
"type": "shell",
"command": "cmd /c c:\\test\\test.bat"
}
]
}
When running the task, It seems to execute it but why can't I see any output from echo (ie "hello") ?
Arguments aren't supposed to be posted in command, so you're task should look like this:
{
"command": "cmd",
"args": ["/c", "c:\\test\\test.bat]
}
However, since Microsoft added auto-detection, the following should work as well:
{
"type": "shell",
"command": ""c:\\test\\test.bat"
}
See the custom task documentation for details.