definition
TypeScript icon, indicating that this package has built-in type declarations

2.0.1 • Public • Published

Definition publish

Generate typescript type definitions and enumerations from business definition json records.

Install

npm i definition

Usage

Create config file.

definition.config.mjs

/**
 * @return {import('definition').ConfigOptions}
 */
export default () => {
  return {
    dir: "./jsons",
    output: "./definitions",
  };
};

Create json records in target directory.

./jsons/hello.json

[
  { "value": "hello", "key": "hello" },
  { "value": "world", "key": "world" }
]

Run command

npx def generate # or npx def g

or add command to scripts with npm run

{
  "scripts": {
    "definition": "def generate" // or def g
  }
}

Result

.
├── hello.ts
└── utils
    ├── index.ts
    ├── kr-map.ts
    ├── kv-map.ts
    ├── types.ts
    └── vr-map.ts

./definitions/hello.ts

import { krMap, vrMap, kvMap } from "./utils";
export const HELLO_records = [{ "value": "hello", "key": "hello" }, { "value": "world", "key": "world" }] as const;
export type HELLO_Records = typeof HELLO_records;
export type HELLO_Values = HELLO_Records[number]["value"];
export enum HELLO_Keys {
    Hello = "Hello",
    World = "World"
}
export const HELLO_key_list = [HELLO_Keys.Hello, HELLO_Keys.World] as const;
export const HELLO_kr = krMap(HELLO_records, HELLO_key_list);
export const HELLO_vr = vrMap(HELLO_records, HELLO_key_list, "value");
export const HELLO_kv = kvMap(HELLO_records, HELLO_key_list, "value");

CLI

Usage: cli [options] [command]

Options:
  -h, --help            display help for command

Commands:
  generate|g [options]
  help [command]        display help for command

command generate

sage: cli generate|g [options]

Options:
  -c, --config [path]  Config file path
  -h, --help           display help for command

ConfigOptions

interface ConfigOptions {
  dir: string; // Json records directory
  output: string; // Definitions output directory
  valueField?: string; // The value field key
  keyField?: string; // The key field key
  keyFormat?: "camelback" | "camelcase" | "underscore"; // The format of the generated key
  keyGen?: (record: Record<string, any>) => string; // Custom key generate function
}

Note: when keyGen is configured, keyFormat will be ignored.

License

MIT

Package Sidebar

Install

npm i definition

Weekly Downloads

24

Version

2.0.1

License

none

Unpacked Size

24.4 kB

Total Files

21

Last publish

Collaborators

  • anuoua