Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add description for typescript facet and build script #971

Merged
merged 12 commits into from
Sep 17, 2024
12 changes: 9 additions & 3 deletions node.js/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ Follow these steps to add TypeScript support:
npm i -g typescript ts-node
```

2. Add a _tsconfig.json_ file to your project.
2. Add a basic _tsconfig.json_ file to your project:

You need to provide a _tsconfig.json_ file in which you configure how you want
to use TypeScript. See the [official TypeScript documentation](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html) for more details.
```sh
cds add typescript
```

You can modify this configuration file to match your project setup. See the [official TypeScript documentation](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html) for more details.
Note that adding the `typescript` facet, [`cds-typer`](../tools/cds-typer) is also automatically added to your project.



Expand Down Expand Up @@ -113,7 +117,9 @@ Run your Jest tests with preset `ts-jest` without precompiling TypeScript files.
jest
```

## Building TypeScript Projects

A dedicated build task for `cds build` is provided as part of the `cds-typer` package. See [Integrate Into Your Multitarget Application](../tools/cds-typer#integrate-into-your-build-process) for more information on how to customize this task.

## TypeScript APIs in `@sap/cds`

Expand Down
29 changes: 14 additions & 15 deletions tools/cds-typer.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,6 @@ The CLI offers several parameters which you can list using the `--help` paramete

<!-- TODO: automatically pull command line options from cds-typer --help -->
```log

> @cap-js/cds-typer@0.22.0 cli
> node lib/cli.js --help

Expand Down Expand Up @@ -379,41 +378,41 @@ You can safely remove and recreate the types at any time.
We especially suggest deleting all generated types when switching between development branches to avoid unexpected behavior from lingering types.

## Integrate Into TypeScript Projects
The types emitted by `cds-typer` can be used in TypeScript projects as well! Depending on your project setup you may have to do some manual configuration.
The types emitted by `cds-typer` can be used in TypeScript projects as well! Depending on your project setup you may have to do some manual configuration for your local development setup.

1. Make sure the directory the types are generated into are part of your project's files. You will either have to add that folder to your `rootDirs` in your _tsconfig.json_ or make sure the types are generated into a directory that is already part of your `rootDir`.
2. Preferably run the project using `cds-ts`.
3. If you have to use `tsc`, for example for deployment, you have to touch up on the generated files. Assume your types are in _@cds-models_ below your project's root directory and your code is transpiled to _dist/_, you would use:
3. If you have to use `tsc`, you have to touch up on the generated files. Assume your types are in _@cds-models_ below your project's root directory and your code is transpiled to _dist/_, you would use:

```sh
tsc && cp -r @cds-models dist
```

## Integrate Into Your CI
As the generated types are build artifacts, we recommend to exclude them from your software versioning process. Still, as using `cds-typer` changes how you include your model in your service implementation, you need to include the emitted files when releasing your project or running tests in your continuous integration pipeline.
As the generated types are build artifacts, we recommend to exclude them from your software versioning process. Still, as using `cds-typer` changes how you include your model in your service implementation, you need to include the emitted files when running tests in your continuous integration pipeline.
You should therefore trigger `cds-typer` as part of your build process. One easy way to do so is to add a variation of the following command to your build script:

```sh
npx @cap-js/cds-typer "*" --outputDirectory @cds-models
```
Make sure to add the quotes around the asterisk so your shell environment does not expand the pattern.

## Integrate Into Your Multitarget Application
Similar to the integration in your CI, you need to add `cds-typer` to the build process of your MTA file as well.
## Integrate Into Your Build Process
Having `cds-typer` present as dependency provides a build task "`typescript`". If your project also depends on `typescript,` this build tasks is automatically included when you run `cds build`.
This build task will make some basic assumptions about the layout of your project. For example, it expects all source files to be contained within the root directory. If you find that the standard behavior does not match your project setup, you can customize this build step by providing a `tsconfig.cdsbuild.json` in the root directory of your project. We recommend the following basic setup for such a file:

::: code-group
```yaml [mta.yaml]
build-parameters:
before-all:
- builder: custom
commands:
- npx cds build --production
- npx @cap-js/cds-typer "*" --outputDirectory gen/srv/@cds-models
```json [tsconfig.cdsbuild.json]
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./gen/srv",
Copy link
Contributor

@LotharBender LotharBender Sep 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@daogrady - outDir seems to match the default value and might not need to be configured. Reading the
value from the cds configuration might even be more robust:
path.join(cds.env.build.target, cds.env.folders.srv)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 I don't think we can dynamically add values here, as this is the recommendation for the basic layout of a tsconfig.json, which is a static file.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought the typescript build task could calculate the default path if outDir isn't set.
Of course, this is only an option if outDir isn't used by the typescript compiler itself which seems to be the case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are correct about that! 🙂
But this part of the documentation is about a tsconfig.json the user could manually create to configure how the build task works, hence the preamble "We recommend the following basic setup for such a file:".

},
"exclude": ["app", "gen"]
}
```
:::

This integration into a custom build ensures that the types are generated into the `gen/srv` folder, so that they are present at runtime.

## About The Facet {#typer-facet}
Type generation can be added to your project as [facet](../tools/cds-cli#cds-add) via `cds add typer`.

Expand Down