Commit c8a6b777 authored by Philipp Nowinski's avatar Philipp Nowinski
Browse files

[FEATURE] rewrite settings to a proper singleton module + add watcher

parent af7694bf
const cliArguments = require('yargs').argv;
const logger = require('./logger');
const chalk = require('chalk');
const tasks = {
css: require('./tasks/css')
};
const settings = require('./settings');
/**
* Task Runner
......@@ -17,9 +15,9 @@ class Run {
process.chdir('..');
let taskArgument = Run._getTaskName();
let taskNames = taskArgument.split(':');
if (tasks.hasOwnProperty(taskNames[0])) {
let task = new tasks[taskNames[0]]();
task.run(taskNames[1] || null);
if (settings.tasks.hasOwnProperty(taskNames[0])) {
let task = settings.tasks[taskNames[0]]();
new task().run(taskNames[1] || null);
} else {
logger.error(`Task ${chalk.bold(taskNames[0])} is not defined`);
}
......
'use strict';
let config;
let extensionPath;
let webPath;
let extension;
const fs = require('fs');
const settings = {};
const path = require('path');
const argv = require('yargs').argv;
function init() {
config = settings.getConfig();
extensionPath = config.directories.basePath;
webPath = config.directories.webPath;
extension = config.extensions[0];
}
/**
* This module is a global settings singleton
* Every module can require this to access all global configurations
*/
class Settings {
/**
* Kick things off
*/
constructor() {
this._config = this.getConfig();
this._extensionPath = this._config.directories.basePath;
this._webPath = this._config.directories.webPath;
this._extension = this._config.extensions[0];
this._tasks = {
css: () => {return require('./tasks/css')},
watch: () => {return require('./tasks/watch')}
};
}
settings.getPath = function(isWebPath) {
let extensionName = (typeof argv.ext !== 'undefined')
? argv.ext
: extension;
return typeof isWebPath !== 'undefined'
? path.join(webPath, extensionName)
: path.join(extensionPath, extensionName);
};
settings.setPath = function(extensionName) {
extension = extensionName;
};
settings.getConfig = function() {
try {
fs.statSync('./sgc-core/gulp/config.json');
return require('./config.json');
} catch(exception) {
return require('../../.sgc-config.json');
/**
* Returns the currently used extension path
*
* @param {Boolean} _isWebPath indicates if the requested path is for web, or file system access
*/
getPath(_isWebPath) {
let extensionName = (typeof argv.ext !== 'undefined')
? argv.ext
: this._extension;
return typeof isWebPath !== 'undefined'
? path.join(this._webPath, extensionName)
: path.join(this._extensionPath, extensionName);
}
};
init();
module.exports = settings;
/**
* Set the current path to a new extension
*
* @param {String} _extensionName The name of the extension which path should be used
*/
setPath(_extensionName) {
this._extension = _extensionName;
}
/**
* Returns the sgc-config object
*/
getConfig() {
try {
fs.statSync('./sgc-core/gulp/config.json');
return require('./config.json');
} catch(exception) {
return require('../../.sgc-config.json');
}
}
/**
* Returns the object containing all tasks
*/
get tasks() {
return this._tasks;
}
}
const instance = new Settings();
Object.freeze(instance);
module.exports = instance;
......@@ -3,6 +3,7 @@ const fs = require('fs');
const path = require('path');
const mkdirp = require('mkdirp');
const chalk = require('chalk');
const settings = require('./settings');
/**
* This is the base class every task must extend
......@@ -13,9 +14,9 @@ module.exports = class task {
* Kick things off
*/
constructor() {
const settings = require('./settings');
this._settings = settings.getConfig();
this._path = settings.getPath();
this._settings = settings;
this._config = this._settings.getConfig();
this._path = this._settings.getPath();
this._logger = require('./logger');
}
......
......@@ -23,8 +23,8 @@ module.exports = class Css extends Task {
async run(_subTask = null) {
if (_subTask) {
const files = await globby([
`${this._path}/${this._settings.directories.sass}/**/*.scss`
].concat(this._settings.css.excludeFromQa));
`${this._path}/${this._config.directories.sass}/**/*.scss`
].concat(this._config.css.excludeFromQa));
if (_subTask === 'qa') {
files.forEach(file => {
this._qa(file);
......@@ -34,7 +34,7 @@ module.exports = class Css extends Task {
}
} else {
const files = await globby([
`${this._path}/${this._settings.directories.sass}/*.scss`,
`${this._path}/${this._config.directories.sass}/*.scss`,
'!**/_*.scss'
]);
files.forEach(file => {
......@@ -49,11 +49,11 @@ module.exports = class Css extends Task {
* @param {String} _fileName The name of the input file
*/
_getOutputPath(_fileName) {
let newSuffix = this._settings.css.pipeline.renameToDotMin
let newSuffix = this._config.css.pipeline.renameToDotMin
? '.min.css'
: '.css';
let fileName = path.basename(_fileName).replace('.scss', newSuffix);
return this._getFullPath(`${this._settings.directories.css}/${fileName}`);
return this._getFullPath(`${this._config.directories.css}/${fileName}`);
}
/**
......@@ -63,7 +63,7 @@ module.exports = class Css extends Task {
*/
_getSourceMapsPath(_fileName) {
let fileName = path.basename(_fileName).replace('.css', '.css.map');
return this._getFullPath(`${this._settings.directories.cssSourceMaps}/${fileName}`);
return this._getFullPath(`${this._config.directories.cssSourceMaps}/${fileName}`);
}
/**
......
const Task = require('../task');
const watch = require('node-watch');
const globby = require('globby');
/**
* The watch-task observes changes in source files and triggers compilation
*/
module.exports = class Watch extends Task {
/**
* Runner function
*/
async run() {
this._watchCss();
}
/**
* Watch for CSS-related files
*/
async _watchCss() {
const files = await globby([
`${this._path}/${this._config.directories.sass}/**/*.scss`
].concat(this._config.css.excludeFromQa));
files.forEach(file => {
watch(file, { recursive: true }, this._onChange.bind(this));
});
}
/**
* Trigger CSS-compilation
*/
_triggerCss() {
let task = this._settings.tasks.css();
new task().run();
}
/**
* This function will execute on file changes
*
* @param {Event} _event The change event
* @param {String} _fileName The path to the file that changed
*/
_onChange(_event, _fileName) {
this._logger.info(`${_fileName} changed`);
this._triggerCss();
}
}
......@@ -2901,6 +2901,11 @@
}
}
},
"node-watch": {
"version": "0.5.8",
"resolved": "https://registry.npmjs.org/node-watch/-/node-watch-0.5.8.tgz",
"integrity": "sha512-HXiCg9YS3IAzEJ4O3CNlUjsoYCxg4hVbo9oAEBQoiJV7p/R7PT6/ekXM9S3D2K3jUjSQOOjtuW/LgcSiwh/btQ=="
},
"nopt": {
"version": "3.0.6",
"resolved": "https://registry.npmjs.org/nopt/-/nopt-3.0.6.tgz",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment