GithubHelp home page GithubHelp logo

simplesky12 / j2t-cli Goto Github PK

View Code? Open in Web Editor NEW

This project forked from chenjinchuang/j2t-cli

0.0 0.0 0.0 53 KB

根据 JSON 文件生成 Typescript 类型声明文件。Generate d.ts file from JSON file.

License: MIT License

JavaScript 0.20% TypeScript 99.80%

j2t-cli's Introduction

j2t-cli

一个根据.json文件生成d.ts类型文件的工具。

安装

npm install -D j2t-cli

快速上手

1. 准备工作

在任意一个工程根目录下新增一个名叫schedule.json 的文件并添加一些内容:

{
  // 这里的 index 代表最终导出的文件名,最终的文件名是 index.d.ts
  "index": [
    // 需要生成类型声明的 json 数据,在这个数组里面的 json 对象
    // 最终都会导出到同一个文件中
    {
      // 导出的类型名称,小驼峰命名,这里最终导出的类型名是 Demo1
      "demo1": {
        "id": 1,
        "name": "qinchen",
        "list": [
          {
            "id": 1,
            "color": "red"
          }
        ]
      }
    }
  ],
  // 这里的 demo 代表最终导出的文件名,最终的文件名是 demo.d.ts
  "demo": [
    {
      // 导出的类型名称,小驼峰命名,这里最终导出的类型名是 Demo2
      "demo2": {
        "id": 1,
        "name": "qinchen",
        "list": [
          {
            "id": 1,
            "color": "red"
          }
        ]
      }
    }
  ]
}

2.1 命令行运行

# 在项目根目录下执行:
j2t

# 成功后提示:
Success! d.ts created in: /yourproject/typings

# 查看目录文件
# /yourproject/typings/
.
├── index.d.ts
└── demo.d.ts

你可以通过在命令行中运行j2t -h 获得更多关于命令行调用时的使用说明。

2.2 在代码中调用

import JSONToType from "j2t-cli"

new JSONToType().run();

// 查看目录文件
// /yourproject/typings/

// .
// ├── index.d.ts
// └── demo.d.ts

JSONToType类提供了几个支持链式调用的方法来改变类的行为:

const demo = {
  "index": [
    {
      demo1: { "id": 1 ... }
    }
  ],
  "demo": [
    {
      demo2: { "id": 1 ... }
    }
  ],
}

new JSONToType()
  .rename("qinchen.json") // 指定读取schedule时的文件名
  .path("/User/qinchen/doc") // 指定 schedule 文件的提取路径
  .output("/User/qinchen/test") // 指定 d.ts 文件的输出路径
  .schedule(demo) // 手动指定 schedule 内容,调用后不会读取 schedule 文件。
  .run();

j2t-cli's People

Contributors

chenjinchuang 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.