pax_global_header00006660000000000000000000000064140440335230014510gustar00rootroot0000000000000052 comment=fb1df5d0e267b48cb7ba8cf25f784989dd7e63a3 grunt-replace-2.0.2/000077500000000000000000000000001404403352300142615ustar00rootroot00000000000000grunt-replace-2.0.2/.editorconfig000066400000000000000000000002231404403352300167330ustar00rootroot00000000000000root = true [*] indent_style = space indent_size = 2 end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true grunt-replace-2.0.2/.gitattributes000066400000000000000000000000231404403352300171470ustar00rootroot00000000000000* text=auto eol=lf grunt-replace-2.0.2/.github/000077500000000000000000000000001404403352300156215ustar00rootroot00000000000000grunt-replace-2.0.2/.github/workflows/000077500000000000000000000000001404403352300176565ustar00rootroot00000000000000grunt-replace-2.0.2/.github/workflows/main.yml000066400000000000000000000006641404403352300213330ustar00rootroot00000000000000name: CI on: - push - pull_request jobs: test: name: Node.js ${{ matrix.node-version }} runs-on: ubuntu-latest strategy: fail-fast: false matrix: node-version: - 14 - 12 - 10 steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v1 with: node-version: ${{ matrix.node-version }} - run: npm install - run: npm test grunt-replace-2.0.2/.gitignore000066400000000000000000000000271404403352300162500ustar00rootroot00000000000000node_modules yarn.lock grunt-replace-2.0.2/.npmrc000066400000000000000000000000231404403352300153740ustar00rootroot00000000000000package-lock=false grunt-replace-2.0.2/CHANGELOG.md000066400000000000000000000005411404403352300160720ustar00rootroot00000000000000## [2.0.2](https://github.com/outaTiME/grunt-replace/compare/2.0.1...2.0.2) (2021-05-03) ### Bug Fixes * upgrade lodash version to 4.17.21 regarding CWE-78 ([a5f3785](https://github.com/outaTiME/grunt-replace/commit/a5f3785aebfcc8b9b3835c68144e284313651d6a)) ## [2.0.2](https://github.com/outaTiME/grunt-replace/compare/2.0.1...2.0.2) (2021-05-03) grunt-replace-2.0.2/README.md000066400000000000000000000167761404403352300155610ustar00rootroot00000000000000# grunt-replace [![Build Status](https://img.shields.io/travis/outaTiME/grunt-replace.svg)](https://travis-ci.org/outaTiME/grunt-replace) [![Version](https://img.shields.io/npm/v/grunt-replace.svg)](https://www.npmjs.com/package/grunt-replace) ![Prerequisite](https://img.shields.io/badge/node-%3E%3D10-blue.svg) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](#) [![Twitter: outa7iME](https://img.shields.io/twitter/follow/outa7iME.svg?style=social)](https://twitter.com/outa7iME) > Replace text patterns with [applause](https://github.com/outaTiME/applause). ## Install From NPM: ```shell npm install grunt-replace --save-dev ``` ## Usage Assuming installation via NPM, you can use `grunt-replace` in your gruntfile like this: ```javascript module.exports = function (grunt) { grunt.initConfig({ replace: { dist: { options: { patterns: [ { match: 'foo', replacement: 'bar' } ] }, files: [ { expand: true, flatten: true, src: ['src/index.html'], dest: 'build/' } ] } } }); grunt.loadNpmTasks('grunt-replace'); grunt.registerTask('default', ['replace']); }; ``` ## Options Supports all the applause [options](https://github.com/outaTiME/applause#options) in addition to the ones below. ### excludeBuiltins Type: `Boolean` Default: `false` If set to `true`, the built-in matching rules are excluded. ### 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. ## Examples ### Basic File `src/manifest.appcache`: ``` CACHE MANIFEST # @@timestamp CACHE: favicon.ico index.html NETWORK: * ``` Task configuration on gruntfile: ```javascript { options: { patterns: [ { match: 'timestamp', replacement: '<%= Date.now() %>' } ] }, 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: https://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, Bootstrap, LESS, Jade, Grunt Software: Sublime Text, Photoshop, LiveReload ``` Task configuration on gruntfile: ```javascript { options: { patterns: [ { match: 'version', replacement: '<%= pkg.version %>' }, { match: 'timestamp', replacement: '<%= Date.now() %>' } ] }, files: [ { expand: true, flatten: true, src: ['src/manifest.appcache', 'src/humans.txt'], dest: 'build/' } ] } ``` ### Cache busting File `src/index.html`: ```html ``` Task configuration on gruntfile: ```javascript { options: { patterns: [ { match: 'timestamp', replacement: '<%= Date.now() %>' } ] }, files: [ { src: ['src/index.html'], dest: 'build/index.html' } ] } ``` ### Include file File `src/index.html`: ```html @@include ``` Task configuration on gruntfile: ```javascript { 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 ``` Task configuration on gruntfile: ```javascript { options: { patterns: [ { match: /(\w+)\s(\w+)/, replacement: '$2, $1' // Replaces "John Smith" with "Smith, John" } ] }, files: [ { expand: true, flatten: true, src: ['src/username.txt'], dest: 'build/' } ] } ``` ### Lookup for `foo` instead of `@@foo` Task configuration on gruntfile: ```javascript { 'opt-1': { options: { patterns: [ { match: /foo/g, // Explicitly using a regexp replacement: 'bar' } ] }, files: [ { expand: true, flatten: true, src: ['src/foo.txt'], dest: 'build/' } ] }, 'opt-2': { options: { patterns: [ { match: 'foo', replacement: 'bar' } ], usePrefix: false // Using the option provided }, files: [ { expand: true, flatten: true, src: ['src/foo.txt'], dest: 'build/' } ] }, 'opt-3': { options: { patterns: [ { match: 'foo', replacement: 'bar' } ], prefix: '' // Removing the prefix manually }, files: [ { expand: true, flatten: true, src: ['src/foo.txt'], dest: 'build/' } ] } } ``` ### How to insert filename in target File `src/app.js`: ```javascript // Filename: @@__SOURCE_FILENAME__ var App = App || (function () { return { // App contents }; })(); window.App = App; ``` Task configuration on gruntfile: ```javascript { options: { // Pass, we use built-in replacements }, files: [ { expand: true, flatten: true, src: ['src/**/*.js'], dest: 'build/' } ] } ``` ## Related - [applause](https://github.com/outaTiME/applause) - Human-friendly replacements ## License MIT © [outaTiME](https://outa.im) grunt-replace-2.0.2/commitlint.config.js000066400000000000000000000001051404403352300202360ustar00rootroot00000000000000module.exports = { extends: ['@commitlint/config-conventional'] }; grunt-replace-2.0.2/gruntfile.js000066400000000000000000000034131404403352300166170ustar00rootroot00000000000000'use strict'; module.exports = function (grunt) { grunt.initConfig({ replace: { simple: { options: { variables: { key: 'value' } }, files: [ { expand: true, flatten: true, src: ['test/fixtures/simple.txt'], dest: 'temp' } ] }, verbose: { options: { variables: { key: 'value' } }, files: [ { expand: true, flatten: true, src: ['test/fixtures/verbose.txt'], dest: 'temp' } ] }, warning: { options: { patterns: [ { match: 'key', replacement: 'value' }, { match: 'undefined-key', replacement: 'value' } ] }, files: [ { expand: true, flatten: true, src: ['test/fixtures/warning.txt'], dest: 'temp' } ] }, 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: 'temp' } ] }, 'built-in': { options: { // Pass }, files: [ { expand: true, flatten: true, src: ['test/fixtures/built-in_*.txt'], dest: 'temp' } ] } } }); grunt.loadTasks('tasks'); grunt.registerTask('default', ['replace:simple', 'replace:built-in']); }; grunt-replace-2.0.2/license000066400000000000000000000021131404403352300156230ustar00rootroot00000000000000MIT License Copyright (c) outaTiME (https://outa.im) 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-2.0.2/package.json000066400000000000000000000033371404403352300165550ustar00rootroot00000000000000{ "name": "grunt-replace", "version": "2.0.2", "description": "Replace text patterns with applause.", "license": "MIT", "repository": "outaTiME/grunt-replace", "author": { "name": "outaTiME", "email": "afalduto@gmail.com", "url": "https://outa.im" }, "engines": { "node": ">=10" }, "scripts": { "test": "eslint . && grunt && ava", "release": "release-it" }, "files": [ "tasks" ], "keywords": [ "gruntplugin", "replace", "replacement", "pattern", "patterns", "match", "text", "string", "regex", "regexp", "json", "yaml", "cson", "flatten", "applause" ], "dependencies": { "applause": "^2.0.0", "chalk": "^4.1.0", "file-sync-cmp": "^0.1.0", "lodash": "^4.17.21" }, "devDependencies": { "@commitlint/cli": "^12.1.1", "@commitlint/config-conventional": "^12.1.1", "@release-it/conventional-changelog": "^2.0.1", "ava": "^3.15.0", "cz-conventional-changelog": "^3.3.0", "eslint": "^7.20.0", "eslint-config-xo": "^0.35.0", "eslint-config-xo-space": "^0.27.0", "grunt": "^1.0.0", "husky": "^6.0.0", "release-it": "^14.4.1", "rimraf": "^3.0.2" }, "eslintConfig": { "extends": [ "xo", "xo-space" ] }, "release-it": { "hooks": { "after:init": [ "npm test" ] }, "git": { "commitMessage": "chore: release v${version}" }, "github": { "release": true }, "plugins": { "@release-it/conventional-changelog": { "preset": "angular", "infile": "CHANGELOG.md" } } }, "config": { "commitizen": { "path": "./node_modules/cz-conventional-changelog" } } } grunt-replace-2.0.2/tasks/000077500000000000000000000000001404403352300154065ustar00rootroot00000000000000grunt-replace-2.0.2/tasks/replace.js000066400000000000000000000150171404403352300173630ustar00rootroot00000000000000'use strict'; 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'; var detectDestType = function (dest) { if (_.endsWith(dest, '/')) { return 'directory'; } return 'file'; }; var unixifyPath = function (filepath) { if (isWindows) { return filepath.replace(/\\/g, '/'); } 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); }; module.exports = function (grunt) { 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; // Force contents if (result === false && options.force === true) { result = content; } if (result !== false) { grunt.verbose.writeln('Replace ' + chalk.cyan(source) + ' → ' + chalk.green(target)); } return result; }, noProcess: options.noProcess || options.processContentExclude }); return res; }; 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__', // eslint-disable-next-line max-params replacement: function (match, offset, string, source, target) { return target; }, builtin: true }, { match: '__TARGET_PATH__', // eslint-disable-next-line max-params replacement: function (match, offset, string, source, target) { return path.dirname(target); }, builtin: true }, { match: '__TARGET_FILENAME__', // eslint-disable-next-line max-params 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); 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( ' using ', applause.options.prefix, ' as a prefix' ); } 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-2.0.2/test/000077500000000000000000000000001404403352300152405ustar00rootroot00000000000000grunt-replace-2.0.2/test/fixtures/000077500000000000000000000000001404403352300171115ustar00rootroot00000000000000grunt-replace-2.0.2/test/fixtures/built-in_source_file.txt000066400000000000000000000000221404403352300237460ustar00rootroot00000000000000@@__SOURCE_FILE__ grunt-replace-2.0.2/test/fixtures/built-in_source_filename.txt000066400000000000000000000000261404403352300246130ustar00rootroot00000000000000@@__SOURCE_FILENAME__ grunt-replace-2.0.2/test/fixtures/built-in_source_path.txt000066400000000000000000000000221404403352300237630ustar00rootroot00000000000000@@__SOURCE_PATH__ grunt-replace-2.0.2/test/fixtures/built-in_target_file.txt000066400000000000000000000000221404403352300237340ustar00rootroot00000000000000@@__TARGET_FILE__ grunt-replace-2.0.2/test/fixtures/built-in_target_filename.txt000066400000000000000000000000261404403352300246010ustar00rootroot00000000000000@@__TARGET_FILENAME__ grunt-replace-2.0.2/test/fixtures/built-in_target_path.txt000066400000000000000000000000221404403352300237510ustar00rootroot00000000000000@@__TARGET_PATH__ grunt-replace-2.0.2/test/fixtures/fail.txt000066400000000000000000000000061404403352300205610ustar00rootroot00000000000000@@key grunt-replace-2.0.2/test/fixtures/simple.txt000066400000000000000000000000061404403352300211370ustar00rootroot00000000000000@@key grunt-replace-2.0.2/test/fixtures/verbose.txt000066400000000000000000000000061404403352300213130ustar00rootroot00000000000000@@key grunt-replace-2.0.2/test/fixtures/warning.txt000066400000000000000000000000061404403352300213130ustar00rootroot00000000000000@@key grunt-replace-2.0.2/test/test.js000066400000000000000000000045261404403352300165640ustar00rootroot00000000000000var test = require('ava'); var grunt = require('grunt'); var path = require('path'); var exec = require('child_process').exec; var rimraf = require('rimraf'); test('should replace simple key with value', function (t) { var result = grunt.file.read('temp/simple.txt'); t.is(result, 'value\n'); }); test.cb('should verbose when "silent" is false', function (t) { exec('grunt replace:verbose', { cwd: path.join(__dirname, '..') }, function (error, stdout) { t.not(stdout.indexOf('1 replacement in 1 file.'), -1); t.end(); }); }); test.cb('should warn when no matches exist', function (t) { exec('grunt replace:warning', { cwd: path.join(__dirname, '..') }, function (error, stdout) { t.is(stdout.indexOf('Warning: Unable to match 1 pattern'), -1); t.end(); }); }); test.cb('should fail when no matches exist and "pedantic" is true', function (t) { exec('grunt replace:fail', { cwd: path.join(__dirname, '..') }, function (error, stdout) { t.is(error.code, 6); t.not(stdout.indexOf('Warning: Unable to match 1 pattern'), -1); t.end(); }); }); // Built-in test('should replace using built-in replacement (__SOURCE_FILE__)', function (t) { var result = grunt.file.read('temp/built-in_source_file.txt'); t.is(result, 'test/fixtures/built-in_source_file.txt\n'); }); test('should replace using built-in replacement (__SOURCE_PATH__)', function (t) { var result = grunt.file.read('temp/built-in_source_path.txt'); t.is(result, 'test/fixtures\n'); }); test('should replace using built-in replacement (__SOURCE_FILENAME__)', function (t) { var result = grunt.file.read('temp/built-in_source_filename.txt'); t.is(result, 'built-in_source_filename.txt\n'); }); test('should replace using built-in replacement (__TARGET_FILE__)', function (t) { var result = grunt.file.read('temp/built-in_target_file.txt'); t.is(result, 'temp/built-in_target_file.txt\n'); }); test('should replace using built-in replacement (__TARGET_PATH__)', function (t) { var result = grunt.file.read('temp/built-in_target_path.txt'); t.is(result, 'temp\n'); }); test('should replace using built-in replacement (__TARGET_FILENAME__)', function (t) { var result = grunt.file.read('temp/built-in_target_filename.txt'); t.is(result, 'built-in_target_filename.txt\n'); }); test.after.always.cb(function (t) { rimraf('temp', t.end); }); grunt-replace-2.0.2/travis.yml000066400000000000000000000000661404403352300163160ustar00rootroot00000000000000language: node_js node_js: - '14' - '12' - '10'