Make ng serve on Angular 12 loads environment.ts instead of environment.prod.ts? - build

I read that Angular 12 changed some default configurations.
Now I'm not able to load environment.ts when starting the app with ng serve; environment.prod.ts is called instead.
I tried to load environment.ts typing ng serve --configuration=development, but environment.prod.ts is called again.
I edited the development property of angular.json this way, by adding the fileReplacements property and it works, but this seems a workaround to me and not the right solution:
"development": {
"buildOptimizer": false,
"optimization": false,
"vendorChunk": true,
"extractLicenses": false,
"sourceMap": true,
"namedChunks": true,
"fileReplacements": [
{
"replace": "src/environments/environment.prod.ts",
"with": "src/environments/environment.ts"
}
]
}
Is there a better solution to start a development environment when running ng serve?

Related

karma config fails to add external project or file in the current project

I have added unit tests in some frontend projects using karma. I have multiple projects in my Git folder. If I run them individually, they work fine. However if there is a dependency of one project in another project it fails to include it. (failed to load JavaScript resource:)
If I run the tests using the html file directly, it runs the tests normally and even loads the external projects without any error. following are my resource roots in my unitTest.qunit.html file:
data-sap-ui-resourceroots='{
"x.y.projectmain": "../../",
"test.unit": "./",
"x.y.project2": "../../../../project2/WebContent"
}'
If I try to include the project same way in my Karma.conf.js it gives an error:
"Failed to resolve dependencies of 'x/y/projectmain/test/unit/AllTests.js' -> 'x/y/projectmain/test/unit/myUnitTest.js' -> 'x.y.project2/util/myfile.js': failed to load 'x.y.project2/util/myfile.js' from ./../../project2/WebContent/util/myfile.js: script load error"
Following are some of my Karma.conf.js settings:
ui5: {
type: "library",
paths: {
src: "projectmain/WebContent",
test: "projectmain/WebContent/test"
},
url: "https://openui5.hana.ondemand.com",
mode: "script",
config: {
async: true,
bindingSyntax: "complex",
compatVersion: "edge",
resourceRoots: {
"x.y.projectmain": "./base/projectmain/WebContent",
// "x.y.project2": path.resolve('../project2/WebContent')
"x.y.project2": "./../../projet2/WebContent"
// "x.y.project2": "./base/projectmain/WebContent/test/resources/project2/WebContent"
// "x.y.project2.util": "./base/project2/WebContent/util"
}
}
,
tests: [
"x.y.projectmain/test/unit/AllTests"
]
},
files: [
'Utils.js',
{ pattern: "../public/Project2/WebContent/utils/myfile.js", included: false, served: true, watched: false, nocache: true },
{ pattern: '../Project2/WebContent/**/*', watched: true, served: true, included: false }
],
// proxies: {
// '/project2/': path.resolve('../../project2/WebContent')
// },
proxies: {
'/x.y.project2/': '/absolute/' + path.resolve('../project2/WebContent'),
'/myfile.js/': '../public/project2/WebContent/util/myfile.js'
},
I have tried many things here. It even refers to the exact file in that external project but it just cant load the file. If I try to load the file manually in the browser it opens fine. But with Karma it gives an error.
My ultimate goal is to add one project as a dependency inside another project. I did check it by copying the whole WebContent folder from Project 2 inside the 'ProjectMain/WebContent/test/Resources/' directory It does work, but that's not appropriate way to include it.
There must be some way where we can register or include one project in another either as a resource root or proxies.

django-redis persisting json data

I have a small django site which controls an anstronomy dome and house automation. On start up the project loads 3 json files: relays, conditions and homeautomation. To avoid constant reading and writing to the Pi4's ssd I load the json files into REDIS (on start up in apps, see below). I already have REDIS running in a docker as the project uses celery.
My problem is that within a few minutes of loading the json into REDIS it clears the data out of cache.
I load the json file in the form of a dictionary (dict) in apps
cache.set("REDIS_ashtreeautomation_dict", dict, timeout=None)
and set
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://redis:6379",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"SERIALIZER": "django_redis.serializers.json.JSONSerializer",
"TIMEOUT": None
}
}
}
I don't need the data to persist if the dockers go down and I don't need db functions. Caching these files is ideal but I need them to 'stay alive' for the lifetime of the server.
Thank you.
Thank you Kevin.
Moving TIMEOUT solved the issue.
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://redis:6379",
"TIMEOUT": None,
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"SERIALIZER": "django_redis.serializers.json.JSONSerializer",
}
}
}
I am going to include some code to catch the long term REDIS 'eviction' policies (i.e. reload the json data). I don't want to delve into the REDIS docker.
Thanks
Ian

how to configure angular to work remotely with django APIs?

I am running a web application, front-end with angular and back-end with django. the thing is: These two frameworks are not running on the same server. how can I configure angular to work remotely with APIs? (I have tested the APIs, and they are just fine)
Check setup proxy for your project from Proxying to a backend server
Basically you need to create a proxy.conf.json file and have settings like:
{
"/api": {
"target": "http://localhost:3000",
"secure": false
}
}
Then you can define your backend hostname, port and available APIs and other settings.
OK, after hours of debugging I finally found it.
FIRST Create a file named proxy.conf.json in /src folder and fill it with this json:
{
"/api": {
"target": "http://test.com/",
"secure": false,
"changeOrigin": true,
"logLevel": "info"
}
}
This line is ESSENTIAL:
"changeOrigin": true,
THEN Edit the angular.json file.In the projects section, find architect and append this line to optionssection:"proxyConfig":"src/proxy.conf.json". So it should look like this:
.
.
.
"options": {
"browserTarget": "some-name:build",
"proxyConfig": "src/proxy.conf.json"
},
.
.
.
NOTE1 Trailing comma is not allowed in JSON.
NOTE2 Loglevel gives you more information.
NOTE3 Thanks to Haifeng for his guide.

How to debug dotnet test in VS Code?

This article describes setting the VS Code settings to point the debugging target at the build output of the unit test project. I have therefore set mine like this:
{
"explorer.confirmDragAndDrop": false,
"git.allowForcePush": true,
"git.autofetch": true,
"window.zoomLevel": 0,
"csharp.unitTestDebuggingOptions": {
"sourceFileMap": {
"C:\\git\\MsTester\\bin\\Debug\\netcoreapp2.1": "C:\\git\\MsTester\\bin\\Debug\\netcoreapp2.1"
}
},
"files.autoSave": "afterDelay",
"files.exclude": {
"**/bin": true,
"**/node_modules": true,
"**/obj": true
},
"csharpfixformat.style.spaces.insideEmptyBraces": false,
"csharpfixformat.style.braces.allowInlines": false,
"csharpfixformat.style.spaces.beforeParenthesis": false,
"csharpfixformat.style.spaces.afterParenthesis": false,
"csharp.format.enable": false,
"extensions.ignoreRecommendations": true
}
However, I am not sure how to setup the launch.json to kick off the dotnet test so that it can attach the debugger.
This is what I've got currently:
{
"version": "0.2.0",
"configurations": [
{
"name": "MsTester",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/MsTester/bin/Debug/netcoreapp2.1/MsTester.dll",
"windows": {
"args": [
"--filter",
"TestCategory=lbshell",
"--logger",
"trx",
"--results-directory",
".\\TestResults",
"--settings",
".\\Features\\runsettings.xml"
],
},
"cwd": "${workspaceFolder}/MsTester",
"console": "internalConsole",
"stopAtEntry": false,
"internalConsoleOptions": "openOnSessionStart"
},
]
}
Is there an option to tell VS Code that it needs to execute dotnet test instead of dotnet run?
I was hoping this page would indicate how to do that, but it does not.
All credit is due to #Lance U. Matthews as he posted this as a comment to an answer to the question: How does one debug an MSTest in VSCode? . I am reposting this as a proper answer because he hasn't done so in a year.
The following answer has been tested on VSCode v1.62.3 and dotnet tools v5.0.403.
Assuming you have a .NET solution with a structure like this:
mysolution.sln
tests\
|---> tests.csproj
|---> UnitTest1.cs
.vscode\
|---> launch.json
|---> tasks.json
Copy the following into the tasks.json file:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": ".NET Core Test with debugger",
"type": "process",
"isBackground": true,
"command": "dotnet",
"args": [ "test" ],
"options":
{
"cwd": "${workspaceFolder}/tests",
"env":
{
"VSTEST_HOST_DEBUG": "1"
},
},
"group": "test", "presentation":
{
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared"
},
"problemMatcher": []
},
]
}
And this into the launch.json file:
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
}
Now set a breakpoint anywhere in your UnitTest1.cs file and run .NET Core Test with debugger task (Terminal->Run task...).
The Test Execution Command Line Tool should launch and at the bottom of your terminal should be something like:
Your Process Id will certainly be different but that is expected behavior.
Now you need to switch to "Run and Debug" tab (ctrl+shift+d) and select ".NET Core Attach" (it should be in the upper left part of the VSCode).
Provide the process id that appeared previously in the terminal:
Now click F5 once and you should reach one of your breakpoints.
This may not answer solve your problem of passing the arguments entirely, but may solve the problem of passing runsettings parameters to the code being debugged. Using this method, I was able to pass the contents of runsettings to my code when running the debugger (i.e. when I click "Debug Test" above the method):
What I ended up doing is this:
First, I wrote a bit of code that will read from the settings file (via TestContext) OR from the user environment if the key was not available in the settings file, i.e.:
[ClassInitialize]
public static void TestClassInitialize(TestContext context)
{
v = Settings.GetSettingValueFromContextOrEnvironment("v", context);
}
Here is the implementation of GetSettingValueFromContextOrEnvironment:
/// <summary>
/// Attempts to read a setting value from the context (populated through runsettings). If the
/// key is not present in the context, the value will be read from the user environment variable
/// instead.
///
/// This allows running the unit test code in VSCode debugger that currently doesn't seem to allow
/// passing runsettings file to the DLL.
/// </summary>
/// <param name="name"></param>
/// <param name="context"></param>
/// <returns></returns>
public static String GetSettingValueFromContextOrEnvironment(string name, TestContext context){
if (context.Properties.ContainsKey(name)){
return context.Properties[name].ToString();
} else {
String envVar = Environment.GetEnvironmentVariable(name, System.EnvironmentVariableTarget.User);
if (envVar == null){
throw new Exception(String.Format("Environment variable '{0}' was not available neither in the context file nor in the environment.", name));
} else {
return envVar;
}
}
}
I then wrote a Powershell that will read my settings file to parse parameters and set them as user environment variables, like this:
<# This script updates user environment variables with parameters stored in *.runsettings file that is provided to it as the only argument on the command line.
Example usage:
.\set_settings_env.ps1 local.runsettings
#>
param (
[Parameter(Mandatory=$true)][string]$settings_file
)
[xml]$xml = Get-Content $settings_file
foreach( $parameter in $xml.RunSettings.TestRunParameters.Parameter)
{
write-host("Setting environment variable: " + $parameter.name)
[Environment]::SetEnvironmentVariable($parameter.name, $parameter.value, "User")
}
So now I am able to both run the code from command line with a specific runsettings file and debug by running the script to set my environment variables.
Perhaps this is more what you are looking for:
How does one debug an MSTest in VSCode?
(also check the comment).
So by using "VSTEST_HOST_DEBUG": "1" you should be able to attach a debug process, similar to how one can do it in Java.

Sencha touch 2 SDK compress JS file

there is a way to specify in app.json, where I include js files, not to minify 1 js file, somethig like
"js": [
{
"path": "sdk/sencha-touch.js"
},
{
"path": "sdk/myjs.js",
"compress": false
},
...
"compress": false -- ofc doesn work , there is something to specify not to compress or minify that js file ?
Thanks for any help!
As fas as I found there is not why to specify not to compress, the way how I do an workaround is:
creating in "resources" folder a "js" folder and dropped my js file in that folder
Updated the app.json file (not to loose the "js" folder when we build production app) like this :
"resources": [
"resources/js",
]
Include the file in index html as a regular script