123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- 'use strict';
- /**
- * Dependencies
- */
- const chalk = require('chalk');
- const parseConfig = require('./helpers/parse-config');
- const getPathsSync = require('./helpers/get-paths-sync');
- const getPathsAsync = require('./helpers/get-paths-async');
- const replaceSync = require('./helpers/replace-sync');
- const replaceAsync = require('./helpers/replace-async');
- const processFile = require('./process-file');
- /**
- * Replace in file helper
- */
- function replaceInFile(config, cb) {
- // If custom processor is provided use it instead
- if (config && config.processor) {
- return processFile(config, cb);
- }
- //Parse config
- try {
- config = parseConfig(config);
- }
- catch (error) {
- if (typeof cb === 'function') {
- return cb(error, null);
- }
- return Promise.reject(error);
- }
- //Get config
- const {files, from, to, dry, verbose} = config;
- //Dry run?
- //istanbul ignore if: No need to test console logs
- if (dry && verbose) {
- console.log(chalk.yellow('Dry run, not making actual changes'));
- }
- //Find paths
- return getPathsAsync(files, config)
- //Make replacements
- .then(paths => Promise.all(
- paths.map(file => replaceAsync(file, from, to, config))
- ))
- //Success handler
- .then(results => {
- if (typeof cb === 'function') {
- cb(null, results);
- }
- return results;
- })
- //Error handler
- .catch(error => {
- if (typeof cb === 'function') {
- cb(error);
- }
- else {
- throw error;
- }
- });
- }
- /**
- * Sync API
- */
- function replaceInFileSync(config) {
- // If custom processor is provided use it instead
- if (config && config.processor) {
- return processFile.processFileSync(config);
- }
- //Parse config
- config = parseConfig(config);
- //Get config, paths, and initialize changed files
- const {files, from, to, dry, verbose} = config;
- const paths = getPathsSync(files, config);
- //Dry run?
- //istanbul ignore if: No need to test console logs
- if (dry && verbose) {
- console.log(chalk.yellow('Dry run, not making actual changes'));
- }
- //Process synchronously and return results
- return paths.map(path => replaceSync(path, from, to, config));
- }
- // Self-reference to support named import
- replaceInFile.replaceInFile = replaceInFile;
- replaceInFile.replaceInFileSync = replaceInFileSync;
- replaceInFile.sync = replaceInFileSync;
- //Export
- module.exports = replaceInFile;
|