@swizzyweb/swerve-manager 0.1.3 → 0.1.5
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/swerve.js +11 -4
- package/dist/utils/getFullImportPath.d.ts +1 -0
- package/dist/utils/getFullImportPath.js +53 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +1 -0
- package/dist/utils/installWebservice.d.ts +0 -1
- package/dist/utils/installWebservice.js +1 -1
- package/package.json +6 -8
- package/src/swerve.ts +10 -5
- package/src/utils/getFullImportPath.ts +60 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/installWebservice.ts +1 -3
- package/test/getFullImportPath.spec.ts +16 -0
- package/jest.config.js +0 -4
package/dist/swerve.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
// @ts-ignore
|
|
2
2
|
import express from "@swizzyweb/express";
|
|
3
|
-
import {
|
|
3
|
+
import { getLoggerForService, } from "./utils/index.js";
|
|
4
4
|
import { SwizzyWinstonLogger, } from "@swizzyweb/swizzy-web-service";
|
|
5
5
|
import os from "node:os";
|
|
6
6
|
import process from "node:process";
|
|
7
7
|
import path from "node:path";
|
|
8
8
|
import { mkdirSync } from "node:fs";
|
|
9
|
+
import { getFullImportPath } from "./utils/getFullImportPath.js";
|
|
9
10
|
export var InstanceType;
|
|
10
11
|
(function (InstanceType) {
|
|
11
12
|
InstanceType["webservice"] = "webservice";
|
|
@@ -50,7 +51,8 @@ export class SwerveManager {
|
|
|
50
51
|
}
|
|
51
52
|
const app = this.apps[`${port}`].app;
|
|
52
53
|
const service = serviceEntry[1];
|
|
53
|
-
const
|
|
54
|
+
const serviceName = serviceEntry[0];
|
|
55
|
+
const packageName = service.packageName;
|
|
54
56
|
const importPathOrName = service.servicePath ?? service.packageName;
|
|
55
57
|
gLogger.debug(`importPathOrName ${importPathOrName}`);
|
|
56
58
|
const serviceArgs = {
|
|
@@ -146,8 +148,13 @@ export class SwerveManager {
|
|
|
146
148
|
const { app, appDataRoot, packageName, port, gLogger, serviceArgs, importPathOrName, serviceKey, } = props;
|
|
147
149
|
try {
|
|
148
150
|
gLogger.info(`Getting webservice package ${packageName} and will run on port ${port}`);
|
|
149
|
-
|
|
150
|
-
|
|
151
|
+
if (packageName) {
|
|
152
|
+
gLogger.debug(`Getting web service with name ${packageName}`);
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
gLogger.debug(`Getting webservice with path: ${importPathOrName}`);
|
|
156
|
+
}
|
|
157
|
+
const fullPath = packageName ?? (await getFullImportPath(importPathOrName));
|
|
151
158
|
const tool = await import(fullPath); //require(fullPath); //require(packageName as string);
|
|
152
159
|
gLogger.debug(`Got service with require: ${JSON.stringify(tool)}`);
|
|
153
160
|
gLogger.debug(`Getting web service from tool...`);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getFullImportPath(importPathOrName: string): Promise<string>;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// cross-runtime getFullImportPath.ts
|
|
2
|
+
// Runtime detection
|
|
3
|
+
// @ts-ignore
|
|
4
|
+
const isDeno = typeof Deno !== "undefined" && "cwd" in Deno;
|
|
5
|
+
// @ts-ignore
|
|
6
|
+
const isBun = typeof Bun !== "undefined";
|
|
7
|
+
// @ts-ignore
|
|
8
|
+
const isNode = !isDeno && !isBun;
|
|
9
|
+
let join;
|
|
10
|
+
let cwd;
|
|
11
|
+
let fromFileUrl;
|
|
12
|
+
// --- Runtime-specific shims ---
|
|
13
|
+
if (isDeno) {
|
|
14
|
+
// @ts-ignore
|
|
15
|
+
// const path = await import("https://deno.land/std@0.203.0/path/mod.ts");
|
|
16
|
+
const path = await import("node:path");
|
|
17
|
+
join = path.join;
|
|
18
|
+
const url = await import("node:url");
|
|
19
|
+
// @ts-ignore
|
|
20
|
+
cwd = Deno.cwd;
|
|
21
|
+
fromFileUrl = url.fileURLToPath;
|
|
22
|
+
// fromFileUrl = path.fromFileUrl;
|
|
23
|
+
}
|
|
24
|
+
else if (isNode) {
|
|
25
|
+
const path = await import("node:path");
|
|
26
|
+
const processMod = await import("node:process");
|
|
27
|
+
const url = await import("node:url");
|
|
28
|
+
join = path.join;
|
|
29
|
+
cwd = processMod.cwd;
|
|
30
|
+
fromFileUrl = url.fileURLToPath;
|
|
31
|
+
}
|
|
32
|
+
else if (isBun) {
|
|
33
|
+
const path = await import("path");
|
|
34
|
+
join = path.join;
|
|
35
|
+
cwd = () => process.cwd();
|
|
36
|
+
fromFileUrl = (u) => new URL(u).pathname; // Bun auto normalizes
|
|
37
|
+
}
|
|
38
|
+
// --- Universal function ---
|
|
39
|
+
export async function getFullImportPath(importPathOrName) {
|
|
40
|
+
let importPath;
|
|
41
|
+
if (importPathOrName.startsWith(".")) {
|
|
42
|
+
importPath = join(cwd(), importPathOrName);
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
importPath = importPathOrName;
|
|
46
|
+
}
|
|
47
|
+
if (importPathOrName === importPath) {
|
|
48
|
+
// Turn into a full absolute path
|
|
49
|
+
const fullUrl = new URL(importPath, import.meta.url).href;
|
|
50
|
+
return fromFileUrl(fullUrl);
|
|
51
|
+
}
|
|
52
|
+
return importPath;
|
|
53
|
+
}
|
package/dist/utils/index.d.ts
CHANGED
package/dist/utils/index.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
1
|
import { ILogger } from "@swizzyweb/swizzy-common";
|
|
2
2
|
export declare function installWebService(appName: string, importPathOrName: string, port: number, expressApp: any, serviceArgs: any, gLogger: ILogger<any>): Promise<any>;
|
|
3
|
-
export declare function getFullImportPath(importPathOrName: string): Promise<string>;
|
|
4
3
|
export declare function getLoggerForService(serviceArgs: any, appName: string, port: number, gLogger: ILogger<any>): ILogger<any>;
|
|
@@ -45,7 +45,7 @@ Failed to install web service, is it installed with NPM? Check package exists in
|
|
|
45
45
|
throw Error(exceptionMessage); //new Error(exceptionMessage);
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
|
-
|
|
48
|
+
async function getFullImportPath(importPathOrName) {
|
|
49
49
|
const importPath = importPathOrName.startsWith(".")
|
|
50
50
|
? path.join(process.cwd(), importPathOrName)
|
|
51
51
|
: importPathOrName;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@swizzyweb/swerve-manager",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"description": "swizzy-swerve is a bootstrapper for swizzy web services. This package will bootstrap and run independent swizzy web services.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -9,26 +9,24 @@
|
|
|
9
9
|
"build": "tsc",
|
|
10
10
|
"server:ts": "ts-node ./src/bootstrap.ts",
|
|
11
11
|
"server": "node dist/bootstrap.js",
|
|
12
|
-
"test": "node
|
|
12
|
+
"test": "npm run test:node && npm run test:bun && npm run test:deno",
|
|
13
|
+
"test:node": "node --test ./test**/*.spec.ts",
|
|
14
|
+
"test:deno": "deno test test/** --allow-env --allow-write --allow-read",
|
|
15
|
+
"test:bun": "bun test",
|
|
13
16
|
"coverage": "jest --coverage"
|
|
14
17
|
},
|
|
15
18
|
"author": "Jason Gallagher",
|
|
16
19
|
"license": "Apache-2.0",
|
|
17
20
|
"devDependencies": {
|
|
18
21
|
"@swizzyweb/express": "^4.19.2",
|
|
19
|
-
"@types/jest": "^29.5.14",
|
|
20
22
|
"@types/node": "^22.7.7",
|
|
21
|
-
"jest": "^29.7.0",
|
|
22
|
-
"ts-jest": "^29.3.2",
|
|
23
23
|
"typescript": "^5.6.3"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@swizzyweb/swerve-manager": "^0.1.2",
|
|
27
26
|
"@swizzyweb/swizzy-common": "^0.3.2",
|
|
28
27
|
"@swizzyweb/swizzy-web-service": "^0.5.3",
|
|
29
28
|
"bun": "^1.2.15",
|
|
30
|
-
"deno": "^2.3.6"
|
|
31
|
-
"ts-node": "^10.9.2"
|
|
29
|
+
"deno": "^2.3.6"
|
|
32
30
|
},
|
|
33
31
|
"repository": {
|
|
34
32
|
"type": "git",
|
package/src/swerve.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
// @ts-ignore
|
|
2
2
|
import express, { Application } from "@swizzyweb/express";
|
|
3
3
|
import {
|
|
4
|
-
getFullImportPath,
|
|
5
4
|
getLoggerForService,
|
|
6
5
|
installWebService,
|
|
7
6
|
SwerveArgs,
|
|
@@ -17,6 +16,7 @@ import process from "node:process";
|
|
|
17
16
|
import { ILogger } from "@swizzyweb/swizzy-common";
|
|
18
17
|
import path from "node:path";
|
|
19
18
|
import { mkdirSync } from "node:fs";
|
|
19
|
+
import { getFullImportPath } from "./utils/getFullImportPath.js";
|
|
20
20
|
|
|
21
21
|
export interface ISwerveManager {
|
|
22
22
|
run(request: RunRequest): Promise<RunResponse>;
|
|
@@ -126,7 +126,8 @@ export class SwerveManager implements ISwerveManager {
|
|
|
126
126
|
const app = this.apps[`${port}`].app;
|
|
127
127
|
|
|
128
128
|
const service = serviceEntry[1];
|
|
129
|
-
const
|
|
129
|
+
const serviceName = serviceEntry[0];
|
|
130
|
+
const packageName = service.packageName;
|
|
130
131
|
const importPathOrName = service.servicePath ?? service.packageName;
|
|
131
132
|
gLogger.debug(`importPathOrName ${importPathOrName}`);
|
|
132
133
|
const serviceArgs: SwerveArgs = {
|
|
@@ -257,9 +258,13 @@ export class SwerveManager implements ISwerveManager {
|
|
|
257
258
|
`Getting webservice package ${packageName} and will run on port ${port}`,
|
|
258
259
|
);
|
|
259
260
|
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
261
|
+
if (packageName) {
|
|
262
|
+
gLogger.debug(`Getting web service with name ${packageName}`);
|
|
263
|
+
} else {
|
|
264
|
+
gLogger.debug(`Getting webservice with path: ${importPathOrName}`);
|
|
265
|
+
}
|
|
266
|
+
const fullPath =
|
|
267
|
+
packageName ?? (await getFullImportPath(importPathOrName));
|
|
263
268
|
const tool = await import(fullPath); //require(fullPath); //require(packageName as string);
|
|
264
269
|
|
|
265
270
|
gLogger.debug(`Got service with require: ${JSON.stringify(tool)}`);
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// cross-runtime getFullImportPath.ts
|
|
2
|
+
|
|
3
|
+
// Runtime detection
|
|
4
|
+
// @ts-ignore
|
|
5
|
+
const isDeno = typeof Deno !== "undefined" && "cwd" in Deno;
|
|
6
|
+
// @ts-ignore
|
|
7
|
+
const isBun = typeof Bun !== "undefined";
|
|
8
|
+
// @ts-ignore
|
|
9
|
+
const isNode = !isDeno && !isBun;
|
|
10
|
+
|
|
11
|
+
let join: (a: string, b: string) => string;
|
|
12
|
+
let cwd: () => string;
|
|
13
|
+
let fromFileUrl: (url: string) => string;
|
|
14
|
+
|
|
15
|
+
// --- Runtime-specific shims ---
|
|
16
|
+
if (isDeno) {
|
|
17
|
+
// @ts-ignore
|
|
18
|
+
// const path = await import("https://deno.land/std@0.203.0/path/mod.ts");
|
|
19
|
+
|
|
20
|
+
const path = await import("node:path");
|
|
21
|
+
join = path.join;
|
|
22
|
+
const url = await import("node:url");
|
|
23
|
+
// @ts-ignore
|
|
24
|
+
cwd = Deno.cwd;
|
|
25
|
+
fromFileUrl = url.fileURLToPath;
|
|
26
|
+
// fromFileUrl = path.fromFileUrl;
|
|
27
|
+
} else if (isNode) {
|
|
28
|
+
const path = await import("node:path");
|
|
29
|
+
const processMod = await import("node:process");
|
|
30
|
+
const url = await import("node:url");
|
|
31
|
+
join = path.join;
|
|
32
|
+
cwd = processMod.cwd;
|
|
33
|
+
fromFileUrl = url.fileURLToPath;
|
|
34
|
+
} else if (isBun) {
|
|
35
|
+
const path = await import("path");
|
|
36
|
+
join = path.join;
|
|
37
|
+
cwd = () => process.cwd();
|
|
38
|
+
fromFileUrl = (u: string) => new URL(u).pathname; // Bun auto normalizes
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// --- Universal function ---
|
|
42
|
+
export async function getFullImportPath(
|
|
43
|
+
importPathOrName: string,
|
|
44
|
+
): Promise<string> {
|
|
45
|
+
let importPath: string;
|
|
46
|
+
|
|
47
|
+
if (importPathOrName.startsWith(".")) {
|
|
48
|
+
importPath = join(cwd(), importPathOrName);
|
|
49
|
+
} else {
|
|
50
|
+
importPath = importPathOrName;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (importPathOrName === importPath) {
|
|
54
|
+
// Turn into a full absolute path
|
|
55
|
+
const fullUrl = new URL(importPath, import.meta.url).href;
|
|
56
|
+
return fromFileUrl(fullUrl);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return importPath;
|
|
60
|
+
}
|
package/src/utils/index.ts
CHANGED
|
@@ -79,9 +79,7 @@ Failed to install web service, is it installed with NPM? Check package exists in
|
|
|
79
79
|
}
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
-
|
|
83
|
-
importPathOrName: string,
|
|
84
|
-
): Promise<string> {
|
|
82
|
+
async function getFullImportPath(importPathOrName: string): Promise<string> {
|
|
85
83
|
const importPath = importPathOrName.startsWith(".")
|
|
86
84
|
? path.join(process.cwd(), importPathOrName)
|
|
87
85
|
: importPathOrName;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import { getFullImportPath } from "../dist/utils/getFullImportPath.js";
|
|
3
|
+
import assert from "node:assert";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
|
|
6
|
+
test("Should get full path with relative path", async () => {
|
|
7
|
+
const fullPath = await getFullImportPath("../");
|
|
8
|
+
assert(path.isAbsolute(fullPath));
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
test("Should get full path from packageName", async () => {
|
|
12
|
+
const fullPath = await getFullImportPath(
|
|
13
|
+
"@swizzyweb/swap-cache-db-web-service",
|
|
14
|
+
);
|
|
15
|
+
assert(path.isAbsolute(fullPath));
|
|
16
|
+
});
|
package/jest.config.js
DELETED