Skip to content Skip to sidebar Skip to footer

Accessing Typescript File Variable Values Using Gulp

I have several typescript files, some of them export a const named APIS. I'm trying to access those exports (I want to concatenated all of them to a single file), but it doesn't se

Solution 1:

This will not work as you think it would work. Gulp itself knows nothing about typescript files, that file is a vinyl-file and has no knowledge about the typescript code within its content.

Edit

Based on your example, you can do something like this:

var gulp = require('gulp');
var concat = require('gulp-concat');
var map = require('gulp-map');
var fs = require('fs');

gulp.task('test', function ()
{
    var allConstants = [];
    var stream = gulp.src('./**/*.service.export.js')
        .pipe(map(function(file)
        {
            var obj = require(file.path);
            if (obj.APIS != null)
                allConstants = allConstants.concat(obj.APIS);
            return file;
        }));

    stream.on("end", function (cb)
    {
        // Do your own formatting herevar content = allConstants.map(function (constants)
        {
            returnObject.keys(constants).reduce(function (aggregatedString, key)
            {
                return aggregatedString + key + " : " + constants[key];
            }, "");
        }).join(", ");

        fs.writeFile('filename.txt', content, cb);
    });
    return stream;
});

Solution 2:

Suggestion

If you want to collect multiple variables into a single file i.e. a common variables file I suggest gulp-replace.

Steps

Create a file, require it and use tags within that file to place your variables.

Advice

If you are already using services don't create an array. Instead create an object (JSON) where every property is a constant. i.e.

varconstants=  {
   const_1:0,
   const_2:1,
   const_3:2,
}

Post a Comment for "Accessing Typescript File Variable Values Using Gulp"