Skip to content

Commit

Permalink
Basic command parsing/validation and flow - just need to build the PO…
Browse files Browse the repository at this point in the history
…ST request to actually minify code.
  • Loading branch information
CraicOverflow89 committed Dec 17, 2019
0 parents commit 971bffb
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 0 deletions.
32 changes: 32 additions & 0 deletions build.gradle
@@ -0,0 +1,32 @@
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.3.50'
}

group 'JavaScriptMinifier'
version '1.0-SNAPSHOT'

repositories {
mavenCentral()
}

dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
}

compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}

jar {
manifest {
attributes 'Main-Class': 'craicoverflow89.javascriptminifier.MainKt'
}
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
}
}
1 change: 1 addition & 0 deletions gradle.properties
@@ -0,0 +1 @@
kotlin.code.style=official
21 changes: 21 additions & 0 deletions license.md
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 James Storer

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
22 changes: 22 additions & 0 deletions readme.md
@@ -0,0 +1,22 @@
JavaScript Minifier
===================

Lightweight command line tool that utilises the great work of [javascript-minifier.com](https://javascript-minifier.com/java), to minify `js` files.

### Usage

You can minify single files or entire directories and optionally specifiy an alternative output directory.

```
$ jsmin src/my_file.js
# Creates my_file.min.js in the src directory
$ jsmin src/my_file.js out
# Creates my_file.min.js in the out directory
$ jsmin src out
# Minifies all files in src and puts results in out
$ jsmin src out -r
# Same as before including subdirectories
```
2 changes: 2 additions & 0 deletions settings.gradle
@@ -0,0 +1,2 @@
rootProject.name = 'JavaScriptMinifier'

116 changes: 116 additions & 0 deletions src/main/kotlin/craicoverflow89/javascriptminifier/main.kt
@@ -0,0 +1,116 @@
package craicoverflow89.javascriptminifier

import java.io.File
import kotlin.system.exitProcess

fun main(args: Array<String>) {

// Missing Arguments
if(args.isEmpty()) {
println("Invalid arguments!")
println(" jsmin [input] [output] [-r]")
exitProcess(-1)
}

// File Paths
val cwd = System.getProperty("user.dir")

// Input File
val inputFile = File("$cwd/${args[0]}")

// Invalid Path
if(!inputFile.exists()) {
println("Could not find the input file!")
exitProcess(-1)
}

// Output Directory
val outputPath = if(args.size > 1 && !args[1].startsWith("-")) {
"%s/%s".format(cwd, args[1].apply {
File(this).apply {

// Existing Directory
if(exists()) {

// Invalid Directory
if(!isDirectory) {
println("Could not use the output file as a directory!")
exitProcess(-1)
}
}

// Create Directory
else mkdir()
}
})
} else if(inputFile.isDirectory) inputFile.absolutePath else inputFile.parentFile.absolutePath

// Parse Flags
val flags: String = if(args.size > 2 || args[1].startsWith("-")) args.let {
if(args[1].startsWith("-")) args[1]
else args[2]
}.let {

// Invalid Argument
if(!it.startsWith("-") || it.length < 2) {
println("Invalid arguments!")
println(" jsmin [input] [output] [-r]")
exitProcess(-1)
}

// Parse Chars
it.substring(1)

} else ""

// Minify Logic
fun minifyFile(input: File, output: String) {

// Read File
val content = input.readText()

// NOTE: this is where a POST request is required

// Write File
File("$output/${input.nameWithoutExtension}.min.js").writeText(content)
}

// Process Logic
fun minifyProcess(input: File, output: String) {

// Process Directory
if(input.isDirectory) input.listFiles().apply {

// Iterate Files
filter {
it.extension == "js"
}.forEach {
minifyFile(it, output)
}

// Process Subdirectories
if(flags.contains("r")) filter {
it.isDirectory
}.forEach {

// Subdirectory Path
"$output/${it.name}".apply {

// Create Directory
File(this).apply {
if(!this.exists()) this.mkdir()
}

// Invoke Process
minifyProcess(it, this)
}
}
}

// Process File
else minifyFile(input, output)
}

// Invoke Process
minifyProcess(inputFile, outputPath)
}
1 change: 1 addition & 0 deletions version
@@ -0,0 +1 @@
0.1.0

0 comments on commit 971bffb

Please sign in to comment.