/usr/lib/nodejs/grunt-replace/tasks/replace.js is in node-grunt-replace 1.0.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | /*
* grunt-replace
*
* Copyright (c) 2016 outaTiME
* Licensed under the MIT license.
* https://github.com/outaTiME/grunt-replace/blob/master/LICENSE-MIT
*/
'use strict';
// plugin
module.exports = function(grunt) {
var path = require('path');
var fs = require('fs');
var chalk = require('chalk');
var _ = require('lodash');
var Applause = require('applause');
var fileSyncCmp = require('file-sync-cmp');
var isWindows = process.platform === 'win32';
// fns
var detectDestType = function(dest) {
if (_.endsWith(dest, '/')) {
return 'directory';
} else {
return 'file';
}
};
var unixifyPath = function(filepath) {
if (isWindows) {
return filepath.replace(/\\/g, '/');
} else {
return filepath;
}
};
var syncTimestamp = function(src, dest) {
var stat = fs.lstatSync(src);
if (path.basename(src) !== path.basename(dest)) {
return;
}
if (stat.isFile() && !fileSyncCmp.equalFiles(src, dest)) {
return;
}
var fd = fs.openSync(dest, isWindows ? 'r+' : 'r');
fs.futimesSync(fd, stat.atime, stat.mtime);
fs.closeSync(fd);
};
var replace = function(source, target, options, applause) {
var res;
grunt.file.copy(source, target, {
encoding: options.encoding,
process: function(content) {
res = applause.replace(content, [source, target]);
var result = res.content;
var count = res.count;
// force contents
if (count === 0) {
// no matches
if (options.force === true) {
result = content;
} else {
// ignore copy
result = false;
}
}
if (result !== false) {
grunt.verbose.writeln('Replace ' + chalk.cyan(source) + ' → ' +
chalk.green(target));
}
return result;
},
noProcess: options.noProcess || options.processContentExclude
});
return res;
};
// register task
grunt.registerMultiTask(
'replace',
'Replace text patterns with applause.',
function() {
// took options
var options = this.options({
encoding: grunt.file.defaultEncoding,
// processContent/processContentExclude deprecated renamed to process/noProcess
processContentExclude: [],
mode: false,
timestamp: false,
patterns: [],
excludeBuiltins: false,
force: true,
silent: false,
pedantic: false
});
// attach builtins
var patterns = options.patterns;
if (options.excludeBuiltins !== true) {
patterns.push({
match: '__SOURCE_FILE__',
replacement: function(match, offset, string, source) {
return source;
},
builtin: true
}, {
match: '__SOURCE_PATH__',
replacement: function(match, offset, string, source) {
return path.dirname(source);
},
builtin: true
}, {
match: '__SOURCE_FILENAME__',
replacement: function(match, offset, string, source) {
return path.basename(source);
},
builtin: true
}, {
match: '__TARGET_FILE__',
replacement: function(match, offset, string, source, target) {
return target;
},
builtin: true
}, {
match: '__TARGET_PATH__',
replacement: function(match, offset, string, source, target) {
return path.dirname(target);
},
builtin: true
}, {
match: '__TARGET_FILENAME__',
replacement: function(match, offset, string, source, target) {
return path.basename(target);
},
builtin: true
});
}
// create applause instance
var applause = Applause.create(_.extend({}, options, {
// pass
}));
// took code from copy task
var isExpandedPair;
var dirs = {};
var tally = {
dirs: 0,
files: 0,
replacements: 0,
details: []
};
this.files.forEach(function(filePair) {
isExpandedPair = filePair.orig.expand || false;
filePair.src.forEach(function(src) {
src = unixifyPath(src);
var dest = unixifyPath(filePair.dest);
if (detectDestType(dest) === 'directory') {
dest = (isExpandedPair) ? dest : path.join(dest, src);
}
if (grunt.file.isDir(src)) {
grunt.file.mkdir(dest);
if (options.mode !== false) {
fs.chmodSync(dest, (options.mode === true) ?
fs.lstatSync(src).mode : options.mode);
}
if (options.timestamp) {
dirs[dest] = src;
}
tally.dirs++;
} else {
var res = replace(src, dest, options, applause);
// TODO: detail will replaced by matches in applause 2.x
tally.details = tally.details.concat(res.detail);
tally.replacements += res.count;
syncTimestamp(src, dest);
if (options.mode !== false) {
fs.chmodSync(dest, (options.mode === true) ?
fs.lstatSync(src).mode : options.mode);
}
tally.files++;
}
if (options.mode !== false) {
fs.chmodSync(dest, (options.mode === true) ?
fs.lstatSync(src).mode : options.mode);
}
});
});
if (options.timestamp) {
Object.keys(dirs).sort(function(a, b) {
return b.length - a.length;
}).forEach(function(dest) {
syncTimestamp(dirs[dest], dest);
});
}
// warn for unmatched patterns in the file list
if (options.silent !== true) {
var count = 0;
patterns.forEach(function(pattern) {
if (pattern.builtin !== true) { // exclude builtins
var found = _.find(tally.details, ['source', pattern]);
if (!found) {
count++;
}
}
});
if (count > 0) {
var strWarn = [
'Unable to match ',
count,
count === 1 ? ' pattern' : ' patterns'
];
if (applause.options.usePrefix === true) {
strWarn.push(
', remember for simple matches (String) we are using the prefix ',
applause.options.prefix,
' for replacement lookup'
);
}
strWarn.push(
'.'
);
if (options.pedantic === true) {
grunt.fail.warn(strWarn.join(''));
} else {
grunt.log.warn(strWarn.join(''));
}
}
var str = [
tally.replacements,
tally.replacements === 1 ? ' replacement' : ' replacements',
' in ',
tally.files,
tally.files === 1 ? ' file' : ' files',
'.'
];
grunt.log.ok(str.join(''));
}
}
);
};
|