Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Group under one single constant name. #38

Open
alajmo opened this issue Jul 27, 2016 · 6 comments
Open

Group under one single constant name. #38

alajmo opened this issue Jul 27, 2016 · 6 comments
Labels

Comments

@alajmo
Copy link

alajmo commented Jul 27, 2016

Hi,

I didn't find the possibility to group under one single constant name.

Example

Given an object like:

{
"person": "samir",
"age": 99
}
I wanted to get this:

angular.module("conf", [])
.constant("CONFIG", {
"person": "samir",
"age": 99
});
but instead got this:

angular.module("conf", [])
.constant("person", "samir")
.constant"age", 99);
Using an optional parameter we can simply group the whole object under a user defined constant.

Let me know if you think this feature makes sense and I'll write the tests and update README to include an example (I just added the option explanation for now).

@guzart
Copy link
Owner

guzart commented Aug 9, 2016

Have you tried something like this?

{ "CONFIG": { "person": "samir", "age": 99 } }

@guzart guzart added the question label Aug 9, 2016
@alajmo
Copy link
Author

alajmo commented Aug 9, 2016

I shouldn't need to change my json file to get the wanted results, but yes it works. Often times settings files don't contain a root key name like:

{
    "CONFIG": {
       "stuff": "stuff..."
    }
}

It just adds an unnecessary nesting level and forces the user to adapt his json files to third party modules which I think should be avoided, especially when there is an easy solution.

@guzart
Copy link
Owner

guzart commented Aug 16, 2016

It's true that you might not want to have that complexity in the configuration file.
What sort of option would you suggest to add to the plugin?
Could show a snippet on how you would like to use the option? Thanks

@alajmo
Copy link
Author

alajmo commented Aug 16, 2016

Well I did create a pull request for a working solution, #35. The functionality is already in your code, we just wrap it under a new user entered field, so it would look something like:

var ngConstant = require('gulp-ng-constant');

gulp.task('config', function () {
  gulp.src('app/config.json')
    .pipe(ngConstant({
      name: 'my.module.config',
      deps: ['ngAnimate'],
      constants: { myPropCnt: 'hola!' },
      wrap: 'amd',
      constantName: 'CONFIG'
    }))
    // Writes config.js to dist/ folder
    .pipe(gulp.dest('dist'));
});

Could name constantName to whatever sounds right, groupUnder, name, key etc.

@regmish
Copy link

regmish commented Jan 6, 2017

+1

@ViieeS
Copy link

ViieeS commented Nov 17, 2017

I found another solution using jeditor plugin:

var jeditor = require("gulp-json-editor");
var ngConstant = require('gulp-ng-constant');

gulp.src('app/config.json')
        .pipe(jeditor(function (json) {
            return {"Constants": json};
        }, {beautify: false}))
        .pipe(ngConstant({
            name: "my.module.constants",
            wrap: "commonjs"
        }))
        .pipe(gulp.dest('dist'));

There is one restriction, you can't use constants (in lowercase) as constant object name because it will be unwrap by ngConstant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants