Comments (16)
Dear @Rmannn ,
ok, i finally had time to tackle this issue - and i properly solved it. For all, that may be stumbling upon such (or a similar) issue, here is what i did:
- i created a new nestjs application called
cli
, next to my normal application. - i added the
cli.ts
code (from your example) to the defaultmain.js
application - i added my commands to the
cli
nestjs application
then, in my build script (i.e., docker), i did something like this:
# build the normal nestjs application
RUN npx ng build api --prod
RUN npx ng build cli --prod
WORKDIR /
# first copy the regular api
COPY /path/to/api .
# and then add the cli files into the same directory
COPY /path/to/cli/main.js ./cli.js
COPY /path/to/cli/main.js.map ./cli.js.map
now, in my api
container, i can simply call
# you may first need to "connect" into your docker container by calling something like
# docker exec -it CONTAINERNAME /bin/sh
node cli.js COMMAND ARGS
I hope this helps ;)
Great work on this library, i really like it!
All the best
from nestjs-console.
Yeah, that was the thing, i would have tried tomorrow, haha.. I thought, however, that it may be better to have everything "in one place" (application) and not split it between two apps..
Thanks a lot for your time and help
from nestjs-console.
@tienne I am really not familiar with nx and I won't be, I think this lib is a real mess to understand...
That said, did you try linking the cli using ts-node by adding an architect section in angular.json or workspace.json ?
Something like this
"architect": {
"build": {
...
},
"console": {
"builder": "@nrwl/workspace:run-commands",
"options": {
"commands": [
{
"command": "ts-node -r tsconfig-paths/register apps/api/src/console.ts"
}
]
}
}
...
from nestjs-console.
for me, nx is more like a "development framework" and a way of structuring my application / libraries. I use nx to structure my apps, lint, build, test, ... all of my components / libraries..
my applications, however, are deployed using docker containers. the latter only contain the bundled application (with respective libraries), but none of the other code from nx. building the application, however, is done using nx.
This nestjs-console
package is used for several jobs:
- seeding my database
- cleanup jobs
- ...
Hope this helps
from nestjs-console.
Thanks again for the detailed explanation!
It helped me a lot. The whole world is busy with corona these days, so take care and have a good day~
from nestjs-console.
Can you share you tsconfig files please ?
Your cli file should be compiled and present. If it's not the case, maybe it's exclude in your. configuration.
from nestjs-console.
Note, that my application runs in a nrwl/nx
context (mono-repository).
My tsconfig.app.json
file (that is used within the ng serve APPNAME
command to build the application) looks like this:
// apps/api/tsconfig.app.json
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"target": "es2017",
"types": ["node"]
},
"exclude": ["**/*.spec.ts"],
"include": ["**/*.ts"]
}
This file, in turn, includes the ./tsconfig.json
file, that is also used for the specs and so on.. The latter looks like this:
// apps/api/tsconfig.json
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"types": ["node", "jest"],
"emitDecoratorMetadata": true
},
"include": ["**/*.ts"]
}
And this file, finally, includes the overall tsconfig.json
file within the mono-repository. This file is used across all applications within the repo (i.e, angular applications, nestjs applications, ionic applications and so on..)
This file in turn, looks as follows:
// tsconfig.json
{
"compileOnSave": false,
"compilerOptions": {
"rootDir": ".",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": true,
"target": "es2015",
"module": "esnext",
"typeRoots": ["node_modules/@types"],
"lib": ["es2017", "dom"],
"skipLibCheck": true,
"skipDefaultLibCheck": true,
"baseUrl": ".",
"paths": {
// ... custom paths for mono-repo builds
}
},
"exclude": ["node_modules", "tmp"]
}
The thing i found is, that the angular.json
file in the mono-repository sets the main.ts
file as "entry point" of the application. My cli.ts
file, in turn, is not referenced anywhere - so it may not be compiled and exported to the dist/apps/api
folder?!
All the best and thanks for your help
from nestjs-console.
The thing i found is, that the angular.json file in the mono-repository sets the main.ts file as "entry point" of the application. My cli.ts file, in turn, is not referenced anywhere - so it may not be compiled and exported to the dist/apps/api folder?!
I do not know how works this kind of builder.
You are building your api (nestjs) using angular. Angular execute the build of the api for you.
I am not familiar with the angular builder so i can't help you.
The console is an other entry point, sharing the same sources as the api and could be build at the same time.
You could try to play with your tsconfig apps/api/tsconfig.json to also include the cli.ts file.
What is strange is that "include": ["**/*.ts"]
tells the compiler to include your cli.ts file.
You could check if his path matches this blob.
from nestjs-console.
currently, the build compiles everything together into one big main.js
file that is stored within the dist/apps/api
directory. However, the cli.ts
file (which is a sibling to the main.ts
file) is not added there. Looks like the angular build only produces one large file to be executed / transferred to the client.
The blob should work, as the main.ts
file is properly picked up
from nestjs-console.
I went to the nrwl/nx repo and It seems it works like multi applications that use shared sources libraries.
You must see your backend app as 2 applications. 1 server application and 1 headless application (the cli).
I think you must create an other app like an other nest app that will share the same sources and use the cli.ts file as entry point.
nrwl/nx should build you cli.js file application and should use shared libraries with your nest app.
from nestjs-console.
We can't do it without modifying your main.ts file. The instance of the app is not the same as the the cli. The cli is headless (NestApplicationContext) and you app is a server (NestApplication)
It seems difficult because we can't add an extra flag to know which context to use, starting the server or boot the cli
However you point a real use case and I will check how we could merge it.
from nestjs-console.
@johannesschobel
Hello
Could you tell me how you wrote and wrote the npm script to run the cli application in the development environment?
from nestjs-console.
Dear @tienne ,
thanks for your question. Basically, i did almost the same in the development environment. My "workflow" in the dev env looks like this:
- start the API with
ng serve api
- build the CLI with
ng build cli
- if the API runs in a docker container, simply
docker exec -it ...
into the container so you have the shell of the API available. - run
node dist/apps/cli/main.js COMMAND ARGS
Dont forget to rebuild the cli (ng build cli
) if you change something on your commands!
Hope this helps
from nestjs-console.
@johannesschobel
Thanks to you, I know what to do!
I want to ask you another question, are you using nx as an actual product?
What is nestjs-console used for?
from nestjs-console.
Glad I could help.. keep it up
from nestjs-console.
What works for me is by adding it as a another project in my nest-cli.json
file like below:
"console": {
"type": "application",
"root": "apps/api",
"entryFile": "console",
"sourceRoot": "apps/api/src",
"compilerOptions": {
"tsConfigPath": "apps/api/tsconfig.app.json"
}
},
Then i just build it with npm run build console
, just make sure you dont that prebuild
script that removes everything (including your main
script) in your dist
folder.
from nestjs-console.
Related Issues (20)
- Examples doesn't work? HOT 2
- Documentation update for using opts HOT 5
- Support in nest v8 HOT 3
- _actionResults is undefined HOT 1
- Unable to add a global option HOT 2
- Unable to register commands in nestjs-console HOT 4
- CLI will _always_ throw an error even with empty promise handler HOT 3
- Can't read CLI options to configure the database HOT 3
- TypeError: parent.addCommand is not a function HOT 5
- Task Not Exiting HOT 2
- Support nestjs@9 HOT 2
- use enableShutdownHooks with nest-console
- ERROR [ExceptionHandler] undefined HOT 1
- Process.env issue HOT 3
- How to pass array of strings? HOT 1
- Example is slow? HOT 2
- Support for NestJs v10 HOT 2
- Module '"nestjs-console"' has no exported member 'createSpinner'. HOT 5
- What does this project do differently compared to nest-commander ? HOT 2
- Unknown createSpinner mention in wiki
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nestjs-console.