confini-js

Javascript adaptation of python-confini
git clone git://git.defalsify.org/confini-js.git
Log | Files | Refs

config.ts (2624B)


      1 import * as fs from 'fs';
      2 import * as ini from 'ini';
      3 import * as path from 'path';
      4 
      5 class Config {
      6 
      7 	filepath: 	string
      8 	store:		Object
      9 	censor:		Array<string>
     10 	require:	Array<string>
     11 	env_prefix:	string
     12 
     13 	constructor(filepath:string) {
     14 		this.filepath = filepath;
     15 		this.store = {};
     16 		this.censor = [];
     17 		this.require = [];
     18 	}
     19 
     20 	public process() {
     21 		const d = fs.readdirSync(this.filepath);
     22 		
     23 		const r = /.*\.ini$/;
     24 		for (let i = 0; i < d.length; i++) {
     25 			const f = d[i];
     26 			if (!f.match(r)) {
     27 				continue
     28 			}
     29 
     30 			const fp = path.join(this.filepath, f);
     31 			const v = fs.readFileSync(fp, 'utf-8');
     32 			const inid = ini.decode(v);
     33 			const inik = Object.keys(inid);
     34 			for (let j = 0; j < inik.length; j++) {
     35 				const k_section = inik[j]
     36 				const k = k_section.toUpperCase();
     37 				Object.keys(inid[k_section]).forEach((k_directive) => {
     38 					const kk = k_directive.toUpperCase();
     39 					const kkk = k + '_' + kk;
     40 
     41 					let r = inid[k_section][k_directive];
     42 					let k_env = kkk;
     43 					if (this.env_prefix !== undefined) {
     44 						k_env = this.env_prefix + '_' + k_env;
     45 					}
     46 					const env = process.env[k_env];
     47 					if (env !== undefined) {
     48 						console.debug('Environment variable ' + k_env + ' overrides ' + kkk);
     49 						r = env;
     50 					}
     51 					this.store[kkk] = r;
     52 				});
     53 			}
     54 		}
     55 	}
     56 
     57 	public override(o:object, prefix:string='') {
     58 		for (const k of Object.keys(o)) {
     59 			if (k.substring(0, prefix.length) != prefix) {
     60 				continue;
     61 			}
     62 			const kk = k.substring(prefix.length);
     63 			if (this.store[kk] === undefined) {
     64 				console.debug('skipping unknown key', kk);
     65 				continue;
     66 			}
     67 			console.debug('Replace ' + kk + ': ' + o[k]);
     68 			this.store[kk] = o[k];
     69 		}
     70 	}
     71 
     72 	public get(s:string) {
     73 		return this.store[s];
     74 	}
     75 
     76 	public toString() {
     77 		let s = '';
     78 		Object.keys(this.store).forEach((k) => {
     79 			s += k + '=' + this.store[k] + '\n';
     80 		});
     81 		return s;
     82 	}
     83 
     84 	public create(): boolean {
     85 		if (fs.existsSync(this.filepath)) {
     86 			const stat = fs.statSync(this.filepath);
     87 			if (!stat.isDirectory()) {
     88 				throw this.filepath + ' is not a directory'
     89 			}
     90 			return false;
     91 		}
     92 		fs.mkdirSync(this.filepath, {
     93 			recursive: true,
     94 		});
     95 		return true;
     96 	}
     97 
     98 	public generate(template:object):boolean {
     99 		if (!this.create()) {
    100 			console.debug('config directory already exists');
    101 			return false;
    102 		}
    103 		Object.keys(template).forEach((k) => {
    104 			const fd = fs.openSync(path.join(this.filepath, k + '.ini'), 'w');
    105 			fs.writeSync(fd, '[' + k + ']\n');
    106 			Object.keys(template[k]).forEach((kk) => {
    107 				fs.writeSync(fd, kk + ' = ' + template[k][kk] + '\n');
    108 			});
    109 			fs.closeSync(fd);
    110 		});
    111 		return true;
    112 	}
    113 }
    114 
    115 export { Config };