Coder Social home page Coder Social logo

definition's Introduction

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

definition's People

Contributors

anuoua avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.