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

[FEATURE] add sourcemaps support and better file handling

parent 9cb2fdbe
const util = require('util');
const fs = require('fs');
const path = require('path');
const mkdirp = require('mkdirp');
const chalk = require('chalk');
/**
* This is the base class every task must extend
*/
module.exports = class task {
/**
* Kick things off
*/
constructor() {
const settings = require('./settings');
this._settings = settings.getConfig();
......@@ -7,11 +19,50 @@ module.exports = class task {
this._logger = require('./logger');
}
/**
* Runs the task. A subTask can be triggered by apending it with a colon
*
* E.g: sgc css – triggers the main Css Task
* sgc css:qa – triggers the subTask qa
*
* @param {String} _subTask Name of the subtask
*/
run(_subTask = null) {
this._logger.error('Task is not implemented');
}
/**
* Logs the error message that _task could not be found
*
* @param {String} _task The task that could not be found
*/
_logTaskNotDefined(_task) {
logger.error(`Task ${chalk.bold(_task)} is not defined`);
}
/**
* Returns the full path (relative to the project dit) for _path
*
* @param {String} _path The path the full path shall be found for
*/
_getFullPath(_path) {
return `${this._path}/${_path}`;
}
/**
* Writes _content to _filePath.
* It will create all necessary folders in _filePath if they don't exist already
*
* @param {String} _filePath The path that shall be written to
* @param {Buffer} _content The file contents
*/
async _writeFile(_filePath, _content) {
let directory = path.dirname(_filePath);
if (!fs.existsSync(directory)) {
mkdirp.sync(directory);
this._logger.info(`Created directory ${chalk.dim(directory)}`);
}
let writeFile = util.promisify(fs.writeFile);
return writeFile(_filePath, _content);
}
}
......@@ -49,7 +49,17 @@ module.exports = class Css extends Task {
*/
_getOutputPath(_fileName) {
let fileName = path.basename(_fileName).replace('.scss', '.css');
return `${this._path}/${this._settings.directories.css}/${fileName}`;
return this._getFullPath(`${this._settings.directories.css}/${fileName}`);
}
/**
* Returns the path to the sourceMap associated with _fileName
*
* @param {String} _fileName The name of the CSS-file the sourceMap is associated with
*/
_getSourceMapsPath(_fileName) {
let fileName = path.basename(_fileName).replace('.css', '.css.map');
return this._getFullPath(`${this._settings.directories.cssSourceMaps}/${fileName}`);
}
/**
......@@ -61,10 +71,13 @@ module.exports = class Css extends Task {
async _compile(_input, _output) {
const render = util.promisify(sass.render);
let result;
const sourceMapPath = this._getSourceMapsPath(_output);
try {
result = await render({
file: _input,
outputStyle: 'compressed'
outputStyle: 'compressed',
outFile: _output,
sourceMap: sourceMapPath
});
} catch(_error) {
this._logger.error(_error);
......@@ -72,12 +85,15 @@ module.exports = class Css extends Task {
let css = await postcss([autoprefixer]).process(result.css, {from: _input});
fs.writeFile(_output, css, _error => {
if (_error) {
return this._logger.error(_error);
}
this._logger.success(`Compiled ${chalk.white(path.basename(_input))}`);
});
try {
await this._writeFile(_output, css);
await this._writeFile(sourceMapPath, result.map);
} catch (_error) {
return this._logger.error(_error.stack);
}
this._logger.success(`Written ${chalk.white(path.basename(_input))}`)
this._logger.success(`Written ${chalk.white(path.basename(sourceMapPath))}`)
}
/**
......
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