Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read package.json in Ember application

We have the following structure (shown just some relevant components) for our yeoman (grunt) based Ember.js project:

myapp/
├── app
├── component.json
├── package.json
├── Gruntfile.js
├── README.md
│   ├── adapters
│   ├── application.js
│   ├── components
│   ├── controllers
│   ├── generated
│   ├── images
│   ├── models
│   ├── routes
│   ├── scripts
│   ├── styles
│   ├── templates
│   └── views
├── dist
├── node_modules
└── test

We need to access, in application.js, some of the values in package.json and component.json. We would like to access this during the deploy process (as a grunt task?). Is this possible? How can this be implemented?

like image 930
blueFast Avatar asked Apr 23 '13 09:04

blueFast


2 Answers

You can also use a ready to use grunt package like this https://github.com/outaTiME/grunt-replace for this kind of task, most common use case is for example to extract the version number and stamp your javascript files with it during a grunt run

example implementation (haven't tested it):

in your Gruntfile.js:

...
replace: {
    dist: {
        options: {
            variables: {
                version: '<%= pkg.version %>'
            }
        },
        prefix: '@@',
        files: [
            {expand: true, flatten: true, src: ['app/application.js', 'app/README.md'], dest: 'dist/'}
        ]
    }
}
...

application.js

...
var App = Framework.Application.create({
    VERSION: @@version,
...
like image 74
intuitivepixel Avatar answered Oct 18 '22 13:10

intuitivepixel


Use grunt.file.readJSON('package.json') to read json from a file for a grunt task.

like image 20
thecodejack Avatar answered Oct 18 '22 13:10

thecodejack