Skip to content

Commit 86c4ee9

Browse files
committed
feat: Rewrote the app in javascript
1 parent a2ac186 commit 86c4ee9

13 files changed

Lines changed: 16380 additions & 98 deletions

File tree

.circleci/config.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ resource_class: &resource_class
1717
executors:
1818
docker-executor:
1919
docker:
20-
- image: cimg/openjdk:11.0
20+
- image: cimg/node:20.10.0
2121
<<: *docker_auth
2222
<<: *resource_class
2323
<<: *work_directory
@@ -54,7 +54,14 @@ jobs:
5454
steps:
5555
- checkout
5656
- setup_remote_docker
57+
- attach_workspace:
58+
at: ~/minify-js
59+
- restore_cache:
60+
keys:
61+
- v1-dependencies-{{ checksum "package.json" }}
62+
- v1-dependencies-
5763
- run: |
64+
npm install
5865
TAG=${CIRCLE_SHA1:0:8}
5966
echo $DOCKER_PASSWORD | docker login -u $DOCKER_USERNAME --password-stdin
6067
docker buildx create --name rockbuilder \
@@ -64,6 +71,10 @@ jobs:
6471
--push \
6572
-t devatherock/minify-js:$TAG \
6673
-t devatherock/minify-js:latest .
74+
- save_cache:
75+
paths:
76+
- ~/node_modules
77+
key: v1-dependencies-{{ checksum "package.json" }}
6778

6879
dockerhub_readme:
6980
docker:

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
.project
22
*.iml
33
.gradle
4-
build/
4+
build/
5+
bin/test
6+
node_modules
7+
.settings
8+
.classpath

Dockerfile

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
FROM node:12-alpine3.12
1+
FROM node:20.10.0-alpine3.18
22
LABEL maintainer="devatherock@gmail.com"
33

4-
RUN npm install -g minify@6.0.1 \
5-
&& npm cache clean --force
6-
RUN apk update \
7-
&& apk add --no-cache moreutils
4+
COPY node_modules /app/node_modules/
5+
COPY bin/cli.mjs /app/bin/cli.mjs
6+
COPY lib /app/lib/
87

9-
COPY entrypoint.sh /entrypoint.sh
10-
ENTRYPOINT ["sh", "/entrypoint.sh"]
8+
ENTRYPOINT ["node", "/app/bin/cli.mjs"]

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ all=false
44
clean:
55
./gradlew clean
66
docker-build:
7+
npm install
78
docker build -t devatherock/minify-js:latest .
9+
rm -rf node_modules
810
test:
911
ifeq ($(all), true)
1012
yamllint -d relaxed . --no-warnings

bin/cli.mjs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env node
2+
import fs from 'fs'
3+
import yargs from 'yargs'
4+
import { hideBin } from 'yargs/helpers'
5+
import { minifyFiles } from '../lib/minify-js.mjs'
6+
import { stringToBoolean, valueFromEnvVariables, getLogger } from '../lib/utils.mjs'
7+
8+
const inputPathVariables = ['PLUGIN_INPUT_PATH', 'PARAMETER_INPUT_PATH', 'INPUT_DIRECTORY']
9+
const outputPathVariables = ['PLUGIN_OUTPUT_PATH', 'PARAMETER_OUTPUT_PATH', 'INPUT_OUTPUT']
10+
const addSuffixVariables = ['PLUGIN_ADD_SUFFIX', 'PARAMETER_ADD_SUFFIX', 'INPUT_ADD_SUFFIX']
11+
12+
const options = yargs(hideBin(process.argv))
13+
.option('input-path', {
14+
alias: 'i',
15+
type: 'string',
16+
description: 'File to minify or a folder containing files to minify',
17+
default: valueFromEnvVariables(inputPathVariables) ? valueFromEnvVariables(inputPathVariables) : process.cwd()
18+
})
19+
.option('output-path', {
20+
alias: 'o',
21+
type: 'string',
22+
description: 'Path where the minified files will be saved',
23+
default: valueFromEnvVariables(outputPathVariables)
24+
})
25+
.option('add-suffix', {
26+
alias: 'a',
27+
type: 'boolean',
28+
description: 'Indicates if the output files should have the suffix `.min` added after the name',
29+
default: valueFromEnvVariables(addSuffixVariables) ? stringToBoolean(valueFromEnvVariables(addSuffixVariables)) : true
30+
})
31+
.parse()
32+
33+
const inputPath = options.i
34+
const outputPath = options.o
35+
const addSuffix = options.a
36+
37+
if (fs.existsSync(inputPath)) {
38+
minifyFiles(inputPath, addSuffix, outputPath)
39+
} else {
40+
getLogger().error('Input path ', inputPath, " doesn't exist")
41+
}

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ dependencies {
1313
test {
1414
useJUnitPlatform()
1515
testLogging {
16+
showStandardStreams = Boolean.getBoolean('test.logs')
1617
events 'passed', 'failed'
1718
}
1819
}

entrypoint.sh

Lines changed: 0 additions & 82 deletions
This file was deleted.

lib/minify-js.mjs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { minify } from 'minify'
2+
import fs from 'fs'
3+
import os from 'os'
4+
import path from 'path'
5+
import { getLogger, isDirectory, pathWithTrailingSeparator } from '../lib/utils.mjs'
6+
7+
const supportedExtensions = ['.js', '.html', '.css']
8+
9+
export async function minifyFile (inputFile, addSuffix, outputPath) {
10+
// Calculate output path
11+
const fileSeparatorIndex = inputFile.lastIndexOf(path.sep)
12+
if (outputPath) {
13+
getLogger().debug('Output path is specified')
14+
15+
// Create output folder if it doesn't exist
16+
if (!fs.existsSync(outputPath)) {
17+
fs.mkdirSync(outputPath, { recursive: true })
18+
}
19+
} else {
20+
// Can be -1 if input file is in the working folder
21+
if (fileSeparatorIndex !== -1) {
22+
outputPath = inputFile.substring(0, fileSeparatorIndex)
23+
} else {
24+
outputPath = ''
25+
}
26+
}
27+
getLogger().debug('Output path: ', outputPath)
28+
29+
// Extract input file name and extension
30+
const inputFileExtension = path.extname(inputFile)
31+
const inputFileName = path.basename(inputFile, inputFileExtension)
32+
33+
// Calculate complete output file name
34+
const outputFileName = pathWithTrailingSeparator(outputPath) + inputFileName + (addSuffix ? '.min' : '') + inputFileExtension
35+
getLogger().debug('Complete output file name: ', outputFileName)
36+
37+
// Minify file and write to output file
38+
const minifiedContent = await minify(inputFile)
39+
getLogger().debug(minifiedContent)
40+
fs.writeFileSync(outputFileName, `${minifiedContent}${os.EOL}`)
41+
42+
getLogger().info('Minified ', inputFile, ' > ', outputFileName)
43+
}
44+
45+
export function minifyFiles (inputPath, addSuffix, outputPath) {
46+
if (isDirectory(inputPath)) {
47+
getLogger().debug('Input path ', inputPath, ' is a directory')
48+
49+
// Loop through all the files in the input path
50+
fs.readdir(inputPath, function (_, files) {
51+
files.forEach(function (file, index) {
52+
minifyFiles(pathWithTrailingSeparator(inputPath) + file, addSuffix, outputPath)
53+
})
54+
})
55+
} else {
56+
getLogger().debug('Input path ', inputPath, ' is a file')
57+
58+
if (supportedExtensions.includes(path.extname(inputPath))) {
59+
minifyFile(inputPath, addSuffix, outputPath)
60+
} else {
61+
getLogger().debug('Skipping file ', inputPath, ' with unsupported extension')
62+
}
63+
}
64+
}

lib/utils.mjs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import fs from 'fs'
2+
import path from 'path'
3+
import SimpleNodeLogger from 'simple-node-logger'
4+
5+
var logger
6+
7+
export function stringToBoolean (value) {
8+
return value === 'true'
9+
}
10+
11+
export function valueFromEnvVariables (envVariables) {
12+
var value
13+
14+
for (var index = 0; index < envVariables.length; index++) {
15+
var variableName = envVariables[index]
16+
value = process.env[variableName]
17+
18+
if (value) {
19+
break
20+
}
21+
}
22+
23+
return value
24+
}
25+
26+
export function pathWithTrailingSeparator (inputPath) {
27+
return inputPath.endsWith(path.sep) ? inputPath : inputPath + path.sep
28+
}
29+
30+
export function getLogger () {
31+
if (!logger) {
32+
logger = SimpleNodeLogger.createSimpleLogger()
33+
34+
if (process.env.LOGGING_LEVEL_ROOT) {
35+
logger.setLevel(process.env.LOGGING_LEVEL_ROOT)
36+
}
37+
}
38+
39+
return logger
40+
}
41+
42+
export function isDirectory (inputPath) {
43+
return fs.lstatSync(inputPath).isDirectory()
44+
}

0 commit comments

Comments
 (0)