Giter VIP home page Giter VIP logo

Comments (16)

johannesschobel avatar johannesschobel commented on May 29, 2024 2

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:

  1. i created a new nestjs application called cli, next to my normal application.
  2. i added the cli.ts code (from your example) to the default main.js application
  3. 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.

johannesschobel avatar johannesschobel commented on May 29, 2024 1

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.

Rmannn avatar Rmannn commented on May 29, 2024 1

@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.

johannesschobel avatar johannesschobel commented on May 29, 2024 1

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.

tienne avatar tienne commented on May 29, 2024 1

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.

Rmannn avatar Rmannn commented on May 29, 2024

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.

johannesschobel avatar johannesschobel commented on May 29, 2024

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.

Rmannn avatar Rmannn commented on May 29, 2024

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.

johannesschobel avatar johannesschobel commented on May 29, 2024

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.

Rmannn avatar Rmannn commented on May 29, 2024

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.

Rmannn avatar Rmannn commented on May 29, 2024

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.

tienne avatar tienne commented on May 29, 2024

@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.

johannesschobel avatar johannesschobel commented on May 29, 2024

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:

  1. start the API with ng serve api
  2. build the CLI with ng build cli
  3. if the API runs in a docker container, simply docker exec -it ... into the container so you have the shell of the API available.
  4. 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.

tienne avatar tienne commented on May 29, 2024

@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.

johannesschobel avatar johannesschobel commented on May 29, 2024

Glad I could help.. keep it up

from nestjs-console.

jovani-delivertrade avatar jovani-delivertrade commented on May 29, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.