Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access absolute path to Gruntfile within Gruntfile?

My Gruntfile.js contains a task for Compass:

grunt.initConfig({

  pkg: grunt.file.readJSON('package.json'),

  // FILE PATHS (custom)
  dir: {
    css:          'resources/styles/css',
    fonts:        'resources/fonts',
    html:         'resources/html',
    images:       'resources/images',
    includes:     'resources/includes',
    node_modules: 'node_modules',
    prototypes:   'resources/html/prototypes',
    resources:    'resources',
    scripts:      'resources/scripts',
    scss:         'resources/styles/scss',
    styles:       'resources/styles',
    styleguide:   'resources/styleguide',
    vendor:       'resources/vendor',
    user: {
       htdocs: '/Volumes/VR\ Mini\ Backup/Backups/Web/Active/myproject/htdocs'
    }
  },

  // COMPASS
  compass: {
    dist: {
      options: {
        ...
        importPath: [
          '<%= dir.user.htdocs %>/<%= dir.resources %>/vendor',
          '<%= dir.user.htdocs %>/<%= dir.resources %>/fonts'
        ]

One of the Compass options is importPath. This expects an absolute/system path. An example importPath is /system/path/to/htdocs/resources/vendor.

As several developers will be working on my project, I'd like to make /system/path/to/htdocs dynamic.

Is there any way to access the path to Gruntfile.js from within this file?

Thanks.

like image 357
Dan Avatar asked Sep 19 '13 20:09

Dan


People also ask

How to run grunt file in command-line?

Installing the CLI. Run sudo npm install -g grunt-cli (Windows users should omit "sudo ", and may need to run the command-line with elevated privileges). The grunt command-line interface comes with a series of options. Use grunt -h from your terminal to show these options.

What is grunt file?

Grunt is a JavaScript task runner, a tool used to automatically perform frequent tasks such as minification, compilation, unit testing, and linting. It uses a command-line interface to run custom tasks defined in a file (known as a Gruntfile). Grunt was created by Ben Alman and is written in Node. js.

What is the grunt plugin?

grunt-templateby Mathias BynensGrunt plugin that interpolates template files with any data you provide and saves the result to another file. web-component-testerby The Polymer Project Authorsweb-component-tester makes testing your web components a breeze!


1 Answers

You can leverage Node's path module.

At the top of your gruntfile, add var path = require('path');.

path.resolve() will give you the absolute path.

like image 153
max Avatar answered Oct 05 '22 08:10

max