Skip to content

Commit

Permalink
Add multi OS support for hybrid module fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ivome committed Sep 9, 2024
1 parent 6ee7ac3 commit 13367e0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
32 changes: 27 additions & 5 deletions fix-hybrid-module.sh
Original file line number Diff line number Diff line change
@@ -1,18 +1,40 @@
#!/bin/bash

# Create package.json for CommonJS
cat >dist/cjs/package.json <<!EOF
{
"type": "commonjs"
}
!EOF

sed -i '' 's/require("graphql\/execution\/values")/require("graphql\/execution\/values.js")/' "dist/cjs/QueryComplexity.js"
# Define the file paths
cjs_file_path="dist/cjs/QueryComplexity.js"
esm_file_path="dist/esm/QueryComplexity.js"
find_path="dist/esm"

# Detect the operating system and use the appropriate sed command
if [[ "$OSTYPE" == "darwin"* ]]; then
# macOS (BSD sed)
sed -i '' 's/require("graphql\/execution\/values")/require("graphql\/execution\/values.js")/' "$cjs_file_path"
else
# Linux (GNU sed)
sed -i 's/require("graphql\/execution\/values")/require("graphql\/execution\/values.js")/' "$cjs_file_path"
fi

# Create package.json for ES modules
cat >dist/esm/package.json <<!EOF
{
"type": "module"
}
!EOF

sed -i '' 's/from '\''graphql\/execution\/values'\'';/from '\''graphql\/execution\/values.mjs'\'';/' "dist/esm/QueryComplexity.js"

# We need to update from 'graphql' to 'graphql/index.mjs' in all files in dist/esm to ensure the GraphQL module is loaded from the same realm
find dist/esm -type f -name "*.js" -exec sed -i '' 's/from '\''graphql'\'';/from '\''graphql\/index.mjs'\'';/' {} +
# Detect the operating system and use the appropriate sed command
if [[ "$OSTYPE" == "darwin"* ]]; then
# macOS (BSD sed)
sed -i '' 's/from '\''graphql\/execution\/values'\'';/from '\''graphql\/execution\/values.mjs'\'';/' "$esm_file_path"
find "$find_path" -type f -name "*.js" -exec sed -i '' 's/from '\''graphql'\'';/from '\''graphql\/index.mjs'\'';/' {} +
else
# Linux (GNU sed)
sed -i 's/from '\''graphql\/execution\/values'\'';/from '\''graphql\/execution\/values.mjs'\'';/' "$esm_file_path"
find "$find_path" -type f -name "*.js" -exec sed -i 's/from '\''graphql'\'';/from '\''graphql\/index.mjs'\'';/' {} +
fi
10 changes: 9 additions & 1 deletion fix-hybrid-module.test.cjs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,12 @@ cat >dist/test/cjs/package.json <<!EOF
}
!EOF

sed -i '' 's/require("graphql\/execution\/values")/require("graphql\/execution\/values.js")/' "dist/test/cjs/QueryComplexity.js"
file_path="dist/test/cjs/QueryComplexity.js"

if [[ "$OSTYPE" == "darwin"* ]]; then
# macOS (BSD sed)
sed -i '' 's/require("graphql\/execution\/values")/require("graphql\/execution\/values.js")/' "$file_path"
else
# Linux (GNU sed)
sed -i 's/require("graphql\/execution\/values")/require("graphql\/execution\/values.js")/' "$file_path"
fi
15 changes: 12 additions & 3 deletions fix-hybrid-module.test.esm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@ cat >dist/test/esm/package.json <<!EOF
}
!EOF

sed -i '' 's/from '\''graphql\/execution\/values'\'';/from '\''graphql\/execution\/values.mjs'\'';/' "dist/test/esm/QueryComplexity.js"
file_path="dist/test/esm/QueryComplexity.js"
find_path="dist/test/esm"

# We need to update from 'graphql' to 'graphql/index.mjs' in all files in dist/esm to ensure the GraphQL module is loaded from the same realm
find dist/test/esm -type f -name "*.js" -exec sed -i '' 's/from '\''graphql'\'';/from '\''graphql\/index.mjs'\'';/' {} +
# Detect the operating system and use the appropriate sed command
if [[ "$OSTYPE" == "darwin"* ]]; then
# macOS (BSD sed)
sed -i '' 's/from '\''graphql\/execution\/values'\'';/from '\''graphql\/execution\/values.mjs'\'';/' "$file_path"
find "$find_path" -type f -name "*.js" -exec sed -i '' 's/from '\''graphql'\'';/from '\''graphql\/index.mjs'\'';/' {} +
else
# Linux (GNU sed)
sed -i 's/from '\''graphql\/execution\/values'\'';/from '\''graphql\/execution\/values.mjs'\'';/' "$file_path"
find "$find_path" -type f -name "*.js" -exec sed -i 's/from '\''graphql'\'';/from '\''graphql\/index.mjs'\'';/' {} +
fi

0 comments on commit 13367e0

Please sign in to comment.