diff --git a/README.md b/README.md index 09da9bd..aa238f8 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ Minifies JS and CSS files with Babel-Minify and CleanCSS | Input | Description | Required | Default Value | | -- | -- | -- | -- | | directory | Directory that contains the files you want to minify. | false | . ( current directory ) | +| exclude_directories | Exclude child folders from the main directory. Combine them in a string separated by semicolon and relative to the main directory eg. `dir1;js/dir2;js/test/dir3`. | false | "" (empty) | | output | Directory that contains the minified files. | false | same as directory | | overwrite | Overwrites the existing files with the minified version. Defaults to false. | false | false | | maxdepth | Descend at most levels (a non-negative integer) levels of directories below the starting-points. | false | "" (empty) | @@ -127,3 +128,28 @@ steps: ``` > Please note that the `repository` when _auto comitting_ has to match `output` in _auto minify_ + +##### Excluding child directories + +``` +steps: + # Checks-out your repository under $GITHUB_WORKSPACE, so auto-minify job can access it + - uses: actions/checkout@v2 + + - name: Auto Minify + uses: nizarmah/auto-minify@v2.1 + with: + directory: 'js' + exclude_directories: 'pdf_js' + + # Auto commits minified files to the repository + # Ignore it if you don't want to commit the files to the repository + - name: Auto committing minified files + uses: stefanzweifel/git-auto-commit-action@v4 + with: + repository: 'js' + commit_message: "Github Action: Auto Minified JS and CSS files" + branch: ${{ github.ref }} +``` + +> Please note that the `exclude_directories` will be relative to the `directory` which here is `js`. In this example the `pdf_js` directory full path would be `js/pdf_js`. \ No newline at end of file diff --git a/action.yml b/action.yml index 4f7620d..a194c6b 100644 --- a/action.yml +++ b/action.yml @@ -8,8 +8,12 @@ inputs: description: "Directory that contains the files you want to minify. Defaults to current directory (.)" required: false default: "." + exclude_directories: + description: "Exclude child folders from the main directory. Combine them in a string separated by semicolon and relative to the main directory eg. 'dir1;js/dir2;js/test/dir3'" + required: false + default: "" output: - description: "Directory that contains the minified files. Defaults to same directory" + description: "Directory that contains the minified files. Defaults to same directory." required: false default: "" overwrite: diff --git a/entrypoint.sh b/entrypoint.sh index 1d2c05b..6b996d0 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -56,19 +56,32 @@ find_files () { optional parameters: - `-maxdepth` - - Clear all optional parameter keys in case the values are empty - So, when appended to the command parameters, no error is caused ' - MAXDEPTH_KEY="-maxdepth" - MAXDEPTH_VAL=$INPUT_MAXDEPTH + if ! [ -z $INPUT_MAXDEPTH ] && [[ $INPUT_MAXDEPTH == ?(-)+([0-9]) ]]; then + MAXDEPTH="-maxdepth ${INPUT_MAXDEPTH}" + else + MAXDEPTH="" + fi + + EXCLUDES=() + if ! [ -z $INPUT_EXCLUDE_DIRECTORIES ]; then + IFS=';' read -ra EXCLUDES_ARRAY <<< "$INPUT_EXCLUDE_DIRECTORIES" + for i in ${EXCLUDES_ARRAY[@]}; do + if [ -z $EXCLUDES ]; then + EXCLUDES+=("-path" "$in_dir/$i") + else + EXCLUDES+=("-o" "-path" "$in_dir/$i") + fi + done + fi - if [ -z $MAXDEPTH_VAL ]; then - MAXDEPTH_KEY="" + if ! [ -z $EXCLUDES ]; then + EXCLUDES="-type d \( ${EXCLUDES[@]} \) -prune -o" fi - find $in_dir ${MAXDEPTH_KEY} ${MAXDEPTH_VAL} -type f -name "*.$1" -not \( -iname "*\.min.$1" \) + COMMAND="find $in_dir ${MAXDEPTH} ${EXCLUDES} -type f -name *.$1 -not \( -iname *\.min.$1 \) -print" + eval ${COMMAND} } exec_minify_js () {