Skip to content

Commit fcc1a18

Browse files
committed
Initial version
0 parents  commit fcc1a18

File tree

3 files changed

+157
-0
lines changed

3 files changed

+157
-0
lines changed

README.md

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# 🚀 npm to pnpm Converter
2+
3+
This script automates the process of converting npm projects to pnpm within a specified directory.
4+
5+
## 📋 Features
6+
7+
- 🔍 Recursively searches for npm projects (identified by `package-lock.json` files)
8+
- 🔄 Converts npm projects to pnpm
9+
- 📦 Installs dependencies using pnpm
10+
- 🔙 Automatic rollback in case of failures
11+
- 📊 Provides a summary of the conversion process
12+
13+
## 🛠️ Prerequisites
14+
15+
- Bash shell
16+
- npm
17+
- pnpm
18+
19+
## 🚀 Usage
20+
21+
1. Clone this repository:
22+
23+
```
24+
git clone https://github.com/yourusername/npm-to-pnpm-converter.git
25+
```
26+
27+
2. Navigate to the script directory:
28+
29+
```
30+
cd npm-to-pnpm-converter
31+
```
32+
33+
3. Make the script executable:
34+
35+
```
36+
chmod +x npmtopnpm.sh
37+
```
38+
39+
4. Run the script, specifying the target directory:
40+
```
41+
./npmtopnpm.sh /path/to/your/projects
42+
```
43+
44+
## 📝 Log File
45+
46+
The script generates a log file named `pnpm_conversion.log` in the current directory. This file contains detailed information about the conversion process.
47+
48+
## 🚨 Error Handling
49+
50+
If an error occurs during the conversion process for a project, the script will:
51+
52+
- Log the error
53+
- Attempt to restore the project to its original state
54+
- Continue with the next project
55+
56+
## 📊 Summary
57+
58+
After processing all projects, the script provides a summary including:
59+
60+
- Total number of projects processed
61+
- Number of successful conversions
62+
- Number of failed conversions
63+
64+
## ⚠️ Caution
65+
66+
Always backup your projects before running this script. While it includes rollback functionality, it's always better to have a separate backup.
67+
68+
## 🤝 Contributing
69+
70+
Contributions, issues, and feature requests are welcome! Feel free to check [issues page](https://github.com/yourusername/npm-to-pnpm-converter/issues).
71+
72+
## 📜 License
73+
74+
This project is [MIT](https://choosealicense.com/licenses/mit/) licensed.

npmtopnpm.sh

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/bin/bash
2+
3+
# Check if a directory argument is passed
4+
if [ -z "$1" ]; then
5+
echo "❌ Usage: $0 <directory>"
6+
exit 1
7+
fi
8+
9+
# Get the directory from the first argument
10+
TARGET_DIR="$1"
11+
12+
# Check if the directory exists
13+
if [ ! -d "$TARGET_DIR" ]; then
14+
echo "❌ Error: Directory $TARGET_DIR does not exist."
15+
exit 1
16+
fi
17+
18+
# Define log file
19+
LOGFILE="pnpm_conversion.log"
20+
21+
# Function to log errors
22+
log_error() {
23+
echo "❌ [ERROR] $1" | tee -a "$LOGFILE"
24+
}
25+
26+
# Function to restore project if something goes wrong
27+
restore_project() {
28+
project_dir=$1
29+
backup_dir=$2
30+
echo "🔄 Restoring original files in $project_dir" | tee -a "$LOGFILE"
31+
# Restore package-lock.json and node_modules from backup
32+
[ -f "$backup_dir/package-lock.json.bak" ] && mv "$backup_dir/package-lock.json.bak" "$project_dir/package-lock.json"
33+
[ -d "$backup_dir/node_modules.bak" ] && mv "$backup_dir/node_modules.bak" "$project_dir/node_modules"
34+
}
35+
36+
# Navigate to the target directory
37+
cd "$TARGET_DIR" || exit
38+
39+
echo "🔍 Searching for npm projects in $TARGET_DIR..." | tee -a "$LOGFILE"
40+
41+
# Find all projects with package-lock.json (which implies npm usage)
42+
find . -name 'package-lock.json' -print0 | while IFS= read -r -d '' file; do
43+
project_dir=$(dirname "$file")
44+
45+
# Check if the project directory still exists
46+
if [ ! -d "$project_dir" ]; then
47+
log_error "Project directory $project_dir no longer exists. Skipping..."
48+
continue
49+
fi
50+
51+
echo "🚀 Converting project in $project_dir to pnpm..." | tee -a "$LOGFILE"
52+
53+
backup_dir="$project_dir/.backup"
54+
mkdir -p "$backup_dir"
55+
56+
# Backup package-lock.json and node_modules
57+
mv "$project_dir/package-lock.json" "$backup_dir/package-lock.json.bak"
58+
mv "$project_dir/node_modules" "$backup_dir/node_modules.bak" 2>/dev/null
59+
60+
# Remove node_modules and install using pnpm
61+
cd "$project_dir" || continue
62+
echo "📦 Running pnpm install in $project_dir" | tee -a "$LOGFILE"
63+
64+
if pnpm install; then
65+
echo "✅ pnpm install succeeded in $project_dir" | tee -a "$LOGFILE"
66+
# Clean up backups if successful
67+
rm -rf "$backup_dir"
68+
else
69+
log_error "pnpm install failed in $project_dir"
70+
# Restore the project if pnpm install fails
71+
restore_project "$project_dir" "$backup_dir"
72+
fi
73+
74+
cd - || exit
75+
done
76+
77+
echo "🎉 Conversion process completed. Check $LOGFILE for details."

pnpm_conversion.log

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
🔍 Searching for npm projects in ...
2+
🔍 Searching for npm projects in ...
3+
🔍 Searching for npm projects in ...
4+
🔍 Searching for npm projects in ...
5+
❌ [ERROR] Target directory '' does not exist.
6+
❌ [ERROR] Target directory '' does not exist.

0 commit comments

Comments
 (0)