Skip to content

Commit

Permalink
Refactor JavaScript modules to TypeScript modules
Browse files Browse the repository at this point in the history
  • Loading branch information
derbenoo authored and decentro-gmbh committed Dec 29, 2018
1 parent 24670dd commit eeed823
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/*
package-lock.json
dist/
6 changes: 5 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
#
####################################################################################

FROM node:10.6.0-stretch
FROM node:10.14-stretch
LABEL maintainer="Benjamin Assadsolimani"
WORKDIR /app

# Add bin folder of node_modules to PATH
ENV PATH="/app/node_modules/.bin:${PATH}"

# Keep container alive so that a dev can attach to it with a shell
ENTRYPOINT tail -f /dev/null
2 changes: 1 addition & 1 deletion example/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
'use strict';

const path = require('path');
const FlexConf = require('../lib/flex-conf');
const { FlexConf } = require('../dist/flex-conf');

const conf = new FlexConf(path.join(__dirname, 'configs'), {
tagDefinitions: {
Expand Down
21 changes: 14 additions & 7 deletions lib/config-file.js → lib/config-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,23 @@

'use strict';

const debug = require('debug')('flex_conf');
const path = require('path');
const fs = require('fs');
import * as debug from 'debug';
import * as fs from 'fs';
import * as path from 'path';

/**
* Configuration file class, representing a single configuration file inside the configuration folder or one of its sub-folders.
*/
class ConfigFile {
export class ConfigFile {
filepath: string;
configFolder: string;
folderTags: Array<any>;
tagDefinitions: any;
tagSeparator: string;
keyValSeparator: string;
tags: any;
namespace: string;

/**
* Create a new configuration file.
* @param {string} filepath - Path to the configuration file.
Expand All @@ -23,7 +32,7 @@ class ConfigFile {
* @param {string} [options.tagSeparator='.'] - Seperation character for tags inside the filename.
* @param {string} [options.keyValSeparator='-'] - Seperation character for a tag's key and value.
*/
constructor(filepath, options = {}) {
constructor(filepath, options: any = {}) {
this.filepath = filepath;
this.configFolder = options.configFolder;
this.folderTags = options.folderTags;
Expand Down Expand Up @@ -110,5 +119,3 @@ class ConfigFile {
}
}
}

module.exports = ConfigFile;
38 changes: 25 additions & 13 deletions lib/flex-conf.js → lib/flex-conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,33 @@

'use strict';

const debug = require('debug')('flex_conf');
const os = require('os');
const fs = require('fs');
const path = require('path');
const nconf = require('nconf');
const ConfigFile = require('./config-file');
const TagDefinition = require('./tag-definition');
const { getFilesFromDir } = require('./utils');
import * as debug from 'debug';
import * as os from 'os';
import * as fs from 'fs';
import * as path from 'path';
import * as nconf from 'nconf';
import { ConfigFile } from './config-file'
import { TagDefinition } from './tag-definition'
import { getFilesFromDir } from './utils'

/**
* FlexConf class, representing the entire config of the config folder
*/
class FlexConf {
export class FlexConf {
configFolder: string;
loadRecursive: boolean;
folderTags: boolean;
postfix: string;
parseArgv: boolean;
parseEnv: boolean;
autoload: boolean;
separator: string;
lowerCase: boolean;
parseValues: boolean;
tagDefinitions: Object;
configFiles: Array<any>;
store: any;

/**
* Create a new configuration instance.
* @param {string} configFolder - Path to the folder that holds all configuration files.
Expand All @@ -33,7 +47,7 @@ class FlexConf {
* @param {boolean} [options.parseValues=false] - Attempt to parse well-known values (e.g. 'false', 'true', 'null', 'undefined', '3', '5.1' and JSON values) into their proper types. If a value cannot be parsed, it will remain a string.
* @param {boolean} [options.autoload=true] - Whether to automatically load all configuration files on instantiation.
*/
constructor(configFolder, options = {}) {
constructor(configFolder: string, options: any = {}) {
// Filesystem options
this.configFolder = configFolder;
this.loadRecursive = options.loadRecursive !== undefined ? options.loadRecursive : true;
Expand Down Expand Up @@ -132,7 +146,7 @@ class FlexConf {
* @param {number} [options.mode=0o600] - File permissions, default to read-only for the owner.
* @returns {string} Path of the saved config file.
*/
saveToFile(namespace, options = {}) {
saveToFile(namespace, options: any = {}) {
const configObject = this.store.get(namespace);
if (!options.filepath) {
options.filepath = path.join(os.tmpdir(), `${namespace}.json`);
Expand All @@ -153,5 +167,3 @@ class FlexConf {
return this.store.get();
}
}

module.exports = FlexConf;
9 changes: 6 additions & 3 deletions lib/tag-definition.js → lib/tag-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@

'use strict';

class TagDefinition {
export class TagDefinition {
name: string;
applies: Function;
map: Function;
score: Function;

/**
* Create a new TagDefinition instance.
* @param {string} name - Name of the tag.
Expand All @@ -21,5 +26,3 @@ class TagDefinition {
this.score = options.score || function () { return 1; };
}
}

module.exports = TagDefinition;
10 changes: 3 additions & 7 deletions lib/utils.js → lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

'use strict';

const path = require('path');
const fs = require('fs');
import * as fs from 'fs';
import * as path from 'path';

/**
* Get all <filetype> files from a directory and all its sub-directories.
Expand All @@ -15,7 +15,7 @@ const fs = require('fs');
* @param {boolean} recursive - Whether to get files from sub-directories.
* @returns {Array<string>} Array of paths of the found '.<filetype>' files (sorted).
*/
function getFilesFromDir(dirpath, filetype = null, recursive = false) {
export function getFilesFromDir(dirpath, filetype = null, recursive = false) {
// Make sure path is absolute
dirpath = path.isAbsolute(dirpath) ? dirpath : path.join(__dirname, dirpath);

Expand Down Expand Up @@ -50,7 +50,3 @@ function getFilesFromDir(dirpath, filetype = null, recursive = false) {

return files;
}

module.exports = {
getFilesFromDir,
};
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "flex_conf",
"version": "1.0.7",
"version": "2.0.0",
"description": "Flexible, tag-based configuration file management.",
"main": "index.js",
"scripts": {
Expand All @@ -19,14 +19,15 @@
"homepage": "https://bitbucket.org/derbenoo/flex_conf#readme",
"dependencies": {
"debug": "^4.1.0",
"nconf": "^0.10.0"
"nconf": "~0.10.0"
},
"devDependencies": {
"@types/node": "^10.12.0",
"@types/node": "^10.12.18",
"eslint": "^5.7.0",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-jsdoc": "^3.8.0",
"jsdoc-to-markdown": "^4.0.1"
"jsdoc-to-markdown": "^4.0.1",
"typescript": "^3.2.2"
}
}
27 changes: 27 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"compileOnSave": false,
"compilerOptions": {
"module": "commonjs",
"target": "es2017",
"alwaysStrict": true,
"outDir": "./dist",
"baseUrl": "./lib",
"sourceMap": false,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": true,
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2017"
],
"noImplicitReturns": true,
"noUnusedLocals": true,
"noUnusedParameters": false,
"paths": {
}
}
}

0 comments on commit eeed823

Please sign in to comment.