Skip to content

Commit 9e15fa7

Browse files
chore(TCDICORE-339): fix git folder deinitialization and allow folder target specification
1 parent 1ed5458 commit 9e15fa7

3 files changed

Lines changed: 74 additions & 8 deletions

File tree

.changeset/purple-rooms-leave.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
"@devgateway/upgrade-wp-customizer": patch
3+
---
4+
5+
### Features
6+
7+
- Allow specifying a target folder when running the CLI, e.g. `npx @devgateway/upgrade-wp-customizer custom/wp-customizer`.
8+
- Validate the target directory and show helpful usage when it does not exist.
9+
- Improve intro/confirmation messaging to display the resolved target path.
10+
11+
### Fixes
12+
13+
- Preserve `.git` (and `node_modules`) during rollback to prevent repository deinitialization when cancelling the migration.
14+
- Run safety checks in the intended target directory.

packages/upgrade-wp-customizer/README.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,32 @@ pnpm add -g @devgateway/upgrade-wp-customizer
2424

2525
## Usage
2626

27-
Navigate to the root of your WordPress Customizer project and run:
27+
You can run the tool from anywhere by specifying the target folder, or navigate to your project root and run without arguments:
28+
2829
```bash
30+
# Specify a target folder (relative or absolute path)
31+
npx @devgateway/upgrade-wp-customizer custom/wp-customizer
32+
33+
# Relative path
34+
npx @devgateway/upgrade-wp-customizer ../other-project
35+
36+
# Run in current directory
2937
npx @devgateway/upgrade-wp-customizer
38+
39+
40+
# With force flag
41+
npx @devgateway/upgrade-wp-customizer custom/wp-customizer --force
3042
```
3143

44+
### Arguments
45+
46+
- `[folder]` (optional)
47+
Path to the WordPress Customizer project directory. Can be a relative or absolute path.
48+
Defaults to the current directory (`.`) if not specified.
49+
3250
### Options
3351

34-
- `--force`
52+
- `--force`
3553
Proceed even if your git directory has uncommitted changes.
3654

3755

@@ -80,8 +98,10 @@ The template includes:
8098

8199
## Safety & Rollback
82100

83-
- If you cancel during the migration, your original project is restored from backup.
101+
- Before migration starts, a backup directory is created in the same parent directory as your target folder (e.g., `custom/wp-customizer-backup-[timestamp]`).
102+
- If you cancel during the migration, your original project is automatically restored from backup.
84103
- If anything goes wrong, you can manually restore from the backup directory created at the start of the process.
104+
- The backup is automatically deleted after a successful migration.
85105

86106

87107
## Requirements

packages/upgrade-wp-customizer/src/index.ts

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ import { yellow, green, red } from 'picocolors';
1010
const __filename = fileURLToPath(import.meta.url);
1111
const __dirname = path.dirname(__filename);
1212

13-
const DEFAULT_SRC = path.resolve(process.cwd());
13+
// Parse command line arguments
14+
const args = process.argv.slice(2);
15+
const force = args.includes('--force');
16+
const targetFolder = args.filter(arg => !arg.startsWith('--'))[0] || '.';
17+
18+
const DEFAULT_SRC = path.resolve(process.cwd(), targetFolder);
1419
const BACKUP_SUFFIX = `-backup-${Date.now()}`;
1520
const BACKUP_DIR = `${DEFAULT_SRC}${BACKUP_SUFFIX}`;
1621

@@ -32,9 +37,32 @@ const BLOCKS_IMPORT_LINE = "import { BLOCKS_NS, BLOCKS_CATEGORY } from '@devgate
3237

3338
const cancel = () => {
3439
if (fs.existsSync(BACKUP_DIR)) {
40+
// Preserve .git and node_modules during rollback
41+
const gitDir = path.join(DEFAULT_SRC, '.git');
42+
const nodeModulesDir = path.join(DEFAULT_SRC, 'node_modules');
43+
const tempGitDir = gitDir + '-temp';
44+
const tempNodeModulesDir = nodeModulesDir + '-temp';
45+
46+
// Temporarily move .git and node_modules if they exist
47+
if (fs.existsSync(gitDir)) {
48+
fs.renameSync(gitDir, tempGitDir);
49+
}
50+
if (fs.existsSync(nodeModulesDir)) {
51+
fs.renameSync(nodeModulesDir, tempNodeModulesDir);
52+
}
53+
3554
// Remove current dir and restore backup
3655
fs.rmSync(DEFAULT_SRC, { recursive: true, force: true });
3756
fs.renameSync(BACKUP_DIR, DEFAULT_SRC);
57+
58+
// Restore .git and node_modules
59+
if (fs.existsSync(tempGitDir)) {
60+
fs.renameSync(tempGitDir, gitDir);
61+
}
62+
if (fs.existsSync(tempNodeModulesDir)) {
63+
fs.renameSync(tempNodeModulesDir, nodeModulesDir);
64+
}
65+
3866
console.log(yellow('Upgrade cancelled. Project rolled back to original state.'));
3967
} else {
4068
console.log(yellow('Operation cancelled.'));
@@ -262,8 +290,12 @@ function deleteUnusedFilesInRoot(rootDir: string) {
262290
}
263291

264292
async function main() {
265-
// Parse --force from process.argv
266-
const force = process.argv.includes('--force');
293+
// Check if target directory exists
294+
if (!fs.existsSync(DEFAULT_SRC)) {
295+
outro(red(`Error: Target directory does not exist: ${DEFAULT_SRC}`));
296+
outro(yellow(`Usage: npx @devgateway/upgrade-wp-customizer [folder] [--force]`));
297+
process.exit(1);
298+
}
267299

268300
// Backup project before making changes
269301
if (fs.existsSync(DEFAULT_SRC)) {
@@ -276,7 +308,7 @@ async function main() {
276308
// Check if git directory is dirty
277309
let isDirty = false;
278310
try {
279-
const gitStatus = execSync('git status --porcelain', { encoding: 'utf8' });
311+
const gitStatus = execSync('git status --porcelain --ignore-submodules', { encoding: 'utf8', cwd: DEFAULT_SRC });
280312
isDirty = gitStatus.trim().length > 0;
281313
} catch (e) {
282314
// If git is not available, ignore
@@ -289,7 +321,7 @@ async function main() {
289321
return;
290322
}
291323

292-
intro('WP Customizer Migration Tool');
324+
intro(`WP Customizer Migration Tool - Target: ${DEFAULT_SRC}`);
293325

294326
const proceed = await confirm({
295327
message: `Proceed with migration? This will overwrite files in: ${DEFAULT_SRC}`,

0 commit comments

Comments
 (0)