@swizzyweb/swerve-manager 0.1.3
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/LICENSE +202 -0
- package/README.md +219 -0
- package/dist/config/config-parser.d.ts +17 -0
- package/dist/config/config-parser.js +43 -0
- package/dist/config/config.d.ts +15 -0
- package/dist/config/config.js +1 -0
- package/dist/config/index.d.ts +2 -0
- package/dist/config/index.js +2 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/swerve.d.ts +82 -0
- package/dist/swerve.js +278 -0
- package/dist/utils/getArgs.d.ts +12 -0
- package/dist/utils/getArgs.js +208 -0
- package/dist/utils/getPackageJson.d.ts +14 -0
- package/dist/utils/getPackageJson.js +78 -0
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.js +2 -0
- package/dist/utils/installWebservice.d.ts +4 -0
- package/dist/utils/installWebservice.js +87 -0
- package/jest.config.js +4 -0
- package/package.json +50 -0
- package/src/config/config-parser.ts +56 -0
- package/src/config/config.ts +15 -0
- package/src/config/index.ts +2 -0
- package/src/index.ts +3 -0
- package/src/swerve.ts +418 -0
- package/src/utils/getArgs.ts +256 -0
- package/src/utils/getPackageJson.ts +90 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/installWebservice.ts +131 -0
- package/test/config/config.spec.ts +23 -0
- package/test/config/serviceConfig.json +19 -0
- package/test/getArgs.spec.ts +331 -0
- package/tsconfig.json +109 -0
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
// @ts-ignore
|
|
2
|
+
import { ILogger } from "@swizzyweb/swizzy-common";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import process from "node:process";
|
|
5
|
+
import { IConfig, IService, KeyValue } from "../config/index.js";
|
|
6
|
+
import { SwerveConfigParser } from "../config/config-parser.js";
|
|
7
|
+
import { deepMerge } from "@swizzyweb/swizzy-common";
|
|
8
|
+
import { getPackageJson } from "./getPackageJson.js";
|
|
9
|
+
|
|
10
|
+
function getHelpText() {
|
|
11
|
+
return `Help --
|
|
12
|
+
npm run server <serviceName> <port (optional)>
|
|
13
|
+
`;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function getServiceNameFromCurrentDirPackage(logger: ILogger<any>) {
|
|
17
|
+
try {
|
|
18
|
+
return process.cwd();
|
|
19
|
+
} catch (e) {
|
|
20
|
+
logger.debug(`Error getting package from current dir package.json ${e}`);
|
|
21
|
+
throw e;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function getAppDataRoot(
|
|
26
|
+
appDataRootPath: string | undefined,
|
|
27
|
+
logger: ILogger<any>,
|
|
28
|
+
) {
|
|
29
|
+
try {
|
|
30
|
+
let directory;
|
|
31
|
+
if (!appDataRootPath || appDataRootPath === ".") {
|
|
32
|
+
directory = getServiceNameFromCurrentDirPackage(logger);
|
|
33
|
+
} else {
|
|
34
|
+
directory = appDataRootPath;
|
|
35
|
+
}
|
|
36
|
+
return directory;
|
|
37
|
+
} catch (e) {
|
|
38
|
+
throw {
|
|
39
|
+
message: `Unable to get app data root ${appDataRootPath}`,
|
|
40
|
+
exception: e,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function getService(serviceName: string | undefined, logger: ILogger<any>) {
|
|
46
|
+
try {
|
|
47
|
+
let directory;
|
|
48
|
+
let packageJson;
|
|
49
|
+
if (!serviceName || serviceName === ".") {
|
|
50
|
+
directory = getServiceNameFromCurrentDirPackage(logger);
|
|
51
|
+
// packageJson = getPackageJsonFromDirectory(directory);
|
|
52
|
+
const packageResult = getPackageJson(directory);
|
|
53
|
+
packageJson = packageResult.packageJson;
|
|
54
|
+
directory = packageResult.servicePath;
|
|
55
|
+
} else if (serviceName && serviceName.startsWith(".")) {
|
|
56
|
+
// directory = serviceName;
|
|
57
|
+
// const jsonPath = path.join(serviceName);
|
|
58
|
+
// packageJson = getPackageJsonFromDirectory(path.resolve(jsonPath));
|
|
59
|
+
const packageResult = getPackageJson(serviceName);
|
|
60
|
+
packageJson = packageResult.packageJson;
|
|
61
|
+
directory = packageResult.servicePath;
|
|
62
|
+
} else {
|
|
63
|
+
return getPackageJson(serviceName);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const response = {
|
|
67
|
+
servicePath: directory,
|
|
68
|
+
packageJson,
|
|
69
|
+
};
|
|
70
|
+
logger.debug(`response ${response}`);
|
|
71
|
+
return response;
|
|
72
|
+
} catch (e) {
|
|
73
|
+
const ex = {
|
|
74
|
+
message: "Error getting service name",
|
|
75
|
+
serviceName,
|
|
76
|
+
error: e,
|
|
77
|
+
};
|
|
78
|
+
throw ex;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export interface SwerveArgs extends IConfig {
|
|
83
|
+
services: KeyValue<IService>;
|
|
84
|
+
port: number;
|
|
85
|
+
appDataRoot?: string;
|
|
86
|
+
serviceArgs?: KeyValue<any>;
|
|
87
|
+
logLevel: string;
|
|
88
|
+
[key: string]: any;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function getDefaultArgs(): SwerveArgs {
|
|
92
|
+
let currentServiceName = undefined;
|
|
93
|
+
|
|
94
|
+
return {
|
|
95
|
+
logLevel: "info",
|
|
96
|
+
services: {},
|
|
97
|
+
port: 3005,
|
|
98
|
+
serviceArgs: {},
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
const ARG_PREFIX = "--";
|
|
102
|
+
const CONFIG_ARG_KEY = "config";
|
|
103
|
+
|
|
104
|
+
function cliArgToPropertyName(rawArg: string): string {
|
|
105
|
+
return rawArg.replace(ARG_PREFIX, "");
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const configParser = new SwerveConfigParser();
|
|
109
|
+
|
|
110
|
+
async function getConfigValuesFromPath(
|
|
111
|
+
configPath: string,
|
|
112
|
+
logger: ILogger<any>,
|
|
113
|
+
) {
|
|
114
|
+
try {
|
|
115
|
+
return await configParser.parse(configPath);
|
|
116
|
+
} catch (e: any) {
|
|
117
|
+
logger.error(`Error occurred parsing config values from path`);
|
|
118
|
+
throw {
|
|
119
|
+
message:
|
|
120
|
+
"Unexpected error occurred when attempting to read serviceConfiguration file",
|
|
121
|
+
configFilePath: configPath,
|
|
122
|
+
error: e,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
const topLevelArgs = new Set();
|
|
127
|
+
topLevelArgs.add("port");
|
|
128
|
+
function isTopLevelArg(key: string) {
|
|
129
|
+
return topLevelArgs.has(key);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function tryParseNumberArg(val): number | boolean {
|
|
133
|
+
try {
|
|
134
|
+
return parseInt(val);
|
|
135
|
+
} catch (e) {
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function parseArgValue(val: string, logger: ILogger<any>) {
|
|
141
|
+
try {
|
|
142
|
+
return JSON.parse(val);
|
|
143
|
+
} catch (e) {
|
|
144
|
+
logger.warn(
|
|
145
|
+
`Exception occurred while parsing arg value ${val}. This is sometimes expected. Error ${e}`,
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return val;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const DEFAULT_PORT = 3005;
|
|
153
|
+
export async function getArgs(
|
|
154
|
+
args: string[],
|
|
155
|
+
logger: ILogger<any>,
|
|
156
|
+
): Promise<SwerveArgs> {
|
|
157
|
+
let argKey = undefined;
|
|
158
|
+
let swerveArgs = getDefaultArgs();
|
|
159
|
+
let configFromFile: IConfig;
|
|
160
|
+
const serviceCounts = new Map<string, number>();
|
|
161
|
+
for (let i = 2; i < args.length; i++) {
|
|
162
|
+
const nextVal = args[i];
|
|
163
|
+
if (argKey) {
|
|
164
|
+
if (argKey == CONFIG_ARG_KEY) {
|
|
165
|
+
if (configFromFile) {
|
|
166
|
+
throw new Error(
|
|
167
|
+
`Config file already specified, you can only specify one config file with the --config arg`,
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
configFromFile = await getConfigValuesFromPath(nextVal, logger);
|
|
171
|
+
argKey = undefined;
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
swerveArgs.serviceArgs[argKey] = parseArgValue(nextVal, logger);
|
|
175
|
+
argKey = undefined;
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
if (nextVal.startsWith(ARG_PREFIX)) {
|
|
179
|
+
argKey = cliArgToPropertyName(nextVal);
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const serviceDetails = getService(nextVal, logger);
|
|
184
|
+
const serviceName = serviceDetails.packageJson.name;
|
|
185
|
+
const serviceIndex = serviceCounts.get(serviceName) ?? 0;
|
|
186
|
+
serviceCounts.set(serviceName, serviceIndex + 1);
|
|
187
|
+
swerveArgs.services[`${serviceDetails.packageJson.name}-${serviceIndex}`] =
|
|
188
|
+
{
|
|
189
|
+
serviceConfiguration: {},
|
|
190
|
+
...serviceDetails,
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
swerveArgs.port =
|
|
195
|
+
configFromFile?.port ?? swerveArgs.serviceArgs?.port ?? DEFAULT_PORT;
|
|
196
|
+
|
|
197
|
+
logger.debug(`configFromFile ${configFromFile}`);
|
|
198
|
+
if (configFromFile?.services) {
|
|
199
|
+
logger.debug(`ConfigFromFile has service`);
|
|
200
|
+
for (const serviceEntry of Object.entries(configFromFile.services)) {
|
|
201
|
+
let { servicePath, packageName } = serviceEntry[1];
|
|
202
|
+
// serviceEntry[1].servicePath ?? serviceEntry[1].packageName;
|
|
203
|
+
if (!servicePath) {
|
|
204
|
+
serviceEntry[1].servicePath = packageName!;
|
|
205
|
+
// throw new Error(
|
|
206
|
+
// `servicePath or packageName must be set in service configurations`,
|
|
207
|
+
// );
|
|
208
|
+
} else {
|
|
209
|
+
const serviceData = getService(servicePath, logger);
|
|
210
|
+
serviceEntry[1].packageName = serviceData.packageJson?.name;
|
|
211
|
+
serviceEntry[1].servicePath = serviceData.servicePath;
|
|
212
|
+
}
|
|
213
|
+
// logger.debug(`ServiceName: ${}`);
|
|
214
|
+
swerveArgs.services[serviceEntry[0]] = await deepMerge(
|
|
215
|
+
await deepMerge(
|
|
216
|
+
swerveArgs.serviceArgs ?? {},
|
|
217
|
+
serviceEntry.values() ?? {},
|
|
218
|
+
),
|
|
219
|
+
configFromFile.services[serviceEntry[0]],
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
logger.debug(`packagedConfigFromFile into services`);
|
|
223
|
+
}
|
|
224
|
+
swerveArgs = deepMerge(swerveArgs, configFromFile ?? {});
|
|
225
|
+
logger.debug(`${JSON.stringify(swerveArgs)}`);
|
|
226
|
+
|
|
227
|
+
if (
|
|
228
|
+
swerveArgs.serviceArgs?.appDataRoot?.startsWith(".") ||
|
|
229
|
+
swerveArgs.serviceArgs?.appDataRoot == undefined
|
|
230
|
+
) {
|
|
231
|
+
const appDataRootPath = path.join(
|
|
232
|
+
process.cwd(),
|
|
233
|
+
// Object.values(swerveArgs.services)[0].servicePath,
|
|
234
|
+
);
|
|
235
|
+
|
|
236
|
+
swerveArgs.appDataRoot = appDataRootPath;
|
|
237
|
+
swerveArgs.serviceArgs.appDataRoot = appDataRootPath;
|
|
238
|
+
} else {
|
|
239
|
+
swerveArgs.appDataRoot = swerveArgs.serviceArgs.appDataRoot;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
if (Object.keys(swerveArgs.services).length < 1) {
|
|
243
|
+
logger.info(`swerveArgs.services.length < 1`);
|
|
244
|
+
const { servicePath, packageJson } = getService(".", logger);
|
|
245
|
+
swerveArgs.services[packageJson.name] = {
|
|
246
|
+
servicePath,
|
|
247
|
+
packageJson,
|
|
248
|
+
appDataRoot: swerveArgs.appDataRoot,
|
|
249
|
+
...swerveArgs.serviceArgs,
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
logger.debug(
|
|
253
|
+
`getArgs complete with parsed swerveArgs: ${JSON.stringify(swerveArgs)}`,
|
|
254
|
+
);
|
|
255
|
+
return swerveArgs;
|
|
256
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Determines whether the input string is a relative or absolute path.
|
|
7
|
+
*/
|
|
8
|
+
function isPath(input: string): boolean {
|
|
9
|
+
return input.startsWith(".") || path.isAbsolute(input);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Resolves the package.json file for a given package name or path.
|
|
14
|
+
* @param packageNameOrPath Package name (e.g., 'lodash', '@nestjs/core') or path (e.g., './node_modules/lodash')
|
|
15
|
+
* @returns Parsed package.json content or null
|
|
16
|
+
*/
|
|
17
|
+
export function getPackageJson(
|
|
18
|
+
packageNameOrPath: string,
|
|
19
|
+
): { packageJson: Record<string, any>; servicePath: string } | null {
|
|
20
|
+
try {
|
|
21
|
+
// Step 1: Resolve to an absolute path
|
|
22
|
+
// const resolvedEntry = isPath(packageNameOrPath)
|
|
23
|
+
// ? require.resolve(path.resolve(packageNameOrPath))
|
|
24
|
+
// : // : require.resolve(packageNameOrPath);
|
|
25
|
+
// require.resolve(packageNameOrPath, { paths: [process.cwd()] });
|
|
26
|
+
const resolvedEntry = isPath(packageNameOrPath)
|
|
27
|
+
? pathToFileURL(path.resolve(packageNameOrPath)).href
|
|
28
|
+
: import.meta.resolve(packageNameOrPath);
|
|
29
|
+
|
|
30
|
+
// Step 2: Traverse upward to find the nearest package.json
|
|
31
|
+
let dir = fileURLToPath(resolvedEntry); //path.dirname(resolvedEntry);
|
|
32
|
+
// console.log(dir);
|
|
33
|
+
while (dir !== path.parse(dir).root) {
|
|
34
|
+
const pkgPath = path.join(dir, "package.json");
|
|
35
|
+
if (fs.existsSync(pkgPath)) {
|
|
36
|
+
const content = fs.readFileSync(pkgPath, "utf-8");
|
|
37
|
+
const packageJson = JSON.parse(content);
|
|
38
|
+
if (!dir || dir === ".") {
|
|
39
|
+
dir = process.cwd();
|
|
40
|
+
} else if (dir.startsWith(".")) {
|
|
41
|
+
dir = path.join(process.cwd(), dir);
|
|
42
|
+
}
|
|
43
|
+
// console.log(dir);
|
|
44
|
+
const entrypoint = path.join(dir, packageJson.main);
|
|
45
|
+
// console.log(entrypoint);
|
|
46
|
+
// console.error(`Entrypoint is ${entrypoint}`);
|
|
47
|
+
// throw `Entrypoint is ${entrypoint}`;
|
|
48
|
+
return { packageJson, servicePath: entrypoint };
|
|
49
|
+
}
|
|
50
|
+
dir = path.dirname(dir);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
throw new Error(`Could not parse package.json`);
|
|
54
|
+
} catch (err) {
|
|
55
|
+
throw new Error(
|
|
56
|
+
`Could not resolve package.json for "${packageNameOrPath}"`,
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Get the nearest package.json of a given import name.
|
|
62
|
+
* @param packageName The name of the imported package (e.g. "lodash")
|
|
63
|
+
*/
|
|
64
|
+
export function getPackageJsonOrig(
|
|
65
|
+
packageName: string,
|
|
66
|
+
): Record<string, any> | null {
|
|
67
|
+
try {
|
|
68
|
+
// Resolve the entry file of the package
|
|
69
|
+
const entryPath = import.meta.resolve(packageName); //require.resolve(packageName);
|
|
70
|
+
|
|
71
|
+
// Traverse up from the resolved file to find package.json
|
|
72
|
+
let dir = path.dirname(entryPath);
|
|
73
|
+
|
|
74
|
+
while (dir !== path.parse(dir).root) {
|
|
75
|
+
const pkgPath = path.join(dir, "package.json");
|
|
76
|
+
|
|
77
|
+
if (fs.existsSync(pkgPath)) {
|
|
78
|
+
const content = fs.readFileSync(pkgPath, "utf8");
|
|
79
|
+
return { packageJson: JSON.parse(content), servicePath: dir };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
dir = path.dirname(dir);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return null;
|
|
86
|
+
} catch (error) {
|
|
87
|
+
//console.error(`Failed to resolve package "${packageName}":`, error);
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
// @ts-ignore
|
|
2
|
+
import express, { Request, Response } from "@swizzyweb/express";
|
|
3
|
+
import {
|
|
4
|
+
BrowserLogger,
|
|
5
|
+
getPackageJsonFromDirectory,
|
|
6
|
+
ILogger,
|
|
7
|
+
} from "@swizzyweb/swizzy-common";
|
|
8
|
+
import { SwizzyWinstonLogger } from "@swizzyweb/swizzy-web-service";
|
|
9
|
+
import { readFileSync } from "fs";
|
|
10
|
+
import path from "node:path";
|
|
11
|
+
import os from "node:os";
|
|
12
|
+
import process from "node:process";
|
|
13
|
+
import { getServiceNameFromCurrentDirPackage } from "./getArgs.js";
|
|
14
|
+
import { mkdirSync } from "node:fs";
|
|
15
|
+
import { fileURLToPath } from "node:url";
|
|
16
|
+
|
|
17
|
+
export async function installWebService(
|
|
18
|
+
appName: string,
|
|
19
|
+
importPathOrName: string,
|
|
20
|
+
port: number,
|
|
21
|
+
expressApp: any,
|
|
22
|
+
serviceArgs: any,
|
|
23
|
+
gLogger: ILogger<any>,
|
|
24
|
+
) {
|
|
25
|
+
const packageName = importPathOrName;
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
gLogger.info(
|
|
29
|
+
`Getting webservice package ${packageName} and will run on port ${port}`,
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
gLogger.debug(`Getting tool with path: ${importPathOrName}`);
|
|
33
|
+
|
|
34
|
+
const fullPath = importPathOrName; //await getFullImportPath(importPathOrName);
|
|
35
|
+
const tool = await import(fullPath); //require(fullPath); //require(packageName as string);
|
|
36
|
+
|
|
37
|
+
gLogger.debug(`Got service with require: ${JSON.stringify(tool)}`);
|
|
38
|
+
gLogger.debug(`Getting web service from tool...`);
|
|
39
|
+
|
|
40
|
+
const appDataPath = path.join(serviceArgs.appDataRoot, "appdata", appName);
|
|
41
|
+
mkdirSync(appDataPath, { recursive: true });
|
|
42
|
+
|
|
43
|
+
const logger = getLoggerForService(
|
|
44
|
+
serviceArgs,
|
|
45
|
+
serviceArgs.appName,
|
|
46
|
+
port,
|
|
47
|
+
gLogger,
|
|
48
|
+
);
|
|
49
|
+
gLogger.debug(`serviceArgs for ${packageName}: ${serviceArgs}`);
|
|
50
|
+
const service = await tool.getWebservice({
|
|
51
|
+
appDataPath,
|
|
52
|
+
...serviceArgs,
|
|
53
|
+
port,
|
|
54
|
+
app: expressApp,
|
|
55
|
+
packageName,
|
|
56
|
+
serviceArgs: { ...serviceArgs },
|
|
57
|
+
logger,
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
logger.debug(`Got web service`);
|
|
61
|
+
|
|
62
|
+
gLogger.debug(`Installing web service...`);
|
|
63
|
+
await service.install({});
|
|
64
|
+
|
|
65
|
+
gLogger.debug(`Installed web service ${packageName}`);
|
|
66
|
+
return service;
|
|
67
|
+
} catch (e) {
|
|
68
|
+
const exceptionMessage = `exception: ${e}
|
|
69
|
+
Failed to install web service, is it installed with NPM? Check package exists in node_modules
|
|
70
|
+
To add, run:
|
|
71
|
+
npm install ${packageName ?? "packageName"}
|
|
72
|
+
args:
|
|
73
|
+
packageName: ${packageName}
|
|
74
|
+
port: ${port}
|
|
75
|
+
`;
|
|
76
|
+
// ${getHelpText}`;
|
|
77
|
+
gLogger.error(`Failed to install web service`);
|
|
78
|
+
throw Error(exceptionMessage); //new Error(exceptionMessage);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export async function getFullImportPath(
|
|
83
|
+
importPathOrName: string,
|
|
84
|
+
): Promise<string> {
|
|
85
|
+
const importPath = importPathOrName.startsWith(".")
|
|
86
|
+
? path.join(process.cwd(), importPathOrName)
|
|
87
|
+
: importPathOrName;
|
|
88
|
+
let fullPath;
|
|
89
|
+
if (importPathOrName === importPath) {
|
|
90
|
+
const fullUrl = import.meta.resolve(importPath, import.meta.url);
|
|
91
|
+
fullPath = fileURLToPath(fullUrl);
|
|
92
|
+
// const pkg = await import(fullUrl, {
|
|
93
|
+
// assert: { type: "json" },
|
|
94
|
+
// });
|
|
95
|
+
|
|
96
|
+
// await require.resolve(importPath, {
|
|
97
|
+
// paths: [process.cwd()],
|
|
98
|
+
// });
|
|
99
|
+
} else {
|
|
100
|
+
fullPath = importPath;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return fullPath;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function getLoggerForService(
|
|
107
|
+
serviceArgs: any,
|
|
108
|
+
appName: string,
|
|
109
|
+
port: number,
|
|
110
|
+
gLogger: ILogger<any>,
|
|
111
|
+
) {
|
|
112
|
+
const logLevel = serviceArgs.logLevel ?? gLogger.getLoggerProps().logLevel;
|
|
113
|
+
const logFileName = serviceArgs.logFileName ?? undefined;
|
|
114
|
+
const ownerName = appName;
|
|
115
|
+
const pid = process.pid;
|
|
116
|
+
const hostName = os.hostname();
|
|
117
|
+
const logDir = serviceArgs.logDir;
|
|
118
|
+
const appDataRoot = serviceArgs.appDataRoot;
|
|
119
|
+
|
|
120
|
+
return gLogger.clone({
|
|
121
|
+
port,
|
|
122
|
+
appName,
|
|
123
|
+
appDataRoot,
|
|
124
|
+
logDir,
|
|
125
|
+
hostName,
|
|
126
|
+
pid,
|
|
127
|
+
logLevel,
|
|
128
|
+
ownerName,
|
|
129
|
+
logFileName,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { IConfig } from "../../dist/config/config.js";
|
|
2
|
+
|
|
3
|
+
describe(`Config tests`, () => {
|
|
4
|
+
it("Should accept service configuration", () => {
|
|
5
|
+
const config: IConfig = {
|
|
6
|
+
port: 3005,
|
|
7
|
+
services: {
|
|
8
|
+
dynserve: {
|
|
9
|
+
packageName: "@swizzyweb/dyn-serve-web-service",
|
|
10
|
+
|
|
11
|
+
serviceConfiguration: {
|
|
12
|
+
myVar: "thisisit",
|
|
13
|
+
butThis: {
|
|
14
|
+
echo: ["isNumber", 1],
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
// Noop, fails if model changed
|
|
22
|
+
});
|
|
23
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"port": 3000,
|
|
3
|
+
"services": {
|
|
4
|
+
"my-first-web-service1": {
|
|
5
|
+
"someOtherArgFromConfig": "ThisIsTheArgValue",
|
|
6
|
+
"someNumberINeed": 100,
|
|
7
|
+
"shouldTimeout": true,
|
|
8
|
+
"nestedJson": {
|
|
9
|
+
"hello": "world",
|
|
10
|
+
"none": false
|
|
11
|
+
},
|
|
12
|
+
"packageName": "@swizzyweb/my-first-web-service",
|
|
13
|
+
"serviceConfiguration": {
|
|
14
|
+
"port": 3001,
|
|
15
|
+
"tableName": "someString"
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|