@spinajs/reflection 1.0.8 → 1.2.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.d.ts +9 -3
- package/lib/index.js +28 -23
- package/lib/index.js.map +1 -1
- package/package.json +12 -30
package/lib/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import * as ts from 'typescript';
|
|
2
|
+
import { Exception } from '@spinajs/exceptions';
|
|
2
3
|
/**
|
|
3
4
|
* Class info structure
|
|
4
5
|
*/
|
|
@@ -20,6 +21,11 @@ export declare class ClassInfo<T> {
|
|
|
20
21
|
*/
|
|
21
22
|
instance?: T;
|
|
22
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Exception thrown when some error during reflection occurs
|
|
26
|
+
*/
|
|
27
|
+
export declare class ReflectionException extends Exception {
|
|
28
|
+
}
|
|
23
29
|
/**
|
|
24
30
|
* Helper class for extracting various information from typescript source code
|
|
25
31
|
*/
|
|
@@ -31,7 +37,7 @@ export declare class TypescriptCompiler {
|
|
|
31
37
|
*
|
|
32
38
|
* Extracts all members info from typescript class eg. method name, parameters, return types etc.
|
|
33
39
|
*
|
|
34
|
-
* @param className name of class to parse
|
|
40
|
+
* @param className - name of class to parse
|
|
35
41
|
*/
|
|
36
42
|
getClassMembers(className: string): Map<string, ts.MethodDeclaration>;
|
|
37
43
|
private walkClassNode;
|
|
@@ -46,7 +52,7 @@ export declare class TypescriptCompiler {
|
|
|
46
52
|
* @param configPath - dir paths taken from app config eg. "system.dirs.controllers". Path MUST be avaible in configuration
|
|
47
53
|
*
|
|
48
54
|
*/
|
|
49
|
-
export declare function ResolveFromFiles(filter: string, configPath: string): (target: any, propertyKey: string | symbol) => void;
|
|
55
|
+
export declare function ResolveFromFiles(filter: string, configPath: string, typeMatcher?: (fileName: string) => string): (target: any, propertyKey: string | symbol) => void;
|
|
50
56
|
/**
|
|
51
57
|
* Returns list of class types found in specified path. It do not resolve / create instances
|
|
52
58
|
*
|
|
@@ -54,4 +60,4 @@ export declare function ResolveFromFiles(filter: string, configPath: string): (t
|
|
|
54
60
|
* @param configPath - dir paths taken from app config eg. "system.dirs.controllers". Path MUST be avaible in configuration
|
|
55
61
|
*
|
|
56
62
|
*/
|
|
57
|
-
export declare function ListFromFiles(filter: string, configPath: string): (target: any, propertyKey: string | symbol) => void;
|
|
63
|
+
export declare function ListFromFiles(filter: string, configPath: string, typeMatcher?: (fileName: string) => string): (target: any, propertyKey: string | symbol) => void;
|
package/lib/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ListFromFiles = exports.ResolveFromFiles = exports.TypescriptCompiler = exports.ClassInfo = void 0;
|
|
3
|
+
exports.ListFromFiles = exports.ResolveFromFiles = exports.TypescriptCompiler = exports.ReflectionException = exports.ClassInfo = void 0;
|
|
4
4
|
const fs = require("fs");
|
|
5
5
|
const glob = require("glob");
|
|
6
6
|
const path = require("path");
|
|
@@ -15,6 +15,12 @@ const log_1 = require("@spinajs/log");
|
|
|
15
15
|
class ClassInfo {
|
|
16
16
|
}
|
|
17
17
|
exports.ClassInfo = ClassInfo;
|
|
18
|
+
/**
|
|
19
|
+
* Exception thrown when some error during reflection occurs
|
|
20
|
+
*/
|
|
21
|
+
class ReflectionException extends exceptions_1.Exception {
|
|
22
|
+
}
|
|
23
|
+
exports.ReflectionException = ReflectionException;
|
|
18
24
|
/**
|
|
19
25
|
* Helper class for extracting various information from typescript source code
|
|
20
26
|
*/
|
|
@@ -30,18 +36,15 @@ class TypescriptCompiler {
|
|
|
30
36
|
*
|
|
31
37
|
* Extracts all members info from typescript class eg. method name, parameters, return types etc.
|
|
32
38
|
*
|
|
33
|
-
* @param className name of class to parse
|
|
39
|
+
* @param className - name of class to parse
|
|
34
40
|
*/
|
|
35
41
|
getClassMembers(className) {
|
|
36
42
|
const members = new Map();
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
})));
|
|
43
|
-
}
|
|
44
|
-
}
|
|
43
|
+
const sourceFile = this.compiled.getSourceFile(this.tsFile);
|
|
44
|
+
// Walk the tree to search for classes
|
|
45
|
+
ts.forEachChild(sourceFile, this.walkClassNode(className, this.walkMemberNode((method) => {
|
|
46
|
+
members.set(method.name.getText(), method);
|
|
47
|
+
})));
|
|
45
48
|
return members;
|
|
46
49
|
}
|
|
47
50
|
walkClassNode(className, callback) {
|
|
@@ -75,8 +78,8 @@ exports.TypescriptCompiler = TypescriptCompiler;
|
|
|
75
78
|
* @param configPath - dir paths taken from app config eg. "system.dirs.controllers". Path MUST be avaible in configuration
|
|
76
79
|
*
|
|
77
80
|
*/
|
|
78
|
-
function ResolveFromFiles(filter, configPath) {
|
|
79
|
-
return _listOrResolveFromFiles(filter, configPath, true);
|
|
81
|
+
function ResolveFromFiles(filter, configPath, typeMatcher) {
|
|
82
|
+
return _listOrResolveFromFiles(filter, configPath, true, typeMatcher);
|
|
80
83
|
}
|
|
81
84
|
exports.ResolveFromFiles = ResolveFromFiles;
|
|
82
85
|
/**
|
|
@@ -86,11 +89,11 @@ exports.ResolveFromFiles = ResolveFromFiles;
|
|
|
86
89
|
* @param configPath - dir paths taken from app config eg. "system.dirs.controllers". Path MUST be avaible in configuration
|
|
87
90
|
*
|
|
88
91
|
*/
|
|
89
|
-
function ListFromFiles(filter, configPath) {
|
|
90
|
-
return _listOrResolveFromFiles(filter, configPath, false);
|
|
92
|
+
function ListFromFiles(filter, configPath, typeMatcher) {
|
|
93
|
+
return _listOrResolveFromFiles(filter, configPath, false, typeMatcher);
|
|
91
94
|
}
|
|
92
95
|
exports.ListFromFiles = ListFromFiles;
|
|
93
|
-
function _listOrResolveFromFiles(filter, configPath, resolve) {
|
|
96
|
+
function _listOrResolveFromFiles(filter, configPath, resolve, typeMatcher) {
|
|
94
97
|
return (target, propertyKey) => {
|
|
95
98
|
if (!filter) {
|
|
96
99
|
throw new exceptions_1.InvalidArgument('filter parameter is null or empty');
|
|
@@ -110,8 +113,8 @@ function _listOrResolveFromFiles(filter, configPath, resolve) {
|
|
|
110
113
|
get: getter,
|
|
111
114
|
});
|
|
112
115
|
function _loadInstances() {
|
|
113
|
-
const config = di_1.DI.
|
|
114
|
-
const logger = di_1.DI.resolve(log_1.
|
|
116
|
+
const config = di_1.DI.get(configuration_1.Configuration);
|
|
117
|
+
const logger = di_1.DI.resolve(log_1.Log, ['reflection']);
|
|
115
118
|
let directories = config.get(configPath);
|
|
116
119
|
if (!directories || directories.length === 0) {
|
|
117
120
|
return [];
|
|
@@ -121,9 +124,9 @@ function _listOrResolveFromFiles(filter, configPath, resolve) {
|
|
|
121
124
|
}
|
|
122
125
|
let promised = false;
|
|
123
126
|
const result = directories
|
|
124
|
-
.map((d) => path.normalize(d))
|
|
125
127
|
.filter((d) => {
|
|
126
|
-
|
|
128
|
+
/* eslint-disable */
|
|
129
|
+
const exists = fs.existsSync(path.normalize(d));
|
|
127
130
|
if (!exists) {
|
|
128
131
|
logger.warn(`Directory ${d} not exists`);
|
|
129
132
|
}
|
|
@@ -133,9 +136,11 @@ function _listOrResolveFromFiles(filter, configPath, resolve) {
|
|
|
133
136
|
.map((f) => {
|
|
134
137
|
logger.trace(`Loading file ${f}`);
|
|
135
138
|
const name = path.parse(f).name;
|
|
136
|
-
const
|
|
139
|
+
const nameToResolve = typeMatcher ? typeMatcher(name) : name;
|
|
140
|
+
/* eslint-disable */
|
|
141
|
+
const type = require(f)[`${nameToResolve}`];
|
|
137
142
|
if (!type) {
|
|
138
|
-
throw new
|
|
143
|
+
throw new ReflectionException(`cannot find class ${nameToResolve} in file ${f}`);
|
|
139
144
|
}
|
|
140
145
|
if (resolve) {
|
|
141
146
|
if (type.prototype instanceof di_1.AsyncModule) {
|
|
@@ -144,7 +149,7 @@ function _listOrResolveFromFiles(filter, configPath, resolve) {
|
|
|
144
149
|
return {
|
|
145
150
|
file: f,
|
|
146
151
|
instance,
|
|
147
|
-
name,
|
|
152
|
+
name: nameToResolve,
|
|
148
153
|
type,
|
|
149
154
|
};
|
|
150
155
|
});
|
|
@@ -153,7 +158,7 @@ function _listOrResolveFromFiles(filter, configPath, resolve) {
|
|
|
153
158
|
return {
|
|
154
159
|
file: f,
|
|
155
160
|
instance: resolve ? di_1.DI.resolve(type) : null,
|
|
156
|
-
name,
|
|
161
|
+
name: nameToResolve,
|
|
157
162
|
type,
|
|
158
163
|
};
|
|
159
164
|
});
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,yBAAyB;AACzB,6BAA6B;AAC7B,6BAA6B;AAC7B,iCAAiC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,yBAAyB;AACzB,6BAA6B;AAC7B,6BAA6B;AAC7B,iCAAiC;AACjC,0DAAuD;AACvD,oCAAqD;AACrD,oDAAiE;AACjE,sCAAmC;AAEnC;;GAEG;AACH,MAAa,SAAS;CAkBrB;AAlBD,8BAkBC;AAED;;GAEG;AACH,MAAa,mBAAoB,SAAQ,sBAAS;CAAG;AAArD,kDAAqD;AAErD;;GAEG;AACH,MAAa,kBAAkB;IAK7B,YAAY,QAAgB;QAC1B,IAAI,CAAC,MAAM,GAAG,QAAQ,CAAC;QAEvB,IAAI,CAAC,QAAQ,GAAG,EAAE,CAAC,aAAa,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE;YAC9C,MAAM,EAAE,EAAE,CAAC,UAAU,CAAC,QAAQ;YAC9B,MAAM,EAAE,EAAE,CAAC,YAAY,CAAC,MAAM;SAC/B,CAAC,CAAC;IACL,CAAC;IAED;;;;;OAKG;IACI,eAAe,CAAC,SAAiB;QACtC,MAAM,OAAO,GAAsC,IAAI,GAAG,EAAgC,CAAC;QAE3F,MAAM,U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CAAC;gCACP,QAAQ;gCACR,IAAI,EAAE,aAAa;gCACnB,IAAI;6BACL,CAAC;wBACJ,CAAC,CAAC,CAAC;qBACJ;iBACF;gBAED,OAAO;oBACL,IAAI,EAAE,CAAC;oBACP,QAAQ,EAAE,OAAO,CAAC,CAAC,CAAC,OAAE,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI;oBAC3C,IAAI,EAAE,aAAa;oBACnB,IAAI;iBACL,CAAC;YACJ,CAAC,CAAC,CAAC;YAEL,OAAO,QAAQ,IAAI,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;QAC5D,CAAC;IACH,CAAC,CAAC;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@spinajs/reflection",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.7",
|
|
4
4
|
"description": "SpinaJS reflection helpers",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"private": false,
|
|
@@ -8,10 +8,12 @@
|
|
|
8
8
|
"test": "ts-mocha -p tsconfig.json test/**/*.test.ts",
|
|
9
9
|
"coverage": "nyc npm run test",
|
|
10
10
|
"build-docs": "rimraf docs && typedoc --options typedoc.json src/",
|
|
11
|
-
"build": "
|
|
11
|
+
"build": "npm run clean && npm run compile",
|
|
12
|
+
"compile": "tsc -p tsconfig.build.json",
|
|
13
|
+
"clean": "",
|
|
12
14
|
"prepare": "npm run build",
|
|
13
|
-
"format": "prettier --write \"src/**/*.ts\"
|
|
14
|
-
"lint": "
|
|
15
|
+
"format": "prettier --write \"src/**/*.ts\"",
|
|
16
|
+
"lint": "eslint -c .eslintrc.js --ext .ts src",
|
|
15
17
|
"prepublishOnly": "npm test && npm run lint",
|
|
16
18
|
"preversion": "npm run lint",
|
|
17
19
|
"version": "npm run format && git add -A src",
|
|
@@ -27,31 +29,11 @@
|
|
|
27
29
|
},
|
|
28
30
|
"homepage": "https://github.com/spinajs/reflection#readme",
|
|
29
31
|
"dependencies": {
|
|
30
|
-
"@spinajs/configuration": "^1.
|
|
31
|
-
"@spinajs/di": "^1.
|
|
32
|
-
"@spinajs/exceptions": "^1.
|
|
33
|
-
"@spinajs/log": "^1.
|
|
32
|
+
"@spinajs/configuration": "^1.2.7",
|
|
33
|
+
"@spinajs/di": "^1.2.7",
|
|
34
|
+
"@spinajs/exceptions": "^1.2.7",
|
|
35
|
+
"@spinajs/log": "^1.2.7",
|
|
36
|
+
"lodash": "^4.17.21"
|
|
34
37
|
},
|
|
35
|
-
"
|
|
36
|
-
"@types/chai": "^4.1.7",
|
|
37
|
-
"@types/chai-as-promised": "^7.1.0",
|
|
38
|
-
"@types/chai-subset": "^1.3.3",
|
|
39
|
-
"@types/lodash": "^4.14.136",
|
|
40
|
-
"@types/mocha": "^5.2.7",
|
|
41
|
-
"chai": "^4.2.0",
|
|
42
|
-
"chai-as-promised": "^7.1.1",
|
|
43
|
-
"chai-subset": "^1.6.0",
|
|
44
|
-
"mocha": "^6.1.4",
|
|
45
|
-
"nyc": "^14.1.1",
|
|
46
|
-
"prettier": "^1.18.2",
|
|
47
|
-
"ts-mocha": "^6.0.0",
|
|
48
|
-
"ts-node": "^8.3.0",
|
|
49
|
-
"tslint": "^5.20.1",
|
|
50
|
-
"tslint-circular-dependencies": "^0.1.0",
|
|
51
|
-
"tslint-config-prettier": "^1.18.0",
|
|
52
|
-
"tslint-config-standard": "^8.0.1",
|
|
53
|
-
"tslint-no-unused-expression-chai": "^0.1.4",
|
|
54
|
-
"typedoc": "^0.14.2",
|
|
55
|
-
"typescript": "^3.5.3"
|
|
56
|
-
}
|
|
38
|
+
"gitHead": "389cf142b4ca63b7850b93fc858184ddcbb43ce1"
|
|
57
39
|
}
|