Skip to content

Commit 8d73da5

Browse files
committed
[MINIFY]: test for javascript added
Signed-off-by: ashish <[email protected]>
1 parent 2892df1 commit 8d73da5

File tree

7 files changed

+176
-7
lines changed

7 files changed

+176
-7
lines changed

package-lock.json

Lines changed: 90 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"jshashes": "^1.0.7",
1818
"minify": "^4.1.3",
1919
"nyc": "^14.1.1",
20+
"ora": "^4.0.2",
2021
"signale": "^1.4.0",
2122
"tslib": "^1.10.0"
2223
},

src/commands/minify.ts

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import {Command, flags} from '@oclif/command'
22

3+
const MinifyJs = require('minify')
4+
35
import Logger from '../utilities/logger'
46
import Utilities from '../utilities/utilities'
57

@@ -26,8 +28,11 @@ export default class Minify extends Command {
2628
args.string = this.getFileString(args)
2729
flags.type = this.getFileType(flags, args)
2830

29-
// this.checkParameters(flags, args)
31+
Logger.info(this, `reading file: ${args.file}`)
32+
Logger.info(this, `file type: ${flags.type}`)
3033

34+
this.checkParameters(flags, args)
35+
this.minifyString(flags, args)
3136
}
3237

3338
// this will get file type either from flags.type or file extension ( if flag is not given)
@@ -36,9 +41,8 @@ export default class Minify extends Command {
3641
return this.getFileTypeFromExtension(flags.type)
3742

3843
let fileExtensionRegex = /\w+\.([a-z]+)/
39-
40-
args.s
41-
44+
let extension = args.file.match(fileExtensionRegex)[1]
45+
return this.getFileTypeFromExtension(extension)
4246
}
4347

4448
private getFileTypeFromExtension(extension: string) {
@@ -68,7 +72,33 @@ export default class Minify extends Command {
6872
Logger.error(this, 'File path not passed')
6973
}
7074

71-
// private checkParameters(flags: unknown, args: { [p: string]: any }) {
72-
// if(validFileType)
73-
// }
75+
// tslint:disable-next-line:no-unused
76+
private checkParameters(flags: unknown, args: { [p: string]: any }) {
77+
if (args.file === undefined || args.file === '')
78+
Logger.error(this, 'File path is empty')
79+
// others already checked
80+
}
81+
82+
//TODO: add image compression also
83+
//TODO: add error handling also
84+
private minifyString(flags: any, args: any) {
85+
Logger.progressStart(this, 'minifying...')
86+
87+
let output = ''
88+
// setTimeout(() => { //TODO: can add spinner for bigger files using promise
89+
switch (flags.type) {
90+
case Minify.JS:
91+
output = MinifyJs.js(args.string); break
92+
case Minify.CSS:
93+
output = MinifyJs.css(args.string); break
94+
case Minify.HTML:
95+
output = MinifyJs.html(args.string); break
96+
default:
97+
Logger.error(this, 'Invalid Minifier Type')
98+
}
99+
Logger.progressStop(this, 'minified')
100+
// }, 1000)
101+
Logger.success(this, `\n${output}`)
102+
103+
}
74104
}

src/utilities/logger.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
// tslint:disable-next-line:file-name-casing
2+
import * as Ora from 'ora'
23
import * as signale from 'signale'
34

45
// tslint:disable-next-line:no-unnecessary-class
56
export default class Logger {
7+
static spinner: any
8+
69
// uses signale for logging withoug thisRef
710
// tslint:disable-next-line:no-unused
811
public static success(thisRef: any, message: string) {
@@ -16,6 +19,17 @@ export default class Logger {
1619
signale.error(`${message}`)
1720
thisRef.exit(0) //added to exit command
1821
}
22+
// tslint:disable-next-line:no-unused
23+
public static progressStart(thisRef: any, message: string) {
24+
// signale.watch(`${message}`)
25+
Logger.spinner = Ora(message)
26+
Logger.spinner.start()
27+
}
28+
29+
// tslint:disable-next-line:no-unused
30+
public static progressStop(thisRef: any, message: string) {
31+
Logger.spinner.succeed(message)
32+
}
1933

2034
// public static logSuccess(thisRef: any, message: string) {
2135
// thisRef.log(` › Success: ${message}`)

test/commands/minify.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,20 @@ describe('minify', () => {
1616
.it('Invalid File Type', ctx => {
1717
expect(ctx.stdout).to.contain('Invalid File Type')
1818
})
19+
20+
test
21+
.stdout()
22+
.command(['minify', 'test/resources/test.js'])
23+
.it('Minify Js', ctx => {
24+
expect(ctx.stdout).to.contain('console.log("lorem ipsum");let x=10;x<10?console.log("x is less than: "+x):x>10?console.log("x is more than: "+x):console.log("x is equals to: "+x);')
25+
})
26+
27+
// using flag -f
28+
test
29+
.stdout()
30+
.command(['minify', '-f', 'test/resources/test.js'])
31+
.it('Minify Js with Flag', ctx => {
32+
expect(ctx.stdout).to.contain('console.log("lorem ipsum");let x=10;x<10?console.log("x is less than: "+x):x>10?console.log("x is more than: "+x):console.log("x is equals to: "+x);')
33+
})
34+
1935
})

test/resources/test.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<html>
2+
<head>
3+
<title>TEST</title>
4+
</head>
5+
<body>
6+
<h1>heading</h1>
7+
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Adipisci aperiam, at cupiditate delectus deleniti fugiat, harum ipsa labore laboriosam magni nulla obcaecati quae repellendus. Distinctio eum impedit iure laborum voluptatem?</p>
8+
</body>
9+
</html>

test/resources/test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
console.log('lorem ipsum');
2+
let x = 10;
3+
4+
if(x<10)
5+
console.log('x is less than: '+x);
6+
else if(x>10)
7+
console.log('x is more than: '+x);
8+
else
9+
console.log('x is equals to: '+x);

0 commit comments

Comments
 (0)