Skip to content

Commit 5aaef81

Browse files
committed
Refactors the main module. Adds unit testing. Fixes #2
1 parent 7b25ff4 commit 5aaef81

File tree

12 files changed

+263
-74
lines changed

12 files changed

+263
-74
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
node_modules
2-
tmp
2+
coverage
3+
.nyc_output
4+
tmp

.jsfmtrc

-12
This file was deleted.

.npmignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
test
2+
coverage
3+
.nyc_output

.travis.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
language: node_js
2+
node_js:
3+
- '6'
4+
- '4'

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# uglifyjs-folder
22

3+
[![Build Status](https://travis-ci.org/ionutvmi/uglifyjs-folder.svg?branch=master)](https://travis-ci.org/ionutvmi/uglifyjs-folder)
4+
35
Command to run uglifyjs on a folder and minify the result in a single file or a new folder.
46

57
## Getting Started

index.js

+50-55
Original file line numberDiff line numberDiff line change
@@ -7,81 +7,76 @@ var extend = require('extend');
77
var fs = require('fs');
88
var mkdirp = require('mkdirp');
99

10-
var _default = {
10+
var defaultOptions = {
1111
comments: true,
1212
output: '',
13-
each: false
13+
each: false,
14+
extension: '.min.js'
1415
};
1516

16-
module.exports = function(dirPath, options) {
17-
var res = '';
18-
var opt = extend({}, _default, options);
17+
module.exports = function (dirPath, options) {
18+
options = extend({}, defaultOptions, options);
1919

2020
// grab and minify all the js files
2121
var files = readDir.readSync(dirPath, ['**.js']);
2222

23-
files.forEach(function(v, i) {
24-
var code, newName;
23+
if (options.each) {
24+
// minify each file individually
25+
files.forEach(function (fileName) {
26+
options.output = isEmpty(options.output) ? '_out_' : options.output;
27+
var newName = path.join(options.output, path.dirname(fileName), path.basename(fileName, path.extname(fileName))) + options.extension;
2528

26-
// minify each file in the new folder
27-
if (opt.each) {
28-
code = uglifyJS.minify(path.join(dirPath, v)).code;
29+
var code = uglifyJS.minify(path.join(dirPath, fileName)).code;
30+
writeFile(newName, code);
31+
});
2932

30-
// build the new path
31-
if ( ! isValidOutput(opt.output)) {
32-
opt.output = '_out_';
33-
}
34-
newName = path.join(opt.output, path.dirname(v), path.basename(v, path.extname(v))) + opt.extension;
33+
} else {
3534

36-
mkdirp.sync(path.dirname(newName));
35+
var result = '';
36+
// concatenate all the files into one
37+
files.forEach(function (fileName) {
38+
if (options.comments) {
39+
result += '/**** ' + fileName + ' ****/\n';
40+
}
3741

38-
fs.writeFile(newName, code, function(err) {
39-
if (err) {
40-
console.log('Error: ' + err);
41-
return;
42-
}
43-
console.log('File ' + newName + ' written successfully !');
44-
});
42+
result += uglifyJS.minify(path.join(dirPath, fileName)).code + '\n';
43+
});
4544

45+
if (isEmpty(options.output)) {
46+
return result;
4647
} else {
47-
// concatenate all the files into one
48-
if (opt.comments) {
49-
res += '/**** ' + v + ' ****/\n';
50-
}
51-
52-
res += uglifyJS.minify(path.join(dirPath, v)).code + '\n';
48+
writeFile(options.output, result);
5349
}
5450

55-
});
56-
57-
// return or write the content in the output file
58-
if (opt.output) {
51+
}
5952

60-
if (!opt.each) {
53+
};
6154

62-
if ( ! isValidOutput(opt.output)) {
63-
opt.output = '_out_.js';
64-
}
65-
fs.writeFile(opt.output, res, function(err) {
66-
if (err) {
67-
console.log('Error: ' + err);
68-
return;
69-
}
70-
console.log('File ' + opt.output + ' written successfully !');
71-
});
72-
}
55+
/**
56+
* Checks if the provided parameter is not an empty string.
57+
*/
58+
function isEmpty(str) {
59+
if (typeof str != 'string' || str.trim() == '') {
60+
return true;
61+
}
62+
return false;
63+
}
7364

74-
} else {
65+
/**
66+
* Writes the code at the specified path.
67+
*/
68+
function writeFile(filePath, code) {
7569

76-
return res;
77-
}
70+
mkdirp(path.dirname(filePath), function () {
7871

72+
fs.writeFile(filePath, code, function (err) {
73+
if (err) {
74+
console.log('Error: ' + err);
75+
return;
76+
}
77+
console.log('File ' + filePath + ' written successfully !');
78+
});
79+
});
7980

80-
function isValidOutput(str) {
81-
if (typeof opt.output != 'string' || opt.output.trim() == '') {
82-
return false;
83-
}
84-
return true;
85-
}
81+
}
8682

87-
};

package.json

+21-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "uglifyjs-folder",
33
"description": "Run uglifyjs on a folder and minify the result in a single file",
4-
"version": "0.3.0",
4+
"version": "1.0.0",
55
"homepage": "https://github.com/ionutvmi/uglifyjs-folder",
66
"author": {
77
"name": "Mihai Ionut Vilcu",
@@ -29,15 +29,30 @@
2929
"directory",
3030
"cli"
3131
],
32-
"preferGlobal": "true",
32+
"preferGlobal": true,
3333
"bin": {
3434
"uglifyjs-folder": "cli.js"
3535
},
36+
"scripts": {
37+
"test": "ava",
38+
"coverage": "nyc --reporter=lcov ava"
39+
},
40+
"ava": {
41+
"files": [
42+
"test/index.js"
43+
]
44+
},
3645
"dependencies": {
37-
"extend": "^3.0.0",
38-
"meow": "^3.7.0",
39-
"mkdirp": "^0.5.1",
46+
"extend": "3.0.0",
47+
"meow": "3.7.0",
48+
"mkdirp": "0.5.1",
4049
"readdir": "0.0.13",
41-
"uglify-js": "^2.7.3"
50+
"uglify-js": "2.7.5"
51+
},
52+
"devDependencies": {
53+
"ava": "0.17.0",
54+
"nyc": "^10.0.0",
55+
"proxyquire": "^1.7.10",
56+
"sinon": "^1.17.6"
4257
}
4358
}

test/fixtures/folder1/file1.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

test/fixtures/folder1/file2.js

Whitespace-only changes.

test/fixtures/folder2/file1.js

Whitespace-only changes.

test/fixtures/folder2/folder2-nested/file2.js

Whitespace-only changes.

test/index.js

+179
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
import test from 'ava';
2+
import proxyquire from 'proxyquire';
3+
import sinon from 'sinon';
4+
import path from 'path';
5+
6+
var customStubs = {
7+
mkdirpStub: function() {}
8+
};
9+
10+
var requireStub = {
11+
fs: {
12+
writeFile: function() {}
13+
},
14+
mkdirp: function() {
15+
return customStubs.mkdirpStub.apply(this, arguments);
16+
},
17+
'uglify-js': {
18+
minify: function() {}
19+
}
20+
};
21+
22+
var uglifyJsFolder = proxyquire('../index', requireStub);
23+
var sandbox;
24+
25+
test.beforeEach(() => {
26+
sandbox = sinon.sandbox.create();
27+
});
28+
29+
test.afterEach(() => {
30+
sandbox.restore();
31+
});
32+
33+
test('exports a functions', t => {
34+
t.is(typeof uglifyJsFolder, 'function');
35+
36+
});
37+
38+
test('calls minify for all the files', t => {
39+
sandbox.stub(requireStub['uglify-js'], 'minify').returns({
40+
code: ''
41+
});
42+
43+
uglifyJsFolder(__dirname + '/fixtures/folder1');
44+
45+
t.deepEqual(requireStub['uglify-js'].minify.callCount, 2);
46+
});
47+
48+
test('calls minify for all nested files', t => {
49+
sandbox.stub(requireStub['uglify-js'], 'minify').returns({
50+
code: ''
51+
});
52+
53+
uglifyJsFolder(__dirname + '/fixtures/folder2');
54+
55+
t.deepEqual(requireStub['uglify-js'].minify.callCount, 2);
56+
});
57+
58+
test('comments are present when minifying in a single file', t => {
59+
sandbox.stub(requireStub['uglify-js'], 'minify').returns({
60+
code: ''
61+
});
62+
63+
var result = uglifyJsFolder(__dirname + '/fixtures/folder1');
64+
65+
t.true(result.indexOf('/**** file1.js ****/') > -1);
66+
t.true(result.indexOf('/**** file2.js ****/') > -1);
67+
});
68+
69+
test('skip comments if disabled', t => {
70+
sandbox.stub(requireStub['uglify-js'], 'minify').returns({
71+
code: ''
72+
});
73+
74+
var result = uglifyJsFolder(__dirname + '/fixtures/folder1', {
75+
comments: false
76+
});
77+
78+
t.true(result.indexOf('/**** file1.js ****/') == -1);
79+
t.true(result.indexOf('/**** file2.js ****/') == -1);
80+
});
81+
82+
test('write file if the output parameter is present', t => {
83+
sandbox.stub(requireStub['uglify-js'], 'minify').returns({
84+
code: ''
85+
});
86+
87+
sandbox.stub(customStubs, 'mkdirpStub').callsArg(1);
88+
sandbox.stub(requireStub.fs, 'writeFile');
89+
90+
var result = uglifyJsFolder(__dirname + '/fixtures/folder1', {
91+
output: 'subfolder/scripts.min.js'
92+
});
93+
94+
t.true(customStubs.mkdirpStub.calledOnce);
95+
t.true(customStubs.mkdirpStub.calledWith('subfolder'));
96+
t.true(requireStub.fs.writeFile.calledWith('subfolder/scripts.min.js'));
97+
});
98+
99+
test('write files in the specified output folder', t => {
100+
sandbox.stub(requireStub['uglify-js'], 'minify').returns({
101+
code: ''
102+
});
103+
104+
sandbox.stub(customStubs, 'mkdirpStub').callsArg(1);
105+
sandbox.stub(requireStub.fs, 'writeFile');
106+
107+
var dirPath = __dirname + '/fixtures/folder1';
108+
var outputPath = 'folder/dist';
109+
110+
var result = uglifyJsFolder(dirPath, {
111+
output: outputPath,
112+
each: true
113+
});
114+
115+
t.deepEqual(customStubs.mkdirpStub.callCount, 2);
116+
t.deepEqual(path.relative(requireStub.fs.writeFile.args[0][0], outputPath + '/file1.min.js'), '');
117+
t.deepEqual(path.relative(requireStub.fs.writeFile.args[1][0], outputPath + '/file2.min.js'), '');
118+
});
119+
120+
test('maintain subfolder structure', t => {
121+
sandbox.stub(requireStub['uglify-js'], 'minify').returns({
122+
code: ''
123+
});
124+
125+
sandbox.stub(customStubs, 'mkdirpStub').callsArg(1);
126+
sandbox.stub(requireStub.fs, 'writeFile');
127+
128+
var dirPath = __dirname + '/fixtures/folder2';
129+
var outputPath = 'folder/dist';
130+
131+
var result = uglifyJsFolder(dirPath, {
132+
output: outputPath,
133+
each: true
134+
});
135+
136+
t.deepEqual(customStubs.mkdirpStub.callCount, 2);
137+
t.deepEqual(path.relative(requireStub.fs.writeFile.args[0][0], outputPath + '/file1.min.js'), '');
138+
t.deepEqual(path.relative(requireStub.fs.writeFile.args[1][0], outputPath + '/folder2-nested/file2.min.js'), '');
139+
});
140+
141+
test('_out_ used as default output folder', t => {
142+
sandbox.stub(requireStub['uglify-js'], 'minify').returns({
143+
code: ''
144+
});
145+
146+
sandbox.stub(customStubs, 'mkdirpStub').callsArg(1);
147+
sandbox.stub(requireStub.fs, 'writeFile');
148+
149+
var dirPath = __dirname + '/fixtures/folder2';
150+
151+
var result = uglifyJsFolder(dirPath, {
152+
each: true
153+
});
154+
155+
t.deepEqual(customStubs.mkdirpStub.callCount, 2);
156+
t.deepEqual(path.relative(requireStub.fs.writeFile.args[0][0], '_out_/file1.min.js'), '');
157+
t.deepEqual(path.relative(requireStub.fs.writeFile.args[1][0], '_out_/folder2-nested/file2.min.js'), '');
158+
});
159+
160+
test('uses custom extension', t => {
161+
sandbox.stub(requireStub['uglify-js'], 'minify').returns({
162+
code: ''
163+
});
164+
165+
sandbox.stub(customStubs, 'mkdirpStub').callsArg(1);
166+
sandbox.stub(requireStub.fs, 'writeFile');
167+
168+
var dirPath = __dirname + '/fixtures/folder2';
169+
170+
var result = uglifyJsFolder(dirPath, {
171+
each: true,
172+
extension: '.test.txx'
173+
});
174+
175+
t.deepEqual(customStubs.mkdirpStub.callCount, 2);
176+
t.deepEqual(path.relative(requireStub.fs.writeFile.args[0][0], '_out_/file1.test.txx'), '');
177+
t.deepEqual(path.relative(requireStub.fs.writeFile.args[1][0], '_out_/folder2-nested/file2.test.txx'), '');
178+
});
179+

0 commit comments

Comments
 (0)