init
This commit is contained in:
commit
1c98dd76f5
24
.eslintrc.json
Normal file
24
.eslintrc.json
Normal file
@ -0,0 +1,24 @@
|
||||
{
|
||||
"parser": "@typescript-eslint/parser",
|
||||
"plugins": [
|
||||
"@typescript-eslint"
|
||||
],
|
||||
"rules": {
|
||||
|
||||
"@typescript-eslint/indent": ["error", 2, {
|
||||
"ignoredNodes": [
|
||||
"FunctionExpression > .params[decorators.length > 0]",
|
||||
"FunctionExpression > .params > :matches(Decorator, :not(:first-child))",
|
||||
"ClassBody.body > PropertyDefinition[decorators.length > 0] > .key"
|
||||
]
|
||||
}],
|
||||
|
||||
"brace-style": ["error", "stroustrup", { "allowSingleLine": true }],
|
||||
|
||||
"semi": ["error", "never"],
|
||||
"@typescript-eslint/semi": ["off"],
|
||||
|
||||
"no-unexpected-multiline": "error",
|
||||
"eol-last": ["error", "always"]
|
||||
}
|
||||
}
|
||||
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
node_modules/
|
||||
build/
|
||||
5
.vscode/settings.json
vendored
Normal file
5
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"editor.codeActionsOnSave": {
|
||||
"source.fixAll.eslint": true
|
||||
}
|
||||
}
|
||||
15
LICENSE
Normal file
15
LICENSE
Normal file
@ -0,0 +1,15 @@
|
||||
ISC License
|
||||
|
||||
Copyright (c) 2022, MAKS-IT (Maksym Sadovnychyy)
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
27
README.md
Normal file
27
README.md
Normal file
@ -0,0 +1,27 @@
|
||||
This project template has purpose to provite initial boilerplate to design node based microservices.
|
||||
|
||||
Main features:
|
||||
* Typescript
|
||||
* Dependency Injection,
|
||||
* Automatic linting
|
||||
* Jest powered unit tests
|
||||
|
||||
|
||||
## Available Scripts
|
||||
|
||||
### `npm start`
|
||||
|
||||
Runs the app in the development mode.<br />
|
||||
|
||||
App will reload if you make edits.<br />
|
||||
You will also see any lint errors in the console.
|
||||
|
||||
### `npm test`
|
||||
|
||||
### `npm build`
|
||||
|
||||
Builds the app for production to the `build` folder.<br />
|
||||
It correctly bundles React in production mode and optimizes the build for the best performance.
|
||||
|
||||
The build is minified and the filenames include the hashes.<br />
|
||||
Your app is ready to be deployed!
|
||||
4
__tests__/appsettings.json
Normal file
4
__tests__/appsettings.json
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"connectionStrings": [],
|
||||
"interval": 5
|
||||
}
|
||||
34
__tests__/test.ts
Normal file
34
__tests__/test.ts
Normal file
@ -0,0 +1,34 @@
|
||||
import "reflect-metadata"
|
||||
import { container } from "tsyringe"
|
||||
|
||||
import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
|
||||
import { InterfaceName as ConfigurationName, IConfiguration } from '../src/interfaces/IConfiguration'
|
||||
import { InterfaceName as TestServiceName, TestService } from '../src/services/testService'
|
||||
|
||||
class Program {
|
||||
constructor() {
|
||||
this.RegisterServices()
|
||||
}
|
||||
|
||||
private RegisterServices = () => {
|
||||
const nodeEnv: string | undefined = process.env.NODE_ENV
|
||||
const rawConfig: Buffer = fs.readFileSync(path.join(__dirname, `appsettings${nodeEnv === 'PROD' ? '.production': ''}.json`))
|
||||
const configuration: IConfiguration = JSON.parse(rawConfig.toString())
|
||||
|
||||
container.register(ConfigurationName, { useValue: configuration })
|
||||
container.register(TestServiceName, { useClass: TestService })
|
||||
}
|
||||
|
||||
public Main = () => {
|
||||
const testService = container.resolve(TestService)
|
||||
|
||||
test('adds 1 + 2 to equal 3', () => {
|
||||
expect(testService.Sum(1, 2)).toBe(3)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const program = new Program
|
||||
program.Main()
|
||||
10
babel.config.js
Normal file
10
babel.config.js
Normal file
@ -0,0 +1,10 @@
|
||||
module.exports = {
|
||||
plugins: [
|
||||
'babel-plugin-transform-typescript-metadata',
|
||||
["@babel/plugin-proposal-decorators", { "legacy": true }]
|
||||
],
|
||||
presets: [
|
||||
['@babel/preset-env', {targets: {node: 'current'}}],
|
||||
'@babel/preset-typescript',
|
||||
]
|
||||
}
|
||||
5
nodemon.json
Normal file
5
nodemon.json
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"execMap": {
|
||||
"ts": "ts-node"
|
||||
}
|
||||
}
|
||||
32
package.json
Normal file
32
package.json
Normal file
@ -0,0 +1,32 @@
|
||||
{
|
||||
"name": "consoleapp-node",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"devDependencies": {
|
||||
"@babel/cli": "^7.17.6",
|
||||
"@babel/core": "^7.17.5",
|
||||
"@babel/plugin-proposal-decorators": "^7.17.2",
|
||||
"@babel/preset-env": "^7.16.11",
|
||||
"@babel/preset-typescript": "^7.16.7",
|
||||
"@types/jest": "^27.4.1",
|
||||
"@types/node": "^17.0.19",
|
||||
"@typescript-eslint/eslint-plugin": "^5.12.1",
|
||||
"@typescript-eslint/parser": "^5.12.1",
|
||||
"babel-jest": "^27.5.1",
|
||||
"babel-plugin-transform-typescript-metadata": "^0.3.2",
|
||||
"eslint": "^8.9.0",
|
||||
"jest": "^27.5.1",
|
||||
"nodemon": "^2.0.15",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"ts-node": "^10.5.0",
|
||||
"tsyringe": "^4.6.0",
|
||||
"typescript": "^4.5.5"
|
||||
},
|
||||
"dependencies": {},
|
||||
"scripts": {
|
||||
"start": "nodemon src/main.ts",
|
||||
"eslint": "eslint src/main.ts",
|
||||
"test": "jest",
|
||||
"build": "npx tsc"
|
||||
}
|
||||
}
|
||||
4
src/appsettings.json
Normal file
4
src/appsettings.json
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"connectionStrings": [],
|
||||
"interval": 5
|
||||
}
|
||||
4
src/appsettings.production.json
Normal file
4
src/appsettings.production.json
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"connectionStrings": [],
|
||||
"interval": 5
|
||||
}
|
||||
30
src/client.ts
Normal file
30
src/client.ts
Normal file
@ -0,0 +1,30 @@
|
||||
import {injectable, inject} from "tsyringe"
|
||||
|
||||
import { IArg } from './interfaces'
|
||||
|
||||
import { InterfaceName as TestServiceName, ITestService } from './services/testService'
|
||||
|
||||
@injectable()
|
||||
class Client {
|
||||
|
||||
private readonly _testService: ITestService
|
||||
|
||||
/** Interfaces don't have type information at runtime,
|
||||
* so we need to decorate them with @inject(...)
|
||||
* so the container knows how to resolve them. */
|
||||
constructor(
|
||||
@inject(TestServiceName)
|
||||
testService: ITestService
|
||||
) {
|
||||
this._testService = testService
|
||||
}
|
||||
|
||||
public Run = (args: IArg []) => {
|
||||
console.log('Server command line argumnets', args)
|
||||
this._testService.TestMethod()
|
||||
}
|
||||
}
|
||||
|
||||
export {
|
||||
Client
|
||||
}
|
||||
11
src/interfaces/IArgs.ts
Normal file
11
src/interfaces/IArgs.ts
Normal file
@ -0,0 +1,11 @@
|
||||
interface IArg {
|
||||
[key: string]: string | number
|
||||
|
||||
index: number,
|
||||
name: string,
|
||||
value: string | number
|
||||
}
|
||||
|
||||
export {
|
||||
IArg
|
||||
}
|
||||
18
src/interfaces/IConfiguration.ts
Normal file
18
src/interfaces/IConfiguration.ts
Normal file
@ -0,0 +1,18 @@
|
||||
const InterfaceName = 'IConfituration'
|
||||
interface IConnectionString {
|
||||
name: string,
|
||||
connectionString: string
|
||||
}
|
||||
|
||||
interface IConfiguration {
|
||||
[key: string]: string | number | IConnectionString []
|
||||
|
||||
connectionStrings: IConnectionString [],
|
||||
interval: number
|
||||
}
|
||||
|
||||
export {
|
||||
InterfaceName,
|
||||
IConnectionString,
|
||||
IConfiguration
|
||||
}
|
||||
5
src/interfaces/index.ts
Normal file
5
src/interfaces/index.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import { IArg } from './IArgs'
|
||||
|
||||
export {
|
||||
IArg
|
||||
}
|
||||
50
src/main.ts
Normal file
50
src/main.ts
Normal file
@ -0,0 +1,50 @@
|
||||
import "reflect-metadata"
|
||||
import { container } from "tsyringe"
|
||||
|
||||
import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
|
||||
import { Client } from "./client"
|
||||
|
||||
import { IArg } from './interfaces'
|
||||
|
||||
import { InterfaceName as ConfigurationName, IConfiguration } from './interfaces/IConfiguration'
|
||||
import { InterfaceName as TestServiceName, TestService } from './services/testService'
|
||||
|
||||
class Program {
|
||||
private readonly _args: IArg []
|
||||
|
||||
constructor() {
|
||||
this._args = process.argv.filter(val => val.includes('--'))
|
||||
.map((val, index) => {
|
||||
const [ name, value ] = val.split('=')
|
||||
return {
|
||||
index,
|
||||
name: name.replace(/^\-+/g,''),
|
||||
value
|
||||
}
|
||||
})
|
||||
|
||||
this.RegisterServices()
|
||||
}
|
||||
|
||||
private RegisterServices = () => {
|
||||
const nodeEnv: string | undefined = process.env.NODE_ENV
|
||||
const rawConfig: Buffer = fs.readFileSync(path.join(__dirname, `appsettings${nodeEnv === 'PROD' ? '.production': ''}.json`))
|
||||
const configuration: IConfiguration = JSON.parse(rawConfig.toString())
|
||||
|
||||
//#region Services registration
|
||||
container.register(ConfigurationName, { useValue: configuration })
|
||||
container.register(TestServiceName, { useClass: TestService })
|
||||
//#endregion
|
||||
}
|
||||
|
||||
public Main = () => {
|
||||
const client = container.resolve(Client)
|
||||
client.Run(this._args)
|
||||
}
|
||||
}
|
||||
|
||||
const program = new Program()
|
||||
program.Main()
|
||||
|
||||
33
src/services/testService.ts
Normal file
33
src/services/testService.ts
Normal file
@ -0,0 +1,33 @@
|
||||
import { inject, injectable } from "tsyringe"
|
||||
|
||||
import { InterfaceName as ConfigurationName, IConfiguration } from "../interfaces/IConfiguration"
|
||||
|
||||
const InterfaceName = 'ITestService'
|
||||
interface ITestService {
|
||||
TestMethod: () => void
|
||||
Sum: (a: number, b: number) => number
|
||||
}
|
||||
|
||||
@injectable()
|
||||
class TestService implements ITestService {
|
||||
private readonly _serviceConfig: IConfiguration
|
||||
|
||||
constructor(
|
||||
@inject(ConfigurationName)
|
||||
serviceConfig: IConfiguration,
|
||||
) {
|
||||
this._serviceConfig = serviceConfig
|
||||
}
|
||||
|
||||
public TestMethod = () => {
|
||||
console.log('Test service works!', this._serviceConfig)
|
||||
}
|
||||
|
||||
public Sum = (a: number, b: number) => a + b
|
||||
}
|
||||
|
||||
export {
|
||||
InterfaceName,
|
||||
TestService,
|
||||
ITestService
|
||||
}
|
||||
109
tsconfig.json
Normal file
109
tsconfig.json
Normal file
@ -0,0 +1,109 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
/* Visit https://aka.ms/tsconfig.json to read more about this file */
|
||||
|
||||
/* Projects */
|
||||
// "incremental": true, /* Enable incremental compilation */
|
||||
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
|
||||
// "tsBuildInfoFile": "./", /* Specify the folder for .tsbuildinfo incremental compilation files. */
|
||||
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects */
|
||||
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
|
||||
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
|
||||
|
||||
/* Language and Environment */
|
||||
"target": "es2020", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
|
||||
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
|
||||
// "jsx": "preserve", /* Specify what JSX code is generated. */
|
||||
"experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
|
||||
"emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
|
||||
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h' */
|
||||
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
|
||||
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using `jsx: react-jsx*`.` */
|
||||
// "reactNamespace": "", /* Specify the object invoked for `createElement`. This only applies when targeting `react` JSX emit. */
|
||||
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
|
||||
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
|
||||
|
||||
/* Modules */
|
||||
"module": "commonjs", /* Specify what module code is generated. */
|
||||
// "rootDir": "./", /* Specify the root folder within your source files. */
|
||||
// "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
|
||||
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
|
||||
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
|
||||
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
|
||||
// "typeRoots": [], /* Specify multiple folders that act like `./node_modules/@types`. */
|
||||
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
|
||||
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
||||
"resolveJsonModule": true, /* Enable importing .json files */
|
||||
// "noResolve": true, /* Disallow `import`s, `require`s or `<reference>`s from expanding the number of files TypeScript should add to a project. */
|
||||
|
||||
/* JavaScript Support */
|
||||
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */
|
||||
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
|
||||
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from `node_modules`. Only applicable with `allowJs`. */
|
||||
|
||||
/* Emit */
|
||||
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
|
||||
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
|
||||
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
||||
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
||||
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */
|
||||
"outDir": "./build", /* Specify an output folder for all emitted files. */
|
||||
// "removeComments": true, /* Disable emitting comments. */
|
||||
// "noEmit": true, /* Disable emitting files from a compilation. */
|
||||
"importHelpers": false, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
|
||||
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types */
|
||||
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
|
||||
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
|
||||
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
||||
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
|
||||
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
|
||||
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
|
||||
// "newLine": "crlf", /* Set the newline character for emitting files. */
|
||||
// "stripInternal": true, /* Disable emitting declarations that have `@internal` in their JSDoc comments. */
|
||||
// "noEmitHelpers": true, /* Disable generating custom helper functions like `__extends` in compiled output. */
|
||||
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
|
||||
// "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */
|
||||
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
|
||||
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
|
||||
|
||||
/* Interop Constraints */
|
||||
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
||||
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
||||
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */
|
||||
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
||||
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
|
||||
|
||||
/* Type Checking */
|
||||
"strict": true, /* Enable all strict type-checking options. */
|
||||
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied `any` type.. */
|
||||
// "strictNullChecks": true, /* When type checking, take into account `null` and `undefined`. */
|
||||
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
|
||||
// "strictBindCallApply": true, /* Check that the arguments for `bind`, `call`, and `apply` methods match the original function. */
|
||||
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
|
||||
// "noImplicitThis": true, /* Enable error reporting when `this` is given the type `any`. */
|
||||
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
|
||||
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
|
||||
// "noUnusedLocals": true, /* Enable error reporting when a local variables aren't read. */
|
||||
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read */
|
||||
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
|
||||
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
|
||||
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
|
||||
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
|
||||
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
|
||||
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type */
|
||||
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
|
||||
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
|
||||
|
||||
/* Completeness */
|
||||
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
|
||||
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
||||
},
|
||||
"include": [
|
||||
"./src/**/*.ts",
|
||||
"./src/appsettings.json",
|
||||
"./src/appsettings.production.json"
|
||||
],
|
||||
"exclude": [
|
||||
"node_modules"
|
||||
]
|
||||
}
|
||||
1594
yarn-error.log
Normal file
1594
yarn-error.log
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user