I have a set of TypeScript files, some of which export a specific variable - named APIS - which contains an array of objects. My goal is to extract the values from all of these exports and save them into a JSON file using Gulp.
Let's consider a directory called services, containing 3 files: service1.ts, service2.ts, and service3.ts.
service1.ts:
...
export const APIS = [ { "field1" : "blabla" } ];
service2.ts:
...
export const APIS = [ { "field2" : "yadayada" }, { "field3" : "yadabla" } ];
service3.ts: - does not export the APIS variable.
My objective is to utilize Gulp to create a JSON file structured like this:
[ { "field1" : "blabla" }, { "field2" : "yadayada" }, { "field3" : "yadabla" } ]
gulpfile.js - the ??? represents the missing code.
gulp.task('default', function () {
return gulp.src('.../services/*.ts')
.pipe(???)
.pipe(concat('export.json'))
.pipe(gulp.dest('./test'));
});
Since I am new to TypeScript and Gulp, I am unsure of how to accomplish this task... any suggestions? :)
EDIT: After realizing there is no ready-made solution for this, it seems I will have to create my own task or plugin. However, I am uncertain about how to proceed with that. Ideally, I am looking for a Gulp plugin (or a combination of plugins) that can handle TypeScript/JavaScript files as objects with properties. This way, I can extract the necessary variable from the file.
My search did not yield such a plugin, only ones that manipulate strings - Treating my TypeScript file as a string and using regex search seems overly complex to me. Am I overlooking something? Is there a more direct method to achieve this?