Skip to content

Instantly share code, notes, and snippets.

@molda
Last active June 18, 2017 09:07
Show Gist options
  • Save molda/b7e93d21e21eca07155b4c0e4c6d129e to your computer and use it in GitHub Desktop.
Save molda/b7e93d21e21eca07155b4c0e4c6d129e to your computer and use it in GitHub Desktop.
require('total.js');
var Fs = require('fs');
function existsSync(filename, file) {
try {
var val = Fs.statSync(filename);
return val ? (file ? val.isFile() : true) : false;
} catch (e) {
return false;
}
}
// seznam uživatelských proměnných definovaných v config souborech
// ukládají se pouze tyto klíče z F.config
F.$user_config = {
common: [], // config
release: [], // config-release
debug: [], // config-debug
test: [] // config-test
};
// přidání configu a uložení
F.$add_user_config = function(name, value, mode) {
mode = mode || 'common';
F.config[name] = value;
// smazat pokud už je přidáno v některém ze souborů config
Object.keys(F.$user_config).forEach(function(key){
F.$user_config[mode] = F.$user_config[mode].remove((c) => c === name);
});
F.$user_config[mode].push(name);
F.$save_user_config();
};
// uložení configu
F.$save_user_config = function() {
Object.keys(F.$user_config).forEach(function(mode){
var user_config = '';
F.$user_config[mode].forEach((name) => { user_config += line(name, F.config[name], typeof(F.config[name])); });
user_config && (Fs.writeFile(U.combine('/', 'config' + (mode === 'common' ? '' : '-' + mode)), user_config, 'utf8', NOOP));
});
function line (name, value ,type) {
if (type === 'object') {
value = JSON.stringify(value);
type = 'JSON';
}
return name.padRight(30) + ' ' + (type !== 'string' ? '(' + type + ')' : '').padRight(9) + ' : ' + value + '\n';
};
};
// čtení config souborů
// dalo by se použít tady https://github.com/totaljs/framework/blob/master/index.js#L8100
// a tady https://github.com/totaljs/framework/blob/master/index.js#L8103
// a for loop by tam být nemusel https://github.com/totaljs/framework/blob/master/index.js#L8127
F.$read_user_config = function(filename, encoding, mode) {
var conf = {};
if (!existsSync(filename) || !Fs.lstatSync(filename).isFile())
return {};
conf = Fs.readFileSync(filename).toString(encoding).parseConfig();
Object.keys(conf).forEach(function(name){
var value = conf[name];
if (!value)
return;
F.$user_config[mode].push(name);
switch (name) {
case 'default-cors-maxage':
case 'default-request-length':
case 'default-websocket-request-length':
case 'default-request-timeout':
case 'default-interval-clear-cache':
case 'default-interval-clear-resources':
case 'default-interval-precompile-views':
case 'default-interval-uptodate':
case 'default-interval-websocket-ping':
case 'default-maximum-file-descriptors':
case 'default-interval-clear-dnscache':
conf[name] = U.parseInt(value);
break;
case 'default-image-consumption':
case 'default-image-quality':
conf[name] = U.parseInt(typeof(value) === 'number' ? value : value.replace(/\%|\s/g, ''));
break;
case 'static-accepts-custom':
accepts = value.replace(REG_ACCEPTCLEANER, '').split(',');
break;
case 'default-root':
if (value)
conf[name] = U.path(value);
break;
case 'static-accepts':
conf[name] = {};
tmp = value.replace(REG_ACCEPTCLEANER, '').split(',');
for (var j = 0; j < tmp.length; j++)
conf[name][tmp[j]] = true;
break;
case 'mail.smtp':
case 'mail.smtp.options':
case 'mail.address.from':
case 'mail.address.copy':
case 'mail.address.bcc':
case 'mail.address.reply':
if (name === 'mail.address.bcc')
tmp = 'mail-address-copy';
else
tmp = name.replace(/\./g, '-');
OBSOLETE(name, 'is renamed to "' + tmp + '"');
conf[tmp] = value;
break;
case 'allow-gzip':
case 'allow-websocket':
case 'allow-performance':
case 'allow-compile-html':
case 'allow-compile-style':
case 'allow-compile-script':
case 'allow-defer':
case 'allow-debug':
case 'disable-strict-server-certificate-validation':
case 'disable-clear-temporary-directory':
case 'trace':
case 'allow-cache-snapshot':
conf[name] = value.toLowerCase() === 'true' || value === '1' || value === 'on';
break;
case 'version':
conf[name] = value;
break;
}
});
return conf;
};
// TEST
var conf = F.$read_user_config(U.combine('/', 'config'), 'utf8', 'common');
console.log('CONF', conf);
U.extend(F.config, conf, true);
F.$add_user_config('number', 123, 'common'); // ukládá automaticky
console.log('CONF2', F.$user_config);
//F.$save_user_config();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment