jdpnielsen / uda-to-typescript

MIT License
0 stars 0 forks source link

uda-to-typescript

npm package Build Status Downloads Issues Commitizen Friendly Semantic Release

Command-line tool to convert Umbraco UDA files to Typescript types.

Install

npm install @jdpnielsen/uda-to-typescript

Quick Start

Usage: uda-to-typescript [options]

CLI to convert Umbraco UDA files to typescript definitions

Options:
  -V, --version          output the version number
  -c, --config <string>  Path for config file. Example: --config ./udaconvert.config.ts
  -i, --input <string>   Glob pattern to match. Example: --input ./files/*.uda
  -o, --output <string>  Where to write output file. Example: --output ./file.ts
  -d, --debug            enables verbose logging (default: false)
  -h, --help             display help for command

Examples:

  $ uda-to-typescript --version
  1.0.0

Without config

  $ uda-to-typescript --input ../your-umbraco-project/src/UmbracoProject/umbraco/Deploy/Revision/*.uda --output ./umbraco-sdk/types.ts

With config

 $ uda-to-typescript
 # or
 $ uda-to-typescript --config ./udaconvert.config.ts

Note: uda-to-typescript uses cosmic-config to locate config files.

File udaconvert.config.ts

import { defineConfig } from '@jdpnielsen/uda-to-typescript';

export default defineConfig({
  input: './petstore.yaml',
  output: './src/petstore.ts',
});

Provide custom datatypes

import { factory } from 'typescript';
import { defineConfig, dataTypes, HandlerConfig } from '@jdpnielsen/uda-to-typescript';

export default defineConfig({
    input: '../umbraco-boilerplate/src/UmbracoProject/umbraco/Deploy/Revision/*.uda',
    output: './src/umbraco/output.ts',
    dataTypes: {
        ...dataTypes, // Provide default types
        'Custom.StatementEditor': {
            editorAlias: 'Custom.StatementEditor',
            /**
             * Used to create any shared setup for the propertyEditor
             */
            init: () => {
                return 'export type Statement = "key" | "val";';
            },
            /**
             * Used to generate any dataType specifc types
             * @example ```ts
             * type MetatitleTextboxType = {
             *  statement: Statement;
             *  prop: string;
             * };
             * ```
             */
            build: (dataType) => {
                return `type ${pascalCase(dataType.Name)}Type = {
                    statement: Statement;
                    prop: ${dataType.DatabaseType === 1 ? 'string' : 'unknown'};
                }`;
            },
            /**
             * Used to generate any usage specific type/reference
             * @example ```ts
             * export type Metadata = BaseDocumentType<'metadata', {
             *  meta_title?: MetatitleTextboxType;
             * }>;
             * ```
             */
            reference: (dataType) => `${pascalCase(dataType.Name)}Type`,
        },
    },
});