commit fddb30b91d3dc08a1c811f5dfe390fc6bb5c1261
parent 10582c30b53de823811106962a4419f54d2e636c
Author: nolash <dev@holbrook.no>
Date: Thu, 10 Dec 2020 16:06:15 +0100
Add config dir and files from template generation
Diffstat:
3 files changed, 29 insertions(+), 2 deletions(-)
diff --git a/CHANGELOG b/CHANGELOG
@@ -1,3 +1,5 @@
+* 0.0.6
+ - Add config dir generation
* 0.0.5
- Remove rouge concat of string literal 'undefined' in env var
* 0.0.4
diff --git a/package.json b/package.json
@@ -1,6 +1,6 @@
{
"name": "confini",
- "version": "0.0.5",
+ "version": "0.0.6",
"description": "Merge all ini configuration files from a folder",
"main": "dist/index.js",
"scripts": {
diff --git a/src/config.ts b/src/config.ts
@@ -79,7 +79,32 @@ class Config {
s += k + '=' + this.store[k] + '\n';
});
return s;
- }
+ }
+
+ private ensureDir() {
+ if (fs.existsSync(this.filepath)) {
+ const stat = fs.statSync(this.filepath);
+ if (!stat.isDirectory()) {
+ throw this.filepath + ' is not a directory'
+ }
+ } else {
+ fs.mkdirSync(this.filepath, {
+ recursive: true,
+ });
+ }
+ }
+
+ public generate(template:object) {
+ this.ensureDir();
+ Object.keys(template).forEach((k) => {
+ const fd = fs.openSync(path.join(this.filepath, k + '.ini'), 'w');
+ fs.writeSync(fd, '[' + k + ']\n');
+ Object.keys(template[k]).forEach((kk) => {
+ fs.writeSync(fd, kk + ' = ' + template[k][kk] + '\n');
+ });
+ fs.closeSync(fd);
+ });
+ }
}
export { Config };