Skip to content

Commit 0a3ead6

Browse files
committed
Merge branch 'code-fixes' into trunk
Signed-off-by: Hrishikesh Patil <[email protected]>
2 parents 3c91bf6 + b30545f commit 0a3ead6

File tree

7 files changed

+187
-150
lines changed

7 files changed

+187
-150
lines changed

.gitlab-ci.yml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ stages:
88
- test
99
- build
1010
- testing
11-
- coverage
11+
- post-test
1212
- publish
1313

1414
Build Latest:
@@ -58,7 +58,7 @@ Test Latest Options:
5858
- .nyc_output/
5959

6060
Coverage Latest:
61-
stage: coverage
61+
stage: post-test
6262
needs: ['Test Latest Options', 'Test Latest API']
6363
image: node:latest
6464
before_script:
@@ -83,7 +83,7 @@ Test LTS Options:
8383
needs: ['Build LTS']
8484

8585
Coverage LTS:
86-
stage: coverage
86+
stage: post-test
8787
needs: ['Test LTS Options', 'Test LTS API']
8888
image: node:lts
8989
before_script:
@@ -98,6 +98,13 @@ Coverage LTS:
9898
reports:
9999
cobertura: coverage/cobertura-coverage.xml
100100

101+
Lint:
102+
stage: post-test
103+
needs: ['Build Latest', 'Build LTS']
104+
image: node:lts
105+
script:
106+
- yarn prettier --check .
107+
101108
Publish:
102109
stage: publish
103110
needs:
@@ -106,6 +113,7 @@ Publish:
106113
'Test LTS API',
107114
'Test Latest Options',
108115
'Test Latest API',
116+
'Lint',
109117
]
110118
image: node:lts
111119
before_script:

CHANGELOG

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
v2.2.1
2+
3+
Fix few CodeClimate issues
4+
Publish package from GitLab CI instead of PC
5+
Update devDependencies
6+
17
v2.2.0
28

39
Use AJAX to show homepage rather than template engine

index.js

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,35 @@ const indexRouter = require('./routes/index');
99
const uploadRouter = require('./routes/upload');
1010
const downloadRouter = require('./routes/download');
1111

12-
// Sets the format to be used for logging
13-
const format =
14-
':method request for :url resulted in status code :status - :response-time ms';
12+
/*
13+
* Sets up logging for the app
14+
*
15+
* Takes express app and adds logger to it
16+
*/
17+
function setLogging(app, loggingLevel, stdout) {
18+
// Sets the format to be used for logging
19+
const format =
20+
':method request for :url resulted in status code :status - :response-time ms';
21+
22+
if (isNaN(loggingLevel) || loggingLevel > 2 || loggingLevel < 0)
23+
loggingLevel = 0;
24+
25+
stdout = stdout || process.stdout;
26+
27+
if (loggingLevel === 2)
28+
app.use(
29+
logger(format, {
30+
stream: stdout,
31+
})
32+
);
33+
else if (loggingLevel === 1)
34+
app.use(
35+
logger(format, {
36+
stream: stdout,
37+
skip: (req, res) => res.statusCode < 400,
38+
})
39+
);
40+
}
1541

1642
/*
1743
* Initialises an Express app with the view generator, plugins and routes
@@ -58,28 +84,6 @@ function handlers(app) {
5884
});
5985
}
6086

61-
/*
62-
* Sets valid values for keys of the options which may be invaild
63-
*
64-
* Takes options object which is modified and returns nothing
65-
*/
66-
function cleanOptions(options) {
67-
if (
68-
isNaN(options.loggingLevel) ||
69-
options.loggingLevel > 2 ||
70-
options.loggingLevel < 0
71-
)
72-
options.loggingLevel = 0;
73-
if (options.restart === undefined) options.restart = true;
74-
if (!Array.isArray(options.files)) options.files = [];
75-
if (!options.destination)
76-
options.destination = path.resolve(
77-
path.basename(require.main.filename),
78-
'../uploads'
79-
);
80-
if (!options.stdout) options.stdout = process.stdout;
81-
}
82-
8387
/*
8488
* Creates an Express app with the options specified
8589
*
@@ -88,23 +92,14 @@ function cleanOptions(options) {
8892
module.exports.init = (options = {}) => {
8993
const app = express();
9094

91-
cleanOptions(options);
92-
93-
if (options.loggingLevel === 2)
94-
app.use(
95-
logger(format, {
96-
stream: options.stdout,
97-
})
98-
);
99-
else if (options.loggingLevel === 1)
100-
app.use(
101-
logger(format, {
102-
stream: options.stdout,
103-
skip: (req, res) => res.statusCode < 400,
104-
})
105-
);
95+
// Set logging only if neccessary
96+
if (options.loggingLevel)
97+
setLogging(app, options.loggingLevel, options.stdout);
10698

10799
return new Promise((resolve, reject) => {
100+
// If restart flag is not set, default to true
101+
if (options.restart === undefined) options.restart = true;
102+
108103
require('./middleware/fileManager')
109104
.initFiles(options)
110105
.then(() => {

middleware/fileManager.js

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ function isPathPresent(arr, path) {
1818
return arr.find(value => value.path === path);
1919
}
2020

21+
/*
22+
* Adds a path to listFiles if not already present in it or directFiles
23+
*
24+
* Takes path of file and returns nothing
25+
*/
26+
function addPath(path) {
27+
if (!isPathPresent(directFiles, path) && !isPathPresent(listFiles, path))
28+
listFiles.push(fileMaker(path));
29+
}
30+
2131
/*
2232
* Reads a file line by line, and add the corresponding data to an array
2333
*
@@ -32,30 +42,22 @@ function readLines(listPath) {
3242
});
3343

3444
// Resolves the promise if the file was read successfully
35-
readInterface.on('close', resolve);
45+
readInterface.on('close', () => {
46+
resolve();
47+
calcSize(listFiles);
48+
});
3649

3750
let lineNumber = 1;
3851

3952
readInterface.on('line', line => {
40-
// Processes every line of the file
41-
if (
42-
!isPathPresent(directFiles, line) &&
43-
!isPathPresent(listFiles, line)
44-
) {
45-
// Adds the path to array ony if it exists
46-
if (fs.existsSync(line)) {
47-
listFiles.push(fileMaker(line));
48-
if (lineNumber === 1) calcSize(listFiles);
49-
}
50-
// Informs in the error message which path doesn't exist
51-
else
52-
reject(
53-
`${line} does not exist! (at ${path.resolve(
54-
process.cwd(),
55-
listPath
56-
)} at ${lineNumber})`
57-
);
58-
}
53+
if (fs.existsSync(line)) addPath(line);
54+
else
55+
reject(
56+
`${line} does not exist! (at ${path.resolve(
57+
process.cwd(),
58+
listPath
59+
)} at ${lineNumber})`
60+
);
5961
lineNumber++;
6062
});
6163
});
@@ -117,11 +119,16 @@ function fileMaker(path) {
117119
* Takes options object and returns promise that resolves when the basic setup is successfully completed
118120
*/
119121
function initFiles(options) {
122+
// Set files to empty array if not defined
123+
if (!Array.isArray(options.files)) options.files = [];
124+
120125
// Filters out unique values from the files flag
121126
directFiles = options.files
122127
.filter((value, index, self) => self.indexOf(value) === index)
123128
.map(fileMaker);
124-
destination = options.destination;
129+
destination =
130+
options.destination ||
131+
path.resolve(path.basename(require.main.filename), '../uploads');
125132

126133
// Starts calculating the size of all the files in files flag
127134
calcSize(directFiles);

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "broadcastem-core",
3-
"version": "2.2.0",
3+
"version": "2.2.1",
44
"description": "The core module for Broadcast 'em application",
55
"keywords": [
66
"broadcast",
@@ -36,8 +36,8 @@
3636
"chai-http": "^4.3.0",
3737
"dev-null": "^0.1.1",
3838
"husky": "^4.2.5",
39-
"mocha": "^8.1.1",
39+
"mocha": "^8.1.2",
4040
"mocha-junit-reporter": "^2.0.0",
41-
"prettier": "^2.0.5"
41+
"prettier": "^2.1.1"
4242
}
4343
}

public/script/root.js

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -106,39 +106,10 @@ UIkit.upload('.js-upload', {
106106
url: '/upload',
107107
multiple: true,
108108
type: 'multipart/form-data',
109-
beforeSend: function () {
110-
console.log('beforeSend', arguments);
111-
},
112-
beforeAll: function () {
113-
console.log('beforeAll', arguments);
114-
},
115-
load: function () {
116-
console.log('load', arguments);
117-
},
118-
error: function () {
119-
console.log('error', arguments);
120-
},
121-
complete: function () {
122-
console.log('complete', arguments);
123-
},
124-
loadStart: function (e) {
125-
console.log('loadStart', arguments);
126-
bar.removeAttribute('hidden');
127-
bar.max = e.total;
128-
bar.value = e.loaded;
129-
},
130-
progress: function (e) {
131-
console.log('progress', arguments);
132-
bar.max = e.total;
133-
bar.value = e.loaded;
134-
},
135-
loadEnd: function (e) {
136-
console.log('loadEnd', arguments);
137-
bar.max = e.total;
138-
bar.value = e.loaded;
139-
},
109+
loadStart: setBarValue,
110+
progress: setBarValue,
111+
loadEnd: setBarValue,
140112
completeAll: function () {
141-
console.log('completeAll', arguments);
142113
setTimeout(function () {
143114
bar.setAttribute('hidden', 'hidden');
144115
}, 1000);
@@ -147,6 +118,15 @@ UIkit.upload('.js-upload', {
147118
},
148119
});
149120

121+
/*
122+
* Sets the value for the progress bar
123+
*/
124+
function setBarValue(e) {
125+
bar.removeAttribute('hidden');
126+
bar.max = e.total;
127+
bar.value = e.loaded;
128+
}
129+
150130
/*
151131
* Once the button for downloading the selected files is clicked, sends the
152132
* appropriate request
@@ -160,13 +140,13 @@ function downloadSelected() {
160140
* files which will be requested for download
161141
*/
162142
function checkboxHit(id) {
163-
if (document.getElementById(`checkbox-${id}`).checked) {
164-
if (!indices.length)
165-
document.getElementById('selected').style.display = 'inline-block';
166-
indices.push(id);
167-
} else {
168-
indices.splice(indices.indexOf(id), 1);
169-
if (!indices.length)
170-
document.getElementById('selected').style.display = 'none';
171-
}
143+
// If checkbox gets selected, adds that id to indices else removes it
144+
document.getElementById(`checkbox-${id}`).checked
145+
? indices.push(id)
146+
: indices.splice(indices.indexOf(id), 1);
147+
148+
// If indices are empty, hide the 'Selected' download button
149+
document.getElementById('selected').style.display = indices.length
150+
? 'inline-block'
151+
: 'none';
172152
}

0 commit comments

Comments
 (0)