liudong
2023-05-29 340f156319b863525e50e900c58e59b86ecb3d5e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * grunt-chmod
 * https://github.com/JamesMGreene/grunt-chmod
 *
 * Copyright (c) 2013 James M. Greene
 * Licensed under the MIT license.
 */
 
'use strict';
 
var shelljs = require('shelljs');
 
module.exports = function(grunt) {
 
  // Please see the Grunt documentation for more information regarding task
  // creation: http://gruntjs.com/creating-tasks
 
  grunt.registerMultiTask('chmod', 'Modify file permissions, a la `chmod`.', function() {
    // Merge task-specific and/or target-specific options with these defaults.
    var options = this.options({
      mode: '',
      emit: false
    });
 
    var shouldEmit = options.emit === true;
    if (shouldEmit) {
      grunt.event.emit('chmod.taskTargetName', this.target);
    }
 
    var logError = createLogErrorFunc(shouldEmit);
    var taskFailure = createTaskFailureFunc(shouldEmit);
    var taskSuccess = createTaskSuccessFunc(shouldEmit);
 
    var mode = options.mode;
 
    // If there isn't any mode to set, then bail out
    if (!mode) {
      logError('No `mode` was specified in the task `options`. Task failed!');
      return taskFailure();
    }
    // If the mode set wasn't a string, then bail out
    if (typeof mode !== 'string') {
      logError('The `mode` specified in the task `options` was not a string. Task failed!');
      return taskFailure();
    }
 
    var fs = require('fs');
    var files = this.filesSrc;
 
    // Iterate over all specified file groups.
    files.forEach(function(path) {
      // Warn on and remove invalid source files (if nonull was set).
      if (!grunt.file.exists(path)) {
        logError('Source dir/file "' + path + '" not found.');
      }
 
      // Write the destination file.
      try {
        shelljs.chmod(mode, path);  //fs.chmodSync(path, mode);
      }
      catch (e) {
        logError('Failed to set `chmod` mode "' + mode + '" on dir/file: ' + path + '\n' + e);
      }
    });
 
    // Fail task if errors were logged.
    if (this.errorCount) {
      return taskFailure();
    }
 
    // Otherwise, print a success message.
    grunt.log.ok(files.length + ' file' + (files.length === 1 ? '' : 's') + ' had their `chmod` mode set to "' + mode + '".');
    return taskSuccess();
  });
 
  var createLogErrorFunc = function(shouldEmit) {
    if (shouldEmit) {
      return function(errorMsg) {
        grunt.event.emit('chmod.error', errorMsg);
        grunt.log.error(errorMsg);
      };
    }
    return function(errorMsg) {
      grunt.log.error(errorMsg);
    };
  };
 
  var createTaskFailureFunc = function(shouldEmit) {
    if (shouldEmit) {
      return function() {
        grunt.event.emit('chmod.fail');
        return false;
      };
    }
    return function() {
      return false;
    };
  };
 
  var createTaskSuccessFunc = function(shouldEmit) {
    if (shouldEmit) {
      return function() {
        grunt.event.emit('chmod.success');
        return true;
      };
    }
    return function() {
      return true;
    };
  };
 
};