This code snippet is a test case for the renameUsingGit
function, aiming to rename all .css
files to .scss
within a specific Git repository.
npm run import -- "test github renamer"
// test it
// rename all scss files to less
//renameUsingGit(
// '/Users/briancullinan/Documents/studysauce4',
// 'src/**/*.css',
// '.css',
// '.scss');
/**
* Renames SCSS files to LESS using Git.
*
* @param {string} directory - Directory path to rename files in.
* @param {string} pattern - Glob pattern of files to rename.
* @param {string} fromExtension - Current file extension.
* @param {string} toExtension - Target file extension.
*/
function renameUsingGit(directory, pattern, fromExtension, toExtension) {
// TODO: Add error handling for directory and pattern
const fs = require('fs');
const path = require('path');
const childProcess = require('child_process');
// Log the operation for clarity
console.log(`Renaming files in ${directory} from ${fromExtension} to ${toExtension}`);
// Check if Git is installed and available
if (!isGitInstalled()) {
throw new Error('Git is not installed. Please install Git before proceeding.');
}
// Get a list of files matching the pattern
const filePaths = getFilePaths(directory, pattern);
// Iterate over each file and rename it using Git
filePaths.forEach((filePath) => {
const fileName = path.basename(filePath);
const newFileName = fileName.replace(`.${fromExtension}`, `.${toExtension}`);
const newFilePath = path.join(path.dirname(filePath), newFileName);
// Use Git to rename the file
renameFileUsingGit(filePath, newFilePath);
});
}
/**
* Checks if Git is installed and available.
*
* @returns {boolean} True if Git is installed, false otherwise.
*/
function isGitInstalled() {
try {
require('child_process').execSync('git --version');
return true;
} catch (error) {
return false;
}
}
/**
* Gets a list of file paths matching the given pattern.
*
* @param {string} directory - Directory path to search in.
* @param {string} pattern - Glob pattern of files to find.
* @returns {Array<string>} List of file paths matching the pattern.
*/
function getFilePaths(directory, pattern) {
const childProcess = require('child_process');
return childProcess.execSync(`find ${directory} -name "${pattern}"`).toString().trim().split('\n');
}
/**
* Renames a file using Git.
*
* @param {string} from - Original file path.
* @param {string} to - Target file path.
*/
function renameFileUsingGit(from, to) {
const childProcess = require('child_process');
childProcess.execSync(`git mv ${from} ${to}`);
}
This code snippet appears to be a test case for the renameUsingGit
function we discussed earlier.
Let's break it down:
// test it
: This is a comment indicating the purpose of the following lines.// rename all scss files to less
: Another comment explaining what the test case aims to achieve.renameUsingGit( ... )
: This is a call to the renameUsingGit
function, passing in the following arguments:
'/Users/briancullinan/Documents/studysauce4'
: This is the path to the root directory of the Git repository where the renaming should occur.'src/**/*.css'
: This is a glob pattern matching all .css
files within the src
directory and its subdirectories..css
: This is the string to be searched for in file names..scss
: This is the string to replace .css
with, effectively renaming all .css
files to .scss
.In essence, this code snippet is a test to see if the renameUsingGit
function can successfully rename all .css
files to .scss
within the specified Git repository.
Let me know if you have any other code snippets you'd like me to explain!