Skip to content

Instantly share code, notes, and snippets.

@ikr7
Last active August 29, 2015 14:07
Show Gist options
  • Save ikr7/dc63303347461b81d725 to your computer and use it in GitHub Desktop.
Save ikr7/dc63303347461b81d725 to your computer and use it in GitHub Desktop.
コンフィグ
var get = exports.get = function(name, key, fs, path){
fs = fs ? fs : require('fs');
path = path ? path : require('path');
var file = path.resolve(__dirname, name + '.json')
if(!fs.existsSync(file)){
console.error('File', file,'does not exist.');
return null;
}
var json = fs.readFileSync(file, 'utf8');
var data;
try{
data = JSON.parse(json);
}catch(err){
console.error('Failed to parse', file, ':', err.message);
return null;
}
return data[key] || null;
};
var set = exports.set = function(name, key, value, fs, path){
fs = fs ? fs : require('fs');
path = path ? path : require('path');
var file = path.resolve(__dirname, name + '.json')
if(!fs.existsSync(file)){
console.error('File', file,'does not exist.');
return null;
}
var json = fs.readFileSync(file, 'utf8');
var data;
try{
data = JSON.parse(json);
}catch(err){
console.error('Failed to parse', file, ':', err.message);
return null;
}
data[key] = value;
var newJson;
try{
newJson = JSON.stringify(data, null, '\t');
}catch(err){
console.error('Failed to stringify data.', err.message);
return null;
}
try{
fs.writeFileSync(file, newJson, 'utf8');
}catch(err){
console.error('Failed to write JSON to', file, '. :', err.message);
return null;
}
return value;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment