How to use the file function from grunt

Find comprehensive JavaScript grunt.file code examples handpicked from public code repositorys.

51
52
53
54
55
56
57
58
59
60
BowerAssets.prototype = Object.create(Emitter.prototype);
BowerAssets.prototype.constructor = BowerAssets;

BowerAssets.prototype.get = function() {
  var bower = this.bower;
  var bowerConfig = grunt.file.readJSON(path.join(this.cwd, this.config));
  var exportsOverride = bowerConfig.exportsOverride;

  var paths = bower.commands.list({paths: true});
  paths.on('end', function(data) {
fork icon121
star icon495
watch icon13

+ 15 other calls in file

21
22
23
24
25
26
27
28
29
30
function existsFileInOutput(filename) {
  return fs.existsSync(path.join(outputDirectory, filename));
}

grunt.config.init({
  pkg: grunt.file.readJSON(path.join(__dirname, 'fixtures', 'app', 'resources', 'app', 'package.json')),
  'create-windows-installer': {
    config: {
      appDirectory: path.join(__dirname, 'fixtures', 'app'),
      outputDirectory: outputDirectory
fork icon104
star icon392
watch icon21

10
11
12
13
14
15
16
17
18
19
// the exported value of the file's module.
function loadConfig(path) {
        var object = {};
        var key;

        grunt.file.expand({ cwd: path + '/' }, '*.{js,txt}').forEach(function(filename) {
                key = filename.replace(/\.[^.]+$/,'');

                if (filename.indexOf('.txt') >= 0) {
                        object[key] = grunt.file.read(path + '/' + filename);
fork icon9
star icon301
watch icon15

+ 23 other calls in file

23
24
25
26
27
28
29
30
31
32
    initTasks(grunt, 'node_tasks')
    initConfigs(grunt, 'node_configs')
}

function initTasks(grunt, folderPath) {
    var pkg = grunt.file.readJSON('package.json')
    var tasks = pkg.devDependencies
    delete tasks.grunt
    for ( var task in tasks ) {
        grunt.loadNpmTasks(task)
fork icon1
star icon3
watch icon3

+ 5 other calls in file

1
2
3
4
5
6
7
8
9
10
var grunt = require('grunt');

module.exports = {
        get: function() {
                replacements = [];
                _.mapObject( grunt.file.readJSON("package.json"), function( val, key ) {

                        if ( !_.isObject(val) ){
                                replacements.push( {
                                        pattern: new RegExp( 'taskRunner_set_' + key, 'g'),
fork icon1
star icon3
watch icon1

12
13
14
15
16
17
18
19
20
21
  process.env.KARMA_BROWSERS = browsers;
  process.env.PROTRACTOR_BROWSERS = browsers;
}

Helpers.config = {
  pkg: grunt.file.readJSON('./package.json'),
  env: process.env
};

Helpers.loadConfig = function(path) {
fork icon45
star icon1
watch icon31

-1
module.exports = grunt.file.readJSON('package.json');
fork icon9
star icon301
watch icon15

145
146
147
148
149
150
151
152
153
154
```
var grunt = require('grunt')

// 配置
grunt.config.init({
    pkg: grunt.file.readJSON('package.json'), // 这里会去获取版本号
    'create-windows-installer': {
        x64: {
            authors: 'xiaobinwu <xiaobin_wu@yahoo.com>', // 作者
            projectUrl: '',
fork icon68
star icon268
watch icon8

27
28
29
30
31
32
33
34
35
36
};

var banner = (function () {


    var pkg = grunt.file.readJSON(path.join(__dirname, '..', '..', 'package.json'));
    var template = [
        '/**!',
        ' This notice must be untouched at all times.',
        '',
fork icon3
star icon0
watch icon5

0
1
2
3
4
5
6
7
8
9
var grunt = require('grunt');
// grunt.renameTask('http', 'http_pages');
// grunt.loadNpmTasks('grunt-http');

function getHttpPathsFromJSON(jsonPath) {
  var pages = grunt.file.readJSON(jsonPath),
      paths = {};

  grunt.util._.forEach(pages, function(page, page_name) {
    paths[page_name] = {
fork icon1
star icon2
watch icon3

140
141
142
143
144
145
146
147
148
149
delete expandOptions.src;
delete expandOptions.dest;

// Expand file mappings.
if (obj.expand) {
  return grunt.file.expandMapping(obj.src, obj.dest, expandOptions).map(function(mapObj) {
    // Copy obj properties to result.
    var result = grunt.util._.extend({}, obj);
    // Make a clone of the orig obj available.
    result.orig = grunt.util._.extend({}, obj);
fork icon3
star icon0
watch icon0

+ 11 other calls in file

60
61
62
63
64
65
66
67
68
69
  tmp: '.tmp',
  //hostname: 'localhost'
  hostname: nconf.get('SERVER_HOSTNAME')
};
var config = {
  pkg: grunt.file.readJSON('package.json'),
  env: process.env
};

grunt.util._.extend(config, loadConfig('./tasks/options/'));
fork icon16
star icon0
watch icon2

25
26
27
28
29
30
31
32
33
34
program
.command('build')
.description('Builds the current urza app.')
.action(function(type,info){
  // set grunt's dir to the urza dir
  grunt.file.setBase(__dirname + '/..')
  setup()
  
  grunt.tasks(['build'])
});
fork icon4
star icon10
watch icon3

+ 3 other calls in file

94
95
96
97
98
99
100
101
102
103

  // Prettify HTML and add a separator between any sibling components.
  var result = Utils.format(components[i], Utils.sep(slug));

  // Write the component to disk.
  grunt.file.write(path.join(dest, basename, slug + '.' + options.ext), result);
} else {
  if (cb2) {
    cb2(null, true);
  }
fork icon2
star icon4
watch icon7

+ 5 other calls in file

96
97
98
99
100
101
102
103
104
105
  return object1;
};

var iterateThroughFiles = function(filename){
  var relativePath = _this.data.src + '/' + filename;
  siteConfig = grunt.file.readJSON(relativePath);

  for (var domain in siteConfig) {
    siteConfig[domain].key = filename.split('.')[0];
  }
fork icon37
star icon0
watch icon2

+ 7 other calls in file

117
118
119
120
121
122
123
124
125

function readChanges() {
  var json;

  try {
    json = grunt.file.readJSON('tmp/changes.json');
  } catch(error) {
    json = {};
  }
fork icon12
star icon79
watch icon19

+ 3 other calls in file

39
40
41
42
43
44
45
46
47
48
var opt = makeOptions(options);

var oldCwd = process.cwd();
var cwd = opt.base != null ? opt.base : oldCwd;

grunt.file.setBase(cwd);

var gruntCliDir = opt.base ? (opt.base + "/") : "";

grunt.task.init([]);
fork icon22
star icon167
watch icon4

85
86
87
88
89
90
91
92
93

        if ( obody != body )
        {
            console.log ( 'Writing ', tfile );

            grunt.file.write(tfile, body);
        }
    });
};
fork icon7
star icon20
watch icon2

+ 3 other calls in file

19
20
21
22
23
24
25
26
27
// prase options
let opts    = this.options()
let data    = this.data
let destdir = opts.dest    || './'
let basedir = opts.basedir || './'
let src     = grunt.file.expand(data.src)
let fonts   = grunt.file.expand(join(basedir, this.target))
let getText = opts.getText!==undefined ? opts.getText : (content)=>html2text.fromString(content)
let type='woff', typeOpts=true
fork icon1
star icon2
watch icon2

+ 9 other calls in file

20
21
22
23
24
25
26
27
28
29
var findBowerJSON = function (BI) {
  var bowerJSON;

  grunt.util._.each(['bower.json', 'component.json'], function (configFile) {
    if (!helpers.is(bowerJSON, 'object') && grunt.file.isFile(configFile)) {
      bowerJSON = grunt.file.readJSON(configFile);

      if (configFile !== 'bower.json') {
        grunt.log.writeln();
        grunt.log.warn(configFile.yellow + ' is deprecated.'.yellow);
fork icon87
star icon0
watch icon1

+ 7 other calls in file