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 cliArguments = require('yargs').argv;
const logger = require('./logger'); const logger = require('./logger');
const chalk = require('chalk'); const chalk = require('chalk');
const tasks = { const settings = require('./settings');
css: require('./tasks/css')
};
/** /**
* Task Runner * Task Runner
...@@ -17,9 +15,9 @@ class Run { ...@@ -17,9 +15,9 @@ class Run {
process.chdir('..'); process.chdir('..');
let taskArgument = Run._getTaskName(); let taskArgument = Run._getTaskName();
let taskNames = taskArgument.split(':'); let taskNames = taskArgument.split(':');
if (tasks.hasOwnProperty(taskNames[0])) { if (settings.tasks.hasOwnProperty(taskNames[0])) {
let task = new tasks[taskNames[0]](); let task = settings.tasks[taskNames[0]]();
task.run(taskNames[1] || null); new task().run(taskNames[1] || null);
} else { } else {
logger.error(`Task ${chalk.bold(taskNames[0])} is not defined`); logger.error(`Task ${chalk.bold(taskNames[0])} is not defined`);
} }
......
'use strict'; 'use strict';
let config;
let extensionPath;
let webPath;
let extension;
const fs = require('fs'); const fs = require('fs');
const settings = {};
const path = require('path'); const path = require('path');
const argv = require('yargs').argv; const argv = require('yargs').argv;
function init() { /**
config = settings.getConfig(); * This module is a global settings singleton
extensionPath = config.directories.basePath; * Every module can require this to access all global configurations
webPath = config.directories.webPath; */
extension = config.extensions[0]; 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') * Returns the currently used extension path
? argv.ext *
: extension; * @param {Boolean} _isWebPath indicates if the requested path is for web, or file system access
return typeof isWebPath !== 'undefined' */
? path.join(webPath, extensionName) getPath(_isWebPath) {
: path.join(extensionPath, extensionName); let extensionName = (typeof argv.ext !== 'undefined')
}; ? argv.ext
: this._extension;
settings.setPath = function(extensionName) { return typeof isWebPath !== 'undefined'
extension = extensionName; ? path.join(this._webPath, extensionName)
}; : path.join(this._extensionPath, extensionName);
settings.getConfig = function() {
try {
fs.statSync('./sgc-core/gulp/config.json');
return require('./config.json');
} catch(exception) {
return require('../../.sgc-config.json');
} }
};
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'); ...@@ -3,6 +3,7 @@ const fs = require('fs');
const path = require('path'); const path = require('path');
const mkdirp = require('mkdirp'); const mkdirp = require('mkdirp');
const chalk = require('chalk'); const chalk = require('chalk');
const settings = require('./settings');
/** /**
* This is the base class every task must extend * This is the base class every task must extend
...@@ -13,9 +14,9 @@ module.exports = class task { ...@@ -13,9 +14,9 @@ module.exports = class task {
* Kick things off * Kick things off
*/ */
constructor() { constructor() {
const settings = require('./settings'); this._settings = settings;
this._settings = settings.getConfig(); this._config = this._settings.getConfig();
this._path = settings.getPath(); this._path = this._settings.getPath();
this._logger = require('./logger'); this._logger = require('./logger');
} }
......
...@@ -23,8 +23,8 @@ module.exports = class Css extends Task { ...@@ -23,8 +23,8 @@ module.exports = class Css extends Task {
async run(_subTask = null) { async run(_subTask = null) {
if (_subTask) { if (_subTask) {
const files = await globby([ const files = await globby([
`${this._path}/${this._settings.directories.sass}/**/*.scss` `${this._path}/${this._config.directories.sass}/**/*.scss`
].concat(this._settings.css.excludeFromQa)); ].concat(this._config.css.excludeFromQa));
if (_subTask === 'qa') { if (_subTask === 'qa') {
files.forEach(file => { files.forEach(file => {
this._qa(file); this._qa(file);
...@@ -34,7 +34,7 @@ module.exports = class Css extends Task { ...@@ -34,7 +34,7 @@ module.exports = class Css extends Task {
} }
} else { } else {
const files = await globby([ const files = await globby([
`${this._path}/${this._settings.directories.sass}/*.scss`, `${this._path}/${this._config.directories.sass}/*.scss`,
'!**/_*.scss' '!**/_*.scss'
]); ]);
files.forEach(file => { files.forEach(file => {
...@@ -49,11 +49,11 @@ module.exports = class Css extends Task { ...@@ -49,11 +49,11 @@ module.exports = class Css extends Task {
* @param {String} _fileName The name of the input file * @param {String} _fileName The name of the input file
*/ */
_getOutputPath(_fileName) { _getOutputPath(_fileName) {
let newSuffix = this._settings.css.pipeline.renameToDotMin let newSuffix = this._config.css.pipeline.renameToDotMin
? '.min.css' ? '.min.css'
: '.css'; : '.css';
let fileName = path.basename(_fileName).replace('.scss', newSuffix); 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 { ...@@ -63,7 +63,7 @@ module.exports = class Css extends Task {
*/ */
_getSourceMapsPath(_fileName) { _getSourceMapsPath(_fileName) {
let fileName = path.basename(_fileName).replace('.css', '.css.map'); 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 @@ ...@@ -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": { "nopt": {
"version": "3.0.6", "version": "3.0.6",
"resolved": "https://registry.npmjs.org/nopt/-/nopt-3.0.6.tgz", "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