Skip to content

Commit

Permalink
Read default output @cds-models from package.json (#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
daogrady authored Dec 2, 2024
1 parent fec5d44 commit bdecc94
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).

## Version 0.30.0 - TBD

## Version 0.29.1 - TBD
### Changed
- [breaking] when running cds-typer in a CAP project, the default for the `outputDirectory` option will be `./@cds-models` instead of `./`. This default takes the lowest precedence after setting it in the project's `cds.env`, or explicitly as CLI argument.

### Fixed
- cds-typer no longer ignores the selected `outputDirectory`, which would also cause an issue during build

Expand Down
3 changes: 3 additions & 0 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ const prepareParameters = (/** @type {any[]} */ argv) => {
}

const main = async (/** @type {any[]} */ argv) => {
// when calling from CLI within a CAP project, make sure plugins (this includes cds-typer)
// are initialised and have their default values injected into cds.env
await cds.plugins
const { positional } = prepareParameters(argv)
compileFromFile(positional)
}
Expand Down
27 changes: 19 additions & 8 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const fs = require('node:fs')
const path = require('node:path')
const cds = require('@sap/cds')
const { camelToSnake, getProjectTargetType } = require('./util')
const { LOG } = require('./logging')
Expand Down Expand Up @@ -32,21 +34,30 @@ const camelSnakeHybrid = target => {
return proxy
}
class Config {
static #defaults = {
propertiesOptional: true,
useEntitiesProxy: false,
inlineDeclarations: 'flat',
outputDirectory: '.',
targetModuleType: 'auto'
/** @type {Record<string, unknown>} */
static #defaults = {}
static get defaults () {
// these are the exact defaults that are used when cds-typer is loaded as cds plugin
// which will shove the values into cds.env. When executed standalone, without cds environment,
// we need to load them from package.json ourselves.
if (Object.keys(Config.#defaults).length) return Config.#defaults
try {
const packageJsonPath = path.resolve(__dirname, path.join('..', 'package.json'))
const pjson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
Config.#defaults = pjson.cds.typer
} catch (e) {
LOG.error(`Failed to load default configuration from package.json: ${e}`)
}
return Config.#defaults
}

values = undefined
proxy = undefined

init () {
this.values = {...Config.#defaults, ...(cds.env.typer ?? {})}
this.values = {...Config.defaults, ...(cds.env.typer ?? {})}
this.proxy = camelSnakeHybrid(this.values)
if (this.values.targetModuleType === 'auto') {
if (this.proxy.targetModuleType === 'auto') {
const type = getProjectTargetType(cds.root)
if (type) {
LOG.info(`automatically detected module type '${type}' in ${cds.root}`)
Expand Down
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@
]
},
"cds": {
"typer": {
"output_directory": "@cds-models",
"inline_declarations": "flat",
"target_module_type": "auto",
"properties_optional": true,
"use_entities_proxy": false
},
"schema": {
"buildTaskType": {
"name": "typescript",
Expand Down

0 comments on commit bdecc94

Please sign in to comment.