# enonic-ts-codegen

> Generate TypeScript-code for Enonic XP

Latest version **0.0.22** (published 2020-10-14) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install enonic-ts-codegen
pnpm add enonic-ts-codegen
yarn add enonic-ts-codegen
bun add enonic-ts-codegen
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.0.22 |
| Published | 2020-10-14 |
| First published | 2019-11-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 25.2 KB |
| Known vulnerabilities | 0 (+17 in 1 direct dependencies) |
| Install scripts | no |
| Author | Tom Arild Jakobsen |
| Maintainers | tajakobsen |
| Keywords | enonic |

## Links

- npm: https://www.npmjs.com/package/enonic-ts-codegen
- Repository: https://github.com/ItemConsulting/enonic-ts-codegen
- Homepage: https://github.com/ItemConsulting/enonic-ts-codegen#readme
- Issues: https://github.com/ItemConsulting/enonic-ts-codegen/issues
- npm.io page: https://npm.io/package/enonic-ts-codegen

## Dependencies (4)

- [xpath](https://npm.io/package/xpath.md) 0.0.27
- [xmldom](https://npm.io/package/xmldom.md) ^0.3.0
- [commander](https://npm.io/package/commander.md) ^3.0.2
- [change-case](https://npm.io/package/change-case.md) ^4.1.0

## Recent versions

- 0.0.22 (latest) — 2020-10-14
- 0.0.21 — 2020-10-13
- 0.0.20 — 2020-09-07
- 0.0.19 — 2020-06-03
- 0.0.18 — 2020-06-03
- 0.0.17 — 2020-04-20
- 0.0.16 — 2020-03-10
- 0.0.15 — 2020-03-09
- 0.0.14 — 2020-02-26
- 0.0.13 — 2020-02-19
- 0.0.12 — 2020-02-13
- 0.0.11 — 2020-02-02
- 0.0.10 — 2020-01-29
- 0.0.9 — 2020-01-24
- 0.0.8 — 2020-01-24
- … 7 more at https://npm.io/package/enonic-ts-codegen/versions

## README

# DEPRECATED

----

**WARNING: This project has been deprecated**, and is not being maintained anymore. 

Please use the new and much better [XP Codegen Gradle Plugin](https://github.com/ItemConsulting/xp-codegen-plugin) instead.

----

[![No Maintenance Intended](http://unmaintained.tech/badge.svg)](http://unmaintained.tech/)

Code generation tool that creates **TypeScript interfaces** based on xmls in Enonic XP. It can create interfaces for:

 * Content types
 * Pages
 * Parts
 * Site
 * Layout
 * Macros
 * Id-provider
 * Mixins
 
It is recommended to let Gradle run this tool on every build, so that we always have a tight cupling between the xmls and TypeScript-code.

## Example

Given that we have a content-type described in **content-types/article/article.xml**:

```xml
<?xml version="1.0" encoding="UTF-8"?>
<content-type>
  <display-name>Article</display-name>
  <super-type>base:structured</super-type>
  <form>
    <input name="title" type="TextLine">
      <label>Title of the article</label>
      <occurrences minimum="1" maximum="1"/>
    </input>

    <input name="body" type="HtmlArea">
      <label>Main text body</label>
      <occurrences minimum="0" maximum="1"/>
    </input>
  </form>
</content-type>
```

Running *enonic-ts-codegen* will generate the a TypeScript-interface in the file **content-types/article/article.ts**:

```typescript
export interface Article {
  /** Title of the article */
  title: string;
 
  /** Main text body */
  body?: string;
}
```

This interface will describe the data shape for this content type. Note that body can be undefined since `occurrences/@minimum` is `0` for `body`, so the key becomes `body?`, since the value can be `undefined`.

## CLI

`xml-to-ts.js` is a command line utility that can generate TypeScript interfaces.

- Build it: `npm run build` or `npm run build:cli`
- Run it from the command line: `node bin/xml-to-ts.js my-xml-file.xml`

### Gradle

`xml-to-ts.js` can be run automatically as part of the`enonic project deploy`
process. First, add the dependency with `npm install enonic-ts-codegen --save`, and then
add a task to `build.gradle`, for example:

```groovy
task generateTypeScriptInterfaces( type: NodeTask, dependsOn: npmInstall ) {
    description = 'Generate TypeScript interfaces'
    environment = [ 'NODE_ENV': nodeEnvironment() ]
    args = [ '--project', '.', '--write-to-file' ]
    script = file( 'node_modules/enonic-ts-codegen/bin/index.js' )
}
```

To run the task as part of the deploy process, add it to the `jar` block:

```
jar {
  // ... other dependencies

  dependsOn += generateTypeScriptInterfaces

  // ...
}
```

---
_Source: https://npm.io/package/enonic-ts-codegen · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
