@nattyjs/common 0.0.1-beta.10 → 0.0.1-beta.12

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/dist/index.cjs CHANGED
@@ -4,6 +4,8 @@ const fs = require('fs');
4
4
  const dotenv = require('dotenv');
5
5
  const dotenvExpand = require('dotenv-expand');
6
6
  const path = require('path');
7
+ const child_process = require('child_process');
8
+ const getPortPlease = require('get-port-please');
7
9
 
8
10
  function _interopNamespaceCompat(e) {
9
11
  if (e && typeof e === 'object' && 'default' in e) return e;
@@ -28,8 +30,11 @@ const commonContainer = new class {
28
30
  this.types = {};
29
31
  }
30
32
  registerType(type) {
31
- for (const key of Object.keys(type))
32
- this.types[key] = type[key];
33
+ if (type) {
34
+ if (typeof type === "string")
35
+ type = JSON.parse(type);
36
+ this.types[type.name] = type;
37
+ }
33
38
  }
34
39
  setupCliConfig(config) {
35
40
  this.nattyCliConfig = config;
@@ -233,20 +238,29 @@ function getPath(pathCollection, isIncludeRoot = true, isCreateFolder = false) {
233
238
  return currentPath;
234
239
  }
235
240
 
241
+ function resolvePath(path$1) {
242
+ return path.resolve(commonContainer.buildOptions.rootDir, path$1);
243
+ }
244
+
236
245
  async function readEnv() {
237
- let filePath = getPath([ENVIRONMENTS, commonContainer.buildOptions.mode && commonContainer.buildOptions.mode !== "dev" ? `.env.${commonContainer.buildOptions.mode}` : `.env`]);
246
+ const envConfig = commonContainer.nattyCliConfig.env;
247
+ let filePath = envConfig?.dictionary ? void 0 : getPath([ENVIRONMENTS, commonContainer.buildOptions.mode && commonContainer.buildOptions.mode !== "dev" ? `.env.${commonContainer.buildOptions.mode}` : `.env`]);
248
+ if (envConfig && envConfig.path)
249
+ filePath = resolvePath(envConfig.path);
238
250
  let parsedEnvTsDefinition = {};
239
- if (fs.existsSync(filePath)) {
251
+ let parsedEnv = commonContainer.nattyCliConfig.env?.dictionary;
252
+ if (!parsedEnv && filePath && fs.existsSync(filePath)) {
240
253
  const { parsed, error } = dotenv__namespace.config({
241
254
  debug: !!process.env.DEBUG || void 0,
242
255
  path: filePath
243
256
  });
244
- const info = getEnvTsDefinition(parsed);
245
- parsedEnvTsDefinition = info.parsedEnvTsDefinition;
246
- commonContainer.setEnvTsDefinition(parsedEnvTsDefinition);
247
- commonContainer.setEnvValueInfo(info.envVariableValueInfo);
248
- dotenvExpand__namespace.expand({ parsed });
257
+ parsedEnv = parsed;
249
258
  }
259
+ const info = getEnvTsDefinition(parsedEnv);
260
+ parsedEnvTsDefinition = info.parsedEnvTsDefinition;
261
+ commonContainer.setEnvTsDefinition(parsedEnvTsDefinition);
262
+ commonContainer.setEnvValueInfo(info.envVariableValueInfo);
263
+ dotenvExpand__namespace.expand({ parsed: parsedEnv });
250
264
  return parsedEnvTsDefinition;
251
265
  }
252
266
 
@@ -587,7 +601,53 @@ function registerType(type) {
587
601
  commonContainer.registerType(type);
588
602
  }
589
603
 
604
+ class AbstractRunner {
605
+ async exec(command, args, cwd = process.cwd()) {
606
+ const options = {
607
+ cwd,
608
+ stdio: "pipe",
609
+ shell: true
610
+ };
611
+ return new Promise((resolve, reject) => {
612
+ const child = child_process.spawn(
613
+ `${command}`,
614
+ [...args],
615
+ options
616
+ );
617
+ child.stdout.on(
618
+ "data",
619
+ (data) => {
620
+ const writeData = data.toString().replace(/\r\n|\n/, "");
621
+ if (writeData.indexOf("[NATTYJS]") > -1) {
622
+ console.log(writeData);
623
+ }
624
+ }
625
+ );
626
+ child.on("close", (code) => {
627
+ if (code === 0) {
628
+ resolve(null);
629
+ } else {
630
+ console.error(
631
+ `${command} ${args}`
632
+ );
633
+ reject();
634
+ }
635
+ });
636
+ });
637
+ }
638
+ }
639
+
640
+ async function getPort() {
641
+ const portNumber = commonContainer.nattyConfig?.port || 3200;
642
+ if (portNumber)
643
+ return portNumber;
644
+ const port = await getPortPlease.getPort({ ports: [portNumber, ...Array(50).fill(3001).map((fillValue, index) => fillValue + index)] });
645
+ commonContainer.nattyConfig.port = port;
646
+ return port;
647
+ }
648
+
590
649
  exports.ALLOW_METHODS = ALLOW_METHODS;
650
+ exports.AbstractRunner = AbstractRunner;
591
651
  exports.ActionFilter = ActionFilter;
592
652
  exports.AuthenticationFilter = AuthenticationFilter;
593
653
  exports.AuthorizationFilter = AuthorizationFilter;
@@ -618,6 +678,7 @@ exports.commonContainer = commonContainer;
618
678
  exports.createPath = createPath;
619
679
  exports.createTestServer = createTestServer;
620
680
  exports.getPath = getPath;
681
+ exports.getPort = getPort;
621
682
  exports.isConstructor = isConstructor;
622
683
  exports.isEqual = isEqual;
623
684
  exports.isFunction = isFunction;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { RequestRouteInfo, IHttpResult, IModelBindingContext, ProblemDetail, IExceptionContext, HttpResponseInit, NattyTestModule, ModelBinding, NattyCliConfig, BuildOptions, TypesInfo, ClassTypeInfo } from '@nattyjs/types';
1
+ import { RequestRouteInfo, IHttpResult, IModelBindingContext, ProblemDetail, IExceptionContext, HttpResponseInit, NattyTestModule, ModelBinding, BuildOptions, TypesInfo, ClassTypeInfo } from '@nattyjs/types';
2
2
 
3
3
  interface ClassType<T> extends Function {
4
4
  new (...args: any[]): T;
@@ -53,6 +53,14 @@ interface GlobalConfig {
53
53
  onException?: ClassType<ExceptionFilter>;
54
54
  }
55
55
 
56
+ interface CorsConfig {
57
+ origin: string[];
58
+ methods: string;
59
+ preflightContinue: boolean;
60
+ optionsSuccessStatus: number;
61
+ credentials: boolean;
62
+ }
63
+
56
64
  interface NattyConfig {
57
65
  app: any;
58
66
  testModule?: NattyTestModule;
@@ -62,6 +70,35 @@ interface NattyConfig {
62
70
  modelBinding?: ModelBinding;
63
71
  global?: GlobalConfig;
64
72
  autoGeneratePort?: boolean;
73
+ port?: number;
74
+ cors?: CorsConfig;
75
+ }
76
+
77
+ declare abstract class AbstractRunner {
78
+ abstract run(): void;
79
+ protected exec(command: string, args: any[], cwd?: string): Promise<null | string>;
80
+ }
81
+
82
+ interface EnvConfig {
83
+ path: string;
84
+ dictionary: {
85
+ [key: string]: any;
86
+ };
87
+ }
88
+
89
+ interface PackageJsonConfig {
90
+ addExports: string[];
91
+ }
92
+
93
+ interface LibraryConfig {
94
+ packageJson: PackageJsonConfig;
95
+ }
96
+
97
+ interface NattyCliConfig {
98
+ library?: Partial<LibraryConfig>;
99
+ env?: Partial<EnvConfig>;
100
+ runner?: ClassType<AbstractRunner>;
101
+ port?: number;
65
102
  }
66
103
 
67
104
  declare const commonContainer: {
@@ -87,6 +124,7 @@ declare const commonContainer: {
87
124
  getMetadataValue(key: string, propName: string): any;
88
125
  get globalConfig(): GlobalConfig;
89
126
  registerType(type: TypesInfo): void;
127
+ types: TypesInfo;
90
128
  };
91
129
 
92
130
  declare const typeContainer: {
@@ -224,4 +262,6 @@ interface NattyAppConfig extends NattyConfig {
224
262
 
225
263
  declare function registerType(type: TypesInfo): void;
226
264
 
227
- export { ALLOW_METHODS, ActionFilter, AuthenticationFilter, AuthorizationFilter, BACK_SLASH_REGEX, BLANK, CONTROLLER, Claim, ClassType, DEFAULT_ACTIONS, DEFAULT_CHILD_PATH, DELETE, ENVIRONMENTS, ExceptionFilter, FrameworkType, GET, GlobalConfig, HTTP_METHOD_ROUTES, IActionExecutedContext, IActionExecutingContext, IExecutionContext, IGNORE_METHODS, List, MetaConfigProps, Middleware, NattyAppConfig, NattyConfig, POST, PUT, RIGHT_SLASH, ROUTE_INSTANCES, ROUTE_METHODS, ROUTE_PATHS, TS_EXTENSION, UserIdentity, commonContainer, createPath, createTestServer, getPath, isConstructor, isEqual, isFunction, isObject, readEnv, readEnvKey, registerType, typeContainer };
265
+ declare function getPort(): Promise<number>;
266
+
267
+ export { ALLOW_METHODS, AbstractRunner, ActionFilter, AuthenticationFilter, AuthorizationFilter, BACK_SLASH_REGEX, BLANK, CONTROLLER, Claim, ClassType, DEFAULT_ACTIONS, DEFAULT_CHILD_PATH, DELETE, ENVIRONMENTS, ExceptionFilter, FrameworkType, GET, GlobalConfig, HTTP_METHOD_ROUTES, IActionExecutedContext, IActionExecutingContext, IExecutionContext, IGNORE_METHODS, List, MetaConfigProps, Middleware, NattyAppConfig, NattyCliConfig, NattyConfig, POST, PUT, RIGHT_SLASH, ROUTE_INSTANCES, ROUTE_METHODS, ROUTE_PATHS, TS_EXTENSION, UserIdentity, commonContainer, createPath, createTestServer, getPath, getPort, isConstructor, isEqual, isFunction, isObject, readEnv, readEnvKey, registerType, typeContainer };
package/dist/index.mjs CHANGED
@@ -3,6 +3,9 @@ import { existsSync } from 'fs';
3
3
  import * as dotenv from 'dotenv';
4
4
  import * as dotenvExpand from 'dotenv-expand';
5
5
  import * as path from 'path';
6
+ import { resolve } from 'path';
7
+ import { spawn } from 'child_process';
8
+ import { getPort as getPort$1 } from 'get-port-please';
6
9
 
7
10
  const commonContainer = new class {
8
11
  constructor() {
@@ -10,8 +13,11 @@ const commonContainer = new class {
10
13
  this.types = {};
11
14
  }
12
15
  registerType(type) {
13
- for (const key of Object.keys(type))
14
- this.types[key] = type[key];
16
+ if (type) {
17
+ if (typeof type === "string")
18
+ type = JSON.parse(type);
19
+ this.types[type.name] = type;
20
+ }
15
21
  }
16
22
  setupCliConfig(config) {
17
23
  this.nattyCliConfig = config;
@@ -215,20 +221,29 @@ function getPath(pathCollection, isIncludeRoot = true, isCreateFolder = false) {
215
221
  return currentPath;
216
222
  }
217
223
 
224
+ function resolvePath(path) {
225
+ return resolve(commonContainer.buildOptions.rootDir, path);
226
+ }
227
+
218
228
  async function readEnv() {
219
- let filePath = getPath([ENVIRONMENTS, commonContainer.buildOptions.mode && commonContainer.buildOptions.mode !== "dev" ? `.env.${commonContainer.buildOptions.mode}` : `.env`]);
229
+ const envConfig = commonContainer.nattyCliConfig.env;
230
+ let filePath = envConfig?.dictionary ? void 0 : getPath([ENVIRONMENTS, commonContainer.buildOptions.mode && commonContainer.buildOptions.mode !== "dev" ? `.env.${commonContainer.buildOptions.mode}` : `.env`]);
231
+ if (envConfig && envConfig.path)
232
+ filePath = resolvePath(envConfig.path);
220
233
  let parsedEnvTsDefinition = {};
221
- if (existsSync(filePath)) {
234
+ let parsedEnv = commonContainer.nattyCliConfig.env?.dictionary;
235
+ if (!parsedEnv && filePath && existsSync(filePath)) {
222
236
  const { parsed, error } = dotenv.config({
223
237
  debug: !!process.env.DEBUG || void 0,
224
238
  path: filePath
225
239
  });
226
- const info = getEnvTsDefinition(parsed);
227
- parsedEnvTsDefinition = info.parsedEnvTsDefinition;
228
- commonContainer.setEnvTsDefinition(parsedEnvTsDefinition);
229
- commonContainer.setEnvValueInfo(info.envVariableValueInfo);
230
- dotenvExpand.expand({ parsed });
240
+ parsedEnv = parsed;
231
241
  }
242
+ const info = getEnvTsDefinition(parsedEnv);
243
+ parsedEnvTsDefinition = info.parsedEnvTsDefinition;
244
+ commonContainer.setEnvTsDefinition(parsedEnvTsDefinition);
245
+ commonContainer.setEnvValueInfo(info.envVariableValueInfo);
246
+ dotenvExpand.expand({ parsed: parsedEnv });
232
247
  return parsedEnvTsDefinition;
233
248
  }
234
249
 
@@ -569,4 +584,49 @@ function registerType(type) {
569
584
  commonContainer.registerType(type);
570
585
  }
571
586
 
572
- export { ALLOW_METHODS, ActionFilter, AuthenticationFilter, AuthorizationFilter, BACK_SLASH_REGEX, BLANK, CONTROLLER, DEFAULT_ACTIONS, DEFAULT_CHILD_PATH, DELETE, ENVIRONMENTS, ExceptionFilter, FrameworkType, GET, HTTP_METHOD_ROUTES, IGNORE_METHODS, List, MetaConfigProps, Middleware, POST, PUT, RIGHT_SLASH, ROUTE_INSTANCES, ROUTE_METHODS, ROUTE_PATHS, TS_EXTENSION, UserIdentity, commonContainer, createPath, createTestServer, getPath, isConstructor, isEqual, isFunction, isObject, readEnv, readEnvKey, registerType, typeContainer };
587
+ class AbstractRunner {
588
+ async exec(command, args, cwd = process.cwd()) {
589
+ const options = {
590
+ cwd,
591
+ stdio: "pipe",
592
+ shell: true
593
+ };
594
+ return new Promise((resolve, reject) => {
595
+ const child = spawn(
596
+ `${command}`,
597
+ [...args],
598
+ options
599
+ );
600
+ child.stdout.on(
601
+ "data",
602
+ (data) => {
603
+ const writeData = data.toString().replace(/\r\n|\n/, "");
604
+ if (writeData.indexOf("[NATTYJS]") > -1) {
605
+ console.log(writeData);
606
+ }
607
+ }
608
+ );
609
+ child.on("close", (code) => {
610
+ if (code === 0) {
611
+ resolve(null);
612
+ } else {
613
+ console.error(
614
+ `${command} ${args}`
615
+ );
616
+ reject();
617
+ }
618
+ });
619
+ });
620
+ }
621
+ }
622
+
623
+ async function getPort() {
624
+ const portNumber = commonContainer.nattyConfig?.port || 3200;
625
+ if (portNumber)
626
+ return portNumber;
627
+ const port = await getPort$1({ ports: [portNumber, ...Array(50).fill(3001).map((fillValue, index) => fillValue + index)] });
628
+ commonContainer.nattyConfig.port = port;
629
+ return port;
630
+ }
631
+
632
+ export { ALLOW_METHODS, AbstractRunner, ActionFilter, AuthenticationFilter, AuthorizationFilter, BACK_SLASH_REGEX, BLANK, CONTROLLER, DEFAULT_ACTIONS, DEFAULT_CHILD_PATH, DELETE, ENVIRONMENTS, ExceptionFilter, FrameworkType, GET, HTTP_METHOD_ROUTES, IGNORE_METHODS, List, MetaConfigProps, Middleware, POST, PUT, RIGHT_SLASH, ROUTE_INSTANCES, ROUTE_METHODS, ROUTE_PATHS, TS_EXTENSION, UserIdentity, commonContainer, createPath, createTestServer, getPath, getPort, isConstructor, isEqual, isFunction, isObject, readEnv, readEnvKey, registerType, typeContainer };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nattyjs/common",
3
- "version": "0.0.1-beta.10",
3
+ "version": "0.0.1-beta.12",
4
4
  "description": "Now I’m the model of a modern major general / The venerated Virginian veteran whose men are all / Lining up, to put me up on a pedestal / Writin’ letters to relatives / Embellishin’ my elegance and eloquence / But the elephant is in the room / The truth is in ya face when ya hear the British cannons go / BOOM",
5
5
  "keywords": [],
6
6
  "author": "ajayojha <ojhaajay@outlook.com>",
@@ -16,11 +16,12 @@
16
16
  },
17
17
  "dependencies": {
18
18
  "dotenv": "16.3.1",
19
- "dotenv-expand": "10.0.0"
19
+ "dotenv-expand": "10.0.0",
20
+ "get-port-please": "3.1.1"
20
21
  },
21
22
  "devDependencies": {
22
23
  "@types/node": "20.3.1",
23
- "@nattyjs/types": "0.0.1-beta.10",
24
+ "@nattyjs/types": "0.0.1-beta.12",
24
25
  "unbuild": "1.2.1"
25
26
  }
26
27
  }