Skip to content

Commit

Permalink
[Fix] tsconfig build module alias (#1)
Browse files Browse the repository at this point in the history
* Added tscpath to resolve typescript path alias for build

* Fixed JSON schema import
  • Loading branch information
MuhammedIrfan authored Jan 3, 2021
1 parent 05159fa commit b034684
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 31 deletions.
11 changes: 9 additions & 2 deletions package-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = {
* Starts the builded app from the dist directory
*/
start: {
script: 'node bin/app.js',
script: 'node bin/index.js',
description: 'Starts the builded app from the dist directory'
},
/**
Expand Down Expand Up @@ -40,7 +40,8 @@ module.exports = {
'nps banner.build',
'nps lint',
'nps clean.bin',
'nps transpile'
'nps transpile',
'nps transformPath'
),
description: 'Builds the app into the bin directory'
},
Expand All @@ -58,6 +59,12 @@ module.exports = {
script: `tsc`,
hiddenFromHelp: true
},
/**
* Transfrom typescript path alias
*/
transformPath : {
script: `tscpaths -p tsconfig.json -s ./src -o ./bin`
},
/**
* Clean files and folders
*/
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"nps-utils": "^1.7.0",
"ts-node": "^9.1.1",
"tsconfig-paths": "^3.9.0",
"tscpaths": "^0.0.9",
"tslib": "^2.0.3",
"tslint": "^6.1.3",
"typescript": "^4.1.3"
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'reflect-metadata';
import { bootstrapMicroframework } from 'microbootstrap';
import { LoggerModule, ConfigurationModule } from '@modules';
import { LoggerModule, ConfigurationModule } from '@modules/index';
import { log } from 'winston';
import { ErrorHandler } from '@middlewares';
import { ErrorHandler } from '@middlewares/index';

bootstrapMicroframework({
config: {
Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/error-handler.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Exception, ConfigurationException } from '@exceptions';
import { Exception, ConfigurationException } from '@exceptions/index';
import { createLogger, transports, format } from "winston";

/**
Expand Down
23 changes: 16 additions & 7 deletions src/modules/configuration.module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { MicroframeworkSettings, MicroframeworkLoader } from 'microbootstrap';
import { util } from 'config';
import { buildYup } from 'schema-to-yup';
import * as jsonSchema from '../../config/schema.json';
import { ConfigurationException } from "@exceptions";
import { readFileSync } from 'fs';
import { join } from 'path';
import { ConfigurationException } from "@exceptions/index";

/**
* Configuration Module validates the config passed through environment variables with the schema provided in /config/schema.json.
Expand All @@ -12,11 +13,19 @@ import { ConfigurationException } from "@exceptions";

export const ConfigurationModule: MicroframeworkLoader = (frameworkSettings: MicroframeworkSettings | undefined) => {
if (frameworkSettings) {
// Build Yup schema from the provided json schema.
const yupSchema = buildYup(jsonSchema, {});
try {
// Read schema.json file from config directory.
const schema = JSON.parse(readFileSync(join('config', 'schema.json'), { encoding: 'utf8' }));

// Using synchronous validation as we need to be sure that configuration are valid before loading other modules.
try { yupSchema.validateSync(util.loadFileConfigs()) }
catch (error) { throw new ConfigurationException(error.message); }
// Build Yup schema from the provided json schema.
const yupSchema = buildYup(schema, {});

// Using synchronous validation as we need to be sure that configuration are valid before loading other modules.
yupSchema.validateSync(util.loadFileConfigs());
}
catch (error) {
if(typeof error === 'string') throw new ConfigurationException(error);
throw new ConfigurationException(error.message);
}
}
}
33 changes: 19 additions & 14 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"module": "commonjs",
"pretty": true,
"sourceMap": true,
"outDir": "bin",
"outDir": "./bin",
"importHelpers": false,
"strict": true,
"noImplicitAny": false,
Expand All @@ -16,25 +16,27 @@
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"moduleResolution": "node",
"baseUrl": ".",
"baseUrl": "./src",
"resolveJsonModule": true,
"esModuleInterop": true,
"paths": {
"*": ["types/*"],
"@modules": [
"./src/modules/index.ts"
"*": [
"types/*"
],
"@services": [
"./src/services/index.ts"
"@modules/*": [
"./modules/*"
],
"@controllers": [
"./src/controllers/index.ts"
"@services/*": [
"./services/*"
],
"@middlewares": [
"./src/middlewares/index.ts"
"@controllers/*": [
"./controllers/*"
],
"@exceptions": [
"./src/exceptions/index.ts"
"@middlewares/*": [
"./middlewares/*"
],
"@exceptions/*": [
"./exceptions/*"
]
},
"allowSyntheticDefaultImports": true,
Expand All @@ -43,5 +45,8 @@
},
"include": [
"src/**/*"
],
"exclude": [
"../config"
]
}
}
Loading

0 comments on commit b034684

Please sign in to comment.