@spinajs/templates 2.0.40 → 2.0.45
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/di/src/array.d.ts +10 -0
- package/lib/di/src/array.js +14 -0
- package/lib/di/src/array.js.map +1 -0
- package/lib/di/src/container-cache.d.ts +17 -0
- package/lib/di/src/container-cache.js +64 -0
- package/lib/di/src/container-cache.js.map +1 -0
- package/lib/di/src/enums.d.ts +31 -0
- package/lib/di/src/enums.js +36 -0
- package/lib/di/src/enums.js.map +1 -0
- package/lib/di/src/helpers.d.ts +25 -0
- package/lib/di/src/helpers.js +69 -0
- package/lib/di/src/helpers.js.map +1 -0
- package/lib/di/src/interfaces.d.ts +150 -0
- package/lib/di/src/interfaces.js +48 -0
- package/lib/di/src/interfaces.js.map +1 -0
- package/lib/di/src/registry.d.ts +14 -0
- package/lib/di/src/registry.js +81 -0
- package/lib/di/src/registry.js.map +1 -0
- package/lib/di/src/types.d.ts +13 -0
- package/lib/di/src/types.js +3 -0
- package/lib/di/src/types.js.map +1 -0
- package/lib/templates/src/cli/render.d.ts +12 -0
- package/lib/templates/src/cli/render.js +86 -0
- package/lib/templates/src/cli/render.js.map +1 -0
- package/lib/templates/src/config/templates.d.ts +9 -0
- package/lib/templates/src/config/templates.js +16 -0
- package/lib/templates/src/config/templates.js.map +1 -0
- package/lib/templates/src/index.d.ts +13 -0
- package/lib/templates/src/index.js +67 -0
- package/lib/templates/src/index.js.map +1 -0
- package/lib/templates/src/interfaces.d.ts +21 -0
- package/lib/templates/src/interfaces.js +71 -0
- package/lib/templates/src/interfaces.js.map +1 -0
- package/package.json +8 -8
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Class } from './types';
|
|
2
|
+
declare global {
|
|
3
|
+
interface ArrayConstructor {
|
|
4
|
+
ofType<T>(type: Class<T> | string): TypedArray<T>;
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
export declare class TypedArray<R> extends Array<R> {
|
|
8
|
+
Type: Class<R> | string;
|
|
9
|
+
constructor(Type: Class<R> | string);
|
|
10
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TypedArray = void 0;
|
|
4
|
+
class TypedArray extends Array {
|
|
5
|
+
constructor(Type) {
|
|
6
|
+
super();
|
|
7
|
+
this.Type = Type;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.TypedArray = TypedArray;
|
|
11
|
+
Array.ofType = function (type) {
|
|
12
|
+
return new TypedArray(type);
|
|
13
|
+
};
|
|
14
|
+
//# sourceMappingURL=array.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"array.js","sourceRoot":"","sources":["../../../../di/src/array.ts"],"names":[],"mappings":";;;AAOA,MAAa,UAAc,SAAQ,KAAQ;IACzC,YAAmB,IAAuB;QACxC,KAAK,EAAE,CAAC;QADS,SAAI,GAAJ,IAAI,CAAmB;IAE1C,CAAC;CACF;AAJD,gCAIC;AAED,KAAK,CAAC,MAAM,GAAG,UAAa,IAAuB;IACjD,OAAO,IAAI,UAAU,CAAI,IAAI,CAAC,CAAC;AACjC,CAAC,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { TypedArray } from './array';
|
|
2
|
+
import { IContainer } from './interfaces';
|
|
3
|
+
import { Class } from './types';
|
|
4
|
+
export declare class ContainerCache {
|
|
5
|
+
private container;
|
|
6
|
+
private cache;
|
|
7
|
+
constructor(container: IContainer);
|
|
8
|
+
[Symbol.iterator](): Generator<{
|
|
9
|
+
key: string;
|
|
10
|
+
value: any;
|
|
11
|
+
}, void, unknown>;
|
|
12
|
+
remove(key: string | Class<any> | TypedArray<any>, parent?: boolean): void;
|
|
13
|
+
add(key: string | Class<any> | object, instance: any): void;
|
|
14
|
+
has(key: string | Class<any> | object | TypedArray<any>, parent?: boolean): boolean;
|
|
15
|
+
get(key: string | Class<any> | TypedArray<any>, parent?: boolean): any;
|
|
16
|
+
clear(): void;
|
|
17
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ContainerCache = void 0;
|
|
4
|
+
const helpers_1 = require("./helpers");
|
|
5
|
+
class ContainerCache {
|
|
6
|
+
constructor(container) {
|
|
7
|
+
this.container = container;
|
|
8
|
+
this.cache = new Map();
|
|
9
|
+
// add to cache container
|
|
10
|
+
// so we can inject container if needed
|
|
11
|
+
this.add(container, container);
|
|
12
|
+
}
|
|
13
|
+
*[Symbol.iterator]() {
|
|
14
|
+
for (const [key, value] of this.cache) {
|
|
15
|
+
for (const v of value) {
|
|
16
|
+
yield { key, value: v };
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
remove(key, parent) {
|
|
21
|
+
if (this.has(key)) {
|
|
22
|
+
this.cache.delete((0, helpers_1.getTypeName)(key));
|
|
23
|
+
}
|
|
24
|
+
else if (parent && this.container.Parent) {
|
|
25
|
+
this.container.Parent.uncache(key);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
add(key, instance) {
|
|
29
|
+
const tName = (0, helpers_1.getTypeName)(key);
|
|
30
|
+
if (this.has(key)) {
|
|
31
|
+
if (this.cache.get(tName).indexOf(instance) === -1) {
|
|
32
|
+
this.cache.get(tName).push(instance);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
this.cache.set(tName, [instance]);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
has(key, parent) {
|
|
40
|
+
if (this.cache.has((0, helpers_1.getTypeName)(key))) {
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
if (parent && this.container.Parent) {
|
|
44
|
+
return this.container.Parent.Cache.has(key, parent);
|
|
45
|
+
}
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
get(key, parent) {
|
|
49
|
+
const tName = (0, helpers_1.getTypeName)(key);
|
|
50
|
+
if (this.cache.has(tName)) {
|
|
51
|
+
return this.cache.get(tName);
|
|
52
|
+
}
|
|
53
|
+
if (parent && this.container.Parent) {
|
|
54
|
+
return this.container.Parent.Cache.get(key, parent);
|
|
55
|
+
}
|
|
56
|
+
return [];
|
|
57
|
+
}
|
|
58
|
+
clear() {
|
|
59
|
+
this.cache.clear();
|
|
60
|
+
this.add(this.container, this.container);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
exports.ContainerCache = ContainerCache;
|
|
64
|
+
//# sourceMappingURL=container-cache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"container-cache.js","sourceRoot":"","sources":["../../../../di/src/container-cache.ts"],"names":[],"mappings":";;;AAEA,uCAAwC;AAIxC,MAAa,cAAc;IAGzB,YAAoB,SAAqB;QAArB,cAAS,GAAT,SAAS,CAAY;QACvC,IAAI,CAAC,KAAK,GAAG,IAAI,GAAG,EAAiB,CAAC;QAEtC,yBAAyB;QACzB,uCAAuC;QACvC,IAAI,CAAC,GAAG,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC;IACjC,CAAC;IAED,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC;QAChB,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,IAAI,CAAC,KAAK,EAAE;YACrC,KAAK,MAAM,CAAC,IAAI,KAAK,EAAE;gBACrB,MAAM,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC;aACzB;SACF;IACH,CAAC;IAEM,MAAM,CAAC,GAA0C,EAAE,MAAgB;QACxE,IAAI,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE;YACjB,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,IAAA,qBAAW,EAAC,GAAG,CAAC,CAAC,CAAC;SACrC;aAAM,IAAI,MAAM,IAAI,IAAI,CAAC,SAAS,CAA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,MAAM,EAAE;YACnC,OAAO,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;SACrD;QAED,OAAO,EAAE,CAAC;IACZ,CAAC;IAEM,KAAK;QACV,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC;QACnB,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,SAAS,CAAC,CAAC;IAC3C,CAAC;CACF;AArED,wCAqEC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How to resolve class
|
|
3
|
+
*/
|
|
4
|
+
export declare enum ResolveType {
|
|
5
|
+
/**
|
|
6
|
+
* Application wise single instance. Default behaviour
|
|
7
|
+
*/
|
|
8
|
+
Singleton = 0,
|
|
9
|
+
/**
|
|
10
|
+
* New instance every time is requested
|
|
11
|
+
*/
|
|
12
|
+
NewInstance = 1,
|
|
13
|
+
/**
|
|
14
|
+
* New instance per child DI container.
|
|
15
|
+
*/
|
|
16
|
+
PerChildContainer = 2,
|
|
17
|
+
/**
|
|
18
|
+
* Singleton per target type
|
|
19
|
+
*
|
|
20
|
+
* eg. if we have multiple registered types at one base type
|
|
21
|
+
* we can resolve any of it once
|
|
22
|
+
*
|
|
23
|
+
* In comparison, Singleton flag means that only one instance can be resolved
|
|
24
|
+
* for base class
|
|
25
|
+
*/
|
|
26
|
+
PerInstance = 3,
|
|
27
|
+
/**
|
|
28
|
+
* Only one instance with given name can exists of the same service
|
|
29
|
+
*/
|
|
30
|
+
PerInstanceCheck = 4
|
|
31
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ResolveType = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* How to resolve class
|
|
6
|
+
*/
|
|
7
|
+
var ResolveType;
|
|
8
|
+
(function (ResolveType) {
|
|
9
|
+
/**
|
|
10
|
+
* Application wise single instance. Default behaviour
|
|
11
|
+
*/
|
|
12
|
+
ResolveType[ResolveType["Singleton"] = 0] = "Singleton";
|
|
13
|
+
/**
|
|
14
|
+
* New instance every time is requested
|
|
15
|
+
*/
|
|
16
|
+
ResolveType[ResolveType["NewInstance"] = 1] = "NewInstance";
|
|
17
|
+
/**
|
|
18
|
+
* New instance per child DI container.
|
|
19
|
+
*/
|
|
20
|
+
ResolveType[ResolveType["PerChildContainer"] = 2] = "PerChildContainer";
|
|
21
|
+
/**
|
|
22
|
+
* Singleton per target type
|
|
23
|
+
*
|
|
24
|
+
* eg. if we have multiple registered types at one base type
|
|
25
|
+
* we can resolve any of it once
|
|
26
|
+
*
|
|
27
|
+
* In comparison, Singleton flag means that only one instance can be resolved
|
|
28
|
+
* for base class
|
|
29
|
+
*/
|
|
30
|
+
ResolveType[ResolveType["PerInstance"] = 3] = "PerInstance";
|
|
31
|
+
/**
|
|
32
|
+
* Only one instance with given name can exists of the same service
|
|
33
|
+
*/
|
|
34
|
+
ResolveType[ResolveType["PerInstanceCheck"] = 4] = "PerInstanceCheck";
|
|
35
|
+
})(ResolveType = exports.ResolveType || (exports.ResolveType = {}));
|
|
36
|
+
//# sourceMappingURL=enums.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"enums.js","sourceRoot":"","sources":["../../../../di/src/enums.ts"],"names":[],"mappings":";;;AAAA;;GAEG;AACH,IAAY,WA+BX;AA/BD,WAAY,WAAW;IACrB;;OAEG;IACH,uDAAS,CAAA;IAET;;OAEG;IACH,2DAAW,CAAA;IAEX;;OAEG;IACH,uEAAiB,CAAA;IAEjB;;;;;;;;OAQG;IACH,2DAAW,CAAA;IAEX;;OAEG;IACH,qEAAgB,CAAA;AAClB,CAAC,EA/BW,WAAW,GAAX,mBAAW,KAAX,mBAAW,QA+BtB"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { TypedArray } from './array';
|
|
2
|
+
import { AsyncService, SyncService } from './interfaces';
|
|
3
|
+
import { Factory, Class } from './types';
|
|
4
|
+
/**
|
|
5
|
+
* Checks if value is constructable type.
|
|
6
|
+
* Checks for [[Construct]] internal function in object.
|
|
7
|
+
*
|
|
8
|
+
* @param value - value to test
|
|
9
|
+
*/
|
|
10
|
+
export declare function isConstructor(value: any): value is Class<unknown>;
|
|
11
|
+
export declare function isFactory(value: any): value is Factory<any>;
|
|
12
|
+
export declare function isObject(value: any): value is object;
|
|
13
|
+
export declare function isAsyncService(value: any): value is AsyncService;
|
|
14
|
+
export declare function isPromise(value: any): value is Promise<any>;
|
|
15
|
+
export declare function isSyncService(value: any): value is SyncService;
|
|
16
|
+
/**
|
|
17
|
+
* For DI purpose we treat Map as array to inject. Difference is, that we fill map by some key
|
|
18
|
+
* provided by mapFunc in \@Autoinject
|
|
19
|
+
* @param value - value to check type
|
|
20
|
+
* @returns
|
|
21
|
+
*/
|
|
22
|
+
export declare function isTypedArray(value: any): value is TypedArray<any>;
|
|
23
|
+
export declare function isMap(value: any): value is Map<string, any>;
|
|
24
|
+
export declare function uniqBy<T>(arr: T[], comparator: (a: T, b: T) => boolean): T[];
|
|
25
|
+
export declare function getTypeName(type: TypedArray<any> | Class<any> | string | object): string;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getTypeName = exports.uniqBy = exports.isMap = exports.isTypedArray = exports.isSyncService = exports.isPromise = exports.isAsyncService = exports.isObject = exports.isFactory = exports.isConstructor = void 0;
|
|
4
|
+
const interfaces_1 = require("./interfaces");
|
|
5
|
+
/**
|
|
6
|
+
* Checks if value is constructable type.
|
|
7
|
+
* Checks for [[Construct]] internal function in object.
|
|
8
|
+
*
|
|
9
|
+
* @param value - value to test
|
|
10
|
+
*/
|
|
11
|
+
function isConstructor(value) {
|
|
12
|
+
try {
|
|
13
|
+
Reflect.construct(String, [], value);
|
|
14
|
+
}
|
|
15
|
+
catch (e) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
exports.isConstructor = isConstructor;
|
|
21
|
+
function isFactory(value) {
|
|
22
|
+
return !isConstructor(value) && typeof value === 'function';
|
|
23
|
+
}
|
|
24
|
+
exports.isFactory = isFactory;
|
|
25
|
+
function isObject(value) {
|
|
26
|
+
return typeof value === 'object';
|
|
27
|
+
}
|
|
28
|
+
exports.isObject = isObject;
|
|
29
|
+
function isAsyncService(value) {
|
|
30
|
+
return value instanceof interfaces_1.AsyncService;
|
|
31
|
+
}
|
|
32
|
+
exports.isAsyncService = isAsyncService;
|
|
33
|
+
function isPromise(value) {
|
|
34
|
+
return value instanceof Promise;
|
|
35
|
+
}
|
|
36
|
+
exports.isPromise = isPromise;
|
|
37
|
+
function isSyncService(value) {
|
|
38
|
+
return value instanceof interfaces_1.SyncService;
|
|
39
|
+
}
|
|
40
|
+
exports.isSyncService = isSyncService;
|
|
41
|
+
/**
|
|
42
|
+
* For DI purpose we treat Map as array to inject. Difference is, that we fill map by some key
|
|
43
|
+
* provided by mapFunc in \@Autoinject
|
|
44
|
+
* @param value - value to check type
|
|
45
|
+
* @returns
|
|
46
|
+
*/
|
|
47
|
+
function isTypedArray(value) {
|
|
48
|
+
return (value instanceof Array && value.constructor.name === 'TypedArray') || isMap(value);
|
|
49
|
+
}
|
|
50
|
+
exports.isTypedArray = isTypedArray;
|
|
51
|
+
function isMap(value) {
|
|
52
|
+
return value instanceof Map && value.constructor.name === 'Map';
|
|
53
|
+
}
|
|
54
|
+
exports.isMap = isMap;
|
|
55
|
+
function uniqBy(arr, comparator) {
|
|
56
|
+
const uniques = [];
|
|
57
|
+
for (const a of arr) {
|
|
58
|
+
if (uniques.findIndex((u) => comparator(a, u)) === -1) {
|
|
59
|
+
uniques.push(a);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return uniques;
|
|
63
|
+
}
|
|
64
|
+
exports.uniqBy = uniqBy;
|
|
65
|
+
function getTypeName(type) {
|
|
66
|
+
return typeof type === 'string' ? type : isTypedArray(type) ? getTypeName(type.Type) : isConstructor(type) ? type.name : type.constructor.name;
|
|
67
|
+
}
|
|
68
|
+
exports.getTypeName = getTypeName;
|
|
69
|
+
//# sourceMappingURL=helpers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../../../di/src/helpers.ts"],"names":[],"mappings":";;;AACA,6CAAyD;AAGzD;;;;;GAKG;AACH,SAAgB,aAAa,CAAC,KAAU;IACtC,IAAI;QACF,OAAO,CAAC,SAAS,CAAC,MAAM,EAAE,EAAE,EAAE,KAAK,CAAC,CAAC;KACtC;IAAC,OAAO,CAAC,EAAE;QACV,OAAO,KAAK,CAAC;KACd;IAED,OAAO,IAAI,CAAC;AACd,CAAC;AARD,sCAQC;AAED,SAAgB,SAAS,CAAC,KAAU;IAClC,OAAO,CAAC,aAAa,CAAC,KAAK,CAAC,IAAI,OAAO,KAAK,KAAK,UAAU,CAAC;AAC9D,CAAC;AAFD,8BAEC;AAED,SAAgB,QAAQ,CAAC,KAAU;IACjC,OAAO,OAAO,KAAK,KAAK,QAAQ,CAAC;AACnC,CAAC;AAFD,4BAEC;AAED,SAAgB,cAAc,CAAC,KAAU;IACvC,OAAO,KAAK,YAAY,yBAAY,CAAC;AACvC,CAAC;AAFD,wCAEC;AAED,SAAgB,SAAS,CAAC,KAAU;IAClC,OAAO,KAAK,YAAY,OAAO,CAAC;AAClC,CAAC;AAFD,8BAEC;AAED,SAAgB,aAAa,CAAC,KAAU;IACtC,OAAO,KAAK,YAAY,wBAAW,CAAC;AACtC,CAAC;AAFD,sCAEC;AAED;;;;;GAKG;AACH,SAAgB,YAAY,CAAC,KAAU;IACrC,OAAO,CAAC,KAAK,YAAY,KAAK,IAAI,KAAK,CAAC,WAAW,CAAC,IAAI,KAAK,YAAY,CAAC,IAAI,KAAK,CAAC,KAAK,CAAC,CAAC;AAC7F,CAAC;AAFD,oCAEC;AAED,SAAgB,KAAK,CAAC,KAAU;IAC9B,OAAO,KAAK,YAAY,GAAG,IAAI,KAAK,CAAC,WAAW,CAAC,IAAI,KAAK,KAAK,CAAC;AAClE,CAAC;AAFD,sBAEC;AAED,SAAgB,MAAM,CAAI,GAAQ,EAAE,UAAmC;IACrE,MAAM,OAAO,GAAG,EAAE,CAAC;IACnB,KAAK,MAAM,CAAC,IAAI,GAAG,EAAE;QACnB,IAAI,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,UAAU,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE;YACrD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;SACjB;KACF;IACD,OAAO,OAAO,CAAC;AACjB,CAAC;AARD,wBAQC;AAED,SAAgB,WAAW,CAAC,IAAoD;IAC9E,OAAO,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC;AACjJ,CAAC;AAFD,kCAEC"}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { ResolveType } from './enums';
|
|
3
|
+
import { Class, Factory } from './types';
|
|
4
|
+
import { EventEmitter } from 'events';
|
|
5
|
+
import { TypedArray } from './array';
|
|
6
|
+
import { Registry } from './registry';
|
|
7
|
+
import { ContainerCache } from './container-cache';
|
|
8
|
+
export interface IInstanceCheck {
|
|
9
|
+
__checkInstance__(creationOptions: any): boolean;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Interface to describe DI binding behaviour
|
|
13
|
+
*/
|
|
14
|
+
export interface IBind {
|
|
15
|
+
/**
|
|
16
|
+
* `as` binding (alias)
|
|
17
|
+
*
|
|
18
|
+
* @param type - base class that is being registered
|
|
19
|
+
*/
|
|
20
|
+
as(type: Class<any> | string): this;
|
|
21
|
+
/**
|
|
22
|
+
* self bind, class should be resolved by its name. Its default behaviour.
|
|
23
|
+
*/
|
|
24
|
+
asSelf(): this;
|
|
25
|
+
/**
|
|
26
|
+
* Registers as value, it wont be resolved or called, just stored as is in container
|
|
27
|
+
* @param type - name of type / key after whitch implementation will be resolved
|
|
28
|
+
* @param override - if true, any value registered before is overriden by new one
|
|
29
|
+
*/
|
|
30
|
+
asValue(type: string, override: boolean): this;
|
|
31
|
+
asValue(type: string): this;
|
|
32
|
+
/**
|
|
33
|
+
*
|
|
34
|
+
* Add plain value to container, value is stored in hashmap for quick access
|
|
35
|
+
* eg. we have multiple value converters and we wanc o(1) access, instead searching for
|
|
36
|
+
* converter for specific type
|
|
37
|
+
*
|
|
38
|
+
* @param type - name of added value
|
|
39
|
+
* @param key - hashmap key
|
|
40
|
+
*/
|
|
41
|
+
asMapValue(type: string, hashKey: string): this;
|
|
42
|
+
/**
|
|
43
|
+
* Registers object as single instance ( singleton )
|
|
44
|
+
*/
|
|
45
|
+
singleInstance(): this;
|
|
46
|
+
}
|
|
47
|
+
export interface ResolvableObject {
|
|
48
|
+
[key: string]: any;
|
|
49
|
+
}
|
|
50
|
+
export interface IContainer extends EventEmitter {
|
|
51
|
+
Cache: ContainerCache;
|
|
52
|
+
Registry: Registry;
|
|
53
|
+
Parent: IContainer;
|
|
54
|
+
clear(): void;
|
|
55
|
+
clearRegistry(): void;
|
|
56
|
+
clearCache(): void;
|
|
57
|
+
register<T>(implementation: Class<T> | Factory<T> | ResolvableObject): IBind;
|
|
58
|
+
unregister<T>(implementation: string | Class<T> | Factory<T> | ResolvableObject): void;
|
|
59
|
+
uncache<T>(source: string | Class<T> | TypedArray<T>, parent?: boolean): void;
|
|
60
|
+
child(): IContainer;
|
|
61
|
+
get<T>(service: TypedArray<T>, parent?: boolean): T[] | null;
|
|
62
|
+
get<T>(service: string | Class<T>, parent?: boolean): T | null;
|
|
63
|
+
get<T>(service: string | Class<T> | TypedArray<T>, parent?: boolean): T | T[] | null;
|
|
64
|
+
getRegisteredTypes<T>(service: string | Class<T> | TypedArray<T>, parent?: boolean): Array<Class<unknown> | Factory<unknown>>;
|
|
65
|
+
isResolved<T>(service: string | Class<T>, parent?: boolean): boolean;
|
|
66
|
+
hasRegistered<T>(service: Class<T> | string | TypedArray<T>, parent?: boolean): boolean;
|
|
67
|
+
hasRegisteredType<T>(source: Class<any> | string | TypedArray<any>, type: Class<T> | string | TypedArray<T> | object, parent?: boolean): boolean;
|
|
68
|
+
resolve<T>(type: string, options?: unknown[], check?: boolean): T;
|
|
69
|
+
resolve<T>(type: string, check?: boolean): T;
|
|
70
|
+
resolve<T>(type: Class<T> | Factory<T>, options?: unknown[] | boolean, check?: boolean): T extends AsyncService ? Promise<T> : T;
|
|
71
|
+
resolve<T>(type: TypedArray<T>, options?: unknown[] | boolean, check?: boolean): T extends AsyncService ? Promise<T[]> : T[];
|
|
72
|
+
resolve<T>(type: Class<T> | Factory<T>, check?: boolean): T extends AsyncService ? Promise<T> : T;
|
|
73
|
+
resolve<T>(type: TypedArray<T>, check?: boolean): T extends AsyncService ? Promise<T[]> : T[];
|
|
74
|
+
resolve<T>(type: Class<T> | TypedArray<T> | string, options?: unknown[] | boolean, check?: boolean): Promise<T | T[]> | T | T[];
|
|
75
|
+
dispose(): Promise<void>;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Injection description definition structure
|
|
79
|
+
*/
|
|
80
|
+
export interface IInjectDescriptor<T> {
|
|
81
|
+
inject: IToInject<T>[];
|
|
82
|
+
resolver: ResolveType;
|
|
83
|
+
}
|
|
84
|
+
export interface IToInject<T> {
|
|
85
|
+
inject: Class<T> | TypedArray<T>;
|
|
86
|
+
autoinject: boolean;
|
|
87
|
+
autoinjectKey: string;
|
|
88
|
+
/**
|
|
89
|
+
* additional data passed to DI when resolving
|
|
90
|
+
*/
|
|
91
|
+
data?: any;
|
|
92
|
+
/**
|
|
93
|
+
* Additional options passed to resolved options
|
|
94
|
+
*/
|
|
95
|
+
options?: any;
|
|
96
|
+
/**
|
|
97
|
+
* Callback used to resolve service
|
|
98
|
+
* name of service we want to resolve
|
|
99
|
+
* eg. we have multiple \@injectable registered
|
|
100
|
+
* and we want specific one
|
|
101
|
+
*
|
|
102
|
+
* It is specifically for use with configuration module
|
|
103
|
+
* where services can be changed in configuration files
|
|
104
|
+
* and allows to use @AutoinjectService() decorator
|
|
105
|
+
*/
|
|
106
|
+
serviceFunc?: (data: string | any[], container: IContainer) => IServiceFuncResult | IServiceFuncResult[];
|
|
107
|
+
mapFunc?: (x: IMappableService) => string;
|
|
108
|
+
}
|
|
109
|
+
export interface IMappableService {
|
|
110
|
+
ServiceName: string;
|
|
111
|
+
}
|
|
112
|
+
export interface IServiceFuncResult {
|
|
113
|
+
service: string;
|
|
114
|
+
options?: any;
|
|
115
|
+
}
|
|
116
|
+
export interface IResolvedInjection {
|
|
117
|
+
instance: unknown;
|
|
118
|
+
autoinject: boolean;
|
|
119
|
+
autoinjectKey: string;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Interface to describe DI resolve strategies. Strategies are used do
|
|
123
|
+
* do some work at object creation eg. initialize objects that inherits from same class
|
|
124
|
+
* specific way but without need for factory function.
|
|
125
|
+
*
|
|
126
|
+
*
|
|
127
|
+
* @see FrameworkModuleSyncService implementation
|
|
128
|
+
*/
|
|
129
|
+
export declare class Service {
|
|
130
|
+
protected resolved: boolean;
|
|
131
|
+
get Resolved(): boolean;
|
|
132
|
+
/**
|
|
133
|
+
* Use to dispose service, relase all resources, stop timers etc.
|
|
134
|
+
*
|
|
135
|
+
*/
|
|
136
|
+
dispose(): Promise<void>;
|
|
137
|
+
}
|
|
138
|
+
export declare abstract class SyncService extends Service {
|
|
139
|
+
resolve(): void;
|
|
140
|
+
}
|
|
141
|
+
export declare class AsyncService extends Service {
|
|
142
|
+
resolve(): Promise<void>;
|
|
143
|
+
}
|
|
144
|
+
export declare abstract class Bootstrapper {
|
|
145
|
+
abstract bootstrap(): Promise<void> | void;
|
|
146
|
+
}
|
|
147
|
+
export interface IAutoinjectOptions {
|
|
148
|
+
mapFunc?: (x: IMappableService) => string;
|
|
149
|
+
options?: any;
|
|
150
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Bootstrapper = exports.AsyncService = exports.SyncService = exports.Service = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Interface to describe DI resolve strategies. Strategies are used do
|
|
6
|
+
* do some work at object creation eg. initialize objects that inherits from same class
|
|
7
|
+
* specific way but without need for factory function.
|
|
8
|
+
*
|
|
9
|
+
*
|
|
10
|
+
* @see FrameworkModuleSyncService implementation
|
|
11
|
+
*/
|
|
12
|
+
// export interface IStrategy {
|
|
13
|
+
// resolve: (target: any, container: IContainer) => void;
|
|
14
|
+
// }
|
|
15
|
+
// export interface IAsyncStrategy {
|
|
16
|
+
// resolveA: (target: any, container: IContainer) => Promise<void>;
|
|
17
|
+
// }
|
|
18
|
+
class Service {
|
|
19
|
+
constructor() {
|
|
20
|
+
this.resolved = false;
|
|
21
|
+
}
|
|
22
|
+
get Resolved() {
|
|
23
|
+
return this.resolved;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Use to dispose service, relase all resources, stop timers etc.
|
|
27
|
+
*
|
|
28
|
+
*/
|
|
29
|
+
async dispose() { }
|
|
30
|
+
}
|
|
31
|
+
exports.Service = Service;
|
|
32
|
+
class SyncService extends Service {
|
|
33
|
+
resolve() {
|
|
34
|
+
this.resolved = true;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
exports.SyncService = SyncService;
|
|
38
|
+
class AsyncService extends Service {
|
|
39
|
+
/* eslint-disable */
|
|
40
|
+
async resolve() {
|
|
41
|
+
this.resolved = true;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
exports.AsyncService = AsyncService;
|
|
45
|
+
class Bootstrapper {
|
|
46
|
+
}
|
|
47
|
+
exports.Bootstrapper = Bootstrapper;
|
|
48
|
+
//# sourceMappingURL=interfaces.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interfaces.js","sourceRoot":"","sources":["../../../../di/src/interfaces.ts"],"names":[],"mappings":";;;AA+IA;;;;;;;GAOG;AACH,+BAA+B;AAC/B,6DAA6D;AAC7D,IAAI;AAEJ,oCAAoC;AACpC,uEAAuE;AACvE,IAAI;AAEJ,MAAa,OAAO;IAApB;QACY,aAAQ,GAAG,KAAK,CAAC;IAU7B,CAAC;IATC,IAAW,QAAQ;QACjB,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;IAED;;;OAGG;IACI,KAAK,CAAC,OAAO,KAAI,CAAC;CAC1B;AAXD,0BAWC;AAED,MAAsB,WAAY,SAAQ,OAAO;IACxC,OAAO;QACZ,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC;IACvB,CAAC;CACF;AAJD,kCAIC;AAED,MAAa,YAAa,SAAQ,OAAO;IACvC,oBAAoB;IACb,KAAK,CAAC,OAAO;QAClB,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC;IACvB,CAAC;CACF;AALD,oCAKC;AAED,MAAsB,YAAY;CAEjC;AAFD,oCAEC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { TypedArray } from './array';
|
|
2
|
+
import { IContainer } from './interfaces';
|
|
3
|
+
import { Class, Factory } from './types';
|
|
4
|
+
export declare class Registry {
|
|
5
|
+
protected container: IContainer;
|
|
6
|
+
protected registry: Map<string, any[]>;
|
|
7
|
+
constructor(container: IContainer);
|
|
8
|
+
clear(): void;
|
|
9
|
+
unregister(type: any): void;
|
|
10
|
+
register(name: string | Class<any> | TypedArray<any>, type: any): void;
|
|
11
|
+
hasRegisteredType(source: Class<any> | string | TypedArray<any>, type: Class<any> | string | TypedArray<any> | object, parent?: boolean): boolean;
|
|
12
|
+
getTypes<T>(service: string | Class<T> | TypedArray<T>, parent?: boolean): Array<Class<unknown> | Factory<unknown>>;
|
|
13
|
+
hasRegistered<T>(service: TypedArray<T> | Class<T> | string, parent?: boolean): boolean;
|
|
14
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Registry = void 0;
|
|
4
|
+
const exceptions_1 = require("@spinajs/exceptions");
|
|
5
|
+
const helpers_1 = require("./helpers");
|
|
6
|
+
class Registry {
|
|
7
|
+
constructor(container) {
|
|
8
|
+
this.container = container;
|
|
9
|
+
this.registry = new Map();
|
|
10
|
+
}
|
|
11
|
+
clear() {
|
|
12
|
+
this.registry.clear();
|
|
13
|
+
}
|
|
14
|
+
unregister(type) {
|
|
15
|
+
const tname = (0, helpers_1.getTypeName)(type);
|
|
16
|
+
this.registry.forEach((value) => {
|
|
17
|
+
const index = value.findIndex((x) => (0, helpers_1.getTypeName)(x) === tname);
|
|
18
|
+
if (index !== -1) {
|
|
19
|
+
value.splice(index, 1);
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
register(name, type) {
|
|
24
|
+
if (!(0, helpers_1.isConstructor)(type) && !(0, helpers_1.isFactory)(type) && !(0, helpers_1.isObject)(type)) {
|
|
25
|
+
throw new exceptions_1.InvalidOperation('cannot register type if its not an class or factory function');
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
const tname = (0, helpers_1.getTypeName)(name);
|
|
29
|
+
const value = this.registry.get(tname);
|
|
30
|
+
if (value) {
|
|
31
|
+
// factory functions, we always add to registry
|
|
32
|
+
// its impossible to check for duplicates
|
|
33
|
+
// all would return 'Function' type
|
|
34
|
+
if ((0, helpers_1.isFactory)(type)) {
|
|
35
|
+
value.push(type);
|
|
36
|
+
}
|
|
37
|
+
else if (!value.find((v) => (0, helpers_1.getTypeName)(v) === (0, helpers_1.getTypeName)(type))) {
|
|
38
|
+
value.push(type);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
this.registry.set(tname, [type]);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
hasRegisteredType(source, type, parent) {
|
|
47
|
+
const sourceName = (0, helpers_1.getTypeName)(source);
|
|
48
|
+
const targetName = (0, helpers_1.getTypeName)(type);
|
|
49
|
+
if (this.registry.has(sourceName)) {
|
|
50
|
+
return this.registry.get(sourceName).find((s) => s.name === targetName) !== undefined;
|
|
51
|
+
}
|
|
52
|
+
else if (parent && this.container.Parent) {
|
|
53
|
+
return this.container.Parent.hasRegisteredType(source, type, parent);
|
|
54
|
+
}
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
getTypes(service, parent = true) {
|
|
58
|
+
if (!service) {
|
|
59
|
+
throw new exceptions_1.InvalidArgument('argument "service" cannot be null or empty');
|
|
60
|
+
}
|
|
61
|
+
const name = (0, helpers_1.getTypeName)(service);
|
|
62
|
+
if (this.registry.has(name)) {
|
|
63
|
+
return this.registry.get(name);
|
|
64
|
+
}
|
|
65
|
+
if (this.container.Parent && parent) {
|
|
66
|
+
return this.container.Parent.getRegisteredTypes(service, parent);
|
|
67
|
+
}
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
hasRegistered(service, parent = true) {
|
|
71
|
+
if (this.registry.has((0, helpers_1.getTypeName)(service))) {
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
else if (parent && this.container.Parent) {
|
|
75
|
+
return this.container.Parent.hasRegistered(service, parent);
|
|
76
|
+
}
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
exports.Registry = Registry;
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,MAAM,IAAI,GAAG,IAAA,qBAAW,EAAC,OAAO,CAAC,CAAC;QAElC,IAAI,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE;YAC3B,OAAO,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;SAChC;QAED,IAAI,IAAI,CAAC,SAAS,CAAC,MAAM,IAAI,MAAM,EAAE;YACnC,OAAO,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,kBAAkB,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;SAClE;QAED,OAAO,IAAI,CAAC;IACd,CAAC;IAEM,aAAa,CAAI,OAA0C,EAAE,MAAM,GAAG,IAAI;QAC/E,IAAI,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAA,qBAAW,EAAC,OAAO,CAAC,CAAC,EAAE;YAC3C,OAAO,IAAI,CAAC;SACb;aAAM,IAAI,MAAM,IAAI,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE;YAC1C,OAAO,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,aAAa,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;SAC7D;QAED,OAAO,KAAK,CAAC;IACf,CAAC;CACF;AA/ED,4BA+EC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { IContainer } from './interfaces';
|
|
2
|
+
export declare type Abstract<T> = Function & {
|
|
3
|
+
prototype: T;
|
|
4
|
+
};
|
|
5
|
+
export declare type Constructor<T> = new (...args: any[]) => T;
|
|
6
|
+
export declare type Class<T> = Abstract<T> | Constructor<T>;
|
|
7
|
+
/**
|
|
8
|
+
* Factory functions should not be normal functions,
|
|
9
|
+
* but arrow functions. Its is by js limitation
|
|
10
|
+
* to detect if object is constructable
|
|
11
|
+
*/
|
|
12
|
+
export declare type Factory<T> = (container: IContainer, ...args: any[]) => T;
|
|
13
|
+
export declare type ClassArray<T> = Class<T>[];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../di/src/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { CliCommand } from '@spinajs/cli';
|
|
2
|
+
import { ILog } from '@spinajs/log';
|
|
3
|
+
interface RenderOptions {
|
|
4
|
+
file?: string;
|
|
5
|
+
model?: string;
|
|
6
|
+
lang?: string;
|
|
7
|
+
}
|
|
8
|
+
export declare class RenderTemplateCommand extends CliCommand {
|
|
9
|
+
protected Log: ILog;
|
|
10
|
+
execute(template: string, options: RenderOptions): Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
19
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
20
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
21
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
22
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
23
|
+
};
|
|
24
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
25
|
+
if (mod && mod.__esModule) return mod;
|
|
26
|
+
var result = {};
|
|
27
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
28
|
+
__setModuleDefault(result, mod);
|
|
29
|
+
return result;
|
|
30
|
+
};
|
|
31
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
32
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
33
|
+
};
|
|
34
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
|
+
exports.RenderTemplateCommand = void 0;
|
|
36
|
+
const index_1 = require("./../index");
|
|
37
|
+
const cli_1 = require("@spinajs/cli");
|
|
38
|
+
const di_1 = require("@spinajs/di");
|
|
39
|
+
const path = __importStar(require("path"));
|
|
40
|
+
const fs = __importStar(require("fs"));
|
|
41
|
+
const log_1 = require("@spinajs/log");
|
|
42
|
+
let RenderTemplateCommand = class RenderTemplateCommand extends cli_1.CliCommand {
|
|
43
|
+
async execute(template, options) {
|
|
44
|
+
this.Log.trace(`Rendering ${template}, options: ${JSON.stringify(options)}`);
|
|
45
|
+
try {
|
|
46
|
+
const templates = await di_1.DI.resolve(index_1.Templates);
|
|
47
|
+
let model = {};
|
|
48
|
+
if (options.model && fs.existsSync(options.model)) {
|
|
49
|
+
this.Log.trace(`Found model file at ${options.model}, trying to load model data ... `);
|
|
50
|
+
const mText = fs.readFileSync(options.model, { encoding: 'utf-8' });
|
|
51
|
+
model = JSON.parse(mText);
|
|
52
|
+
}
|
|
53
|
+
if (options && options.file) {
|
|
54
|
+
this.Log.trace(`Rendering template to file ${options.file} ...`);
|
|
55
|
+
const dir = path.dirname(options.file);
|
|
56
|
+
if (!fs.existsSync(dir)) {
|
|
57
|
+
this.Log.trace(`Directory ${dir} not exits, creating ...`);
|
|
58
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
59
|
+
}
|
|
60
|
+
await templates.renderToFile(template, model, options.file, options.lang);
|
|
61
|
+
this.Log.success(`Rendering template ${template} to file ${options.file} succeded !`);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
const result = await templates.render(template, model, options.lang);
|
|
65
|
+
this.Log.success(`Rendering template ${template} succeded !`);
|
|
66
|
+
// print out to console in raw format
|
|
67
|
+
console.log(result);
|
|
68
|
+
}
|
|
69
|
+
catch (err) {
|
|
70
|
+
this.Log.error(`Cannot render template ${template}, reason: ${err.message}, stack: ${err.stack}`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
__decorate([
|
|
75
|
+
(0, log_1.Logger)('templates'),
|
|
76
|
+
__metadata("design:type", Object)
|
|
77
|
+
], RenderTemplateCommand.prototype, "Log", void 0);
|
|
78
|
+
RenderTemplateCommand = __decorate([
|
|
79
|
+
(0, cli_1.Command)('template-render', 'Renders template, usefull for template testing'),
|
|
80
|
+
(0, cli_1.Argument)('template', 'template path, must be in one of directory provided in configs'),
|
|
81
|
+
(0, cli_1.Option)('-f, --file [file]', false, 'path for file, where parsing result will be saved'),
|
|
82
|
+
(0, cli_1.Option)('-m, --model [model]', false, 'path to optional model data, passed to template, in json format'),
|
|
83
|
+
(0, cli_1.Option)('-l, --lang [lang]', false, 'optional language. Language data must be in directories configured in intl module. If none provided, default language is used')
|
|
84
|
+
], RenderTemplateCommand);
|
|
85
|
+
exports.RenderTemplateCommand = RenderTemplateCommand;
|
|
86
|
+
//# sourceMappingURL=render.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"render.js","sourceRoot":"","sources":["../../../../src/cli/render.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;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path) {
|
|
5
|
+
return (0, path_1.resolve)((0, path_1.normalize)((0, path_1.join)(__dirname, path)));
|
|
6
|
+
}
|
|
7
|
+
const templates = {
|
|
8
|
+
system: {
|
|
9
|
+
dirs: {
|
|
10
|
+
templates: [dir('./../templates')],
|
|
11
|
+
cli: [dir('./../cli')],
|
|
12
|
+
},
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
exports.default = templates;
|
|
16
|
+
//# sourceMappingURL=templates.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"templates.js","sourceRoot":"","sources":["../../../../src/config/templates.ts"],"names":[],"mappings":";;AAAA,+BAAgD;AAEhD,SAAS,GAAG,CAAC,IAAY;IACvB,OAAO,IAAA,cAAO,EAAC,IAAA,gBAAS,EAAC,IAAA,WAAI,EAAC,SAAS,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC;AACnD,CAAC;AAED,MAAM,SAAS,GAAG;IAChB,MAAM,EAAE;QACN,IAAI,EAAE;YACJ,SAAS,EAAE,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC;YAClC,GAAG,EAAE,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;SACvB;KACF;CACF,CAAC;AAEF,kBAAe,SAAS,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { AsyncService } from '@spinajs/di';
|
|
2
|
+
import { Log } from '@spinajs/log';
|
|
3
|
+
import { TemplateRenderer } from './interfaces';
|
|
4
|
+
export * from './interfaces';
|
|
5
|
+
/**
|
|
6
|
+
* Inject INTL module for language support. We does nothing but to initialize module for use in templates.
|
|
7
|
+
*/
|
|
8
|
+
export declare class Templates extends AsyncService {
|
|
9
|
+
protected Log: Log;
|
|
10
|
+
protected Renderers: Map<string, TemplateRenderer>;
|
|
11
|
+
render(template: string, model: unknown, language?: string): Promise<string>;
|
|
12
|
+
renderToFile(template: string, model: unknown, filePath: string, language?: string): Promise<void>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
14
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
15
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
16
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
17
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
18
|
+
};
|
|
19
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
20
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
21
|
+
};
|
|
22
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
23
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.Templates = void 0;
|
|
27
|
+
const exceptions_1 = require("@spinajs/exceptions");
|
|
28
|
+
const di_1 = require("@spinajs/di");
|
|
29
|
+
const log_1 = require("@spinajs/log");
|
|
30
|
+
const interfaces_1 = require("./interfaces");
|
|
31
|
+
const path_1 = require("path");
|
|
32
|
+
const intl_1 = require("@spinajs/intl");
|
|
33
|
+
__exportStar(require("./interfaces"), exports);
|
|
34
|
+
/**
|
|
35
|
+
* Inject INTL module for language support. We does nothing but to initialize module for use in templates.
|
|
36
|
+
*/
|
|
37
|
+
let Templates = class Templates extends di_1.AsyncService {
|
|
38
|
+
async render(template, model, language) {
|
|
39
|
+
const extension = (0, path_1.extname)(template);
|
|
40
|
+
if (!this.Renderers.has(extension)) {
|
|
41
|
+
throw new exceptions_1.InvalidOperation(`No renderer for file ${template} with extension ${extension}`);
|
|
42
|
+
}
|
|
43
|
+
return await this.Renderers.get(extension).render(template, model, language);
|
|
44
|
+
}
|
|
45
|
+
async renderToFile(template, model, filePath, language) {
|
|
46
|
+
const extension = (0, path_1.extname)(template);
|
|
47
|
+
if (!this.Renderers.has(extension)) {
|
|
48
|
+
throw new exceptions_1.InvalidOperation(`No renderer for file ${template} with extension ${extension}`);
|
|
49
|
+
}
|
|
50
|
+
return await this.Renderers.get(extension).renderToFile(template, model, filePath, language);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
__decorate([
|
|
54
|
+
(0, log_1.Logger)('templates'),
|
|
55
|
+
__metadata("design:type", log_1.Log)
|
|
56
|
+
], Templates.prototype, "Log", void 0);
|
|
57
|
+
__decorate([
|
|
58
|
+
(0, di_1.Autoinject)(interfaces_1.TemplateRenderer, {
|
|
59
|
+
mapFunc: (x) => x.ServiceName,
|
|
60
|
+
}),
|
|
61
|
+
__metadata("design:type", Map)
|
|
62
|
+
], Templates.prototype, "Renderers", void 0);
|
|
63
|
+
Templates = __decorate([
|
|
64
|
+
(0, di_1.Inject)(intl_1.Intl)
|
|
65
|
+
], Templates);
|
|
66
|
+
exports.Templates = Templates;
|
|
67
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/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AE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,WAAW;KAC9B,CAAC;8BACmB,GAAG;4CAA2B;AAPxC,SAAS;IADrB,IAAA,WAAM,EAAC,WAAI,CAAC;GACA,SAAS,CA0BrB;AA1BY,8BAAS"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { IMappableService } from './../../di/src/interfaces';
|
|
2
|
+
import { AsyncService } from '@spinajs/di';
|
|
3
|
+
import { Log } from '@spinajs/log';
|
|
4
|
+
export declare abstract class TemplateRenderer extends AsyncService implements IMappableService {
|
|
5
|
+
protected Log: Log;
|
|
6
|
+
protected TemplatePaths: string[];
|
|
7
|
+
protected TemplateFiles: Map<string, string[]>;
|
|
8
|
+
abstract get Type(): string;
|
|
9
|
+
abstract get Extension(): string;
|
|
10
|
+
get ServiceName(): string;
|
|
11
|
+
abstract render(templatePath: string, model: unknown, language?: string): Promise<string>;
|
|
12
|
+
abstract renderToFile(templatePath: string, model: unknown, filePath: string, language?: string): Promise<void>;
|
|
13
|
+
/**
|
|
14
|
+
* Function used for precompiling templates at load time. Not all template engines can support it, leave it empty if so.
|
|
15
|
+
*
|
|
16
|
+
* @param templateName - template name
|
|
17
|
+
* @param path - template full path
|
|
18
|
+
*/
|
|
19
|
+
protected abstract compile(templateName: string, path: string): Promise<void>;
|
|
20
|
+
resolve(): Promise<void>;
|
|
21
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.TemplateRenderer = void 0;
|
|
13
|
+
const configuration_1 = require("@spinajs/configuration");
|
|
14
|
+
const di_1 = require("@spinajs/di");
|
|
15
|
+
const log_1 = require("@spinajs/log");
|
|
16
|
+
const path_1 = require("path");
|
|
17
|
+
const glob_1 = require("glob");
|
|
18
|
+
class TemplateRenderer extends di_1.AsyncService {
|
|
19
|
+
constructor() {
|
|
20
|
+
super(...arguments);
|
|
21
|
+
this.TemplateFiles = new Map();
|
|
22
|
+
}
|
|
23
|
+
get ServiceName() {
|
|
24
|
+
// we map this service by extension
|
|
25
|
+
return this.Extension;
|
|
26
|
+
}
|
|
27
|
+
async resolve() {
|
|
28
|
+
for (const path of this.TemplatePaths) {
|
|
29
|
+
const files = glob_1.glob.sync((0, path_1.join)(path, `/**/*${this.Extension}`));
|
|
30
|
+
for (const file of files) {
|
|
31
|
+
const templateName = file.substring(path.length + 1, file.length);
|
|
32
|
+
if (this.TemplateFiles.has(templateName)) {
|
|
33
|
+
this.Log.trace(`Template ${templateName} is overriden by ${file}`);
|
|
34
|
+
this.TemplateFiles.get(templateName).push(file);
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
this.Log.trace(`Found template ${templateName} file at path ${file}`);
|
|
38
|
+
this.TemplateFiles.set(templateName, [file]);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
for (const [templateName, path] of this.TemplateFiles) {
|
|
43
|
+
if (path.length === 0) {
|
|
44
|
+
this.Log.warn(`Template ${templateName} don't have any files`);
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
this.Log.trace(`Compiling template ${templateName}, at path ${path[path.length - 1]}`);
|
|
48
|
+
// compile only last template ( newest )
|
|
49
|
+
// templates can be overriden by other modules / libs
|
|
50
|
+
// or app
|
|
51
|
+
try {
|
|
52
|
+
await this.compile(templateName, path[path.length - 1]);
|
|
53
|
+
}
|
|
54
|
+
catch (err) {
|
|
55
|
+
this.Log.error(`Cannot compile template ${templateName} with file ${path[path.length - 1]}, reason: ${JSON.stringify(err)}`);
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
this.Log.trace(`Compiling template ${templateName}, at path ${path[path.length - 1]} finished`);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
__decorate([
|
|
63
|
+
(0, log_1.Logger)('renderer'),
|
|
64
|
+
__metadata("design:type", log_1.Log)
|
|
65
|
+
], TemplateRenderer.prototype, "Log", void 0);
|
|
66
|
+
__decorate([
|
|
67
|
+
(0, configuration_1.Config)('system.dirs.templates', { defaultValue: [] }),
|
|
68
|
+
__metadata("design:type", Array)
|
|
69
|
+
], TemplateRenderer.prototype, "TemplatePaths", void 0);
|
|
70
|
+
exports.TemplateRenderer = TemplateRenderer;
|
|
71
|
+
//# sourceMappingURL=interfaces.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interfaces.js","sourceRoot":"","sources":["../../../src/interfaces.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,0DAAgD;AAChD,oCAA2C;AAC3C,sCAA2C;AAC3C,+BAA4B;AAC5B,+BAA4B;AAG5B,MAAsB,gBAAiB,SAAQ,iBAAY;IAA3D;;QAOY,kBAAa,GAA0B,IAAI,GAAG,EAAoB,CAAC;IA4D/E,CAAC;IAtDC,IAAW,WAAW;QACpB,mCAAmC;QACnC,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,CAAC;IAaM,KAAK,CAAC,OAAO;QAClB,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,aAAa,EAAE;YACrC,MAAM,KAAK,GAAG,WAAI,CAAC,IAA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,EAAE;gBACZ,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,2BAA2B,YAAY,cAAc,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,aAAa,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;gBAE7H,SAAS;aACV;YAED,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,sBAAsB,YAAY,aAAa,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,WAAW,CAAC,CAAC;SACjG;IACH,CAAC;CACF;AAjEC;IADC,IAAA,YAAM,EAAC,UAAU,CAAC;8BACJ,SAAG;6CAAC;AAGnB;IADC,IAAA,sBAAM,EAAC,uBAAuB,EAAE,EAAE,YAAY,EAAE,EAAE,EAAE,CAAC;;uDACpB;AALpC,4CAmEC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@spinajs/templates",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.45",
|
|
4
4
|
"description": "templates support eg. html file generation",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"private": false,
|
|
@@ -45,16 +45,16 @@
|
|
|
45
45
|
"@types/pug": "^2.0.4"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@spinajs/cli": "^2.0.
|
|
49
|
-
"@spinajs/configuration": "^2.0.
|
|
50
|
-
"@spinajs/di": "^2.0.
|
|
48
|
+
"@spinajs/cli": "^2.0.45",
|
|
49
|
+
"@spinajs/configuration": "^2.0.45",
|
|
50
|
+
"@spinajs/di": "^2.0.45",
|
|
51
51
|
"@spinajs/exceptions": "^2.0.39",
|
|
52
|
-
"@spinajs/internal-logger": "^2.0.
|
|
53
|
-
"@spinajs/intl": "^2.0.
|
|
54
|
-
"@spinajs/log": "^2.0.
|
|
52
|
+
"@spinajs/internal-logger": "^2.0.45",
|
|
53
|
+
"@spinajs/intl": "^2.0.45",
|
|
54
|
+
"@spinajs/log": "^2.0.45",
|
|
55
55
|
"handlebars": "^4.7.7",
|
|
56
56
|
"pug": "^3.0.0",
|
|
57
57
|
"tempfile": "3.0.0"
|
|
58
58
|
},
|
|
59
|
-
"gitHead": "
|
|
59
|
+
"gitHead": "3aa423c570eae952dc79fe6a4704c3d2ccc178be"
|
|
60
60
|
}
|