@cap-js/cds-typer 0.29.0 → 0.30.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -4,7 +4,16 @@ All notable changes to this project will be documented in this file.
4
4
  This project adheres to [Semantic Versioning](http://semver.org/).
5
5
  The format is based on [Keep a Changelog](http://keepachangelog.com/).
6
6
 
7
- ## Version 0.30.0 - TBD
7
+ ## Version 0.31.0 - TBD
8
+
9
+ ## Version 0.30.0 - 2024-12-02
10
+
11
+ ### Changed
12
+ - [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.
13
+
14
+ ### Fixed
15
+ - cds-typer no longer ignores the selected `outputDirectory`, which would also cause an issue during build
16
+
8
17
 
9
18
  ## Version 0.29.0 - 2024-11-20
10
19
  ### Added
package/lib/cli.js CHANGED
@@ -142,6 +142,7 @@ const addCLIParamsToConfig = params => {
142
142
  }
143
143
  }
144
144
 
145
+ // when changing/ adding anything in here, make sure to adjust scripts/write-cds-typer-schema.js accordingly!
145
146
  const flags = enrichFlagSchema({
146
147
  outputDirectory: {
147
148
  desc: 'Root directory to write the generated files to.',
@@ -258,6 +259,9 @@ const prepareParameters = (/** @type {any[]} */ argv) => {
258
259
  }
259
260
 
260
261
  const main = async (/** @type {any[]} */ argv) => {
262
+ // when calling from CLI within a CAP project, make sure plugins (this includes cds-typer)
263
+ // are initialised and have their default values injected into cds.env
264
+ await cds.plugins
261
265
  const { positional } = prepareParameters(argv)
262
266
  compileFromFile(positional)
263
267
  }
package/lib/config.js CHANGED
@@ -1,3 +1,5 @@
1
+ const fs = require('node:fs')
2
+ const path = require('node:path')
1
3
  const cds = require('@sap/cds')
2
4
  const { camelToSnake, getProjectTargetType } = require('./util')
3
5
  const { LOG } = require('./logging')
@@ -32,21 +34,30 @@ const camelSnakeHybrid = target => {
32
34
  return proxy
33
35
  }
34
36
  class Config {
35
- static #defaults = {
36
- propertiesOptional: true,
37
- useEntitiesProxy: false,
38
- inlineDeclarations: 'flat',
39
- outputDirectory: '.',
40
- targetModuleType: 'auto'
37
+ /** @type {Record<string, unknown>} */
38
+ static #defaults = {}
39
+ static get defaults () {
40
+ // these are the exact defaults that are used when cds-typer is loaded as cds plugin
41
+ // which will shove the values into cds.env. When executed standalone, without cds environment,
42
+ // we need to load them from package.json ourselves.
43
+ if (Object.keys(Config.#defaults).length) return Config.#defaults
44
+ try {
45
+ const packageJsonPath = path.resolve(__dirname, path.join('..', 'package.json'))
46
+ const pjson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
47
+ Config.#defaults = pjson.cds.typer
48
+ } catch (e) {
49
+ LOG.error(`Failed to load default configuration from package.json: ${e}`)
50
+ }
51
+ return Config.#defaults
41
52
  }
42
53
 
43
54
  values = undefined
44
55
  proxy = undefined
45
56
 
46
57
  init () {
47
- this.values = {...Config.#defaults, ...(cds.env.typer ?? {})}
58
+ this.values = {...Config.defaults, ...(cds.env.typer ?? {})}
48
59
  this.proxy = camelSnakeHybrid(this.values)
49
- if (this.values.targetModuleType === 'auto') {
60
+ if (this.proxy.targetModuleType === 'auto') {
50
61
  const type = getProjectTargetType(cds.root)
51
62
  if (type) {
52
63
  LOG.info(`automatically detected module type '${type}' in ${cds.root}`)
package/lib/file.js CHANGED
@@ -10,7 +10,6 @@ const { proxyAccessFunction } = require('./components/javascript')
10
10
  const { createObjectOf, stringIdent } = require('./printers/wrappers')
11
11
  const { configuration } = require('./config')
12
12
  const { CJSPrinter, ESMPrinter } = require('./printers/javascript')
13
- const { getProjectTargetType } = require('./util')
14
13
 
15
14
  const AUTO_GEN_NOTE = '// This is an automatically generated file. Please do not change its contents manually!'
16
15
 
@@ -109,6 +108,9 @@ class Library extends File {
109
108
  * Source file containing several buffers.
110
109
  */
111
110
  class SourceFile extends File {
111
+ /** @type {import('../lib/printers/javascript').Printer | undefined} */
112
+ #jsPrinter
113
+
112
114
  /**
113
115
  * @param {string | Path} path - path to the file
114
116
  */
@@ -146,8 +148,10 @@ class SourceFile extends File {
146
148
  this.services = { buffer: new Buffer(), names: [] }
147
149
  /** @type {Record<string,string[]>} */
148
150
  this.entityProxies = {}
149
- /** @type {import('../lib/printers/javascript').Printer} */
150
- this.jsPrinter = configuration.targetModuleType === 'esm'
151
+ }
152
+
153
+ get jsPrinter () {
154
+ return this.#jsPrinter ??= configuration.targetModuleType === 'esm'
151
155
  ? new ESMPrinter()
152
156
  : new CJSPrinter()
153
157
  }
@@ -17,7 +17,7 @@ class JavaScriptPrinter {
17
17
  /**
18
18
  * @abstract
19
19
  * @param {string} alias - what the import should be known as within the importing file
20
- * @param {string} from - the package/ location to import from
20
+ * @param {string} from - the package/ location to import from
21
21
  * @returns {string}
22
22
  */
23
23
  // eslint-disable-next-line no-unused-vars
package/lib/visitor.js CHANGED
@@ -8,7 +8,7 @@ const { SourceFile, FileRepository, Buffer, Path } = require('./file')
8
8
  const { FlatInlineDeclarationResolver, StructuredInlineDeclarationResolver } = require('./components/inline')
9
9
  const { Resolver } = require('./resolution/resolver')
10
10
  const { LOG } = require('./logging')
11
- const { docify, createPromiseOf, createUnionOf, createKeysOf, createElementsOf, stringIdent, createDraftsOf, createDraftOf, createIntersectionOf, createReturnTypeOf } = require('./printers/wrappers')
11
+ const { docify, createPromiseOf, createUnionOf, createKeysOf, createElementsOf, stringIdent, createDraftsOf, createDraftOf, createIntersectionOf } = require('./printers/wrappers')
12
12
  const { csnToEnumPairs, propertyToInlineEnumName, isInlineEnumType, stringifyEnumType } = require('./components/enum')
13
13
  const { isReferenceType } = require('./components/reference')
14
14
  const { empty } = require('./components/typescript')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cap-js/cds-typer",
3
- "version": "0.29.0",
3
+ "version": "0.30.0",
4
4
  "description": "Generates .ts files for a CDS model to receive code completion in VS Code",
5
5
  "main": "index.js",
6
6
  "repository": "github:cap-js/cds-typer",
@@ -63,6 +63,13 @@
63
63
  ]
64
64
  },
65
65
  "cds": {
66
+ "typer": {
67
+ "output_directory": "@cds-models",
68
+ "inline_declarations": "flat",
69
+ "target_module_type": "auto",
70
+ "properties_optional": true,
71
+ "use_entities_proxy": false
72
+ },
66
73
  "schema": {
67
74
  "buildTaskType": {
68
75
  "name": "typescript",