@cap-js/cds-typer
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -7,4 +7,8 @@ # Change Log | ||
## Version 0.3.1 - TBD | ||
## Version 0.4.1 - TBD | ||
## Version 0.4.0 - 2023-07-06 | ||
### Added | ||
- Support for enums when they are defined separately (not inline in the property type of an entity) | ||
## Version 0.3.0 - 2023-06-26 | ||
@@ -11,0 +15,0 @@ ### Added |
@@ -104,7 +104,7 @@ 'use strict' | ||
this.types = new Buffer() | ||
/** @type {{ buffer: Buffer, fqs: {name: string, fq: string}[]}} */ | ||
this.enums = { buffer: new Buffer(), fqs: [] } | ||
/** @type {Buffer} */ | ||
this.enums = new Buffer() | ||
/** @type {Buffer} */ | ||
this.classes = new Buffer() | ||
/** @type {{ buffer: Buffer, names: string[]} */ | ||
/** @type {{ buffer: Buffer, names: string[]}} */ | ||
this.actions = { buffer: new Buffer(), names: [] } | ||
@@ -195,9 +195,22 @@ /** @type {Buffer} */ | ||
addEnum(fq, name, kvs) { | ||
this.enums.add(`export enum ${name} {`) | ||
this.enums.indent() | ||
// CDS differ from TS enums as they can use bools as value (TS: only number and string) | ||
// So we have to emulate enums by adding an object (name -> value mappings) | ||
// and a type containing all disctinct values. | ||
// We can get away with this as TS doesn't feature nominal typing, so the structure | ||
// is all we care about. | ||
this.enums.fqs.push({ name, fq }) | ||
const bu = this.enums.buffer | ||
bu.add('// enum') | ||
bu.add(`export const ${name} = {`) | ||
bu.indent() | ||
const vals = new Set() | ||
for (const [k, v] of kvs) { | ||
this.enums.add(`${k} = ${v},`) | ||
bu.add(`${k}: ${v},`) | ||
vals.add(v) | ||
} | ||
this.enums.outdent() | ||
this.enums.add('}') | ||
bu.outdent() | ||
bu.add('}') | ||
bu.add(`export type ${name} = ${[...vals].join(' | ')}`) | ||
bu.add('') | ||
} | ||
@@ -277,3 +290,3 @@ | ||
this.types.join(), | ||
this.enums.join(), | ||
this.enums.buffer.join(), | ||
namespaces.join(), | ||
@@ -306,2 +319,4 @@ this.aspects.join(), // needs to be before classes | ||
.concat(this.actions.names.map(name => `module.exports.${name} = '${name}'`)) | ||
.concat(['// enums']) | ||
.concat(this.enums.fqs.map(({fq, name}) => `module.exports.${name} = Object.fromEntries(Object.entries(cds.model.definitions['${fq}'].enum).map(([k,v]) => [k,v.val]))`)) | ||
.join('\n') + '\n' | ||
@@ -308,0 +323,0 @@ } |
@@ -211,3 +211,3 @@ /* eslint-disable indent */ | ||
delete entity[attr] | ||
this.logger.info(`Removing inherited attribute ${attr} from ${entity.name}.`) | ||
//this.logger.info(`Removing inherited attribute ${attr} from ${entity.name}.`) | ||
} | ||
@@ -219,11 +219,9 @@ } | ||
let i = 0 | ||
if (entity.includes) { | ||
while ( | ||
(getSingularAnnotation(entity) || getPluralAnnotation(entity)) && | ||
i < entity.includes.length | ||
) { | ||
const parent = this.csn.definitions[entity.includes[i]] | ||
Object.values(annotations).flat().forEach(an => erase(entity, parent, an)) | ||
i++ | ||
} | ||
while ( | ||
(getSingularAnnotation(entity) || getPluralAnnotation(entity)) && | ||
i < (entity.includes ?? []).length | ||
) { | ||
const parent = csn.definitions[entity.includes[i]] | ||
Object.values(annotations).flat().forEach(an => erase(entity, parent, an)) | ||
i++ | ||
} | ||
@@ -230,0 +228,0 @@ } |
@@ -263,6 +263,8 @@ 'use strict' | ||
if ('enum' in type) { | ||
// in case of strings, wrap in quotes and fallback to key to make sure values are attached for every key | ||
const val = (k,v) => type.type === 'cds.String' ? `"${v ?? k}"` : v | ||
file.addEnum( | ||
name, | ||
clean, | ||
Object.entries(type.enum).map(([k, v]) => [k, v.val]) | ||
Object.entries(type.enum).map(([k, v]) => [k, val(k, v.val)]) | ||
) | ||
@@ -269,0 +271,0 @@ } else { |
{ | ||
"name": "@cap-js/cds-typer", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "Generates .ts files for a CDS model to receive code completion in VS Code", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -9,3 +9,3 @@ # CDS type generator for JavaScript | ||
## Requirements and Setup | ||
This project is [available as `@cds-js/cds-typer`](https://www.npmjs.com/package/@cap-js/cds-typer) as NPM package. | ||
This project is [available as `@cap-js/cds-typer`](https://www.npmjs.com/package/@cap-js/cds-typer) as NPM package. | ||
@@ -49,2 +49,4 @@ ### Usage | ||
_Note:_ the above command generates types for the model contained within the mentioned `schema.cds` file. If you have multiple `.cds` files that are included via `using` statements by `schema.cds`, then those files will also be included in the type generation process. If you have `.cds` files that are _not_ in some way included in `schema.cds`, you have to explicitly pass those as positional argument as well, if you want types for them. | ||
_cds-typer_ comes with rudimentary CLI support and a few command line options: | ||
@@ -51,0 +53,0 @@ |
184
99758
15
1942