Skip to content

Commit

Permalink
ci: add LICENSE file to all published packages
Browse files Browse the repository at this point in the history
  • Loading branch information
tien committed Jun 27, 2024
1 parent f7f8eda commit 78cc1a2
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/packages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ jobs:

- run: yarn workspaces foreach -At --no-private version 0.0.0-snapshot.$(git rev-parse --short HEAD) --immediate

- run: yarn workspaces foreach -At --no-private npm publish --access public --tag snapshot --tolerate-republish
- run: scripts/publish.sh
env:
YARN_NPM_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
60 changes: 60 additions & 0 deletions scripts/publish.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/bin/bash

# Check if LICENSE file exists in the root directory
if [ ! -f LICENSE ]; then
echo "LICENSE file not found in the root directory."
exit 1
fi

# Check if .gitignore file exists
if [ ! -f .gitignore ]; then
echo ".gitignore file not found in the root directory."
exit 1
fi

# Read .gitignore into an array, ignoring comments and empty lines
gitignore_patterns=()
while IFS= read -r line; do
# Ignore comments and empty lines
[[ "$line" =~ ^#.*$ || -z "$line" ]] && continue
gitignore_patterns+=("$line")
done <.gitignore

# Create an array to keep track of created LICENSE files
created_files=()

# Function to check if a path matches any .gitignore pattern
is_ignored() {
local path=$1
for pattern in "${gitignore_patterns[@]}"; do
if [[ $path == $pattern || $path == $pattern/* ]]; then
return 0
fi
done
return 1
}

# Find all directories containing a package.json file
while IFS= read -r -d '' package_json; do
dir=$(dirname "$package_json")
# Check if directory is ignored
if ! is_ignored "$dir"; then
# Check if LICENSE file already exists in the directory
if [ ! -f "$dir/LICENSE" ]; then
# Copy the LICENSE file to the directory
cp LICENSE "$dir"
created_files+=("$dir/LICENSE")
echo "Copied LICENSE to $dir"
fi
fi
done < <(find . -name package.json -type f -not -path "*/node_modules/*" -print0)

yarn workspaces foreach -At --no-private npm publish --access public --tag snapshot --tolerate-republish

# Delete only the created LICENSE files
for file in "${created_files[@]}"; do
if [ -f "$file" ]; then
rm "$file"
echo "Deleted $file"
fi
done

0 comments on commit 78cc1a2

Please sign in to comment.