Skip to content
Snippets Groups Projects
Commit 3ab6e042 authored by João Victor Tozatti Risso's avatar João Victor Tozatti Risso
Browse files

Change directory creation to gulpfile

parent 69ecb4be
No related branches found
No related tags found
1 merge request!13Fix runtime error when the logs directory is not present
Pipeline #
const fs = require('fs');
const gulp = require('gulp'); const gulp = require('gulp');
const babel = require('gulp-babel'); const babel = require('gulp-babel');
...@@ -12,10 +14,23 @@ const Cache = require('gulp-file-cache'); ...@@ -12,10 +14,23 @@ const Cache = require('gulp-file-cache');
const cache = new Cache(); const cache = new Cache();
function createLogDir() {
const logDirPath = 'build/logs';
fs.access(logDirPath, fs.F_OK, (err) => {
if (err) {
console.info(`Logs directory not found, creating it.`);
fs.mkdir(logDirPath, 0o700, (dirErr) => {
console.error(`Failed to create logs directory.\n\tError: ${dirErr}`);
});
}
});
}
/** /**
* Compile source files * Compile source files
*/ */
function compile() { gulp.task('compile', () => {
createLogDir();
// run ESLint // run ESLint
gulp.src('src/**/*.js') gulp.src('src/**/*.js')
.pipe(eslint()) .pipe(eslint())
...@@ -31,10 +46,9 @@ function compile() { ...@@ -31,10 +46,9 @@ function compile() {
// copy configuration file to build directory // copy configuration file to build directory
gulp.src('config.json') gulp.src('config.json')
.pipe(gulp.dest('build')); .pipe(gulp.dest('build'));
});
} gulp.task('build', ['compile']);
gulp.task('build', compile);
gulp.task('test', ['build'], () => { gulp.task('test', ['build'], () => {
gulp.src('test/test.js', {read: false}) gulp.src('test/test.js', {read: false})
...@@ -47,13 +61,12 @@ gulp.task('test', ['build'], () => { ...@@ -47,13 +61,12 @@ gulp.task('test', ['build'], () => {
}); });
}); });
gulp.task('watch', [], () => { gulp.task('watch', ['compile'], () => {
console.log('Watching source directory for changes'); console.log('Watching source directory for changes');
compile();
gulp.watch('src/**/*.js').on('change', () => { gulp.watch('src/**/*.js').on('change', () => {
console.log('Recompiling source'); console.log('Recompiling source');
compile(); gulp.start('compile');
console.log('Source recompilation done') console.log('Source recompilation done');
}); });
}); });
......
...@@ -2,8 +2,6 @@ const config = require('./config'); ...@@ -2,8 +2,6 @@ const config = require('./config');
const winston = require('winston'); const winston = require('winston');
const fs = require('fs');
winston.emitErrs = true; winston.emitErrs = true;
function getFilePath(module) { function getFilePath(module) {
...@@ -13,25 +11,28 @@ function getFilePath(module) { ...@@ -13,25 +11,28 @@ function getFilePath(module) {
function logger(module) { function logger(module) {
const logPath = `${process.cwd()}/logs`; const logPath = `${process.cwd()}/logs`;
/* Check if logs directory exists */ const maxLogFiles = 5;
try { const maxLogSize = 5242880;
fs.accessSync(logPath, fs.F_OK, () => {});
} catch (err) {
// if logs directory does not exist, create it.
fs.mkdirSync(logPath, 0o700, (dirErr) => {
throw new Error(`Failed to create logs directory!\n\tError: ${dirErr}`);
});
}
const log = new winston.Logger({ const log = new winston.Logger({
transports: [ transports: [
new winston.transports.File({ new winston.transports.File({
name: 'info-log', name: 'info-log',
level: 'info', level: 'info',
filename: `${logPath}/all.log`, filename: `${logPath}/simcaq-info.log`,
handleException: true, handleException: true,
json: false, json: false,
maxSize: 5242880, // 5MB maxSize: maxLogSize, // 5MB
maxFiles: 2, maxFiles: maxLogFiles,
colorize: false,
}),
new winston.transports.File({
name: 'error-log',
level: 'error',
filename: `${logPath}/simcaq-error.log`,
handleException: true,
json: false,
maxSize: maxLogSize, // 5MB
maxFiles: maxLogFiles,
colorize: false, colorize: false,
}), }),
new winston.transports.Console({ new winston.transports.Console({
...@@ -45,7 +46,6 @@ function logger(module) { ...@@ -45,7 +46,6 @@ function logger(module) {
], ],
exitOnError: false, exitOnError: false,
}); });
const debugMode = (typeof config.get('debug') === 'undefined') ? const debugMode = (typeof config.get('debug') === 'undefined') ?
config.get('debug') : false; config.get('debug') : false;
if (!debugMode) { if (!debugMode) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment