Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use the "version" property in package.json as the app's directory in grunt?

I'm managing a project written in angularjs, with the default directory structure, excluding the app directory which uses versioning (ie. "app/0.0.0/", "app/0.1.0/" etc..).

I'm trying to use grunt's package.json file's "version" property to load the correct directory so I won't have to manually change the app path in gruntfile.js but for some reason I keep getting "Cannot GET /" when I run "grunt server".

To better explain this, here's a sample of my gruntfile.js:

var yeomanConfig = {
app: 'app/<% pkg.version %>/',
dist: 'dist'
...

grunt.initConfig({
  yeoman: yeomanConfig,
  pkg: grunt.file.readJSON('package.json'),
...

If I manually change the app property to "app/0.0.0" it works like a charm so I'm guessing this has something to do with the templating.

Any ideas?

Thank you very much for the help.

Edit: Thank you for the correction Andreas and Matjaz, but this doesn't solve the problem and gives the same error... This solves the problem for me but without the templating system:

var pkgVersion = grunt.file.readJSON('package.json').version;
// configurable paths
var yeomanConfig = {
  app: 'app/'+pkgVersion,
  dist: 'dist'
};

It's pretty ugly but it works. Hoping for a proper solution.

like image 303
onearmfrog Avatar asked Oct 01 '13 11:10

onearmfrog


People also ask

What is directory in package json?

json is a file in the root directory of a Node. js project that holds various information relevant to the project. This file gives information to npm that allows it to identify the project as well as handle the project's dependencies.

Is grunt still used?

The Grunt community is still going strong and both tools look like they're going to be around for a while yet. I should mention that another up and coming alternative to task runners like Grunt and Gulp is simply using npm scripts with command-line tools.


2 Answers

The best way to handle above scenario is that, In the grunt.initConfig define the package.json

grunt.initConfig({
  pkg: grunt.file.readJSON("package.json")
})

Once above initialized you can use the properties of the package.json throughout the grunt.js file.

<%= pkg.version %>
<%= pkg.homepage %>
like image 105
kds Avatar answered Oct 21 '22 03:10

kds


To echo data use <%=:

<%= pkg.version %>
like image 44
Matjaz Lipus Avatar answered Oct 21 '22 05:10

Matjaz Lipus