Skip to content

Commit

Permalink
Fixes #43, add new silent flag and closes #62.
Browse files Browse the repository at this point in the history
  • Loading branch information
outaTiME committed Apr 30, 2015
1 parent 3813eb6 commit 6b201a2
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 42 deletions.
19 changes: 18 additions & 1 deletion Gruntfile.js
Expand Up @@ -2,7 +2,7 @@
/*
* grunt-replace
*
* Copyright (c) 2014 outaTiME
* Copyright (c) 2015 outaTiME
* Licensed under the MIT license.
* https://github.com/outaTiME/grunt-replace/blob/master/LICENSE-MIT
*/
Expand Down Expand Up @@ -43,6 +43,23 @@ module.exports = function (grunt) {
{expand: true, flatten: true, src: ['test/fixtures/simple.txt'], dest: 'tmp/'}
]
},
warning: {
options: {
patterns: [
/* {
match: 'key',
replacement: 'value'
}, */
{
match: 'undefined-key',
replacement: 'value'
}
]
},
files: [
{expand: true, flatten: true, src: ['test/fixtures/warning.txt'], dest: 'tmp/'}
]
},
'built-in': {
options: {
// pass
Expand Down
2 changes: 1 addition & 1 deletion LICENSE-MIT
@@ -1,4 +1,4 @@
Copyright (c) 2014 outaTiME
Copyright (c) 2015 outaTiME

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
Expand Down
7 changes: 7 additions & 0 deletions docs/README.md
Expand Up @@ -75,6 +75,12 @@ Default: `false`

Whether to copy or set the existing file permissions. Set to `true` to copy the existing file permissions. Or set to the mode, i.e.: `0644`, that copied files will be set to.

#### silent
Type: `Boolean`
Default: `false`

If set to `true`, removes the output from stdout.

### Built-in Replacements

Few matching rules are provided by default and can be used anytime (these will be affected by the `options` given):
Expand Down Expand Up @@ -392,6 +398,7 @@ replace: {

## Release History

* 2015-05-01   v0.9.0   Output available via --verbose flag. The mode option now also applies to directories. Fix path issue on Windows. Display warning message when no matches and overall of replacements.
* 2014-10-10   v0.8.0   Escape regexp when matching type is `String`.
* 2014-08-26   v0.7.9   Fixes backwards incompatible changes introduced in NPM.
* 2014-06-10   v0.7.8   Remove node v.8.0 support and third party dependencies updated. Force flag now are true by default.
Expand Down
13 changes: 7 additions & 6 deletions package.json
@@ -1,7 +1,7 @@
{
"name": "grunt-replace",
"description": "Replace text patterns with applause.",
"version": "0.8.0",
"version": "0.9.0",
"homepage": "http://github.com/outaTiME/grunt-replace",
"author": {
"name": "outaTiME",
Expand All @@ -21,25 +21,26 @@
}
],
"engines": {
"node": ">= 0.10.0"
"node": ">=0.10.0"
},
"scripts": {
"release": "scripts/release.sh",
"test": "grunt test"
},
"dependencies": {
"chalk": "^0.5.0",
"applause": "0.3.4"
"chalk": "^1.0.0",
"lodash": "^3.1.0",
"applause": "0.4.0"
},
"devDependencies": {
"grunt": "^0.4.0",
"grunt-contrib-clean": "^0.6.0",
"grunt-contrib-jshint": "^0.10.0",
"grunt-contrib-jshint": "^0.11.0",
"grunt-mocha-test": "^0.12.0",
"grunt-contrib-watch": "^0.6.0"
},
"peerDependencies": {
"grunt": "~0.4.0"
"grunt": ">=0.4.0"
},
"keywords": [
"gruntplugin",
Expand Down
2 changes: 1 addition & 1 deletion scripts/generate.js
Expand Up @@ -3,7 +3,7 @@
* grunt-replace
* http://gruntjs.com/
*
* Copyright (c) 2014 outaTiME
* Copyright (c) 2015 outaTiME
* Licensed under the MIT license.
* https://github.com/outaTiME/grunt-replace/blob/master/LICENSE-MIT
*/
Expand Down
117 changes: 85 additions & 32 deletions tasks/replace.js
Expand Up @@ -2,7 +2,7 @@
/*
* grunt-replace
*
* Copyright (c) 2014 outaTiME
* Copyright (c) 2015 outaTiME
* Licensed under the MIT license.
* https://github.com/outaTiME/grunt-replace/blob/master/LICENSE-MIT
*/
Expand All @@ -16,6 +16,7 @@ module.exports = function (grunt) {
var path = require('path');
var fs = require('fs');
var chalk = require('chalk');
var _ = require('lodash');
var Applause = require('applause');

grunt.registerMultiTask('replace', 'Replace text patterns with applause.', function () {
Expand All @@ -24,12 +25,13 @@ module.exports = function (grunt) {

var options = this.options({
encoding: grunt.file.defaultEncoding,
mode: false,
// processContent/processContentExclude deprecated renamed to process/noProcess
processContentExclude: [],
mode: false,
patterns: [],
excludeBuiltins: false,
force: true,
verbose: true
silent: false
});

// attach builtins
Expand All @@ -41,74 +43,122 @@ module.exports = function (grunt) {
match: '__SOURCE_FILE__',
replacement: function (match, offset, string, source, target) {
return source;
}
},
builtin: true
}, {
match: '__SOURCE_PATH__',
replacement: function (match, offset, string, source, target) {
return path.dirname(source);
}
},
builtin: true
}, {
match: '__SOURCE_FILENAME__',
replacement: function (match, offset, string, source, target) {
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(options);
var applause = Applause.create(_.extend({}, options, {
// private
detail: true
}));

// took code from copy task

var dest;
var isExpandedPair;
var tally = {
dirs: 0,
files: 0,
replacements: 0,
details: []
};

this.files.forEach(function (filePair) {
isExpandedPair = filePair.orig.expand || false;
var dest = filePair.dest;
var isExpandedPair = filePair.orig.expand || false;
filePair.src.forEach(function (src) {
if (detectDestType(filePair.dest) === 'directory') {
dest = (isExpandedPair) ? filePair.dest : unixifyPath(path.join(filePair.dest, src));
} else {
dest = filePair.dest;
src = unixifyPath(src);
dest = unixifyPath(dest);
if (detectDestType(dest) === 'directory') {
dest = (isExpandedPair) ? dest : path.join(dest, src);
}
if (grunt.file.isDir(src)) {
grunt.file.mkdir(dest);
tally.dirs++;
} else {
replace(src, dest, options, applause);
if (options.mode !== false) {
fs.chmodSync(dest, (options.mode === true) ? fs.lstatSync(src).mode : options.mode);
}
var res = replace(src, dest, options, applause);
tally.details = tally.details.concat(res.detail);
tally.replacements += res.count;
tally.files++;
}
if (options.mode !== false) {
fs.chmodSync(dest, (options.mode === true) ? fs.lstatSync(src).mode : options.mode);
}
});
});

patterns.forEach(function (pattern){
if (pattern.found !== true){
grunt.log.warn('Unable to match pattern: ' + pattern.match);
// 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'
);
}
grunt.log.warn(strWarn.join(''));
}
});
var str = [
chalk.cyan(tally.replacements),
tally.replacements === 1 ? ' replacement' : ' replacements',
' in ',
chalk.cyan(tally.files),
tally.files === 1 ? ' file' : ' files'
];
grunt.log.writeln(str.join(''));
}

});

var detectDestType = function (dest) {
var lastChar = dest.slice(-1);
if (lastChar === '/') {
if (_.endsWith(dest, '/')) {
return 'directory';
} else {
return 'file';
Expand All @@ -124,22 +174,25 @@ module.exports = function (grunt) {
};

var replace = function (source, target, options, applause) {
var res;
grunt.file.copy(source, target, {
encoding: options.encoding,
process: function (contents) {
var result = applause.replace(contents, [source, target]);
process: function (content) {
res = applause.replace(content, [source, target]);
var result = res.content;
// force contents
if (result === false && options.force === true) {
result = contents;
result = content;
}
if (result !== false && options.verbose === true) {
grunt.log.writeln('Replace ' + chalk.cyan(source) + ' → ' +
if (result !== false) {
grunt.verbose.writeln('Replace ' + chalk.cyan(source) + ' → ' +
chalk.green(target));
}
return result;
},
noProcess: options.noProcess || options.processContentExclude
});
return res;
};

};
1 change: 1 addition & 0 deletions test/fixtures/warning.txt
@@ -0,0 +1 @@
@@key
2 changes: 1 addition & 1 deletion test/replace_test.js
Expand Up @@ -2,7 +2,7 @@
/*
* grunt-replace
*
* Copyright (c) 2014 outaTiME
* Copyright (c) 2015 outaTiME
* Licensed under the MIT license.
* https://github.com/outaTiME/grunt-replace/blob/master/LICENSE-MIT
*/
Expand Down

0 comments on commit 6b201a2

Please sign in to comment.