Skip to content

Commit

Permalink
refactor: use config module
Browse files Browse the repository at this point in the history
  • Loading branch information
Arylo committed Dec 22, 2018
1 parent b4bea90 commit 509f820
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
44 changes: 36 additions & 8 deletions lib/index.ts
@@ -1,12 +1,12 @@
import * as YAML from "config-yaml";
import * as crypto from "crypto";
import * as fs from "fs";
import * as ftconfig from "ftconfig";
import merge = require("lodash.merge");
import * as path from "path";

// tslint:disable-next-line:no-namespace
declare namespace Config {
export type Format = "json" | "yaml" | string;
export type Format = "json" | "yaml" | "json5" | "hjson" | "ini" | "toml" | string;
type TrueFormat = symbol | Format;

interface IStoreObj {
Expand All @@ -29,17 +29,45 @@ class Config<T extends { }> {

private parsers: Array<Config.IParserrObj<T>> = [{
filter: /\.json$/,
format: "yaml",
format: "json",
handler: (filepath) => {
const fileOptions = { encoding: "utf-8" };
const filedata = fs.readFileSync(filepath, fileOptions);
return JSON.parse(filedata);
const options = { encoding: "utf-8", type: "json" };
return ftconfig.readFile(filepath, options).toObject();
}
}, {
filter: /\.ya?ml$/,
format: "json",
format: "yaml",
handler: (filepath) => {
const options = { encoding: "utf-8", type: "yaml" };
return ftconfig.readFile(filepath, options).toObject();
}
}, {
filter: /\.json5$/,
format: "json5",
handler: (filepath) => {
const options = { encoding: "utf-8", type: "json5" };
return ftconfig.readFile(filepath, options).toObject();
}
}, {
filter: /\.hjson$/,
format: "hjson",
handler: (filepath) => {
const options = { encoding: "utf-8", type: "hjson" };
return ftconfig.readFile(filepath, options).toObject();
}
}, {
filter: /\.toml$/,
format: "toml",
handler: (filepath) => {
const options = { encoding: "utf-8", type: "toml" };
return ftconfig.readFile(filepath, options).toObject();
}
}, {
filter: /\.ini$/,
format: "ini",
handler: (filepath) => {
return YAML(filepath);
const options = { encoding: "utf-8", type: "ini" };
return ftconfig.readFile(filepath, options).toObject();
}
}];

Expand Down
5 changes: 2 additions & 3 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "y-config",
"version": "2.0.1",
"version": "2.0.2",
"description": "Create public configuration for the project",
"main": "dist/index.js",
"types": "lib/index.ts",
Expand Down Expand Up @@ -45,7 +45,6 @@
"homepage": "https://github.com/Arylo/y-config#readme",
"license": "MIT",
"devDependencies": {
"@types/config-yaml": "^1.1.0",
"@types/find-up": "^2.1.1",
"@types/lodash.merge": "^4.6.3",
"@types/node": "^10.1.2",
Expand All @@ -62,7 +61,7 @@
"typescript": "^2.8.3"
},
"dependencies": {
"config-yaml": "^1.1.1",
"ftconfig": "^1.1.1",
"lodash.merge": "^4.6.1"
}
}

0 comments on commit 509f820

Please sign in to comment.