yeah i run this in through my package json, caveat folder path is my local ref ```const { execSync

yeah i run this in through my package json, caveat folder path is my local ref

const { execSync } = require('child_process');
const { spawnSync } = require('child_process');
const fs = require('fs');
const path = require('path');

// Get current git branch
const branch = execSync('git rev-parse --abbrev-ref HEAD').toString().trim();

// Sanitize branch name to be used as a folder name
const sanitizedBranch = branch.replace(/[^a-zA-Z0-9-_]/g, '_');

// Define the persist folder path
const persistFolder = path.resolve(`../whateverFolder/.wrangler/state/${sanitizedBranch}/`);

// Ensure the persist folder exists
if (!fs.existsSync(persistFolder)) {
  fs.mkdirSync(persistFolder, { recursive: true });
  console.log(`Created persist folder: ${persistFolder}`);
} else {
  console.log(`Persist folder already exists: ${persistFolder}`);
}

console.log(`Persisting data to: ${persistFolder}`);

// Run wrangler dev with the dynamic persist folder
const result = spawnSync('wrangler', [
  'dev',
  `--persist-to=${persistFolder}`
], { stdio: 'inherit' });

if (result.error) {
  console.error('Error running wrangler:', result.error);
  process.exit(1);
}
Was this page helpful?