Skip to content

Instantly share code, notes, and snippets.

@DavidWells
Created August 6, 2025 16:00
Show Gist options
  • Save DavidWells/a7e781759b30ff7fc110f7cfc12a8b92 to your computer and use it in GitHub Desktop.
Save DavidWells/a7e781759b30ff7fc110f7cfc12a8b92 to your computer and use it in GitHub Desktop.
Automatically setup git worktree dirs and copy over .env files
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