123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- /*
- * grunt-replace
- *
- * Copyright (c) 2014 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 Applause = require('applause');
- grunt.registerMultiTask('replace', 'Replace text patterns with applause.', function () {
- // took options
- var options = this.options({
- encoding: grunt.file.defaultEncoding,
- mode: false,
- processContentExclude: [],
- patterns: [],
- excludeBuiltins: false,
- force: true,
- verbose: true
- });
- // attach builtins
- var patterns = options.patterns;
- if (options.excludeBuiltins !== true) {
- patterns.push({
- match: '__SOURCE_FILE__',
- replacement: function (match, offset, string, source, target) {
- return source;
- }
- }, {
- match: '__SOURCE_PATH__',
- replacement: function (match, offset, string, source, target) {
- return path.dirname(source);
- }
- }, {
- match: '__SOURCE_FILENAME__',
- replacement: function (match, offset, string, source, target) {
- return path.basename(source);
- }
- }, {
- match: '__TARGET_FILE__',
- replacement: function (match, offset, string, source, target) {
- return target;
- }
- }, {
- match: '__TARGET_PATH__',
- replacement: function (match, offset, string, source, target) {
- return path.dirname(target);
- }
- }, {
- match: '__TARGET_FILENAME__',
- replacement: function (match, offset, string, source, target) {
- return path.basename(target);
- }
- });
- }
- // create applause instance
- var applause = Applause.create(options);
- // took code from copy task
- var dest;
- var isExpandedPair;
- this.files.forEach(function (filePair) {
- 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;
- }
- if (grunt.file.isDir(src)) {
- grunt.file.mkdir(dest);
- } else {
- replace(src, dest, options, applause);
- if (options.mode !== false) {
- fs.chmodSync(dest, (options.mode === true) ? fs.lstatSync(src).mode : options.mode);
- }
- }
- });
- });
- });
- var detectDestType = function (dest) {
- var lastChar = dest.slice(-1);
- if (lastChar === '/') {
- return 'directory';
- } else {
- return 'file';
- }
- };
- var unixifyPath = function (filepath) {
- if (process.platform === 'win32') {
- return filepath.replace(/\\/g, '/');
- } else {
- return filepath;
- }
- };
- var replace = function (source, target, options, applause) {
- grunt.file.copy(source, target, {
- encoding: options.encoding,
- process: function (contents) {
- var result = applause.replace(contents, [source, target]);
- // force contents
- if (result === false && options.force === true) {
- result = contents;
- }
- if (result !== false && options.verbose === true) {
- grunt.log.writeln('Replace ' + chalk.cyan(source) + ' → ' +
- chalk.green(target));
- }
- return result;
- },
- noProcess: options.noProcess || options.processContentExclude
- });
- };
- };
|