typescript - 使用 Gulp 将 JSON 配置到 TypeScript 类

标签 typescript gulp

我正在尝试使用 gulp 任务从 JSON 配置创建一个 typescript 类。

input

    {
      "ApiEndpoint": "http://localhost:5000/api"
    }

希望得到像

这样的输出
  public static get ApiEndpoint(): string {
    return "http://localhost:5000/api";
  }

尝试使用 gulp-ts-config 但它在 template.ts 中抛出错误。不知何故,我无法让它在我的系统中工作。是否有任何其他 gulp 插件可以通过我们实现相同的目的?

最佳答案

我的包.json

{
    "devDependencies": {
        "gulp": "3.9.1",
        "gulp-ts-config": "1.0.15"
    }
}

我的 gulpfile.js

var gulp = require('gulp');
var gulpTsConfig = require('gulp-ts-config');

gulp.task('test', function () {
    gulp.src('appsettings.json')
        .pipe(gulpTsConfig('AppSettings'))
        .pipe(gulp.dest('Scripts'))
});

appSettings.json

{
    "ApiEndpoint": "http://localhost:5000/api"
}

运行任务后创建的 typescript

export class AppSettings {

  public static get ApiEndpoint(): string {
    return "http://localhost:5000/api";
  }
}

引用文献:https://www.npmjs.com/package/gulp-ts-config

如果我更改我的 appSettings.json 以包含多个对象以转换为 typescript 函数

{
    "ApiEndpoint": "http://localhost:5000/api",
    "HelloWorld":  "HelloWorld"
}

结果ts文件

export class AppSettings {

  public static get ApiEndpoint(): string {
    return "http://localhost:5000/api";
  }
  public static get HelloWorld(): string {
    return "HelloWorld";
  }
}

您从 gulp-ts-config 收到的具体错误是什么?我正在使用带有 npm 版本 5.5.1 和节点版本 v8.9.3 的 Visual Studio 2017

关于typescript - 使用 Gulp 将 JSON 配置到 TypeScript 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47984798/

相关文章:

javascript - Laravel 缩小 css 和 js 文件

linux - 如何在 gulp 中获取当前文件夹名称?

typescript - Nuxt3 : script setup cannot redeclare block-scoped variable

typescript - Typescript 中带有变量的字符串文字类型

javascript - angular - 基于 mat-select 选项的条件元素

css - Browsersync 和/或 Gulp 不断重置 CSS

node.js - 配置 Gulp 'watch' 任务以进行非常基本的服务器重启(Node JS)

mongodb - 在TS文件中编写mongo shell脚本

javascript - Angular2 表单页面重新加载?

node.js - 需要帮助了解 gulp 和 webpack-stream 的工作原理