Created
August 6, 2025 16:00
-
-
Save DavidWells/a7e781759b30ff7fc110f7cfc12a8b92 to your computer and use it in GitHub Desktop.
Automatically setup git worktree dirs and copy over .env files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { execSync } = require('child_process') | |
const path = require('path') | |
const fs = require('fs') | |
const YOUR_DIR = 'your-dir-here' | |
const MASTER_BRANCH_DIR = path.resolve(__dirname, `../../${YOUR_DIR}`) | |
const THIS_BRANCH_DIR = path.resolve(__dirname, '../') | |
// Function to run commands and handle errors | |
function runCommand(command, cwd) { | |
try { | |
console.log(`Running: ${command} in ${cwd}`) | |
execSync(command, { stdio: 'inherit', cwd }) | |
} catch (error) { | |
console.error(`Failed to execute: ${command}`) | |
console.error(error.message) | |
process.exit(1) | |
} | |
} | |
// Function to determine package manager | |
function getPackageManager(dir) { | |
return fs.existsSync(path.join(dir, 'pnpm-lock.yaml')) ? 'pnpm' : 'npm' | |
} | |
// Function to find all package.json directories | |
function findPackageJsonDirs(rootDir) { | |
const packageDirs = [] | |
function traverse(dir) { | |
const files = fs.readdirSync(dir) | |
if (files.includes('package.json')) { | |
packageDirs.push(dir) | |
} | |
files.forEach(file => { | |
const fullPath = path.join(dir, file) | |
if (fs.statSync(fullPath).isDirectory() && file !== 'node_modules' && file !== '.git') { | |
traverse(fullPath) | |
} | |
}) | |
} | |
traverse(rootDir) | |
return packageDirs | |
} | |
function copyEnvFiles(sourceDir, targetDir) { | |
console.log('READING .env files from master branch...', sourceDir) | |
console.log('WRITING .env files to this branch...', targetDir) | |
try { | |
// Ensure target directory exists | |
fs.mkdirSync(targetDir, { recursive: true }) | |
function processDirectory(currentSourceDir, currentTargetDir) { | |
// Read source directory | |
const files = fs.readdirSync(currentSourceDir) | |
// Filter for all .env files (including extensions) | |
const envFiles = files.filter(file => | |
file === '.env' || file.startsWith('.env.') | |
) | |
// Copy any .env files found in this directory | |
for (const file of envFiles) { | |
const sourcePath = path.join(currentSourceDir, file) | |
const targetPath = path.join(currentTargetDir, file) | |
fs.copyFileSync(sourcePath, targetPath) | |
console.log(`Copied ${path.relative(sourceDir, sourcePath)}`) | |
} | |
// Process subdirectories | |
for (const file of files) { | |
const fullPath = path.join(currentSourceDir, file) | |
if (fs.statSync(fullPath).isDirectory() && file !== 'node_modules' && file !== '.git') { | |
const newTargetDir = path.join(currentTargetDir, file) | |
fs.mkdirSync(newTargetDir, { recursive: true }) | |
processDirectory(fullPath, newTargetDir) | |
} | |
} | |
} | |
processDirectory(sourceDir, targetDir) | |
console.log('Successfully copied .env files maintaining directory structure') | |
} catch (error) { | |
console.error('Error copying .env files:', error.message) | |
} | |
} | |
// Main setup function | |
function setup() { | |
console.log('๐ Starting project setup...') | |
// Find all package.json directories | |
const rootDir = path.resolve(__dirname, '..') | |
const packageDirs = findPackageJsonDirs(rootDir) | |
console.log('\n๐ฆ Installing dependencies for all workspaces...') | |
packageDirs.forEach(dir => { | |
const packageManager = getPackageManager(dir) | |
console.log(`\nInstalling dependencies in: ${path.relative(rootDir, dir)} using ${packageManager}`) | |
runCommand(`${packageManager} install`, dir) | |
}) | |
// Copy .env files from master branch only if we're not in the master branch | |
if (THIS_BRANCH_DIR !== MASTER_BRANCH_DIR) { | |
console.log('\n๐ Copying .env files from master branch...') | |
copyEnvFiles(MASTER_BRANCH_DIR, THIS_BRANCH_DIR) | |
} else { | |
console.log('\n๐ Skipping .env file copy as we are in the master branch') | |
} | |
console.log('\nโ Setup completed successfully!') | |
} | |
// Run the setup | |
setup() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment