-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathconvert_to_esm.sh
More file actions
43 lines (34 loc) · 1.26 KB
/
Copy pathconvert_to_esm.sh
File metadata and controls
43 lines (34 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/bin/bash
# Convert all CommonJS files to ESM
# List of files to convert
files=(
"lib/get_youtube_song.js"
"lib/lastfm_api.js"
"lib/jiosaavn.js"
"lib/youtube-search.js"
"routes/youtube.js"
"routes/entities.js"
"routes/jiosaavn.js"
"routes/api.js"
"routes/explore.js"
)
for file in "${files[@]}"; do
echo "Converting $file..."
# Replace require() with import
sed -i '' "s/const \(.*\) = require('\(.*\)');/import \1 from '\2.js';/g" "$file"
sed -i '' "s/const { \(.*\) } = require('\(.*\)');/import { \1 } from '\2.js';/g" "$file"
sed -i '' "s/require('\(.*\)')/import('\1.js')/g" "$file"
# Replace module.exports with export
sed -i '' "s/module.exports = \(.*\);/export default \1;/g" "$file"
sed -i '' "s/module.exports = {/export {/g" "$file"
# Fix .js.js double extension
sed -i '' "s/\.js\.js/\.js/g" "$file"
# Fix node modules (remove .js for them)
sed -i '' "s/from 'express\.js'/from 'express'/g" "$file"
sed -i '' "s/from 'axios\.js'/from 'axios'/g" "$file"
sed -i '' "s/from 'cors\.js'/from 'cors'/g" "$file"
sed -i '' "s/from 'helmet\.js'/from 'helmet'/g" "$file"
sed -i '' "s/from 'morgan\.js'/from 'morgan'/g" "$file"
sed -i '' "s/from 'dotenv\.js'/from 'dotenv'/g" "$file"
done
echo "Conversion complete!"