pax_global_header00006660000000000000000000000064127055046460014523gustar00rootroot0000000000000052 comment=2133feb6f055e06a10e171195d4d47afbb52026d grunt-replace-1.0.1/000077500000000000000000000000001270550464600142725ustar00rootroot00000000000000grunt-replace-1.0.1/.editorconfig000066400000000000000000000002541270550464600167500ustar00rootroot00000000000000root = true [*] indent_style = space indent_size = 2 end_of_line = lf insert_final_newline = true trim_trailing_whitespace = true [*.md] trim_trailing_whitespace = false grunt-replace-1.0.1/.gitattributes000066400000000000000000000000131270550464600171570ustar00rootroot00000000000000* text=autogrunt-replace-1.0.1/.gitignore000066400000000000000000000000511270550464600162560ustar00rootroot00000000000000node_modules npm-debug.log tmp .DS_Store grunt-replace-1.0.1/.jshintrc000066400000000000000000000003421270550464600161160ustar00rootroot00000000000000{ "boss": true, "curly": true, "eqeqeq": true, "eqnull": true, "immed": true, "latedef": true, "newcap": true, "noarg": true, "node": true, "sub": true, "undef": true, "unused": true, "mocha": true } grunt-replace-1.0.1/.travis.yml000066400000000000000000000002411270550464600164000ustar00rootroot00000000000000sudo: false language: node_js node_js: - "0.10" - "0.12" - "4" - "5" - "iojs" matrix: fast_finish: true cache: directories: - node_modules grunt-replace-1.0.1/Gruntfile.js000066400000000000000000000050161270550464600165710ustar00rootroot00000000000000 /* * grunt-replace * * Copyright (c) 2016 outaTiME * Licensed under the MIT license. * https://github.com/outaTiME/grunt-replace/blob/master/LICENSE-MIT */ 'use strict'; module.exports = function(grunt) { grunt.initConfig({ jshint: { options: { jshintrc: '.jshintrc' }, gruntfile: { src: 'Gruntfile.js' }, tasks: { src: ['tasks/**/*.js'] }, test: { src: ['test/**/*.js'] } }, clean: { test: ['tmp'] }, replace: { simple: { options: { variables: { key: 'value' } }, files: [ {expand: true, flatten: true, src: ['test/fixtures/simple.txt'], dest: 'tmp/'} ] }, verbose: { options: { variables: { key: 'value' } }, files: [ {expand: true, flatten: true, src: ['test/fixtures/verbose.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/'} ] }, fail: { options: { pedantic: true, patterns: [ { match: 'key', replacement: 'value' }, { match: 'undefined-key', replacement: 'value' } ] }, files: [ {expand: true, flatten: true, src: ['test/fixtures/fail.txt'], dest: 'tmp/'} ] }, 'built-in': { options: { // pass }, files: [ {expand: true, flatten: true, src: ['test/fixtures/built-in_*.txt'], dest: 'tmp/'} ] } }, mochaTest: { test: { options: { reporter: 'spec' }, src: '<%= jshint.test.src %>' } } }); grunt.loadTasks('tasks'); grunt.loadNpmTasks('grunt-contrib-jshint'); grunt.loadNpmTasks('grunt-contrib-clean'); grunt.loadNpmTasks('grunt-mocha-test'); grunt.registerTask('test', [ 'clean', 'replace:simple', 'replace:built-in', 'mochaTest' ]); grunt.registerTask('default', [ 'jshint', 'test' ]); }; grunt-replace-1.0.1/LICENSE-MIT000066400000000000000000000020341270550464600157250ustar00rootroot00000000000000Copyright (c) 2016 outaTiME Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. grunt-replace-1.0.1/README.md000066400000000000000000000373461270550464600155660ustar00rootroot00000000000000# grunt-replace [![Build Status](https://img.shields.io/travis/outaTiME/grunt-replace.svg)](https://travis-ci.org/outaTiME/grunt-replace) [![NPM Version](https://img.shields.io/npm/v/grunt-replace.svg)](https://npmjs.org/package/grunt-replace) > Replace text patterns with [applause](https://github.com/outaTiME/applause). ## Install From NPM: ```shell npm install grunt-replace --save-dev ``` ## Replace Task Assuming installation via NPM, you can use `grunt-replace` in your gruntfile like this: ```javascript module.exports = function (grunt) { grunt.loadNpmTasks('grunt-replace'); grunt.initConfig({ replace: { dist: { options: { patterns: [ { match: 'foo', replacement: 'bar' } ] }, files: [ {expand: true, flatten: true, src: ['src/index.html'], dest: 'build/'} ] } } }); grunt.registerTask('default', 'replace'); }; ``` ### Options #### patterns Type: `Array` Define patterns that will be used to replace the contents of source files. #### patterns.match Type: `String|RegExp` Indicates the matching expression. If matching type is `String` we use a simple variable lookup mechanism `@@string` (in any other case we use the default regexp replace logic): ```javascript { patterns: [ { match: 'foo', replacement: 'bar' // replaces "@@foo" to "bar" } ] } ``` #### patterns.replacement or patterns.replace Type: `String|Function|Object` Indicates the replacement for match, for more information about replacement check out the [String.replace]. You can specify a function as replacement. In this case, the function will be invoked after the match has been performed. The function's result (return value) will be used as the replacement string. ```javascript { patterns: [ { match: /foo/g, replacement: function () { return 'bar'; // replaces "foo" to "bar" } } ] } ``` Also supports object as replacement (we create string representation of object using [JSON.stringify]): ```javascript { patterns: [ { match: /foo/g, replacement: [1, 2, 3] // replaces "foo" with string representation of "array" object } ] } ``` > The replacement only resolve the [special replacement patterns] when using regexp for matching. [String.replace]: http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace [JSON.stringify]: http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify [special replacement patterns]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_string_as_a_parameter #### patterns.json Type: `Object` If an attribute `json` is found in pattern definition we flatten the object using `delimiter` concatenation and each key–value pair will be used for the replacement (simple variable lookup mechanism and no regexp support). ```javascript { patterns: [ { json: { "key": "value" // replaces "@@key" to "value" } } ] } ``` Also supports nested objects: ```javascript { patterns: [ { json: { "key": "value", // replaces "@@key" to "value" "inner": { // replaces "@@inner" with string representation of "inner" object "key": "value" // replaces "@@inner.key" to "value" } } } ] } ``` For deferred invocations is possible to define functions: ```javascript { patterns: [ { json: function (done) { done({ key: 'value' }); } } ] } ``` #### patterns.yaml Type: `String` If an attribute `yaml` found in pattern definition it will be converted and then processed like [json attribute](#patternsjson). ```javascript { patterns: [ { yaml: 'key: value' // replaces "@@key" to "value" } ] } ``` For deferred invocations is possible to define functions: ```javascript { patterns: [ { yaml: function (done) { done('key: value'); } } ] } ``` #### patterns.cson Type: `String` If an attribute `cson` is found in pattern definition it will be converted and then processed like [json attribute](#patternsjson). ```javascript { patterns: [ { cson: 'key: \'value\'' } ] } ``` For deferred invocations is possible to define functions: ```javascript { patterns: [ { cson: function (done) { done('key: \'value\''); } } ] } ``` #### variables Type: `Object` This is the old way to define patterns using plain object (simple variable lookup mechanism and no regexp support). You can still use this but for more control you should use the new `patterns` way. ```javascript { variables: { 'key': 'value' // replaces "@@key" to "value" } } ``` #### prefix Type: `String` Default: `@@` The prefix added for matching (prevent bad replacements / easy way). > This only applies for simple variable lookup mechanism. #### usePrefix Type: `Boolean` Default: `true` If set to `false`, we match the pattern without `prefix` concatenation (useful when you want to lookup a simple string). > This only applies for simple variable lookup mechanism. #### preservePrefix Type: `Boolean` Default: `false` If set to `true`, we preserve the `prefix` in target. > This only applies for simple variable lookup mechanism and when `patterns.replacement` is a string. #### delimiter Type: `String` Default: `.` The delimiter used to flatten when using object as replacement. #### preserveOrder Type: `Boolean` Default: `false` If set to `true`, we preserve the patterns definition order, otherwise these will be sorted (in ascending order) to prevent replacement issues like `head` / `header` (typo regexps will be resolved at last). #### excludeBuiltins Type: `Boolean` Default: `false` If set to `true`, we exclude built-in matching rules. #### force Type: `Boolean` Default: `true` Force the copy of files even when those files don't have any match found for replacement. #### noProcess Type: `String` This option is an advanced way to control which file contents are processed. > `processContentExclude` has been renamed to `noProcess` and the option name will be removed in the future. #### encoding Type: `String` Default: `grunt.file.defaultEncoding` The file encoding to copy files with. #### mode Type: `Boolean` or `Number` 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. #### timestamp Type: `Boolean` Default: `false` Whether to preserve the timestamp attributes(atime and mtime) when copying files. Set to true to preserve files timestamp. But timestamp will not be preserved when the file contents or name are changed during copying. #### silent Type: `Boolean` Default: `false` If set to `true`, removes the output from stdout. #### pedantic Type: `Boolean` Default: `false` If set to `true`, the task will fail with a `grunt.fail.warn` when no matches are present. ### Built-in Replacements Few matching rules are provided by default and can be used anytime (these will be affected by the `options` given): * `__SOURCE_FILE__`: Replace match with the source file. * `__SOURCE_PATH__`: Replace match with the path of source file. * `__SOURCE_FILENAME__`: Replace match with the filename of source file. * `__TARGET_FILE__`: Replace match with the target file. * `__TARGET_PATH__`: Replace match with the path of target file. * `__TARGET_FILENAME__`: Replace match with the filename of target file. > If you are looking how to use an `built-in` replacements, check out the [How to insert filename in target](#how-to-insert-filename-in-target) usage. ### Usage Examples #### Basic File `src/manifest.appcache`: ``` CACHE MANIFEST # @@timestamp CACHE: favicon.ico index.html NETWORK: * ``` Gruntfile, define pattern (for timestamp) and the source files for lookup: ```js replace: { dist: { options: { patterns: [ { match: 'timestamp', replacement: '<%= grunt.template.today() %>' } ] }, files: [ {expand: true, flatten: true, src: ['src/manifest.appcache'], dest: 'build/'} ] } } ``` #### Multiple matching File `src/manifest.appcache`: ``` CACHE MANIFEST # @@timestamp CACHE: favicon.ico index.html NETWORK: * ``` File `src/humans.txt`: ``` __ _ _ _/__ /./|,//_` /_//_// /_|/// //_, outaTiME v.@@version /* TEAM */ Web Developer / Graphic Designer: Ariel Oscar Falduto Site: http://www.outa.im Twitter: @outa7iME Contact: afalduto at gmail dot com From: Buenos Aires, Argentina /* SITE */ Last update: @@timestamp Standards: HTML5, CSS3, robotstxt.org, humanstxt.org Components: H5BP, Modernizr, jQuery, Twitter Bootstrap, LESS, Jade, Grunt Software: Sublime Text 2, Photoshop, LiveReload ``` Gruntfile: ```js replace: { dist: { options: { patterns: [ { match: 'version', replacement: '<%= pkg.version %>' }, { match: 'timestamp', replacement: '<%= grunt.template.today() %>' } ] }, files: [ {expand: true, flatten: true, src: ['src/manifest.appcache', 'src/humans.txt'], dest: 'build/'} ] } } ``` #### Cache busting File `src/assets/index.html`: ```html ``` Gruntfile: ```js replace: { dist: { options: { patterns: [ { match: 'timestamp', replacement: '<%= new Date().getTime() %>' } ] }, files: [ {src: ['src/assets/index.html'], dest: 'build/index.html'} ] } } ``` #### Include file File `src/index.html`: ```html @@include ``` Gruntfile: ```js replace: { dist: { options: { patterns: [ { match: 'include', replacement: '<%= grunt.file.read("includes/content.html") %>' } ] }, files: [ {expand: true, flatten: true, src: ['src/index.html'], dest: 'build/'} ] } } ``` #### Regular expression File `src/username.txt`: ``` John Smith ``` Gruntfile: ```js replace: { dist: { options: { patterns: [ { match: /(\w+)\s(\w+)/, replacement: '$2, $1' // replaces "John Smith" to "Smith, John" } ] }, files: [ {expand: true, flatten: true, src: ['src/username.txt'], dest: 'build/'} ] } } ``` #### Lookup for `foo` instead of `@@foo` Gruntfile: ```js // option 1 (explicitly using an regexp) replace: { dist: { options: { patterns: [ { match: /foo/g, replacement: 'bar' } ] }, files: [ {expand: true, flatten: true, src: ['src/foo.txt'], dest: 'build/'} ] } } // option 2 (easy way) replace: { dist: { options: { patterns: [ { match: 'foo', replacement: 'bar' } ], usePrefix: false }, files: [ {expand: true, flatten: true, src: ['src/foo.txt'], dest: 'build/'} ] } } // option 3 (old way) replace: { dist: { options: { patterns: [ { match: 'foo', replacement: 'bar' } ], prefix: '' // remove prefix }, files: [ {expand: true, flatten: true, src: ['src/foo.txt'], dest: 'build/'} ] } } ``` #### How to insert filename in target File `src/app.js`: ```js // filename: @@__SOURCE_FILENAME__ var App = App || (function () { return { // app contents }; }()); window.App = App; ``` Gruntfile: ```js replace: { dist: { options: { // pass, we use built-in replacements }, files: [ {expand: true, flatten: true, src: ['src/**/*.js'], dest: 'build/'} ] } } ``` ## Release History * 2016-04-19   v1.0.1   Fix bad README.md file. * 2016-04-19   v1.0.0   Add timestamp option to disable preserving timestamp when copying. Bump devDependencies. Point main to task and remove peerDeps. * 2015-09-09   v0.11.0   Improvements in handling patterns. Fix plain object representation issue. More test cases. * 2015-08-20   v0.10.2   Restore verbose after new pedantic option. * 2015-08-19   v0.10.0   Last [applause](https://github.com/outaTiME/applause) integration and package.json update. * 2015-08-06   v0.9.3   New pedantic option (thanks [@donkeybanana](https://github.com/donkeybanana)). Fix issue with special characters attributes ($$, $&, $`, $', $n or $nn) on JSON, YAML and CSON. * 2015-05-07   v0.9.2   Fix regression issue with empty string in replacement. * 2015-05-01   v0.9.1   Better output. * 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. Update to [applause](https://github.com/outaTiME/applause) v0.4.0. * 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. * 2014-04-20   v0.7.7   JSON / YAML / CSON as function supported. Readme updated (thanks [@milanlandaverde](https://github.com/milanlandaverde)). * 2014-03-23   v0.7.6   Readme updated. * 2014-03-22   v0.7.5   Modular core renamed to [applause](https://github.com/outaTiME/applause). Performance improvements. Expression flag removed. New pattern matching for CSON object. More test cases, readme updated and code cleanup. * 2014-03-21   v0.7.4   Test cases in Mocha, readme updated and code cleanup. * 2014-03-17   v0.7.3   Update script files for readme file generation. * 2014-03-12   v0.7.2   Typo error, replace task name again. * 2014-03-11   v0.7.1   Task name update. * 2014-03-11   v0.7.0   New [pattern-replace](https://github.com/outaTiME/pattern-replace) modular core for replacements. * 2014-02-13   v0.6.2   Attach process data for function replacements (source / target). Add delimiter option for object as replacement. Dependencies updated. * 2014-02-06   v0.6.1   Rename excludePrefix to preservePrefix (more readable) and adds usePrefix flag. Support the noProcess option like [grunt-contrib-copy](https://github.com/gruntjs/grunt-contrib-copy). * 2014-02-05   v0.6.0   Object replacement allowed. New excludePrefix flag (thanks [@shinnn](https://github.com/shinnn)). Encoding / Mode options added. * 2013-09-18   v0.5.1   New pattern matching for JSON object. * 2013-09-17   v0.5.0   Regular expression matching now supported and notation has been updated but is backward compatible. * 2013-05-03   v0.4.4   Fix escape $ before performing regexp replace (thanks [@warpech](https://github.com/warpech)). * 2013-04-14   v0.4.3   Detect path destinations correctly on Windows. * 2013-04-02   v0.4.2   Add peerDependencies and update description. * 2013-04-02   v0.4.1   Add trace when force flag. * 2013-02-28   v0.4.0   First official release for Grunt 0.4.0. * 2012-11-20   v0.3.2   New examples added. * 2012-09-25   v0.3.1   Rename grunt-contrib-lib dep to grunt-lib-contrib, add force flag. * 2012-09-25   v0.3.0   General cleanup and consolidation. Global options depreciated. --- Task submitted by [Ariel Falduto](http://outa.im/) grunt-replace-1.0.1/docs/000077500000000000000000000000001270550464600152225ustar00rootroot00000000000000grunt-replace-1.0.1/docs/README.md000066400000000000000000000257421270550464600165130ustar00rootroot00000000000000# grunt-replace [![Build Status](https://img.shields.io/travis/outaTiME/grunt-replace.svg)](https://travis-ci.org/outaTiME/grunt-replace) [![NPM Version](https://img.shields.io/npm/v/grunt-replace.svg)](https://npmjs.org/package/grunt-replace) > Replace text patterns with [applause](https://github.com/outaTiME/applause). ## Install From NPM: ```shell npm install grunt-replace --save-dev ``` ## Replace Task Assuming installation via NPM, you can use `grunt-replace` in your gruntfile like this: ```javascript module.exports = function (grunt) { grunt.loadNpmTasks('grunt-replace'); grunt.initConfig({ replace: { dist: { options: { patterns: [ { match: 'foo', replacement: 'bar' } ] }, files: [ {expand: true, flatten: true, src: ['src/index.html'], dest: 'build/'} ] } } }); grunt.registerTask('default', 'replace'); }; ``` ### Options @@options #### excludeBuiltins Type: `Boolean` Default: `false` If set to `true`, we exclude built-in matching rules. #### force Type: `Boolean` Default: `true` Force the copy of files even when those files don't have any match found for replacement. #### noProcess Type: `String` This option is an advanced way to control which file contents are processed. > `processContentExclude` has been renamed to `noProcess` and the option name will be removed in the future. #### encoding Type: `String` Default: `grunt.file.defaultEncoding` The file encoding to copy files with. #### mode Type: `Boolean` or `Number` 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. #### timestamp Type: `Boolean` Default: `false` Whether to preserve the timestamp attributes(atime and mtime) when copying files. Set to true to preserve files timestamp. But timestamp will not be preserved when the file contents or name are changed during copying. #### silent Type: `Boolean` Default: `false` If set to `true`, removes the output from stdout. #### pedantic Type: `Boolean` Default: `false` If set to `true`, the task will fail with a `grunt.fail.warn` when no matches are present. ### Built-in Replacements Few matching rules are provided by default and can be used anytime (these will be affected by the `options` given): * `__SOURCE_FILE__`: Replace match with the source file. * `__SOURCE_PATH__`: Replace match with the path of source file. * `__SOURCE_FILENAME__`: Replace match with the filename of source file. * `__TARGET_FILE__`: Replace match with the target file. * `__TARGET_PATH__`: Replace match with the path of target file. * `__TARGET_FILENAME__`: Replace match with the filename of target file. > If you are looking how to use an `built-in` replacements, check out the [How to insert filename in target](#how-to-insert-filename-in-target) usage. ### Usage Examples #### Basic File `src/manifest.appcache`: ``` CACHE MANIFEST # @@timestamp CACHE: favicon.ico index.html NETWORK: * ``` Gruntfile, define pattern (for timestamp) and the source files for lookup: ```js replace: { dist: { options: { patterns: [ { match: 'timestamp', replacement: '<%= grunt.template.today() %>' } ] }, files: [ {expand: true, flatten: true, src: ['src/manifest.appcache'], dest: 'build/'} ] } } ``` #### Multiple matching File `src/manifest.appcache`: ``` CACHE MANIFEST # @@timestamp CACHE: favicon.ico index.html NETWORK: * ``` File `src/humans.txt`: ``` __ _ _ _/__ /./|,//_` /_//_// /_|/// //_, outaTiME v.@@version /* TEAM */ Web Developer / Graphic Designer: Ariel Oscar Falduto Site: http://www.outa.im Twitter: @outa7iME Contact: afalduto at gmail dot com From: Buenos Aires, Argentina /* SITE */ Last update: @@timestamp Standards: HTML5, CSS3, robotstxt.org, humanstxt.org Components: H5BP, Modernizr, jQuery, Twitter Bootstrap, LESS, Jade, Grunt Software: Sublime Text 2, Photoshop, LiveReload ``` Gruntfile: ```js replace: { dist: { options: { patterns: [ { match: 'version', replacement: '<%= pkg.version %>' }, { match: 'timestamp', replacement: '<%= grunt.template.today() %>' } ] }, files: [ {expand: true, flatten: true, src: ['src/manifest.appcache', 'src/humans.txt'], dest: 'build/'} ] } } ``` #### Cache busting File `src/assets/index.html`: ```html ``` Gruntfile: ```js replace: { dist: { options: { patterns: [ { match: 'timestamp', replacement: '<%= new Date().getTime() %>' } ] }, files: [ {src: ['src/assets/index.html'], dest: 'build/index.html'} ] } } ``` #### Include file File `src/index.html`: ```html @@include ``` Gruntfile: ```js replace: { dist: { options: { patterns: [ { match: 'include', replacement: '<%= grunt.file.read("includes/content.html") %>' } ] }, files: [ {expand: true, flatten: true, src: ['src/index.html'], dest: 'build/'} ] } } ``` #### Regular expression File `src/username.txt`: ``` John Smith ``` Gruntfile: ```js replace: { dist: { options: { patterns: [ { match: /(\w+)\s(\w+)/, replacement: '$2, $1' // replaces "John Smith" to "Smith, John" } ] }, files: [ {expand: true, flatten: true, src: ['src/username.txt'], dest: 'build/'} ] } } ``` #### Lookup for `foo` instead of `@@foo` Gruntfile: ```js // option 1 (explicitly using an regexp) replace: { dist: { options: { patterns: [ { match: /foo/g, replacement: 'bar' } ] }, files: [ {expand: true, flatten: true, src: ['src/foo.txt'], dest: 'build/'} ] } } // option 2 (easy way) replace: { dist: { options: { patterns: [ { match: 'foo', replacement: 'bar' } ], usePrefix: false }, files: [ {expand: true, flatten: true, src: ['src/foo.txt'], dest: 'build/'} ] } } // option 3 (old way) replace: { dist: { options: { patterns: [ { match: 'foo', replacement: 'bar' } ], prefix: '' // remove prefix }, files: [ {expand: true, flatten: true, src: ['src/foo.txt'], dest: 'build/'} ] } } ``` #### How to insert filename in target File `src/app.js`: ```js // filename: @@__SOURCE_FILENAME__ var App = App || (function () { return { // app contents }; }()); window.App = App; ``` Gruntfile: ```js replace: { dist: { options: { // pass, we use built-in replacements }, files: [ {expand: true, flatten: true, src: ['src/**/*.js'], dest: 'build/'} ] } } ``` ## Release History * 2016-04-19   v1.0.1   Fix bad README.md file. * 2016-04-19   v1.0.0   Add timestamp option to disable preserving timestamp when copying. Bump devDependencies. Point main to task and remove peerDeps. * 2015-09-09   v0.11.0   Improvements in handling patterns. Fix plain object representation issue. More test cases. * 2015-08-20   v0.10.2   Restore verbose after new pedantic option. * 2015-08-19   v0.10.0   Last [applause](https://github.com/outaTiME/applause) integration and package.json update. * 2015-08-06   v0.9.3   New pedantic option (thanks [@donkeybanana](https://github.com/donkeybanana)). Fix issue with special characters attributes ($$, $&, $`, $', $n or $nn) on JSON, YAML and CSON. * 2015-05-07   v0.9.2   Fix regression issue with empty string in replacement. * 2015-05-01   v0.9.1   Better output. * 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. Update to [applause](https://github.com/outaTiME/applause) v0.4.0. * 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. * 2014-04-20   v0.7.7   JSON / YAML / CSON as function supported. Readme updated (thanks [@milanlandaverde](https://github.com/milanlandaverde)). * 2014-03-23   v0.7.6   Readme updated. * 2014-03-22   v0.7.5   Modular core renamed to [applause](https://github.com/outaTiME/applause). Performance improvements. Expression flag removed. New pattern matching for CSON object. More test cases, readme updated and code cleanup. * 2014-03-21   v0.7.4   Test cases in Mocha, readme updated and code cleanup. * 2014-03-17   v0.7.3   Update script files for readme file generation. * 2014-03-12   v0.7.2   Typo error, replace task name again. * 2014-03-11   v0.7.1   Task name update. * 2014-03-11   v0.7.0   New [pattern-replace](https://github.com/outaTiME/pattern-replace) modular core for replacements. * 2014-02-13   v0.6.2   Attach process data for function replacements (source / target). Add delimiter option for object as replacement. Dependencies updated. * 2014-02-06   v0.6.1   Rename excludePrefix to preservePrefix (more readable) and adds usePrefix flag. Support the noProcess option like [grunt-contrib-copy](https://github.com/gruntjs/grunt-contrib-copy). * 2014-02-05   v0.6.0   Object replacement allowed. New excludePrefix flag (thanks [@shinnn](https://github.com/shinnn)). Encoding / Mode options added. * 2013-09-18   v0.5.1   New pattern matching for JSON object. * 2013-09-17   v0.5.0   Regular expression matching now supported and notation has been updated but is backward compatible. * 2013-05-03   v0.4.4   Fix escape $ before performing regexp replace (thanks [@warpech](https://github.com/warpech)). * 2013-04-14   v0.4.3   Detect path destinations correctly on Windows. * 2013-04-02   v0.4.2   Add peerDependencies and update description. * 2013-04-02   v0.4.1   Add trace when force flag. * 2013-02-28   v0.4.0   First official release for Grunt 0.4.0. * 2012-11-20   v0.3.2   New examples added. * 2012-09-25   v0.3.1   Rename grunt-contrib-lib dep to grunt-lib-contrib, add force flag. * 2012-09-25   v0.3.0   General cleanup and consolidation. Global options depreciated. --- Task submitted by [Ariel Falduto](http://outa.im/) grunt-replace-1.0.1/package.json000066400000000000000000000023031270550464600165560ustar00rootroot00000000000000{ "name": "grunt-replace", "description": "Replace text patterns with applause.", "version": "1.0.1", "author": { "name": "outaTiME", "url": "http://outa.im/" }, "repository": "outaTiME/grunt-replace", "license": "MIT", "engines": { "node": ">=0.10.0" }, "main": "tasks/replace.js", "scripts": { "release": "scripts/release.sh", "test": "grunt test" }, "dependencies": { "chalk": "^1.1.0", "lodash": "^4.11.0", "applause": "1.2.2", "file-sync-cmp": "^0.1.0" }, "devDependencies": { "grunt": "^1.0.0", "grunt-contrib-clean": "^1.0.0", "grunt-contrib-jshint": "^1.0.0", "grunt-mocha-test": "^0.12.0", "mocha": "^2.4.5" }, "keywords": [ "gruntplugin", "replace", "replacement", "pattern", "patterns", "match", "text", "string", "regex", "regexp", "json", "yaml", "cson", "flatten" ], "files": [ "tasks" ], "eslintConfig": { "extends": [ "eslint:recommended" ], "env": { "node": true, "mocha": true }, "rules": { "quotes": [ 2, "single" ], "indent": [ 2, 2 ] } } } grunt-replace-1.0.1/scripts/000077500000000000000000000000001270550464600157615ustar00rootroot00000000000000grunt-replace-1.0.1/scripts/generate.js000066400000000000000000000021541270550464600201130ustar00rootroot00000000000000 /* * grunt-replace * http://gruntjs.com/ * * Copyright (c) 2016 outaTiME * Licensed under the MIT license. * https://github.com/outaTiME/grunt-replace/blob/master/LICENSE-MIT */ var fs = require('fs'); var filename = 'node_modules/applause/README.md'; var readme = fs.readFileSync(filename, 'utf8'); // initialize section var sections = {}; // http://regex101.com/r/wJ2wW8 var pattern = /(\n#{3}\s)(.*)([\s\S]*?)(?=\1|$)/ig; var match; while ((match = pattern.exec(readme)) !== null) { var section = match[2]; var contents = match[3]; // trace /* var msg = "Found " + section + " → "; msg += "Next match starts at " + pattern.lastIndex; console.log(msg); */ sections[section] = contents; } // write readme var Applause = require('applause'); var options = { patterns: [{ match: 'options', replacement: function() { var name = 'Applause Options'; return sections[name] || '_(Coming soon)_'; // empty } }] }; var applause = Applause.create(options); var result = applause.replace(fs.readFileSync('docs/README.md', 'utf8')); fs.writeFileSync('README.md', result.content, 'utf8'); grunt-replace-1.0.1/scripts/release.sh000077500000000000000000000010121270550464600177320ustar00rootroot00000000000000#!/bin/sh # bail immediately on error set -e # execute test files npm test # documentation build node scripts/generate.js CHANGES=$(git diff --numstat | wc -l) CHANGES_CACHED=$(git diff --cached --numstat | wc -l) TOTAL_CHANGES=$(($CHANGES + $CHANGES_CACHED)) # create the commit, tag the commit with the proper version if [ $TOTAL_CHANGES -ne "0" ] then git add --all git commit -am "Version $npm_package_version released." fi git tag $npm_package_version git push git push --tags # publish to npm npm publish grunt-replace-1.0.1/tasks/000077500000000000000000000000001270550464600154175ustar00rootroot00000000000000grunt-replace-1.0.1/tasks/replace.js000066400000000000000000000156361270550464600174030ustar00rootroot00000000000000 /* * 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('')); } } ); }; grunt-replace-1.0.1/test/000077500000000000000000000000001270550464600152515ustar00rootroot00000000000000grunt-replace-1.0.1/test/fixtures/000077500000000000000000000000001270550464600171225ustar00rootroot00000000000000grunt-replace-1.0.1/test/fixtures/built-in_source_file.txt000066400000000000000000000000221270550464600237570ustar00rootroot00000000000000@@__SOURCE_FILE__ grunt-replace-1.0.1/test/fixtures/built-in_source_filename.txt000066400000000000000000000000261270550464600246240ustar00rootroot00000000000000@@__SOURCE_FILENAME__ grunt-replace-1.0.1/test/fixtures/built-in_source_path.txt000066400000000000000000000000221270550464600237740ustar00rootroot00000000000000@@__SOURCE_PATH__ grunt-replace-1.0.1/test/fixtures/built-in_target_file.txt000066400000000000000000000000221270550464600237450ustar00rootroot00000000000000@@__TARGET_FILE__ grunt-replace-1.0.1/test/fixtures/built-in_target_filename.txt000066400000000000000000000000261270550464600246120ustar00rootroot00000000000000@@__TARGET_FILENAME__ grunt-replace-1.0.1/test/fixtures/built-in_target_path.txt000066400000000000000000000000221270550464600237620ustar00rootroot00000000000000@@__TARGET_PATH__ grunt-replace-1.0.1/test/fixtures/fail.txt000066400000000000000000000000061270550464600205720ustar00rootroot00000000000000@@key grunt-replace-1.0.1/test/fixtures/simple.txt000066400000000000000000000000061270550464600211500ustar00rootroot00000000000000@@key grunt-replace-1.0.1/test/fixtures/verbose.txt000066400000000000000000000000061270550464600213240ustar00rootroot00000000000000@@key grunt-replace-1.0.1/test/fixtures/warning.txt000066400000000000000000000000061270550464600213240ustar00rootroot00000000000000@@key grunt-replace-1.0.1/test/replace_test.js000066400000000000000000000061151270550464600202640ustar00rootroot00000000000000 /* * grunt-replace * * Copyright (c) 2016 outaTiME * Licensed under the MIT license. * https://github.com/outaTiME/grunt-replace/blob/master/LICENSE-MIT */ 'use strict'; // dependencies var assert = require('assert'); var grunt = require('grunt'); var path = require('path'); var exec = require('child_process').exec; // test describe('grunt-replace', function() { var expect; var result; it('should replace simple key with value', function(done) { expect = 'value\n'; result = grunt.file.read('tmp/simple.txt'); assert.equal(result, expect); done(); } ); it('should verbose when "silent" is false', function(done) { exec('grunt replace:verbose', { cwd: path.join(__dirname, '..') }, function(error, stdout) { assert.notEqual(stdout.indexOf('1 replacement in 1 file.'), -1); done(); }); } ); it('should warn when no matches exist', function(done) { exec('grunt replace:warning', { cwd: path.join(__dirname, '..') }, function(error, stdout) { assert.equal(stdout.indexOf('Warning: Unable to match 1 pattern'), -1); done(); }); } ); it('should fail when no matches exist and "pedantic" is true', function(done) { exec('grunt replace:fail', { cwd: path.join(__dirname, '..') }, function(error, stdout) { assert.equal(error.code, 6); assert.notEqual(stdout.indexOf('Warning: Unable to match 1 pattern'), -1); done(); }); } ); // built-in it('should replace using built-in replacement (__SOURCE_FILE__)', function(done) { expect = 'test/fixtures/built-in_source_file.txt\n'; result = grunt.file.read('tmp/built-in_source_file.txt'); assert.equal(result, expect); done(); } ); it('should replace using built-in replacement (__SOURCE_PATH__)', function(done) { expect = 'test/fixtures\n'; result = grunt.file.read('tmp/built-in_source_path.txt'); assert.equal(result, expect); done(); } ); it('should replace using built-in replacement (__SOURCE_FILENAME__)', function(done) { expect = 'built-in_source_filename.txt\n'; result = grunt.file.read('tmp/built-in_source_filename.txt'); assert.equal(result, expect); done(); } ); it('should replace using built-in replacement (__TARGET_FILE__)', function(done) { expect = 'tmp/built-in_target_file.txt\n'; result = grunt.file.read('tmp/built-in_target_file.txt'); assert.equal(result, expect); done(); } ); it('should replace using built-in replacement (__TARGET_PATH__)', function(done) { expect = 'tmp\n'; result = grunt.file.read('tmp/built-in_target_path.txt'); assert.equal(result, expect); done(); } ); it('should replace using built-in replacement (__TARGET_FILENAME__)', function(done) { expect = 'built-in_target_filename.txt\n'; result = grunt.file.read('tmp/built-in_target_filename.txt'); assert.equal(result, expect); done(); } ); });