Hi in atom we got builtin node and npm binary without installing in the OS that can be call from extension.
This is the path of the default binary in atom
/usr/share/atom/resources/app/apm/bin/npm
/usr/share/atom/resources/app/apm/bin/npx
Does VSCode provide it ?
We have a strict rule not to cluttering our development computer with any unnecessary binary ( e.g we use docker for php, using virtualenv for python and node related project )
In our case for our web development ( php and python mainly ) we use babel to transpile all our file.js on save, in atom we use language-babel extension that allow us to transpile using atom node but with project node_modules package. So our babel dependencies is install inside a project and not clutter the OS, and doesn't disturb other project.
On VSCode babel extension I check they don't have this capabilities. Any info on this or is this not do able in VSCode ?
You can install and run VS Code without Node, which does suggest it has Node baked in. However, at this time (June 2022) you can't debug or run JavaScript on Node until you install Node separately.
Separate installation decouples your editor dependencies from your code dependencies.
Your code can debug/run on a version of Node that differs from the version VS Code uses.
Those not using Node are not obliged to install the CLI toolchain for it.
VS Code supports a multitude of languages. Baking in their toolchains would make it enormous.
This is the closest setup in vscode / codium for non nodejs project
OS only require to install nodejs for running babel ( npm , npx not require )
All node package inside project is install using docker
example using Makefile
SHELL := /bin/bash
THIS_FILE := $(lastword $(MAKEFILE_LIST))
PROJECT_NAME := "$$(basename `pwd` | cut -d. -f1 )"
yarn:
docker run --rm -it \
-v $$(pwd)/${PROJECT_NAME}:/srv/${PROJECT_NAME} \
-w /srv/${PROJECT_NAME} \
-e NODE_ENV=development \
--user $$(id -u):$$(id -g) \
node:lts-slim yarn $(filter-out $#,$(MAKECMDGOALS))
init and install babel inside project
make yarn init
make yarn -- add -D #babel/cli
make yarn -- add -D #babel/core
make yarn -- add -D #babel/preset-react
make yarn -- add -D babel-preset-minify
Create ${workspaceRoot}/.babelrc
{
"comments" : false,
"sourceMaps": true,
"only": [
"./asset/js/src"
],
"presets": [
"#babel/preset-react",
["minify", {
"mangle" : true,
"builtIns": false,
"keepClassName" : true
}]
]
}
Create ${workspaceRoot}/.vscode/tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label" : "Babel Watch",
"type": "shell",
"group": "none",
"command" : "${workspaceRoot}/node_modules/.bin/babel",
"args" : [
"${workspaceRoot}/asset/js/src/",
"--config-file=${workspaceRoot}/.babelrc",
"--out-dir=${workspaceRoot}/asset/js/dist/",
"--watch"
],
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared",
"showReuseMessage": true,
"clear": false
},
"runOptions": {
"runOn": "folderOpen"
}
}
]
}
Then Enable Automatic tasks in folder to run on folder open
Press F1
Search Tasks: Manage Automatic Task in Folder
Select Tasks: Manage Automatic Task in Folder
Select Allow Automatic Tasks in Folder
On next reopen folder / project babel will watch the folder specify in tasks
It looks like there is no way to allow automatic task in folder from .vscode/settings.json so need enable this on every project once in their own dev computer
Related
I have a project which has a build step, however, I need to make sure that the file firebase.config.json exists before running the build command.
With that, I have two NPM scripts:
// package.json
{
...,
"nx": {
"targets": {
"prepare": {
"outputs": ["firebase.config.json"]
},
"build": {
"outputs": ["dist"],
"dependsOn": [
{
"target": "prepare",
"projects": "self"
}
]
}
}
},
"scripts": {
"prepare": "firebase apps:sdkconfig web $FIREBASE_APP_ID_SHOP --json | jq .result.sdkConfig > firebase.config.json",
"build": "VITE_FIREBASE_CONFIG=$(cat ./firebase.config.json) vite build",
},
...
}
So with the above, every time I run nx build app it will first run prepare and build the firebase.config.json file.
However, every time I make a change to any of the source files inside my project, prepare re-runs even though the firebase.config.json is already present.
Is it possible for nx to only run a target if the file declared under outputs is not present?
If you are in a bash environment you can modify your prepare script to be the following (note the original command has been shortened with ellipses for readability).
// package.json
{
"scripts":{
"prepare": "CONFIG=firebase.config.json; [ -f \"$CONFIG\" ] || firebase apps:sdkconfig ... | jq ... > \"$CONFIG\""
}
}
The above prepare script will still run, but it should not spend any time reproducing the configuration file if it already exists.
CONFIG=firebase.config.json is just putting our file in a bash environment variable so we can use it in multiple places (helps prevent typos). [ -f "$CONFIG" ] will return true if $CONFIG holds a filename which corresponds to an existing file. If it returns true, it will short-circuit the || (OR) command.
If you want further verification of this technique, you can test this concept at the terminal with the command [ -f somefile.txt ] || echo "File does not exist". If somefile.txt does not exist, then the echo will run. If the file does exist, then the echo will not run.
A slightly-related side-note: while you clearly can do this all in the package.json configuration, if your nx workspace is going to grow to include other libraries or applications, I highly recommend splitting up all your workspace configuration into the default nx configuration files: nx.json, workspace.json, and the per-project project.json files for the sake of readability/maintainability.
Best of luck!
I have a situation where I’m trying to build my angular application with production config and deploy to multiple environments, say, ng build --configuration=production
The work flow here is when I build using the above command (ng build --configuration=production), the environment.ts file gets replaced with environment.prod.ts
The configurations I have in environment.prod.ts is as follows,
export const environment = {
production: true,
environment: 'Production',
_webApiHost: 'prodsomename.company.com/api/',
};
The configurations I have in environmrnt.test.ts is as follows,
export const environment = {
production: true,
environment: 'Test',
_webApiHost: 'testsomename.company.com/api/',
};
The setting I have on angular.json file is as follows,
"configurations": {
"production": {
"fileReplacements": [ {
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
} ],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [ {
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
} ]
},
"test": {
"fileReplacements": [ {
"replace": "src/assets/configs/environment.ts",
"with": "src/assets/configs/environment.test.ts"
} ],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [ {
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
} ]
}
}
If I build the solution for every environment separately and deploy to appropriate environment as below figure,
it works like charm, which mean the,
testApp communicates to _webApiHost: testsomename.company.com/api/ and
prodApp communicates to _webApiHost: prodsomename.company.com/api/
In the above case the artifact which is tested by QA is different from the artifact which is deployed to production, which is not the ideal way of pushing the code to production.
But my concern is I want to build the app only once and deploy it to multiple environments, where each environment will communicate to appropriate api, like below figure,
When I build it using the command ng build --configuration=production, the environment.ts file will have production configurations,
export const environment = {
production: true,
environment: 'Production',
_webApiHost: 'prodsomename.company.com/api/',
};
So if that artifact is deployed to test environment,
the testApp is trying to communicate with _webApiHost: 'prodsomename.company.com/api/, which is not right.
Here is the Azure DevOps build pipeline powershell script I use to build the solution.
Set-Location "$(Build.Repository.LocalPath)\Buffini.Web.UI\Angular"
Write-Host 'Angular Install Starting'
npm install -g #angular/cli#8.0.6 -Verbose
Write-Host 'Angular Install Finished'
Write-Host 'NPM Install Starting'
npm install -Verbose
Write-Host 'NPM Install Finished'
Write-Host 'NPM Update Starting'
npm update -Verbose
Write-Host 'NPM Update Finished'
Write-Host 'NPM Audit Starting'
npm audit fix -Verbose
Write-Host 'NPM Audit Finished'
Write-Host 'Angular Build Starting'
ng build --configuration=production --deleteOutputPath=true
Write-Host 'Angular Build Finished'
I have tried searching for a solution online but I couldn’t find any.
Please help me in resolving the issue. I’ll highly appreciate your time and help on this. Thanks in advance.
To replace app configurations in runtime time. You need to create config.json file which contains the dynamic configurations (eg. _webApiHost). You can check the example code in this blog to fetch the config.json.
In the you pipeline, you can add extension tasks to replace the config.json contents before deploying to different environment(eg. test, production).
In this way you only need to build your angular app once, and only need replace the config.json contents accordingly before deploying to different environment.
The available extensions you can check out. Magic Chunks task, or
RegEx Match & Replace Task. You can check this thread for the example to use these tasks.
I won't pretend this an answer, necessarily, but it's long enough of a thought to not fit in the comments area. Perhaps you will find it helpful. (Full disclosure: I'm not using Azure, but rather GitLab. So there would be some translation necessary, regardless, if you find this approach of use.)
Anyway, I was asking the same question a while back. After some digging, I found this link helpful
Using that guidance, I did the following:
First, I do a basic docker build. In that build I have various environment files "ready for the asking" in a folder. The configuration file that actually drives the app is at the root.
I then do another docker build, this one whose sole purpose is to take the first build and give it the desired configuration. (I do it this way because the first build is slow, but I'd like to push to production without rebuilding.)
Next I do the environment build that I want.
For staging, for example: In my GitLab build CI/CD pipeline yaml, I have a line like this....
docker build -t xxxxx --build-arg SERVE_CONFIGURATION=staging -f [A-Docker-File] .
The docker file is the same for all environments, but based upon the passed in argument, this docker build pulls a different file and slams it into the driver's seat. Since there are no secrets in an Angular deployment, it doesn't matter that there are extra (unused) configuration files lurking in the folder structure (though if one were motivated, one could easily delete them.)
Anyway, inside that 2nd docker build, I have...
...
FROM registry.gitlab.com/xxxxxxxxxxxx/compiled as default-config
FROM registry.gitlab.com/xxxxxxxxxxxx/compiled as final-config
COPY --from=default-config /usr/share/nginx/html/environments/environment.$SERVE_CONFIGURATION.js /usr/share/nginx/html/environment.js
So this docker image is nothing more than a build off the prior image, but with the desired environment file in its proper place.
Anyway, I've probably left out some details, but I'm not sure this will help you and will stop here.
I have my package.json and task:start script
"scripts": {
"task:start": "rm -rf bin && npm run prepare_env && npm run build",
"prepare_env": "bash ../scripts/prepare-node-modules.sh",
"build": "tsc --watch"
},
I wanna have build command to prepare my whole environment in vscode tasks.
I am trying new 2.0.0 version of task for vscode, but npm task doesn't work.
"version": "2.0.0",
"tasks": [
{
"type": "npm",
"script": "task:start",
"group": {
"kind": "build",
"isDefault": true
}
}
]
The output is:
> Executing task: npm run task:start <
Usage: npm <command>
where <command> is one of:
access, adduser, bin, bugs, c, cache, completion, config,
ddp, dedupe, deprecate, dist-tag, docs, edit, explore, get,
help, help-search, i, init, install, install-test, it, link,
list, ln, login, logout, ls, outdated, owner, pack, ping,
prefix, prune, publish, rb, rebuild, repo, restart, root,
run, run-script, s, se, search, set, shrinkwrap, star,
stars, start, stop, t, tag, team, test, tst, un, uninstall,
unpublish, unstar, up, update, v, version, view, whoami
npm <cmd> -h quick help on <cmd>
npm -l display full usage info
npm help <term> search for help on <term>
npm help npm involved overview
Specify configs in the ini-formatted file:
C:\Users\Dariusz\.npmrc
or on the command line via: npm <command> --key value
Config info can be viewed via: npm help config
npm#3.10.10 C:\Program Files\nodejs\node_modules\npm
Which doesn't make any sense. My previus configuration works well...
"version": "0.1.0",
"command": "npm",
"isShellCommand": true,
"showOutput": "always",
"suppressTaskName": true,
"tasks": [
{
"taskName": "install",
"args": [
"run",
"task:start"
],
"isBuildCommand": true
}
]
but I wanna use new one. My configuration:
vscode 1.15.1
windows 10
When running this command:
kubectl apply -f tenten
I get this error:
unable to decode "tenten\.angular-cli.json": Object 'Kind' is missing in '{
"project": {
"$schema": "./node_modules/#angular/cli/lib/config/schema.json",
"name": "tenten"
},
"apps": [{
"root": "src/main/webapp/",
"outDir": "target/www/app",
"assets": [
"content",
"favicon.ico"
],
"index": "index.html",
"main": "app/app.main.ts",
"polyfills": "app/polyfills.ts",
"test": "",
"tsconfig": "../../../tsconfig.json",
"prefix": "jhi",
"mobile": false,
"styles": [
"content/scss/vendor.scss",
"content/scss/global.scss"
],
"scripts": []
}],
It looks like you're running this from the parent directory of your applications. You should 1) create a directory that's parallel to your applications and 2) run yo jhipster:kubernetes in it. Then run kubectl apply -f tenten in that directory after you've built and pushed your docker images. For example, here's the output when I run it from the kubernetes directory in my jhipster-microservices-example project.
± yo jhipster:kubernetes
_-----_
| | ╭──────────────────────────────────────────╮
|--(o)--| │ Update available: 2.0.0 (current: 1.8.5) │
`---------´ │ Run npm install -g yo to update. │
( _´U`_ ) ╰──────────────────────────────────────────╯
/___A___\ /
| ~ |
__'.___.'__
´ ` |° ´ Y `
⎈ [BETA] Welcome to the JHipster Kubernetes Generator ⎈
Files will be generated in folder: /Users/mraible/dev/jhipster-microservices-example/kubernetes
WARNING! kubectl 1.2 or later is not installed on your computer.
Make sure you have Kubernetes installed. Read http://kubernetes.io/docs/getting-started-guides/binary_release/
Found .yo-rc.json config file...
? Which *type* of application would you like to deploy? Microservice application
? Enter the root directory where your gateway(s) and microservices are located ../
2 applications found at /Users/mraible/dev/jhipster-microservices-example/
? Which applications do you want to include in your configuration? (Press <space> to select, <a> to toggle all, <i> to i
nverse selection)blog, store
JHipster registry detected as the service discovery and configuration provider used by your apps
? Enter the admin password used to secure the JHipster Registry admin
? What should we use for the Kubernetes namespace? default
? What should we use for the base Docker repository name? mraible
? What command should we use for push Docker image to repository? docker push
Checking Docker images in applications' directories...
ls: no such file or directory: /Users/mraible/dev/jhipster-microservices-example/blog/target/docker/blog-*.war
identical blog/blog-deployment.yml
identical blog/blog-service.yml
identical blog/blog-postgresql.yml
identical blog/blog-elasticsearch.yml
identical store/store-deployment.yml
identical store/store-service.yml
identical store/store-mongodb.yml
conflict registry/jhipster-registry.yml
? Overwrite registry/jhipster-registry.yml? overwrite this and all others
force registry/jhipster-registry.yml
force registry/application-configmap.yml
WARNING! Kubernetes configuration generated with missing images!
To generate Docker image, please run:
./mvnw package -Pprod docker:build in /Users/mraible/dev/jhipster-microservices-example/blog
WARNING! You will need to push your image to a registry. If you have not done so, use the following commands to tag and push the images:
docker image tag blog mraible/blog
docker push mraible/blog
docker image tag store mraible/store
docker push mraible/store
You can deploy all your apps by running:
kubectl apply -f registry
kubectl apply -f blog
kubectl apply -f store
Use these commands to find your application's IP addresses:
kubectl get svc blog
See the end of my blog post Develop and Deploy Microservices with JHipster for more information.
I'm trying to use coffeeify with budo so I do not have to add the extension to my require statements. I have tried passing these commands through budo's browserify options
budo src/app.coffee --live --serve bundle.js -- -t coffeeify --extension=".coffee"
budo src/app.coffee --live --serve bundle.js -- -t [coffeeify --extension=".coffee"]
I also tried inserting the browserify transform into my package.json
"browserify: {
"transform": ["coffeeify", {"extension": ".coffee"}]
}
Here is something that works for me (took me forever to figure it out, the hard part being getting watchify to work with coffeescript). Everything is in the package.yaml. Invoke npm start from your top folder and it will do the trick. npm puts all the locally installed node binaries in your PATH for you (they normally live under node_modules/.bin).
{
"name": "my-package",
"version": "1.0.0",
"private": true,
"scripts": {
"start": "(cd src; budo app.coffee:bundle.js --dir . --live --verbose -- --extension=.coffee | garnish)"
},
"browserify": {
"extension": [ ".coffee" ],
"transform": [ ["coffeeify"], ["brfs"] ]
},
"devDependencies": {
"brfs": "1.4.1",
"browserify": "11.1.0",
"budo": "^5.1.5",
"coffee-script": "latest",
"coffeeify": "^1.1.0",
"garnish": "^3.2.1",
"watchify": "3.4.0"
}
}
I have my source code under the src folder, and a file named app.coffee which includes (or require in node.js terms) my whole application. I have an index.html in my src folder which reference the bundle.js through from an html script tag.
The command to start budo is inside my package.json. It does cd into my src folder first.
The trick is to specify some configuration in the browserify block: the extension .coffee needs to be present, and a list of transforms as well. I tried to have everything on the command line but never got it to work
After npm start is invoked, since I pass the --live argument to budo everything works like magic and edit/saves to my documents do trigger a browser reload/refresh.
To deploy or release you'll probably need another target to minify with uglify.js. I still have a script that does that manually in 2 steps, the first step calls browserify and the second step calls uglify.js explicitely.
As a remark, recent version of budo do the piping into garnish for you I've heard.
Another tip is to look at what the React folks are doing to transform their .jsx files, as it is in theory extremely close to what the coffeescript folks need to do. There seems to be a huge momentum around React so hopefully React people will have figured those build problems first.