提取打字稿用出口来一饮而尽JSON文件 [英] Extracting typescript exports to json file using gulp
问题描述
我有几个打字稿的文件,其中一些出口一定的无功 - 名为APIS - 这是对象的数组。
我想用一饮而尽提取所有这些出口到一个JSON文件中的值,并管他们。
I have several typescript files, some of them export a certain var - named APIS - which is an array of objects. I want to extract the values of all of these exports, and pipe them to a json file using gulp.
例如,我有一个文件夹命名服务,与3个文件:service1.ts,service2.ts,service3.ts
For example, I have a folder named services, with 3 files: service1.ts, service2.ts, service3.ts.
service1.ts:
...
export const APIS = [ { "field1" : "blabla" } ];
service2.ts:
...
export const APIS = [ { "field2" : "yadayada" }, { "field3" : "yadabla" } ];
service3.ts: - 不导出API的VAR
service3.ts: - does not export the APIS var.
我想在奥得使用一饮而尽创建一个JSON文件看起来是这样的:
I want to use gulp in oder create a json file that looks something like this:
[ { "field1" : "blabla" }, { "field2" : "yadayada" }, { "field3" : "yadabla" } ]
gulpfile.js - 在?是失踪code的占位符。
gulpfile.js - the ??? is a placeholder for the missing code.
gulp.task('default', function () {
return gulp.src('.../services/*.ts')
.pipe(???)
.pipe(concat('export.json'))
.pipe(gulp.dest('./test'));
});
我是新来的两个打字稿及放大器;一饮而尽,所以我不知道如何实现这一目标...任何想法? :)
I'm new to both typescript & gulp, so I'm not sure how to achieve this... any ideas? :)
编辑:所以,我的理解是有这个问题没有开箱即用的解决方案,我需要作家自己的任务/插件。我真的不知道如何实现,虽然。
理想情况下,我要的是找到一个大口插件(或插件的组合),可以处理TS / js文件与属性的对象。然后,我可以提取我从文件中所需要的变种。
So, I understand that there's no OOTB solution for this, and I need to writer my own task / plugin. I'm not really sure how to achieve that, though. Ideally, what I want is to find a gulp plugin (or a combination of plugins) that can handle ts / js files as objects with properties. Then I can extract the var I need from the file.
我真的无法找到类似的东西,只是字符串操作插件 - 治疗我的TS文件作为一个字符串,并使用与正则表达式搜索似乎过于复杂了我。有什么事,我在这里丢失?有没有一种更直接的方式做到这一点?
I couldn't really find something like that, only string manipulation plugins - Treating my ts file as a string and using search with regex seems overly complicated to me. Is there something I'm missing here? is there a more straight-forward way to do this?
推荐答案
这是我落得这样做,它为我工作。我在这里并主张它的情况下,任何人发现它是有用的。 :)
This is what I ended up doing, and it worked for me. I'm positing it here in case anyone else finds it useful. :)
相反.TS,我保存在.js文件的出口,即:
Instead of .ts, I saved the exports in .js files, i.e:
service2.export.js:
service2.export.js:
exports.APIS = [ { "field2" : "yadayada" }, { "field3" : "yadabla" } ];
基于这里给出的答案
: http://stackoverflow.com/a/36869651/3007732 我创建了一个一饮而尽任务如下:
Based on the answer given here: http://stackoverflow.com/a/36869651/3007732 I created a gulp task as following:
var gulp = require('gulp');
var concat = require('gulp-concat');
var map = require('gulp-map');
var fs = require('fs');
var allServices;
gulp.task('default', function() {
var allServices = [];
var stream = gulp.src('./**/*.export.js')
.pipe(map(function(file) {
var obj = require(file.path);
if (obj.APIS != null) {
allServices.push.apply(allServices, obj.APIS);
}
return file;
}));
stream.on("end", function (cb)
{
fs.writeFile('./export.json', JSON.stringify(allServices), cb);
});
return stream;
});
,现在我得到export.json以下的输出:
and now I get the following output in export.json:
[ { "field1" : "blabla" }, { "field2" : "yadayada" }, { "field3" : "yadabla" } ]
这正是我想要的。
which is exactly what I wanted.
这篇关于提取打字稿用出口来一饮而尽JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!