guzart / gulp-ng-constant

Gulp plugin for dynamic generation of angular constant modules.
MIT License
109 stars 26 forks source link

merge constants from files in the stream #8

Closed guzart closed 10 years ago

guzart commented 10 years ago

from @intellix comment on #6

I'm trying to have 2 configs merged so I can have global configuration and environment specific ones like:

config-global.json {
    ENV: {
        name: 'MyApp'
    }
}
config-dev.json {
    ENV: {
        apiEndpoint: 'localhost'     
    }
}

to create:

config.js {
    ENV: {
        name: 'MyApp'
        apiEndpoint: 'localhost'
    }
}

so that the following will get the constants from config/global.json and extend or override them with config/dev.json:

gulp
  .src([ 'config/global.json', 'config/dev.json' ])
  .pipe($.ngConstant({
    name: 'config'
  }))
  .pipe(gulp.dest('src/app'));
ojacquemart commented 10 years ago

Maybe, it could be useful to use jsoncombine internally before transforming the json files?

intellix commented 10 years ago

Also note that I had to transform the jsoncombine response to remove the filenames :) https://github.com/guzart/gulp-ng-constant/pull/6#issuecomment-58148962

guzart commented 10 years ago

I don't think there's a need to add a dependency to jsoncombine, because internally we already have the contents of the file from the stream, it's just a matter of persisting the object between files and using lodash to extend it.

On the other hand, it makes me wonder what the expected functionality should be when there are multiple files on the stream.

So when you have this code:

gulp
  .src([ 'config/global.json', 'config/dev.json' ])
  .pipe($.ngConstant({ name: 'config' }))
  .pipe(gulp.dest('src/app'));

Do you expect it to behave as a multiplexer and output one file with all the contents merged? or Do you expect it to generate multiple files?

I believe it should be the latter, and that if you want a multiplexer, you should use a plugin like jsoncombine. And then, json combine or alternative, could have an option to extend.

gulp
  .src([ 'config/global.json', 'config/dev.json' ])
  .pipe(jsoncombine('config.json', { extend: true })
  .pipe($.ngConstant({ name: 'config' }))
  .pipe(gulp.dest('src/app'));
ojacquemart commented 10 years ago

You are right about the streams and jconsombine usage.

By default, when you have many files in entry, it should generate multiple files. To concat have all contents merged, as you said, jsoncombine or any alternative could be used.