Skip to content

Commit

Permalink
Merge pull request #19 from devatherock/target-some-files
Browse files Browse the repository at this point in the history
feat: Added ability to target only specific files within the input path
  • Loading branch information
devatherock authored Nov 26, 2023
2 parents 3d1fcca + bfc8ae6 commit 82f0211
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Changelog

## [Unreleased]
### Added
- [#5](https://github.com/devatherock/minify-js/issues/5): Ability to minify only specific files within the input directory

### Changed
- Rewrote the shell script in Node JS

## [2.0.0] - 2023-06-04
### Changed
Expand Down
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,22 @@ The following parameters can be set to configure the action.

* **add_suffix** - Indicates if the output files should have the suffix `.min` added after the name. Default is true

* **inclusions** - Multi-line string, each line of which contains a regex representing files/paths within the input directory to include/minify. By default, all files in the input directory will be minified

```yaml
jobs:
build:
runs-on: ubuntu-latest # Docker-based github actions have to run on a linux environment
steps:
- name: HTML/CSS/JS Minifier
uses: docker://devatherock/minify-js:2.0.0
uses: docker://devatherock/minify-js:3.0.0
with:
directory: 'src' # Optional
output: 'minify/src' # Optional
add_suffix: false # Optional
inclusions: |- # Optional
.*assets.*
.*static/index.html
```
### Docker
Expand All @@ -41,7 +46,8 @@ docker run --rm \
-e PARAMETER_INPUT_PATH=/work/src \
-e PARAMETER_OUTPUT_PATH=/work/minify/src \
-e PARAMETER_ADD_SUFFIX=false \
devatherock/minify-js:2.0.0
-e PARAMETER_INCLUSIONS=".*assets.*\n.*static/index.html" \
devatherock/minify-js:3.0.0
```

### vela
Expand All @@ -55,17 +61,22 @@ The following parameters can be set to configure the plugin.

* **add_suffix** - Indicates if the output files should have the suffix `.min` added after the name. Default is true

* **inclusions** - Multi-line string, each line of which contains a regex representing files to include/minify. By default, all files in the input directory will be minified

```yaml
steps:
- name: minify_js
ruleset:
branch: master
event: push
image: devatherock/minify-js:2.0.0
image: devatherock/minify-js:3.0.0
parameters:
input_path: src
output_path: minify/src
add_suffix: false
inclusions: |-
.*assets.*
.*static/index.html
```
### CircleCI
Expand All @@ -75,12 +86,13 @@ version: 2.1
jobs:
minify_js:
docker:
- image: devatherock/minify-js:2.0.0
- image: devatherock/minify-js:3.0.0
working_directory: ~/my-repo
environment:
PARAMETER_INPUT_PATH: src
PARAMETER_OUTPUT_PATH: minify/src
PARAMETER_ADD_SUFFIX: false
PARAMETER_INCLUSIONS: '.*assets.*\n.*static/index.html'
steps:
- checkout
- run: sh /entrypoint.sh
Expand Down
18 changes: 17 additions & 1 deletion bin/cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { stringToBoolean, valueFromEnvVariables, getLogger } from '../lib/utils.
const inputPathVariables = ['PLUGIN_INPUT_PATH', 'PARAMETER_INPUT_PATH', 'INPUT_DIRECTORY']
const outputPathVariables = ['PLUGIN_OUTPUT_PATH', 'PARAMETER_OUTPUT_PATH', 'INPUT_OUTPUT']
const addSuffixVariables = ['PLUGIN_ADD_SUFFIX', 'PARAMETER_ADD_SUFFIX', 'INPUT_ADD_SUFFIX']
const inclusionsVariables = ['PLUGIN_INCLUSIONS', 'PARAMETER_INCLUSIONS', 'INPUT_INCLUSIONS']

const options = yargs(hideBin(process.argv))
.option('input-path', {
Expand All @@ -28,14 +29,29 @@ const options = yargs(hideBin(process.argv))
description: 'Indicates if the output files should have the suffix `.min` added after the name',
default: valueFromEnvVariables(addSuffixVariables) ? stringToBoolean(valueFromEnvVariables(addSuffixVariables)) : true
})
.option('inclusions', {
type: 'string',
description: 'Multi-line string, each line of which contains a regex representing files to include/minify',
default: valueFromEnvVariables(inclusionsVariables)
})
.parse()

const inputPath = options.i
const outputPath = options.o
const addSuffix = options.a
var inclusions = []

if (options.inclusions) {
const inclusionParts = options.inclusions.replace('\\n', '\n').split(/[\r\n]+/)
getLogger().debug('Inclusions: ', inclusionParts)

for (var index = 0; index < inclusionParts.length; index++) {
inclusions.push(RegExp(inclusionParts[index]))
}
}

if (fs.existsSync(inputPath)) {
minifyFiles(inputPath, addSuffix, outputPath)
minifyFiles(inputPath, addSuffix, outputPath, inclusions)
} else {
getLogger().error('Input path ', inputPath, " doesn't exist")
}
10 changes: 7 additions & 3 deletions lib/minify-js.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,25 @@ export async function minifyFile (inputFile, addSuffix, outputPath) {
getLogger().info('Minified ', inputFile, ' > ', outputFileName)
}

export function minifyFiles (inputPath, addSuffix, outputPath) {
export function minifyFiles (inputPath, addSuffix, outputPath, inclusions) {
if (isDirectory(inputPath)) {
getLogger().debug('Input path ', inputPath, ' is a directory')

// Loop through all the files in the input path
fs.readdir(inputPath, function (_, files) {
files.forEach(function (file, index) {
minifyFiles(pathWithTrailingSeparator(inputPath) + file, addSuffix, outputPath)
minifyFiles(pathWithTrailingSeparator(inputPath) + file, addSuffix, outputPath, inclusions)
})
})
} else {
getLogger().debug('Input path ', inputPath, ' is a file')

if (supportedExtensions.includes(path.extname(inputPath))) {
minifyFile(inputPath, addSuffix, outputPath)
if (inclusions.length === 0 || inclusions.some((regex) => regex.test(inputPath))) {
minifyFile(inputPath, addSuffix, outputPath)
} else {
getLogger().debug('Skipping file ', inputPath, " that doesn't match any inclusions")
}
} else {
getLogger().debug('Skipping file ', inputPath, ' with unsupported extension')
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,46 @@ class MinifyJsDockerSpec extends Specification {
'github'
]
}

@Unroll
void 'test minify - inclusions specified. ci: #ci'() {
given:
String outputHtmlFile = "${System.properties['user.dir']}/src/test/resources/output/index.min.html"
String outputJsFile = "${System.properties['user.dir']}/src/test/resources/output/scripts.min.js"
String outputCssFile = "${System.properties['user.dir']}/src/test/resources/output/main.min.css"

when:
def output = ProcessUtil.executeCommand(['docker', 'run', '--rm',
'-v', "${System.properties['user.dir']}:/work",
'-w=/work',
'-e', "${config[ci].inputPathParam}=/work/src/test/resources",
'-e', "${config[ci].outputPathParam}=/work/src/test/resources/output",
'-e', "${config[ci].envPrefix}INCLUSIONS=.*scripts/scripts.js.*\n.*static/index.*",
imageName])

then:
output[0] == 0
output[1].contains('Minified /work/src/test/resources/static/index.html > /work/src/test/resources/output/index.min.html')
output[1].contains('Minified /work/src/test/resources/scripts/scripts.js > /work/src/test/resources/output/scripts.min.js')
new File(outputHtmlFile).text ==
'<!doctype html><title>Test title</title><div id=layout><div id=main><div class=header><h1>Test body</h1></div></div></div>\n'
new File(outputJsFile).text ==
'$((function(){$("#templateAndModelForm *:input[type!=hidden]:first").focus()}));\n'

then: "css output file should not exist as it wasn't specified in the inclusions"
Files.notExists(Paths.get(outputCssFile))
!output[1].contains('Minified /work/src/test/resources/static/main.css > /work/src/test/resources/output/main.min.css')

cleanup:
Files.deleteIfExists(Paths.get(outputHtmlFile))
Files.deleteIfExists(Paths.get(outputJsFile))
Files.deleteIfExists(Paths.get(outputCssFile))

where:
ci << [
'drone',
'vela',
'github'
]
}
}

0 comments on commit 82f0211

Please sign in to comment.