Skip to content

Commit 00f3e39

Browse files
committed
Fix tooling to be executable by node
1 parent b639879 commit 00f3e39

7 files changed

Lines changed: 52 additions & 966 deletions

File tree

actions/gls-action/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"scripts": {
99
"build": "vite build",
1010
"lint": "eslint .",
11-
"test": "vitest run"
11+
"test": "vitest run",
12+
"start": "node dist/index.js"
1213
}
1314
}

actions/gls-action/src/helpers.ts

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import {ZodError, ZodObject} from "zod";
22
import {createAuxiliaryTypeStore, printNode, zodToTs} from "zod-to-ts";
33
import ts from "typescript";
44
import axios from "axios";
5-
import * as path from "node:path";
6-
import {readdir, stat} from "fs/promises";
75
import {
86
HerculesRuntimeFunctionDefinition,
97
HerculesFunctionContext,
@@ -37,7 +35,6 @@ import {
3735
ShipmentRequestData,
3836
ShipmentRequestDataSchema
3937
} from "./types/requests/shipmentRequest";
40-
import { fileURLToPath } from 'url';
4138

4239

4340
export const DEFAULT_SIGNATURE_FOR_SERVICES = "shipment: GLS_SHIPMENT, printingOptions: GLS_PRINTING_OPTIONS, returnOptions?: GLS_RETURN_OPTIONS, customContent?: GLS_CUSTOM_CONTENT"
@@ -306,32 +303,17 @@ export async function postShipmentHelper(context: HerculesFunctionContext, servi
306303
}
307304
}
308305

309-
const __filename = fileURLToPath(import.meta.url);
310-
const __dirname = path.dirname(__filename);
311-
312306
export async function loadAllDefinitions(sdk: ActionSdk) {
313-
const baseDir = path.resolve(__dirname, "definitions");
314-
await loadFromDirectory(baseDir, sdk);
315-
}
316-
317-
async function loadFromDirectory(dir: string, sdk: ActionSdk) {
318-
const entries = await readdir(dir);
319-
320-
for (const entry of entries) {
321-
const fullPath = path.join(dir, entry);
322-
const stats = await stat(fullPath);
307+
const modules = import.meta.glob('./definitions/**/*.ts');
323308

324-
if (stats.isDirectory()) {
325-
await loadFromDirectory(fullPath, sdk);
326-
} else if (entry.endsWith(".ts") && !entry.endsWith(".test.ts")) {
327-
const mod = await import(fullPath);
309+
for (const path in modules) {
310+
const mod: any = await modules[path]();
328311

329-
if (typeof mod.register === "function") {
330-
try {
331-
await mod.register(sdk);
332-
} catch (error) {
333-
console.log(`Error registering functions from file ${entry}:`, error);
334-
}
312+
if (typeof mod.register === 'function') {
313+
try {
314+
await mod.register(sdk);
315+
} catch (error) {
316+
console.log(`Error registering functions from ${path}:`, error);
335317
}
336318
}
337319
}

actions/gls-action/src/index.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,19 @@ export const sdk = createSdk({
1010
version: process.env.HERCULES_SDK_VERSION || "0.0.0",
1111
})
1212

13-
try {
14-
await loadAllDefinitions(sdk)
15-
connectToSdk()
16-
} catch (error) {
17-
console.error(error)
13+
async function main() {
14+
try {
15+
await loadAllDefinitions(sdk)
16+
connectToSdk()
17+
} catch (error) {
18+
console.error(error)
19+
}
1820
}
21+
main().catch(err => {
22+
console.error(err)
23+
process.exit(1)
24+
})
25+
1926

2027
function connectToSdk() {
2128
sdk.connect().then(() => {

actions/gls-action/vite.config.ts

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
11
import { defineConfig } from 'vite';
2-
import dts from 'vite-plugin-dts';
32
import { resolve } from 'path';
43

54
export default defineConfig({
65
build: {
7-
target: "node18",
6+
target: 'node18',
87
ssr: true,
9-
lib: {
10-
entry: resolve(__dirname, 'src/index.ts'),
11-
name: 'centaurus',
12-
fileName: 'centaurus',
13-
formats: ['es']
14-
},
8+
outDir: 'dist',
9+
emptyOutDir: true,
1510
rollupOptions: {
16-
external: (id) =>
17-
['fs', 'path', 'typescript'].includes(id) || id.startsWith('node:')
11+
input: resolve(__dirname, 'src/index.ts'),
12+
external: [
13+
'fs',
14+
'path',
15+
'os',
16+
'crypto',
17+
'stream',
18+
'util',
19+
'events',
20+
'buffer',
21+
'url',
22+
'zlib',
23+
'node:fs',
24+
'node:path'
25+
]
1826
}
19-
},
20-
plugins: [
21-
dts({
22-
insertTypesEntry: true,
23-
include: ['src/**/*.ts'],
24-
afterDiagnostic: diagnostics => {
25-
if (diagnostics.length > 0) {
26-
throw new Error("dts failed");
27-
}
28-
}
29-
})
30-
]
27+
}
3128
});

0 commit comments

Comments
 (0)