@spinajs/configuration 1.0.15 → 1.1.4

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.
File without changes
@@ -0,0 +1,8 @@
1
+ module.exports = {
2
+ configuration: {
3
+ isDevelopment: process.env.NODE_ENV === "development",
4
+ isProduction: process.env.NODE_ENV === "production",
5
+ isElectron: process.version.hasOwnProperty("electron")
6
+ }
7
+ };
8
+ //# sourceMappingURL=configuration.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"configuration.js","sourceRoot":"","sources":["../../src/config/configuration.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,OAAO,GAAG;IACb,aAAa,EAAE;QACX,aAAa,EAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,aAAa;QACtD,YAAY,EAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY;QACrD,UAAU,EAAM,OAAO,CAAC,OAAO,CAAC,cAAc,CAAC,UAAU,CAAC;KAC7D;CACJ,CAAA"}
@@ -0,0 +1,53 @@
1
+ import { IContainer } from '@spinajs/di';
2
+ import { ConfigurationSource } from './sources';
3
+ import { Configuration, ConfigurationOptions } from './types';
4
+ export declare class FrameworkConfiguration extends Configuration {
5
+ /**
6
+ * Apps configuration base dir, where to look for app config
7
+ */
8
+ AppBaseDir: string;
9
+ /**
10
+ * Current running app name
11
+ */
12
+ RunApp: string;
13
+ /**
14
+ * Loaded & merged configuration
15
+ */
16
+ protected Config: any;
17
+ protected CustomConfigPaths: string[];
18
+ protected Sources: ConfigurationSource[];
19
+ /**
20
+ *
21
+ * @param app application name, pass it when you run in application mode
22
+ * @param baseDir configuration base dir, where to look for application configs
23
+ * @param cfgCustomPaths custom cfg paths eg. to load config from non standard folders ( usefull in tests )
24
+ */
25
+ constructor(options?: ConfigurationOptions);
26
+ /**
27
+ * Get config value for given property. If value not exists it returns default value, if default value is not provided returns undefined
28
+ *
29
+ * @param path - path to property eg. ["system","dirs"]
30
+ * @param defaultValue - optional, if value at specified path not exists returns default value
31
+ * @returns { T | undefined }
32
+ */
33
+ get<T>(path: string[] | string, defaultValue?: T): T;
34
+ /**
35
+ * Sets at given path configuration value. Use when you want to override config
36
+ * loaded from files programatically
37
+ *
38
+ * @param path config path
39
+ * @param value value to set
40
+ */
41
+ set(path: string[] | string, value: any): void;
42
+ resolveAsync(container: IContainer): Promise<void>;
43
+ protected dir(toJoin: string): string;
44
+ /**
45
+ * adds app dirs to system.dirs config
46
+ */
47
+ protected applyAppDirs(): void;
48
+ /**
49
+ * runs configuration func on files
50
+ * eg. when you want to configure stuff at beginning eq. external libs
51
+ */
52
+ protected configure(): void;
53
+ }
@@ -0,0 +1,129 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
15
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
16
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
17
+ 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;
18
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
19
+ };
20
+ var __importStar = (this && this.__importStar) || function (mod) {
21
+ if (mod && mod.__esModule) return mod;
22
+ var result = {};
23
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
24
+ __setModuleDefault(result, mod);
25
+ return result;
26
+ };
27
+ var __metadata = (this && this.__metadata) || function (k, v) {
28
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
29
+ };
30
+ Object.defineProperty(exports, "__esModule", { value: true });
31
+ exports.FrameworkConfiguration = void 0;
32
+ const di_1 = require("@spinajs/di");
33
+ const exceptions_1 = require("@spinajs/exceptions");
34
+ const path_1 = require("path");
35
+ const sources_1 = require("./sources");
36
+ const types_1 = require("./types");
37
+ const util_1 = require("./util");
38
+ const _ = __importStar(require("lodash"));
39
+ let FrameworkConfiguration = class FrameworkConfiguration extends types_1.Configuration {
40
+ /**
41
+ *
42
+ * @param app application name, pass it when you run in application mode
43
+ * @param baseDir configuration base dir, where to look for application configs
44
+ * @param cfgCustomPaths custom cfg paths eg. to load config from non standard folders ( usefull in tests )
45
+ */
46
+ constructor(options) {
47
+ var _a, _b, _c, _d;
48
+ super();
49
+ /**
50
+ * Apps configuration base dir, where to look for app config
51
+ */
52
+ this.AppBaseDir = './';
53
+ /**
54
+ * Current running app name
55
+ */
56
+ this.RunApp = "";
57
+ /**
58
+ * Loaded & merged configuration
59
+ */
60
+ this.Config = {};
61
+ this.CustomConfigPaths = (_a = options === null || options === void 0 ? void 0 : options.cfgCustomPaths) !== null && _a !== void 0 ? _a : [];
62
+ this.RunApp = (_b = options === null || options === void 0 ? void 0 : options.app) !== null && _b !== void 0 ? _b : (0, util_1.parseArgv)('--app');
63
+ this.AppBaseDir = (_d = (_c = options === null || options === void 0 ? void 0 : options.appBaseDir) !== null && _c !== void 0 ? _c : (0, util_1.parseArgv)('--apppath')) !== null && _d !== void 0 ? _d : (0, path_1.join)(__dirname, '../apps/');
64
+ }
65
+ /**
66
+ * Get config value for given property. If value not exists it returns default value, if default value is not provided returns undefined
67
+ *
68
+ * @param path - path to property eg. ["system","dirs"]
69
+ * @param defaultValue - optional, if value at specified path not exists returns default value
70
+ * @returns { T | undefined }
71
+ */
72
+ get(path, defaultValue) {
73
+ return _.get(this.Config, path, defaultValue);
74
+ }
75
+ /**
76
+ * Sets at given path configuration value. Use when you want to override config
77
+ * loaded from files programatically
78
+ *
79
+ * @param path config path
80
+ * @param value value to set
81
+ */
82
+ set(path, value) {
83
+ this.Config = _.set(this.Config, path, value);
84
+ }
85
+ async resolveAsync(container) {
86
+ if (!container.hasRegistered(sources_1.ConfigurationSource)) {
87
+ throw new exceptions_1.InvalidOperation("No configuration sources configured. Please ensure that config module have any source to read from !");
88
+ }
89
+ this.Sources = await container.resolve(Array.ofType(sources_1.ConfigurationSource), [this.RunApp, this.CustomConfigPaths, this.AppBaseDir]);
90
+ await Promise.all(this.Sources.map(s => s.Load())).then(result => {
91
+ result.map(c => _.merge(this.Config, c));
92
+ });
93
+ this.applyAppDirs();
94
+ this.configure();
95
+ await super.resolveAsync(container);
96
+ }
97
+ dir(toJoin) {
98
+ return (0, path_1.normalize)((0, path_1.join)((0, path_1.resolve)(this.BaseDir), toJoin));
99
+ }
100
+ /**
101
+ * adds app dirs to system.dirs config
102
+ */
103
+ applyAppDirs() {
104
+ if (!this.RunApp) {
105
+ return;
106
+ }
107
+ for (const prop of Object.keys(this.get(['system', 'dirs'], []))) {
108
+ this.get(['system', 'dirs', prop]).push(this.dir(`/${this.RunApp}/${prop}`));
109
+ }
110
+ }
111
+ /**
112
+ * runs configuration func on files
113
+ * eg. when you want to configure stuff at beginning eq. external libs
114
+ */
115
+ configure() {
116
+ for (const prop of Object.keys(this.Config)) {
117
+ const subconfig = this.Config[prop];
118
+ if (_.isFunction(subconfig.configure)) {
119
+ subconfig.configure.apply(this);
120
+ }
121
+ }
122
+ }
123
+ };
124
+ FrameworkConfiguration = __decorate([
125
+ (0, di_1.Injectable)(types_1.Configuration),
126
+ __metadata("design:paramtypes", [Object])
127
+ ], FrameworkConfiguration);
128
+ exports.FrameworkConfiguration = FrameworkConfiguration;
129
+ //# sourceMappingURL=configuration.js.map
@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,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,IAAI,CAAC,MAAM,IAAI,IAAI,EAAE,CAAC,CAAC,CAAC;SACxF;IACH,CAAC;IAED;;;OAGG;IACO,SAAS;QACjB,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE;YAC3C,MAAM,SAAS,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;YAEpC,IAAI,CAAC,CAAC,UAAU,CAAC,SAAS,CAAC,SAAS,CAAC,EAAE;gBACrC,SAAS,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;aACjC;SACF;IACH,CAAC;CACF,CAAA;AA3GY,sBAAsB;IADlC,IAAA,eAAU,EAAC,qBAAa,CAAC;;GACb,sBAAsB,CA2GlC;AA3GY,wDAAsB"}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Injects configuration value to given class property
3
+ *
4
+ * @param path path to configuration value eg. "app.dirs.stats"
5
+ * @param dafaultValue default value if path not exists
6
+ * @returns
7
+ */
8
+ export declare function Config(path: string, dafaultValue?: any): (target?: any, key?: string) => any;
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Config = void 0;
4
+ const di_1 = require("@spinajs/di");
5
+ const types_1 = require("./types");
6
+ /**
7
+ * Injects configuration value to given class property
8
+ *
9
+ * @param path path to configuration value eg. "app.dirs.stats"
10
+ * @param dafaultValue default value if path not exists
11
+ * @returns
12
+ */
13
+ function Config(path, dafaultValue) {
14
+ return (target, key) => {
15
+ let config = null;
16
+ const getter = () => {
17
+ if (!config) {
18
+ config = di_1.DI.get(types_1.Configuration);
19
+ }
20
+ return config.get(path, dafaultValue);
21
+ };
22
+ Object.defineProperty(target, key, {
23
+ get: getter,
24
+ enumerable: false,
25
+ configurable: false
26
+ });
27
+ };
28
+ }
29
+ exports.Config = Config;
30
+ //# sourceMappingURL=decorators.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decorators.js","sourceRoot":"","sources":["../src/decorators.ts"],"names":[],"mappings":";;;AAAA,oCAAiC;AACjC,mCAAuC;AAEvC;;;;;;GAMG;AACH,SAAgB,MAAM,CAAC,IAAY,EAAE,YAAkB;IACnD,OAAO,CAAC,MAAY,EAAE,GAAY,EAAO,EAAE;QAEvC,IAAI,MAAM,GAAkB,IAAI,CAAC;QAEjC,MAAM,MAAM,GAAG,GAAG,EAAE;YAChB,IAAI,CAAC,MAAM,EAAE;gBACT,MAAM,GAAG,OAAE,CAAC,GAAG,CAAC,qBAAa,CAAC,CAAC;aAClC;YACD,OAAO,MAAM,CAAC,GAAG,CAAC,IAAI,EAAE,YAAY,CAAC,CAAC;QAC1C,CAAC,CAAA;QAED,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,GAAG,EAAE;YAC/B,GAAG,EAAE,MAAM;YACX,UAAU,EAAE,KAAK;YACjB,YAAY,EAAE,KAAK;SACtB,CAAC,CAAC;IACP,CAAC,CAAA;AACL,CAAC;AAlBD,wBAkBC"}
package/lib/index.d.ts CHANGED
@@ -1,70 +1,4 @@
1
- import { IContainer, SyncModule } from '@spinajs/di';
2
- export declare abstract class Configuration extends SyncModule {
3
- /**
4
- * Configuration base dir, where to look for app config
5
- */
6
- BaseDir: string;
7
- /**
8
- * Current running app name
9
- */
10
- RunApp: string;
11
- /**
12
- * Get config value for given property. Returns any if value is present, default value if provided or null when empty
13
- *
14
- * @param path - path to property eg. ["system","dirs"] or "system" or "system.dirs"
15
- */
16
- abstract get<T>(path: string[] | string, defaultValue?: T): T;
17
- }
18
- export declare class FrameworkConfiguration extends Configuration {
19
- /**
20
- * Configuration base dir, where to look for app config
21
- */
22
- BaseDir: string;
23
- /**
24
- * Current running app name
25
- */
26
- RunApp: string;
27
- /**
28
- * Default dirs to check for configuration files
29
- */
30
- protected CONFIG_DIRS: string[];
31
- /**
32
- * Loaded & merged configuration
33
- */
34
- protected Config: any;
35
- protected CustomConfigPaths: string[];
36
- /**
37
- *
38
- * @param app application name, pass it when you run in application mode
39
- * @param baseDir configuration base dir, where to look for application configs
40
- * @param cfgCustomPaths custom cfg paths eg. to load config from non standard folders ( usefull in tests )
41
- */
42
- constructor(app?: string, appBaseDir?: string, cfgCustomPaths?: string[]);
43
- /**
44
- * Get config value for given property. Returns Maybe<any> if value is present or Maybe<null> when empty
45
- *
46
- * @param path - path to property eg. ["system","dirs"]
47
- * @param defaultValue - optional, if value at specified path not exists returns default value
48
- */
49
- get<T>(path: string[] | string, defaultValue?: T): T;
50
- resolve(_container: IContainer): void;
51
- protected load(extension: string, callback: (file: string) => any): void;
52
- protected dir(toJoin: string): string;
53
- /**
54
- * adds app dirs to system.dirs config
55
- */
56
- protected _appDirs(): void;
57
- /**
58
- * runs configuration func on files
59
- * eg. when you want to configure stuff at beginning eq. external libs
60
- */
61
- protected configure(): void;
62
- /**
63
- * Just prints framework version
64
- */
65
- protected version(): void;
66
- /**
67
- * Gets app name passed to CLI & adds proper config dirs to merge
68
- */
69
- protected configureApp(): void;
70
- }
1
+ export * from "./types";
2
+ export * from "./sources";
3
+ export * from "./decorators";
4
+ export * from "./configuration";
package/lib/index.js CHANGED
@@ -1,213 +1,17 @@
1
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);
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
10
11
  };
11
12
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.FrameworkConfiguration = exports.Configuration = void 0;
13
- const di_1 = require("@spinajs/di");
14
- const fs = require("fs");
15
- const glob = require("glob");
16
- const _ = require("lodash");
17
- const path_1 = require("path");
18
- function log(message) {
19
- if (typeof jasmine === 'undefined') {
20
- console.log('[ CONFIGURATION ] ' + message);
21
- }
22
- }
23
- function merge(to, from) {
24
- _.mergeWith(to, from, (src, dest) => {
25
- if (_.isArray(src) && _.isArray(dest)) {
26
- const tmp = src.concat(dest);
27
- return _.uniqWith(tmp, _.isEqual);
28
- }
29
- else if (!src) {
30
- return dest;
31
- }
32
- });
33
- return to;
34
- }
35
- // clean require cache config
36
- // http://stackoverflow.com/questions/9210542/node-js-require-cache-possible-to-invalidate
37
- function uncache(file) {
38
- delete require.cache[file];
39
- return file;
40
- }
41
- function filterDirs(dir) {
42
- if (fs.existsSync(dir)) {
43
- log(`Found config dir at ${dir}`);
44
- return true;
45
- }
46
- return false;
47
- }
48
- class Configuration extends di_1.SyncModule {
49
- }
50
- exports.Configuration = Configuration;
51
- let FrameworkConfiguration = class FrameworkConfiguration extends Configuration {
52
- /**
53
- *
54
- * @param app application name, pass it when you run in application mode
55
- * @param baseDir configuration base dir, where to look for application configs
56
- * @param cfgCustomPaths custom cfg paths eg. to load config from non standard folders ( usefull in tests )
57
- */
58
- constructor(app, appBaseDir, cfgCustomPaths) {
59
- var _a;
60
- super();
61
- /**
62
- * Configuration base dir, where to look for app config
63
- */
64
- this.BaseDir = './';
65
- /**
66
- * Current running app name
67
- */
68
- this.RunApp = undefined;
69
- /**
70
- * Default dirs to check for configuration files
71
- */
72
- this.CONFIG_DIRS = [
73
- // this module path
74
- path_1.normalize(path_1.join(path_1.resolve(__dirname), '/../config')),
75
- // other @spinajs modules paths
76
- '/node_modules/@spinajs/orm/lib/config',
77
- '/node_modules/@spinajs/orm-sqlite/lib/config',
78
- '/node_modules/@spinajs/orm-sql/lib/config',
79
- '/node_modules/@spinajs/log/lib/config',
80
- '/node_modules/@spinajs/intl/lib/config',
81
- '/node_modules/@spinajs/cli/lib/config',
82
- '/node_modules/@spinajs/acl/lib/config',
83
- '/node_modules/@spinajs/http/lib/config',
84
- '/node_modules/@spinajs/acl-http/lib/config',
85
- '/node_modules/@spinajs/jobs/lib/config',
86
- '/node_modules/@spinajs/acl-session-provider-dynamodb/lib/config',
87
- // project paths - last to allow overwrite @spinajs conf
88
- '/lib/config',
89
- '/dist/config',
90
- '/build/config',
91
- '/config',
92
- ];
93
- /**
94
- * Loaded & merged configuration
95
- */
96
- this.Config = {};
97
- this.CustomConfigPaths = cfgCustomPaths !== null && cfgCustomPaths !== void 0 ? cfgCustomPaths : [];
98
- this.RunApp = app !== null && app !== void 0 ? app : parseArgv('--app');
99
- this.BaseDir = (_a = appBaseDir !== null && appBaseDir !== void 0 ? appBaseDir : parseArgv('--appPath')) !== null && _a !== void 0 ? _a : path_1.join(__dirname, '../apps/');
100
- log(`Running app: ${this.RunApp}`);
101
- log(`Base dir at: ${this.BaseDir}`);
102
- function parseArgv(param) {
103
- const index = process.argv.indexOf(param);
104
- if (index === -1 || process.argv.length <= index + 1) {
105
- return undefined;
106
- }
107
- return process.argv[index + 1];
108
- }
109
- }
110
- /**
111
- * Get config value for given property. Returns Maybe<any> if value is present or Maybe<null> when empty
112
- *
113
- * @param path - path to property eg. ["system","dirs"]
114
- * @param defaultValue - optional, if value at specified path not exists returns default value
115
- */
116
- get(path, defaultValue) {
117
- return _.get(this.Config, path, defaultValue);
118
- }
119
- resolve(_container) {
120
- this.configureApp();
121
- this.load('js', (file) => {
122
- uncache(file);
123
- return require(file);
124
- });
125
- this.load('json', (file) => {
126
- return JSON.parse(fs.readFileSync(file, 'utf-8'));
127
- });
128
- this.version();
129
- this._appDirs();
130
- this.configure();
131
- }
132
- load(extension, callback) {
133
- const basePath = this.RunApp ? this.BaseDir : findBasePath(process.cwd());
134
- this.CONFIG_DIRS.map(f => path_1.join(basePath, f))
135
- .concat(this.CustomConfigPaths)
136
- .filter(filterDirs)
137
- // get all config files
138
- .map(d => glob.sync(d + `/**/*.${extension}`))
139
- // flatten files
140
- .reduce((prev, current) => {
141
- return prev.concat(_.flattenDeep(current));
142
- }, [])
143
- // normalize & resolve paths to be sure
144
- .map((f) => path_1.normalize(path_1.resolve(f)))
145
- // info about files about to load
146
- .map(f => {
147
- log(`Found file at: ${f}`);
148
- return f;
149
- })
150
- .map(callback)
151
- // load & merge configs
152
- .map(_.curry(merge)(this.Config));
153
- function findBasePath(path) {
154
- if (fs.existsSync(path_1.join(path, 'node_modules'))) {
155
- return path;
156
- }
157
- return findBasePath(path_1.resolve(path, '..'));
158
- }
159
- }
160
- dir(toJoin) {
161
- return path_1.normalize(path_1.join(path_1.resolve(this.BaseDir), toJoin));
162
- }
163
- /**
164
- * adds app dirs to system.dirs config
165
- */
166
- _appDirs() {
167
- if (_.isEmpty(this.RunApp)) {
168
- return;
169
- }
170
- for (const prop of Object.keys(this.get(['system', 'dirs'], []))) {
171
- this.get(['system', 'dirs', prop]).push(this.dir(`/${this.RunApp}/${prop}`));
172
- }
173
- }
174
- /**
175
- * runs configuration func on files
176
- * eg. when you want to configure stuff at beginning eq. external libs
177
- */
178
- configure() {
179
- for (const prop of Object.keys(this.Config)) {
180
- const subconfig = this.Config[prop];
181
- if (_.isFunction(subconfig.configure)) {
182
- subconfig.configure.call(subconfig);
183
- }
184
- }
185
- }
186
- /**
187
- * Just prints framework version
188
- */
189
- version() {
190
- const version = this.get('system.version', undefined);
191
- if (version) {
192
- log(`APP VERSION: ${version.major}.${version.minor}`);
193
- }
194
- else {
195
- log('APP VERSION UNKNOWN');
196
- }
197
- }
198
- /**
199
- * Gets app name passed to CLI & adds proper config dirs to merge
200
- */
201
- configureApp() {
202
- if (typeof this.RunApp === 'string') {
203
- log(`Application to run: ${this.RunApp}`);
204
- this.CONFIG_DIRS.push(`./${this.RunApp}/config`);
205
- }
206
- }
207
- };
208
- FrameworkConfiguration = __decorate([
209
- di_1.Injectable(Configuration),
210
- __metadata("design:paramtypes", [String, String, Array])
211
- ], FrameworkConfiguration);
212
- exports.FrameworkConfiguration = FrameworkConfiguration;
13
+ __exportStar(require("./types"), exports);
14
+ __exportStar(require("./sources"), exports);
15
+ __exportStar(require("./decorators"), exports);
16
+ __exportStar(require("./configuration"), exports);
213
17
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,oCAAiE;AACjE,yBAAyB;AACzB,6BAA6B;AAC7B,4BAA4B;AAC5B,+BAAgD;AAehD,SAAS,GAAG,CAAC,OAAe;IAC1B,IAAI,OAAO,OAAO,KAAK,WAAW,EAAE;QAClC,OAAO,CAAC,GAAG,CAAC,oBAAoB,GAAG,OAAO,CAAC,CAAC;KAC7C;AACH,CAAC;AAED,SAAS,KAAK,CAAC,EAAO,EAAE,IAAS;IAC/B,CAAC,CAAC,SAAS,CAAC,EAAE,EAAE,IAAI,EAAE,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE;QAClC,IAAI,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE;YACrC,MAAM,GAAG,GAAG,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;YAC7B,OAAO,CAAC,CAAC,QAAQ,CAAC,GAAG,EAAE,C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AM,OAAO,GAAG,IAAI,CAAC,GAAG,CAAoB,gBAAgB,EAAE,SAAS,CAAC,CAAC;QAEzE,IAAI,OAAO,EAAE;YACX,GAAG,CAAC,gBAAgB,OAAO,CAAC,KAAK,IAAI,OAAO,CAAC,KAAK,EAAE,CAAC,CAAC;SACvD;aAAM;YACL,GAAG,CAAC,qBAAqB,CAAC,CAAC;SAC5B;IACH,CAAC;IAED;;OAEG;IACO,YAAY;QACpB,IAAI,OAAO,IAAI,CAAC,MAAM,KAAK,QAAQ,EAAE;YACnC,GAAG,CAAC,uBAAuB,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;YAC1C,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,KAAK,IAAI,CAAC,MAAM,SAAS,CAAC,CAAC;SAClD;IACH,CAAC;CACF,CAAA;AAxLY,sBAAsB;IADlC,eAAU,CAAC,aAAa,CAAC;;GACb,sBAAsB,CAwLlC;AAxLY,wDAAsB"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,0CAAwB;AACxB,4CAA0B;AAC1B,+CAA6B;AAC7B,kDAAgC"}
@@ -0,0 +1,32 @@
1
+ export declare abstract class ConfigurationSource {
2
+ abstract Load(): Promise<any>;
3
+ }
4
+ export declare abstract class BaseFileSource extends ConfigurationSource {
5
+ protected RunApp?: string;
6
+ protected CustomConfigPaths?: string[];
7
+ protected appBaseDir?: string;
8
+ /**
9
+ * Configuration base dir, where to look for app config
10
+ */
11
+ BaseDir: string;
12
+ /**
13
+ * Default dirs to check for configuration files
14
+ */
15
+ protected CONFIG_DIRS: string[];
16
+ constructor(RunApp?: string, CustomConfigPaths?: string[], appBaseDir?: string);
17
+ protected load(extension: string, callback: (file: string) => any): {};
18
+ }
19
+ export declare class JsFileSource extends BaseFileSource {
20
+ /**
21
+ * Default dirs to check for configuration files
22
+ */
23
+ protected CONFIG_DIRS: string[];
24
+ Load(): Promise<any>;
25
+ }
26
+ export declare class JsonFileSource extends BaseFileSource {
27
+ /**
28
+ * Default dirs to check for configuration files
29
+ */
30
+ protected CONFIG_DIRS: string[];
31
+ Load(): Promise<any>;
32
+ }
package/lib/sources.js ADDED
@@ -0,0 +1,161 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
15
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
16
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
17
+ 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;
18
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
19
+ };
20
+ var __importStar = (this && this.__importStar) || function (mod) {
21
+ if (mod && mod.__esModule) return mod;
22
+ var result = {};
23
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
24
+ __setModuleDefault(result, mod);
25
+ return result;
26
+ };
27
+ Object.defineProperty(exports, "__esModule", { value: true });
28
+ exports.JsonFileSource = exports.JsFileSource = exports.BaseFileSource = exports.ConfigurationSource = void 0;
29
+ const di_1 = require("@spinajs/di");
30
+ const glob_1 = require("glob");
31
+ const _ = require("lodash");
32
+ const path_1 = require("path");
33
+ const util_1 = require("./util");
34
+ const fs = __importStar(require("fs"));
35
+ const path = __importStar(require("path"));
36
+ function mergeArrays(target, source) {
37
+ if (_.isArray(target)) {
38
+ return target.concat(source);
39
+ }
40
+ }
41
+ const DEFAULT_CONFIG_DIRS = [
42
+ // this module path
43
+ (0, path_1.normalize)((0, path_1.join)((0, path_1.resolve)(__dirname), '/../config')),
44
+ // for tests, in src dir
45
+ (0, path_1.normalize)((0, path_1.join)((0, path_1.resolve)(__dirname), '/config')),
46
+ // other @spinajs modules paths
47
+ 'node_modules/@spinajs/*/lib/config',
48
+ // project paths - last to allow overwrite @spinajs conf
49
+ 'lib/config',
50
+ 'dist/config',
51
+ 'build/config',
52
+ 'config',
53
+ ];
54
+ class ConfigurationSource {
55
+ }
56
+ exports.ConfigurationSource = ConfigurationSource;
57
+ class BaseFileSource extends ConfigurationSource {
58
+ constructor(RunApp, CustomConfigPaths, appBaseDir) {
59
+ super();
60
+ this.RunApp = RunApp;
61
+ this.CustomConfigPaths = CustomConfigPaths;
62
+ this.appBaseDir = appBaseDir;
63
+ /**
64
+ * Configuration base dir, where to look for app config
65
+ */
66
+ this.BaseDir = './';
67
+ /**
68
+ * Default dirs to check for configuration files
69
+ */
70
+ this.CONFIG_DIRS = [];
71
+ }
72
+ load(extension, callback) {
73
+ const config = {};
74
+ let dirs = this.CONFIG_DIRS;
75
+ const basePath = (0, util_1.findBasePath)(process.cwd());
76
+ if (this.RunApp) {
77
+ dirs = dirs.concat([(0, path_1.join)(this.appBaseDir, `/${this.RunApp}/config`)]);
78
+ }
79
+ if (this.CustomConfigPaths) {
80
+ dirs = dirs.concat(this.CustomConfigPaths);
81
+ }
82
+ dirs.map(f => path.isAbsolute(f) ? f : (0, path_1.join)(basePath, f))
83
+ // get all config files
84
+ .map(d => glob_1.glob.sync(path.join(d, `/**/${extension}`)))
85
+ // flatten files
86
+ .reduce((prev, current) => {
87
+ return prev.concat(_.flattenDeep(current));
88
+ }, [])
89
+ // normalize & resolve paths to be sure
90
+ .map((f) => (0, path_1.normalize)((0, path_1.resolve)(f)))
91
+ .map(callback)
92
+ .filter((v) => v !== null)
93
+ // load & merge configs
94
+ .map((c) => _.mergeWith(config, c, mergeArrays));
95
+ return config;
96
+ }
97
+ }
98
+ exports.BaseFileSource = BaseFileSource;
99
+ let JsFileSource = class JsFileSource extends BaseFileSource {
100
+ constructor() {
101
+ super(...arguments);
102
+ /**
103
+ * Default dirs to check for configuration files
104
+ */
105
+ this.CONFIG_DIRS = DEFAULT_CONFIG_DIRS;
106
+ }
107
+ async Load() {
108
+ const common = this.load('!(*.dev|*.prod).js', _load);
109
+ if (process.env.NODE_ENV) {
110
+ if (process.env.NODE_ENV === "development") {
111
+ return _.mergeWith(common, this.load("*.dev.js", _load), mergeArrays);
112
+ }
113
+ else if (process.env.NODE_ENV === "production") {
114
+ return _.mergeWith(common, this.load("*.prod.js", _load), mergeArrays);
115
+ }
116
+ }
117
+ return common;
118
+ function _load(file) {
119
+ (0, util_1.uncache)(file);
120
+ return require(file);
121
+ }
122
+ }
123
+ };
124
+ JsFileSource = __decorate([
125
+ (0, di_1.Injectable)(ConfigurationSource)
126
+ ], JsFileSource);
127
+ exports.JsFileSource = JsFileSource;
128
+ let JsonFileSource = class JsonFileSource extends BaseFileSource {
129
+ constructor() {
130
+ super(...arguments);
131
+ /**
132
+ * Default dirs to check for configuration files
133
+ */
134
+ this.CONFIG_DIRS = DEFAULT_CONFIG_DIRS;
135
+ }
136
+ async Load() {
137
+ const common = this.load('!(*.dev|*.prod).json', _load);
138
+ if (process.env.NODE_ENV) {
139
+ if (process.env.NODE_ENV === "development") {
140
+ return _.mergeWith(common, this.load("*.dev.json", _load), mergeArrays);
141
+ }
142
+ else if (process.env.NODE_ENV === "production") {
143
+ return _.mergeWith(common, this.load("*.prod.json", _load), mergeArrays);
144
+ }
145
+ }
146
+ return common;
147
+ function _load(file) {
148
+ try {
149
+ return JSON.parse(fs.readFileSync(file, "utf-8"));
150
+ }
151
+ catch (err) {
152
+ return null;
153
+ }
154
+ }
155
+ }
156
+ };
157
+ JsonFileSource = __decorate([
158
+ (0, di_1.Injectable)(ConfigurationSource)
159
+ ], JsonFileSource);
160
+ exports.JsonFileSource = JsonFileSource;
161
+ //# sourceMappingURL=sources.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sources.js","sourceRoot":"","sources":["../src/sources.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,oCAAyC;AACzC,+BAA4B;AAC5B,4BAA6B;AAC7B,+BAAgD;AAChD,iCAA+C;AAC/C,uCAAyB;AACzB,2CAA6B;AAG7B,SAAS,WAAW,CAAC,MAAW,EAAE,MAAW;IACzC,IAAI,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE;QACnB,OAAO,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;KAChC;AACL,CAAC;AAED,MAAM,mBAAmB,GAAG;IACxB,mBAAmB;IACnB,IAAA,gBAAS,EAAC,IAAA,WAAI,EAAC,IAAA,cAAO,EAAC,SAAS,CAAC,EAAE,YAAY,CAAC,CAAC;IAEjD,wBAAwB;IACxB,IAAA,gBAAS,EAAC,IAAA,WAAI,EAAC,IAAA,cAAO,EAAC,SAAS,CAAC,EAAE,SAAS,CAAC,CAAC;IAG9C,+BAA+B;IAC/B,oCAAoC;IAGpC,wDAAwD;IACxD,YAAY;IACZ,aAAa;IACb,cAAc;IACd,QAAQ;CACX,CAAC;AAEF,MAAsB,mBAAmB;CAExC;AAFD,kDAEC;AAED,MAAsB,cAAe,SAAQ,mBAAmB;IAW5D,YAAsB,MAAe,EAAY,iBAA4B,EAAY,UAAmB;QACxG,KAAK,EAAE,CAAC;QADU,WAAM,GAAN,MAAM,CAAS;QAAY,sBAAiB,GAAjB,iBAAiB,CAAW;QAAY,eAAU,GAAV,UAAU,CAAS;QAV5G;;WAEG;QACI,YAAO,GAAW,IAAI,CAAC;QAE9B;;WAEG;QACO,gBAAW,GAAa,EAAE,CAAC;IAIrC,CAAC;IAES,IAAI,CAAC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D,CAAC;IAtBU,KAAK,CAAC,IAAI;QACb,MAAM,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,sBAAsB,EAAE,KAAK,CAAC,CAAC;QAExD,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,EAAE;YACtB,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,aAAa,EAAE;gBACxC,OAAO,CAAC,CAAC,SAAS,CAAC,MAAM,EAAE,IAAI,CAAC,IAAI,CAAC,YAAY,EAAE,KAAK,CAAC,EAAE,WAAW,CAAC,CAAC;aAC3E;iBAAM,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY,EAAE;gBAC9C,OAAO,CAAC,CAAC,SAAS,CAAC,MAAM,EAAE,IAAI,CAAC,IAAI,CAAC,aAAa,EAAE,KAAK,CAAC,EAAE,WAAW,CAAC,CAAC;aAC5E;SACJ;QAED,OAAO,MAAM,CAAC;QAEd,SAAS,KAAK,CAAC,IAAY;YAEvB,IAAI;gBACA,OAAO,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,YAAY,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC;aACrD;YAAC,OAAO,GAAG,EAAE;gBACV,OAAO,IAAI,CAAC;aACf;QACL,CAAC;IACL,CAAC;CACJ,CAAA;AA7BY,cAAc;IAD1B,IAAA,eAAU,EAAC,mBAAmB,CAAC;GACnB,cAAc,CA6B1B;AA7BY,wCAAc"}
package/lib/types.d.ts ADDED
@@ -0,0 +1,54 @@
1
+ import { AsyncModule } from '@spinajs/di';
2
+ /**
3
+ * App version struct
4
+ */
5
+ export interface IFrameworkVersion {
6
+ minor: number;
7
+ major: number;
8
+ }
9
+ export interface ConfigurationOptions {
10
+ /**
11
+ * application name, pass it when you run in application mode
12
+ */
13
+ app?: string;
14
+ /**
15
+ * configuration base dir, where to look for application configs
16
+ */
17
+ appBaseDir?: string;
18
+ /**
19
+ * custom cfg paths eg. to load config from non standard folders ( usefull in tests )
20
+ */
21
+ cfgCustomPaths?: string[];
22
+ /**
23
+ * Should watch for changes in config files
24
+ */
25
+ watchFileChanges?: boolean;
26
+ }
27
+ export declare abstract class Configuration extends AsyncModule {
28
+ /**
29
+ * Configuration base dir, where to look for app config
30
+ */
31
+ BaseDir: string;
32
+ /**
33
+ * Current running app name
34
+ */
35
+ RunApp: string;
36
+ /**
37
+ * Apps configuration base dir, where to look for app config
38
+ */
39
+ AppBaseDir: string;
40
+ /**
41
+ * Get config value for given property. Returns any if value is present, default value if provided or null when empty
42
+ *
43
+ * @param path - path to property eg. ["system","dirs"] or "system" or "system.dirs"
44
+ */
45
+ abstract get<T>(path: string[] | string, defaultValue?: T): T;
46
+ /**
47
+ * Sets at given path configuration value. Use when you want to override config
48
+ * loaded from files programatically
49
+ *
50
+ * @param path config path
51
+ * @param value value to set
52
+ */
53
+ abstract set(path: string[] | string, value: any): void;
54
+ }
package/lib/types.js ADDED
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Configuration = void 0;
4
+ const di_1 = require("@spinajs/di");
5
+ class Configuration extends di_1.AsyncModule {
6
+ }
7
+ exports.Configuration = Configuration;
8
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";;;AAAA,oCAA0C;AAgC1C,MAAsB,aAAc,SAAQ,gBAAW;CAgCtD;AAhCD,sCAgCC"}
package/lib/util.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ export declare function parseArgv(param: string): string;
2
+ export declare function findBasePath(path: string): string;
3
+ export declare function merge(to: any, from: any): void;
4
+ export declare function uncache(file: string): string;
5
+ export declare function filterDirs(dir: string): boolean;
package/lib/util.js ADDED
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ exports.filterDirs = exports.uncache = exports.merge = exports.findBasePath = exports.parseArgv = void 0;
23
+ const fs = __importStar(require("fs"));
24
+ const _ = require("lodash");
25
+ const path_1 = require("path");
26
+ function parseArgv(param) {
27
+ const index = process.argv.indexOf(param);
28
+ if (index === -1 || process.argv.length <= index + 1) {
29
+ return undefined;
30
+ }
31
+ return process.argv[index + 1];
32
+ }
33
+ exports.parseArgv = parseArgv;
34
+ function findBasePath(path) {
35
+ if (fs.existsSync((0, path_1.join)(path, 'node_modules'))) {
36
+ return path;
37
+ }
38
+ return findBasePath((0, path_1.resolve)(path, '..'));
39
+ }
40
+ exports.findBasePath = findBasePath;
41
+ function merge(to, from) {
42
+ _.mergeWith(to, from, (src, dest) => {
43
+ if (_.isArray(src) && _.isArray(dest)) {
44
+ const tmp = src.concat(dest);
45
+ return _.uniqWith(tmp, _.isEqual);
46
+ }
47
+ else if (!src) {
48
+ return dest;
49
+ }
50
+ });
51
+ return to;
52
+ }
53
+ exports.merge = merge;
54
+ // clean require cache config
55
+ // http://stackoverflow.com/questions/9210542/node-js-require-cache-possible-to-invalidate
56
+ function uncache(file) {
57
+ delete require.cache[file];
58
+ return file;
59
+ }
60
+ exports.uncache = uncache;
61
+ function filterDirs(dir) {
62
+ if (fs.existsSync(dir)) {
63
+ return true;
64
+ }
65
+ return false;
66
+ }
67
+ exports.filterDirs = filterDirs;
68
+ //# sourceMappingURL=util.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"util.js","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA,uCAAyB;AACzB,4BAA6B;AAC7B,+BAAqC;AAOrC,SAAgB,SAAS,CAAC,KAAa;IACnC,MAAM,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;IAE1C,IAAI,KAAK,KAAK,CAAC,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,MAAM,IAAI,KAAK,GAAG,CAAC,EAAE;QAClD,OAAO,SAAS,CAAC;KACpB;IAED,OAAO,OAAO,CAAC,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC;AACnC,CAAC;AARD,8BAQC;AAED,SAAgB,YAAY,CAAC,IAAY;IACrC,IAAI,EAAE,CAAC,UAAU,CAAC,IAAA,WAAI,EAAC,IAAI,EAAE,cAAc,CAAC,CAAC,EAAE;QAC3C,OAAO,IAAI,CAAC;KACf;IAED,OAAO,YAAY,CAAC,IAAA,cAAO,EAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;AAC7C,CAAC;AAND,oCAMC;AAED,SAAgB,KAAK,CAAC,EAAO,EAAE,IAAS;IACpC,CAAC,CAAC,SAAS,CAAC,EAAE,EAAE,IAAI,EAAE,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE;QAChC,IAAI,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE;YACnC,MAAM,GAAG,GAAG,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;YAC7B,OAAO,CAAC,CAAC,QAAQ,CAAC,GAAG,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC;SACrC;aAAM,IAAI,CAAC,GAAG,EAAE;YACb,OAAO,IAAI,CAAC;SACf;IACL,CAAC,CAAC,CAAC;IAEH,OAAO,EAAE,CAAC;AACd,CAAC;AAXD,sBAWC;AAGD,6BAA6B;AAC7B,0FAA0F;AAC1F,SAAgB,OAAO,CAAC,IAAY;IAChC,OAAO,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAC3B,OAAO,IAAI,CAAC;AAChB,CAAC;AAHD,0BAGC;AAED,SAAgB,UAAU,CAAC,GAAW;IAClC,IAAI,EAAE,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE;QACpB,OAAO,IAAI,CAAC;KACf;IACD,OAAO,KAAK,CAAC;AACjB,CAAC;AALD,gCAKC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spinajs/configuration",
3
- "version": "1.0.15",
3
+ "version": "1.1.4",
4
4
  "description": "framework configuration module",
5
5
  "main": "lib/index.js",
6
6
  "private": false,
@@ -9,6 +9,7 @@
9
9
  "coverage": "nyc npm run test",
10
10
  "build-docs": "rimraf docs && typedoc --options typedoc.json src/",
11
11
  "build": "tsc",
12
+ "build-watch": "tsc -w",
12
13
  "prepare": "npm run build",
13
14
  "format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
14
15
  "lint": "tslint -p tsconfig.json",
@@ -20,6 +21,7 @@
20
21
  "files": [
21
22
  "lib/**/*"
22
23
  ],
24
+ "types": "lib",
23
25
  "repository": {
24
26
  "type": "git",
25
27
  "url": "git+https://github.com/spinajs/configuration.git"
@@ -37,7 +39,9 @@
37
39
  },
38
40
  "homepage": "https://github.com/spinajs/configuration#readme",
39
41
  "dependencies": {
40
- "@spinajs/di": "^1.0.16",
42
+ "@spinajs/di": "^1.1.7",
43
+ "@spinajs/exceptions": "^1.0.5",
44
+ "@spinajs/log": "^1.1.0",
41
45
  "glob": "^7.1.4",
42
46
  "lodash": "^4.17.14"
43
47
  },
@@ -45,23 +49,24 @@
45
49
  "@types/bunyan": "^1.8.6",
46
50
  "@types/chai": "^4.1.7",
47
51
  "@types/chai-as-promised": "^7.1.0",
52
+ "@types/glob": "^7.2.0",
48
53
  "@types/lodash": "^4.14.136",
49
- "@types/mocha": "^5.2.7",
50
- "@types/sinon": "^7.0.13",
54
+ "@types/mocha": "^9.1.0",
55
+ "@types/sinon": "^10.0.8",
51
56
  "chai": "^4.2.0",
52
57
  "chai-as-promised": "^7.1.1",
53
- "mocha": "^6.1.4",
54
- "nyc": "^14.1.1",
55
- "prettier": "^1.18.2",
56
- "sinon": "^7.3.2",
57
- "ts-mocha": "^6.0.0",
58
- "ts-node": "^8.3.0",
59
- "tslint": "^5.20.1",
58
+ "mocha": "^9.2.0",
59
+ "nyc": "^15.1.0",
60
+ "prettier": "^2.5.1",
61
+ "sinon": "^12.0.1",
62
+ "ts-mocha": "^9.0.2",
63
+ "ts-node": "^10.4.0",
64
+ "tslint": "^6.1.3",
60
65
  "tslint-circular-dependencies": "^0.1.0",
61
66
  "tslint-config-prettier": "^1.18.0",
62
- "tslint-config-standard": "^8.0.1",
67
+ "tslint-config-standard": "^9.0.0",
63
68
  "tslint-no-unused-expression-chai": "^0.1.4",
64
- "typedoc": "^0.14.2",
65
- "typescript": "^3.7.3"
69
+ "typedoc": "^0.22.11",
70
+ "typescript": "^4.2.3"
66
71
  }
67
72
  }