Skip to content

Instantly share code, notes, and snippets.

@mikerourke
Created March 6, 2018 03:48
Show Gist options
  • Save mikerourke/b44ad96dae8d7b2927fb51a536286004 to your computer and use it in GitHub Desktop.
Save mikerourke/b44ad96dae8d7b2927fb51a536286004 to your computer and use it in GitHub Desktop.
Refactoring Execution: Git Move Script
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const sh = require('shelljs');
const _ = require('lodash');
const sourcePath = path.resolve(process.cwd(), 'src');
// This is the new /src/redux folder that gets created:
const reduxPath = path.resolve(sourcePath, 'redux');
// I used "entities" instead of "modules", but they represent the same thing:
const entities = [
'app',
'projects',
'schedules',
'users',
];
const createReduxFolders = () => {
if (!fs.existsSync(reduxPath)) fs.mkdirSync(reduxPath);
// Code to create entities folders in /src/redux...
};
// Executes a `git mv` command (I omitted some additional code that validates
// if the file already exists for brevity).
const gitMoveFile = (sourcePath, targetPath) => {
console.log(chalk.cyan(`Moving ${sourcePath} to ${targetPath}`));
const command = `git mv ${sourcePath} ${targetPath}`;
sh.exec(command);
console.log(chalk.green('Move successful.'));
};
const moveReduxFiles = () => {
entities.forEach(entity => {
['actions', 'reducers', 'selectors'].forEach(reduxType => {
// Get the file associated with the specified entity for the specified reduxType,
// so the first file might be /src/actions/app.js:
const sourceFile = path.resolve(sourcePath, reduxType, `${entity}.js`);
if (fs.existsSync(sourceFile)) {
// Capitalize the reduxType to append to the file name (e.g. appActions.js):
const fileSuffix = _.capitalize(reduxType);
// Build the path to the target file, so this would be /src/redux/app/appActions.js:
const targetPath = `${reduxPath}/${entity}`;
const targetFile = `${targetPath}/${entity}${fileSuffix}.js`;
// Execute a `git mv` command for the file:
gitMoveFile(sourceFile, targetFile);
}
});
});
};
moveReduxFiles();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment