Skip to content

Commit

Permalink
chore(sass): building of dynamic sass
Browse files Browse the repository at this point in the history
  • Loading branch information
polonel committed Nov 7, 2018
1 parent d22e914 commit f01cf82
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 67 deletions.
4 changes: 4 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ function dbCallback(err, db) {
function(next) {
require('./src/settings/defaults').init(next);
},
function(next) {
winston.debug('Building dynamic sass...');
require('./src/sass/buildsass').build(next);
},
function(next) {
//Start Task Runners
require('./src/taskrunner');
Expand Down
9 changes: 9 additions & 0 deletions src/controllers/api/v1/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,14 @@ apiSettings.testMailer = function(req, res) {
});
};

apiSettings.buildsass = function(req, res) {
var buildsass = require('../../../sass/buildsass');
buildsass.build(function(err) {
if (err) return res.status(400).json({success: false, error: err});

return res.json({success: true});
});
};


module.exports = apiSettings;
66 changes: 1 addition & 65 deletions src/helpers/viewdata/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,79 +17,15 @@ var async = require('async'),
winston = require('winston'),
moment = require('moment'),
permissions = require('../../permissions'),
settingSchema = require('../../models/setting'),
settingUtil = require('../../settings/settingsUtil'),

sass = require('node-sass');

settingSchema = require('../../models/setting');

var viewController = {};
var viewdata = {};
viewdata.notifications = {};
viewdata.users = {};

var nconf = require('nconf');
var sassOptionsDefaults = {
indentedSyntax: true,
includePaths: [
nconf.get('base_dir') + '/src/sass'
],
outputStyle: 'compressed'
};

function sassVariable(name, value) {
return '$' + name + ': ' + value + '\n';
}

function sassVariables(variablesObj) {
return Object.keys(variablesObj).map(function (name) {
return sassVariable(name, variablesObj[name]);
}).join('\n');
}

function sassImport(path) {
return '@import \'' + path + '\'\n';
}

function dynamicSass(entry, vars, success, error) {
var dataString =
sassVariables(vars) +
sassImport(entry);
var sassOptions = _.assign({}, sassOptionsDefaults, {
data: dataString
});

sass.render(sassOptions, function (err, result) {
return (err)
? error(err)
: success(result.css.toString());
});
}

viewController.getData = function(request, cb) {
async.parallel([
function(callback) {
settingUtil.getSettings(function(err, s) {
if (!err && s) {
var settings = s.data.settings;

dynamicSass('app.sass', {
header_background: settings.colorHeaderBG.value,
header_primary: settings.colorHeaderPrimary.value,
primary: settings.colorPrimary.value,
secondary: settings.colorSecondary.value,
tertiary: settings.colorTertiary.value,
quaternary: settings.colorQuaternary.value
}, function(result) {
var fs = require('fs');
var themeCss = nconf.get('base_dir') + '/public/css/app.min.css';
fs.writeFileSync(themeCss, result);
return callback();
}, callback);
} else
return callback();
});
},
function(callback) {
settingSchema.getSetting('gen:sitetitle', function(err, setting) {
if (!err && setting && setting.value)
Expand Down
9 changes: 7 additions & 2 deletions src/public/js/angularjs/controllers/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -1616,9 +1616,14 @@ define([
'Content-Type': 'application/json'
}
}).then(function successCallback() {
helpers.UI.showSnackbar('Color Scheme Saved. Reloading...', false);
// Call rebuild of app.min.css
$timeout(function() { $window.location.reload(); }, 1000);
$http.get('/api/v1/settings/buildsass')
.then(function successCallback() {
helpers.UI.showSnackbar('Color Scheme Saved. Reloading...', false);
$timeout(function() { $window.location.reload(); }, 1000);
}, function errorCallback(err) {
helpers.UI.showSnackbar(err, true);
});
}, function errorCallback(err) {
helpers.UI.showSnackbar(err, true);
});
Expand Down
1 change: 1 addition & 0 deletions src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ function mainRoutes(router, middleware, controllers) {

router.put('/api/v1/settings', middleware.api, controllers.api.settings.updateSetting);
router.post('/api/v1/settings/testmailer', middleware.api, controllers.api.settings.testMailer);
router.get('/api/v1/settings/buildsass', middleware.api, controllers.api.settings.buildsass);

router.get('/api/v1/plugins/list/installed', middleware.api, function(req, res) { return res.json({success: true, loadedPlugins: global.plugins}); });
router.get('/api/v1/plugins/install/:packageid', middleware.api, middleware.isAdmin, controllers.api.plugins.installPlugin);
Expand Down
82 changes: 82 additions & 0 deletions src/sass/buildsass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
. .o8 oooo
.o8 "888 `888
.o888oo oooo d8b oooo oooo .oooo888 .ooooo. .oooo.o 888 oooo
888 `888""8P `888 `888 d88' `888 d88' `88b d88( "8 888 .8P'
888 888 888 888 888 888 888ooo888 `"Y88b. 888888.
888 . 888 888 888 888 888 888 .o o. )88b 888 `88b.
"888" d888b `V88V"V8P' `Y8bod88P" `Y8bod8P' 8""888P' o888o o888o
========================================================================
Created: 11/7/2018
Author: Chris Brame
*/

var _ = require('lodash');
var sass = require('node-sass');
var nconf = require('nconf');
var settingUtil = require('../settings/settingsUtil');

var buildsass = {};

var sassOptionsDefaults = {
indentedSyntax: true,
includePaths: [
nconf.get('base_dir') + '/src/sass'
],
outputStyle: 'compressed'
};

function sassVariable(name, value) {
return '$' + name + ': ' + value + '\n';
}

function sassVariables(variablesObj) {
return Object.keys(variablesObj).map(function (name) {
return sassVariable(name, variablesObj[name]);
}).join('\n');
}

function sassImport(path) {
return '@import \'' + path + '\'\n';
}

function dynamicSass(entry, vars, success, error) {
var dataString =
sassVariables(vars) +
sassImport(entry);
var sassOptions = _.assign({}, sassOptionsDefaults, {
data: dataString
});

sass.render(sassOptions, function (err, result) {
return (err)
? error(err)
: success(result.css.toString());
});
}

buildsass.build = function(callback) {
settingUtil.getSettings(function(err, s) {
if (!err && s) {
var settings = s.data.settings;

dynamicSass('app.sass', {
header_background: settings.colorHeaderBG.value,
header_primary: settings.colorHeaderPrimary.value,
primary: settings.colorPrimary.value,
secondary: settings.colorSecondary.value,
tertiary: settings.colorTertiary.value,
quaternary: settings.colorQuaternary.value
}, function(result) {
var fs = require('fs');
var themeCss = nconf.get('base_dir') + '/public/css/app.min.css';
fs.writeFileSync(themeCss, result);
return callback();
}, callback);
} else
return callback();
});
};

module.exports = buildsass;

0 comments on commit f01cf82

Please sign in to comment.