@faasjs/dev 8.0.0-beta.6 → 8.0.0-beta.8

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.
@@ -0,0 +1,183 @@
1
+ let _faasjs_logger = require("@faasjs/logger");
2
+ let _faasjs_node_utils = require("@faasjs/node-utils");
3
+ let node_fs = require("node:fs");
4
+ let node_fs_promises = require("node:fs/promises");
5
+ let node_path = require("node:path");
6
+
7
+ //#region src/server_config.ts
8
+ function resolveFaasStaging() {
9
+ return process.env.FaasEnv || "development";
10
+ }
11
+ function resolveServerConfig(root, logger, defaultBase = "/") {
12
+ const projectRoot = (0, node_path.resolve)(root);
13
+ const staging = resolveFaasStaging();
14
+ const srcRoot = (0, node_path.join)(projectRoot, "src");
15
+ const config = (0, _faasjs_node_utils.loadConfig)(srcRoot, (0, node_path.join)(srcRoot, "index.func.ts"), staging, logger);
16
+ const server = config && typeof config === "object" ? config.server : void 0;
17
+ return {
18
+ root: server && typeof server.root === "string" && server.root.length ? (0, node_path.resolve)(projectRoot, server.root) : projectRoot,
19
+ base: server && typeof server.base === "string" && server.base.length ? server.base : defaultBase,
20
+ staging
21
+ };
22
+ }
23
+
24
+ //#endregion
25
+ //#region src/typegen.ts
26
+ function normalizeSlashes(path) {
27
+ return path.replace(/\\/g, "/");
28
+ }
29
+ function normalizeRoute(path) {
30
+ const normalized = path.replace(/\/+/g, "/");
31
+ if (!normalized.length || normalized === "/") return "/";
32
+ return normalized.endsWith("/") ? normalized.slice(0, -1) : normalized;
33
+ }
34
+ function toRoute(srcRoot, file) {
35
+ const noTsPath = normalizeSlashes((0, node_path.relative)(srcRoot, file)).replace(/\.ts$/, "");
36
+ if (noTsPath === "index.func") return {
37
+ route: "/",
38
+ priority: 2
39
+ };
40
+ if (noTsPath === "default.func") return {
41
+ route: "/*",
42
+ priority: 1
43
+ };
44
+ if (noTsPath.endsWith("/index.func")) return {
45
+ route: normalizeRoute(`/${noTsPath.slice(0, -11)}`),
46
+ priority: 2
47
+ };
48
+ if (noTsPath.endsWith("/default.func")) return {
49
+ route: normalizeRoute(`/${noTsPath.slice(0, -13)}/*`),
50
+ priority: 1
51
+ };
52
+ if (noTsPath.endsWith(".func")) return {
53
+ route: normalizeRoute(`/${noTsPath.slice(0, -5)}`),
54
+ priority: 3
55
+ };
56
+ throw Error(`[faas-types] Invalid func filename: ${file}`);
57
+ }
58
+ function toImportPath(fromFile, targetFile) {
59
+ const importPath = normalizeSlashes((0, node_path.relative)((0, node_path.dirname)(fromFile), targetFile)).replace(/\.ts$/, "");
60
+ if (importPath.startsWith(".")) return importPath;
61
+ return `./${importPath}`;
62
+ }
63
+ function parsePluginTypes(config) {
64
+ const pluginConfig = config.plugins;
65
+ if (!pluginConfig || typeof pluginConfig !== "object") return [];
66
+ const pluginTypes = /* @__PURE__ */ new Set();
67
+ for (const key in pluginConfig) {
68
+ const data = pluginConfig[key];
69
+ if (typeof data === "string" && data.length) {
70
+ pluginTypes.add(data);
71
+ continue;
72
+ }
73
+ if (data && typeof data === "object") {
74
+ if (typeof data.type === "string" && data.type.length) pluginTypes.add(data.type);
75
+ else pluginTypes.add(key);
76
+ continue;
77
+ }
78
+ pluginTypes.add(key);
79
+ }
80
+ return Array.from(pluginTypes).sort((a, b) => a.localeCompare(b));
81
+ }
82
+ async function readFuncFiles(dir) {
83
+ const result = [];
84
+ async function walk(currentDir) {
85
+ const entries = await (0, node_fs_promises.readdir)(currentDir, { withFileTypes: true });
86
+ for (const entry of entries) {
87
+ if (entry.name === ".faasjs" || entry.name === "node_modules") continue;
88
+ const filePath = (0, node_path.join)(currentDir, entry.name);
89
+ if (entry.isDirectory()) {
90
+ await walk(filePath);
91
+ continue;
92
+ }
93
+ if (entry.isFile() && entry.name.endsWith(".func.ts")) result.push(filePath);
94
+ }
95
+ }
96
+ await walk(dir);
97
+ return result.sort((a, b) => a.localeCompare(b));
98
+ }
99
+ function formatTypes(items) {
100
+ const actionLines = items.map((item) => {
101
+ return ` ${JSON.stringify(item.route)}: InferFaasAction<InferFaasFunc<typeof import(${JSON.stringify(item.importPath)})>>`;
102
+ });
103
+ const eventLines = items.map((item) => {
104
+ const plugins = item.pluginTypes.length ? `[${item.pluginTypes.map((type) => JSON.stringify(type)).join(", ")}]` : "[]";
105
+ return ` ${JSON.stringify(item.route)}: InferPluginEvent<${plugins}>`;
106
+ });
107
+ return `/**
108
+ * Generated by @faasjs/dev.
109
+ *
110
+ * Do not edit this file manually.
111
+ */
112
+ import type { Func, InferPluginEvent } from '@faasjs/func'
113
+ import type { InferFaasAction, InferFaasFunc } from '@faasjs/types'
114
+
115
+ declare module '@faasjs/types' {
116
+ interface FaasActions {
117
+ ${actionLines.length ? `${actionLines.join("\n")}\n` : ""} }
118
+
119
+ interface FaasEvents {
120
+ ${eventLines.length ? `${eventLines.join("\n")}\n` : ""} }
121
+ }
122
+ `;
123
+ }
124
+ function isTypegenSourceFile(filePath) {
125
+ return /\.func\.ts$/.test(filePath) || /(^|[\\/])faas\.ya?ml$/.test(filePath);
126
+ }
127
+ async function generateFaasTypes(options = {}) {
128
+ const logger = options.logger || new _faasjs_logger.Logger("FaasJs:Typegen");
129
+ const { root: projectRoot, staging } = resolveServerConfig(options.root || process.cwd(), logger);
130
+ const srcRoot = (0, node_path.join)(projectRoot, "src");
131
+ const output = (0, node_path.join)(srcRoot, ".faasjs", "types.d.ts");
132
+ if (!(0, node_fs.existsSync)(srcRoot)) throw Error(`[faas-types] Source directory not found: ${srcRoot}`);
133
+ const files = await readFuncFiles(srcRoot);
134
+ const routeMap = /* @__PURE__ */ new Map();
135
+ for (const file of files) {
136
+ const { route, priority } = toRoute(srcRoot, file);
137
+ const pluginTypes = parsePluginTypes((0, _faasjs_node_utils.loadConfig)(srcRoot, file, staging, logger));
138
+ const importPath = toImportPath(output, file);
139
+ const prev = routeMap.get(route);
140
+ if (!prev || priority > prev.priority) routeMap.set(route, {
141
+ route,
142
+ importPath,
143
+ pluginTypes,
144
+ priority
145
+ });
146
+ }
147
+ const items = Array.from(routeMap.values()).sort((a, b) => a.route.localeCompare(b.route));
148
+ const content = formatTypes(items);
149
+ let changed = true;
150
+ try {
151
+ if (await (0, node_fs_promises.readFile)(output, "utf8") === content) changed = false;
152
+ } catch (_error) {}
153
+ if (changed) {
154
+ await (0, node_fs_promises.mkdir)((0, node_path.dirname)(output), { recursive: true });
155
+ await (0, node_fs_promises.writeFile)(output, content);
156
+ }
157
+ return {
158
+ output,
159
+ changed,
160
+ fileCount: files.length,
161
+ routeCount: items.length
162
+ };
163
+ }
164
+
165
+ //#endregion
166
+ Object.defineProperty(exports, 'generateFaasTypes', {
167
+ enumerable: true,
168
+ get: function () {
169
+ return generateFaasTypes;
170
+ }
171
+ });
172
+ Object.defineProperty(exports, 'isTypegenSourceFile', {
173
+ enumerable: true,
174
+ get: function () {
175
+ return isTypegenSourceFile;
176
+ }
177
+ });
178
+ Object.defineProperty(exports, 'resolveServerConfig', {
179
+ enumerable: true,
180
+ get: function () {
181
+ return resolveServerConfig;
182
+ }
183
+ });
@@ -0,0 +1,166 @@
1
+ import { Logger } from "@faasjs/logger";
2
+ import { loadConfig } from "@faasjs/node-utils";
3
+ import { existsSync } from "node:fs";
4
+ import { mkdir, readFile, readdir, writeFile } from "node:fs/promises";
5
+ import { dirname, join, relative, resolve } from "node:path";
6
+
7
+ //#region src/server_config.ts
8
+ function resolveFaasStaging() {
9
+ return process.env.FaasEnv || "development";
10
+ }
11
+ function resolveServerConfig(root, logger, defaultBase = "/") {
12
+ const projectRoot = resolve(root);
13
+ const staging = resolveFaasStaging();
14
+ const srcRoot = join(projectRoot, "src");
15
+ const config = loadConfig(srcRoot, join(srcRoot, "index.func.ts"), staging, logger);
16
+ const server = config && typeof config === "object" ? config.server : void 0;
17
+ return {
18
+ root: server && typeof server.root === "string" && server.root.length ? resolve(projectRoot, server.root) : projectRoot,
19
+ base: server && typeof server.base === "string" && server.base.length ? server.base : defaultBase,
20
+ staging
21
+ };
22
+ }
23
+
24
+ //#endregion
25
+ //#region src/typegen.ts
26
+ function normalizeSlashes(path) {
27
+ return path.replace(/\\/g, "/");
28
+ }
29
+ function normalizeRoute(path) {
30
+ const normalized = path.replace(/\/+/g, "/");
31
+ if (!normalized.length || normalized === "/") return "/";
32
+ return normalized.endsWith("/") ? normalized.slice(0, -1) : normalized;
33
+ }
34
+ function toRoute(srcRoot, file) {
35
+ const noTsPath = normalizeSlashes(relative(srcRoot, file)).replace(/\.ts$/, "");
36
+ if (noTsPath === "index.func") return {
37
+ route: "/",
38
+ priority: 2
39
+ };
40
+ if (noTsPath === "default.func") return {
41
+ route: "/*",
42
+ priority: 1
43
+ };
44
+ if (noTsPath.endsWith("/index.func")) return {
45
+ route: normalizeRoute(`/${noTsPath.slice(0, -11)}`),
46
+ priority: 2
47
+ };
48
+ if (noTsPath.endsWith("/default.func")) return {
49
+ route: normalizeRoute(`/${noTsPath.slice(0, -13)}/*`),
50
+ priority: 1
51
+ };
52
+ if (noTsPath.endsWith(".func")) return {
53
+ route: normalizeRoute(`/${noTsPath.slice(0, -5)}`),
54
+ priority: 3
55
+ };
56
+ throw Error(`[faas-types] Invalid func filename: ${file}`);
57
+ }
58
+ function toImportPath(fromFile, targetFile) {
59
+ const importPath = normalizeSlashes(relative(dirname(fromFile), targetFile)).replace(/\.ts$/, "");
60
+ if (importPath.startsWith(".")) return importPath;
61
+ return `./${importPath}`;
62
+ }
63
+ function parsePluginTypes(config) {
64
+ const pluginConfig = config.plugins;
65
+ if (!pluginConfig || typeof pluginConfig !== "object") return [];
66
+ const pluginTypes = /* @__PURE__ */ new Set();
67
+ for (const key in pluginConfig) {
68
+ const data = pluginConfig[key];
69
+ if (typeof data === "string" && data.length) {
70
+ pluginTypes.add(data);
71
+ continue;
72
+ }
73
+ if (data && typeof data === "object") {
74
+ if (typeof data.type === "string" && data.type.length) pluginTypes.add(data.type);
75
+ else pluginTypes.add(key);
76
+ continue;
77
+ }
78
+ pluginTypes.add(key);
79
+ }
80
+ return Array.from(pluginTypes).sort((a, b) => a.localeCompare(b));
81
+ }
82
+ async function readFuncFiles(dir) {
83
+ const result = [];
84
+ async function walk(currentDir) {
85
+ const entries = await readdir(currentDir, { withFileTypes: true });
86
+ for (const entry of entries) {
87
+ if (entry.name === ".faasjs" || entry.name === "node_modules") continue;
88
+ const filePath = join(currentDir, entry.name);
89
+ if (entry.isDirectory()) {
90
+ await walk(filePath);
91
+ continue;
92
+ }
93
+ if (entry.isFile() && entry.name.endsWith(".func.ts")) result.push(filePath);
94
+ }
95
+ }
96
+ await walk(dir);
97
+ return result.sort((a, b) => a.localeCompare(b));
98
+ }
99
+ function formatTypes(items) {
100
+ const actionLines = items.map((item) => {
101
+ return ` ${JSON.stringify(item.route)}: InferFaasAction<InferFaasFunc<typeof import(${JSON.stringify(item.importPath)})>>`;
102
+ });
103
+ const eventLines = items.map((item) => {
104
+ const plugins = item.pluginTypes.length ? `[${item.pluginTypes.map((type) => JSON.stringify(type)).join(", ")}]` : "[]";
105
+ return ` ${JSON.stringify(item.route)}: InferPluginEvent<${plugins}>`;
106
+ });
107
+ return `/**
108
+ * Generated by @faasjs/dev.
109
+ *
110
+ * Do not edit this file manually.
111
+ */
112
+ import type { Func, InferPluginEvent } from '@faasjs/func'
113
+ import type { InferFaasAction, InferFaasFunc } from '@faasjs/types'
114
+
115
+ declare module '@faasjs/types' {
116
+ interface FaasActions {
117
+ ${actionLines.length ? `${actionLines.join("\n")}\n` : ""} }
118
+
119
+ interface FaasEvents {
120
+ ${eventLines.length ? `${eventLines.join("\n")}\n` : ""} }
121
+ }
122
+ `;
123
+ }
124
+ function isTypegenSourceFile(filePath) {
125
+ return /\.func\.ts$/.test(filePath) || /(^|[\\/])faas\.ya?ml$/.test(filePath);
126
+ }
127
+ async function generateFaasTypes(options = {}) {
128
+ const logger = options.logger || new Logger("FaasJs:Typegen");
129
+ const { root: projectRoot, staging } = resolveServerConfig(options.root || process.cwd(), logger);
130
+ const srcRoot = join(projectRoot, "src");
131
+ const output = join(srcRoot, ".faasjs", "types.d.ts");
132
+ if (!existsSync(srcRoot)) throw Error(`[faas-types] Source directory not found: ${srcRoot}`);
133
+ const files = await readFuncFiles(srcRoot);
134
+ const routeMap = /* @__PURE__ */ new Map();
135
+ for (const file of files) {
136
+ const { route, priority } = toRoute(srcRoot, file);
137
+ const pluginTypes = parsePluginTypes(loadConfig(srcRoot, file, staging, logger));
138
+ const importPath = toImportPath(output, file);
139
+ const prev = routeMap.get(route);
140
+ if (!prev || priority > prev.priority) routeMap.set(route, {
141
+ route,
142
+ importPath,
143
+ pluginTypes,
144
+ priority
145
+ });
146
+ }
147
+ const items = Array.from(routeMap.values()).sort((a, b) => a.route.localeCompare(b.route));
148
+ const content = formatTypes(items);
149
+ let changed = true;
150
+ try {
151
+ if (await readFile(output, "utf8") === content) changed = false;
152
+ } catch (_error) {}
153
+ if (changed) {
154
+ await mkdir(dirname(output), { recursive: true });
155
+ await writeFile(output, content);
156
+ }
157
+ return {
158
+ output,
159
+ changed,
160
+ fileCount: files.length,
161
+ routeCount: items.length
162
+ };
163
+ }
164
+
165
+ //#endregion
166
+ export { isTypegenSourceFile as n, resolveServerConfig as r, generateFaasTypes as t };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faasjs/dev",
3
- "version": "8.0.0-beta.6",
3
+ "version": "8.0.0-beta.8",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",
@@ -27,41 +27,35 @@
27
27
  },
28
28
  "funding": "https://github.com/sponsors/faasjs",
29
29
  "scripts": {
30
- "build": "tsup-node --entry src/index.ts --entry src/cli.ts --config ../../tsup.config.ts"
30
+ "build": "tsdown --entry src/index.ts --entry src/cli.ts --config ../../tsdown.config.ts"
31
31
  },
32
32
  "files": [
33
33
  "dist",
34
34
  "faas-types.mjs"
35
35
  ],
36
36
  "peerDependencies": {
37
- "@electric-sql/pglite": "*",
38
- "@faasjs/deep_merge": ">=8.0.0-beta.6",
39
- "@faasjs/func": ">=8.0.0-beta.6",
40
- "@faasjs/http": ">=8.0.0-beta.6",
41
- "@faasjs/server": ">=8.0.0-beta.6",
42
- "@faasjs/knex": ">=8.0.0-beta.6",
43
- "@faasjs/load": ">=8.0.0-beta.6",
44
- "@faasjs/logger": ">=8.0.0-beta.6",
37
+ "@faasjs/func": ">=8.0.0-beta.8",
38
+ "@faasjs/http": ">=8.0.0-beta.8",
39
+ "@faasjs/knex": ">=8.0.0-beta.8",
40
+ "@faasjs/logger": ">=8.0.0-beta.8",
41
+ "@faasjs/node-utils": ">=8.0.0-beta.8",
42
+ "@faasjs/server": ">=8.0.0-beta.8",
45
43
  "@types/node": "*",
46
44
  "knex": "*",
47
45
  "vite": "*",
48
- "vitest": "*",
49
- "knex-pglite": "*"
46
+ "vitest": "*"
50
47
  },
51
48
  "devDependencies": {
52
- "@electric-sql/pglite": "*",
53
- "@faasjs/deep_merge": ">=8.0.0-beta.6",
54
- "@faasjs/func": ">=8.0.0-beta.6",
55
- "@faasjs/http": ">=8.0.0-beta.6",
56
- "@faasjs/server": ">=8.0.0-beta.6",
57
- "@faasjs/knex": ">=8.0.0-beta.6",
58
- "@faasjs/load": ">=8.0.0-beta.6",
59
- "@faasjs/logger": ">=8.0.0-beta.6",
49
+ "@faasjs/func": ">=8.0.0-beta.8",
50
+ "@faasjs/http": ">=8.0.0-beta.8",
51
+ "@faasjs/knex": ">=8.0.0-beta.8",
52
+ "@faasjs/logger": ">=8.0.0-beta.8",
53
+ "@faasjs/node-utils": ">=8.0.0-beta.8",
54
+ "@faasjs/server": ">=8.0.0-beta.8",
60
55
  "@types/node": "*",
61
56
  "knex": "*",
62
57
  "vite": "*",
63
- "vitest": "*",
64
- "knex-pglite": "*"
58
+ "vitest": "*"
65
59
  },
66
60
  "engines": {
67
61
  "node": ">=24.0.0",
@@ -1,221 +0,0 @@
1
- import { existsSync } from 'fs';
2
- import { readFile, mkdir, writeFile, readdir } from 'fs/promises';
3
- import { resolve, join, dirname, relative } from 'path';
4
- import { loadConfig } from '@faasjs/load';
5
- import { Logger } from '@faasjs/logger';
6
-
7
- var __defProp = Object.defineProperty;
8
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
9
- var __getOwnPropNames = Object.getOwnPropertyNames;
10
- var __hasOwnProp = Object.prototype.hasOwnProperty;
11
- var __export = (target, all) => {
12
- for (var name in all)
13
- __defProp(target, name, { get: all[name], enumerable: true });
14
- };
15
- var __copyProps = (to, from, except, desc) => {
16
- if (from && typeof from === "object" || typeof from === "function") {
17
- for (let key of __getOwnPropNames(from))
18
- if (!__hasOwnProp.call(to, key) && key !== except)
19
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
20
- }
21
- return to;
22
- };
23
- var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
24
- function resolveFaasStaging() {
25
- return process.env.FaasEnv || "development";
26
- }
27
- function resolveServerConfig(root, logger, defaultBase = "/") {
28
- const projectRoot = resolve(root);
29
- const staging = resolveFaasStaging();
30
- const srcRoot = join(projectRoot, "src");
31
- const config = loadConfig(
32
- srcRoot,
33
- join(srcRoot, "index.func.ts"),
34
- staging,
35
- logger
36
- );
37
- const server = config && typeof config === "object" ? config.server : void 0;
38
- const resolvedRoot = server && typeof server.root === "string" && server.root.length ? resolve(projectRoot, server.root) : projectRoot;
39
- const resolvedBase = server && typeof server.base === "string" && server.base.length ? server.base : defaultBase;
40
- return {
41
- root: resolvedRoot,
42
- base: resolvedBase,
43
- staging
44
- };
45
- }
46
-
47
- // src/typegen.ts
48
- function normalizeSlashes(path) {
49
- return path.replace(/\\/g, "/");
50
- }
51
- function normalizeRoute(path) {
52
- const normalized = path.replace(/\/+/g, "/");
53
- if (!normalized.length || normalized === "/") return "/";
54
- return normalized.endsWith("/") ? normalized.slice(0, -1) : normalized;
55
- }
56
- function toRoute(srcRoot, file) {
57
- const relativePath = normalizeSlashes(relative(srcRoot, file));
58
- const noTsPath = relativePath.replace(/\.ts$/, "");
59
- if (noTsPath === "index.func") return { route: "/", priority: 2 };
60
- if (noTsPath === "default.func") return { route: "/*", priority: 1 };
61
- if (noTsPath.endsWith("/index.func"))
62
- return {
63
- route: normalizeRoute(`/${noTsPath.slice(0, -"/index.func".length)}`),
64
- priority: 2
65
- };
66
- if (noTsPath.endsWith("/default.func"))
67
- return {
68
- route: normalizeRoute(`/${noTsPath.slice(0, -"/default.func".length)}/*`),
69
- priority: 1
70
- };
71
- if (noTsPath.endsWith(".func"))
72
- return {
73
- route: normalizeRoute(`/${noTsPath.slice(0, -".func".length)}`),
74
- priority: 3
75
- };
76
- throw Error(`[faas-types] Invalid func filename: ${file}`);
77
- }
78
- function toImportPath(fromFile, targetFile) {
79
- const fromDir = dirname(fromFile);
80
- const importPath = normalizeSlashes(relative(fromDir, targetFile)).replace(
81
- /\.ts$/,
82
- ""
83
- );
84
- if (importPath.startsWith(".")) return importPath;
85
- return `./${importPath}`;
86
- }
87
- function parsePluginTypes(config) {
88
- const pluginConfig = config.plugins;
89
- if (!pluginConfig || typeof pluginConfig !== "object") return [];
90
- const pluginTypes = /* @__PURE__ */ new Set();
91
- for (const key in pluginConfig) {
92
- const data = pluginConfig[key];
93
- if (typeof data === "string" && data.length) {
94
- pluginTypes.add(data);
95
- continue;
96
- }
97
- if (data && typeof data === "object") {
98
- if (typeof data.type === "string" && data.type.length)
99
- pluginTypes.add(data.type);
100
- else pluginTypes.add(key);
101
- continue;
102
- }
103
- pluginTypes.add(key);
104
- }
105
- return Array.from(pluginTypes).sort((a, b) => a.localeCompare(b));
106
- }
107
- async function readFuncFiles(dir) {
108
- const result = [];
109
- async function walk(currentDir) {
110
- const entries = await readdir(currentDir, {
111
- withFileTypes: true
112
- });
113
- for (const entry of entries) {
114
- if (entry.name === ".faasjs" || entry.name === "node_modules") continue;
115
- const filePath = join(currentDir, entry.name);
116
- if (entry.isDirectory()) {
117
- await walk(filePath);
118
- continue;
119
- }
120
- if (entry.isFile() && entry.name.endsWith(".func.ts"))
121
- result.push(filePath);
122
- }
123
- }
124
- await walk(dir);
125
- return result.sort((a, b) => a.localeCompare(b));
126
- }
127
- function formatTypes(items) {
128
- const actionLines = items.map((item) => {
129
- return ` ${JSON.stringify(item.route)}: __FaasFuncAction<__FaasModuleFunc<typeof import(${JSON.stringify(item.importPath)})>>`;
130
- });
131
- const eventLines = items.map((item) => {
132
- const plugins = item.pluginTypes.length ? `[${item.pluginTypes.map((type) => JSON.stringify(type)).join(", ")}]` : "[]";
133
- return ` ${JSON.stringify(item.route)}: InferPluginEvent<${plugins}>`;
134
- });
135
- return `/**
136
- * Generated by @faasjs/dev.
137
- *
138
- * Do not edit this file manually.
139
- */
140
- import type { Func, InferPluginEvent } from '@faasjs/func'
141
-
142
- type __FaasModuleFunc<TModule> = TModule extends { func: infer TFunc }
143
- ? TFunc extends Func
144
- ? TFunc
145
- : never
146
- : TModule extends { default: infer TFunc }
147
- ? TFunc extends Func
148
- ? TFunc
149
- : never
150
- : never
151
-
152
- type __FaasFuncAction<TFunc extends Func> = {
153
- Params: Parameters<ReturnType<TFunc['export']>['handler']>[0]['params']
154
- Data: Awaited<ReturnType<ReturnType<TFunc['export']>['handler']>>
155
- }
156
-
157
- declare module '@faasjs/types' {
158
- interface FaasActions {
159
- ${actionLines.length ? `${actionLines.join("\n")}
160
- ` : ""} }
161
-
162
- interface FaasEvents {
163
- ${eventLines.length ? `${eventLines.join("\n")}
164
- ` : ""} }
165
- }
166
- `;
167
- }
168
- function isTypegenSourceFile(filePath) {
169
- return /\.func\.ts$/.test(filePath) || /(^|[\\/])faas\.ya?ml$/.test(filePath);
170
- }
171
- async function generateFaasTypes(options = {}) {
172
- const logger = options.logger || new Logger("FaasJs:Typegen");
173
- const { root: projectRoot, staging } = resolveServerConfig(
174
- options.root || process.cwd(),
175
- logger
176
- );
177
- const srcRoot = join(projectRoot, "src");
178
- const output = join(srcRoot, ".faasjs", "types.d.ts");
179
- if (!existsSync(srcRoot))
180
- throw Error(`[faas-types] Source directory not found: ${srcRoot}`);
181
- const files = await readFuncFiles(srcRoot);
182
- const routeMap = /* @__PURE__ */ new Map();
183
- for (const file of files) {
184
- const { route, priority } = toRoute(srcRoot, file);
185
- const config = loadConfig(srcRoot, file, staging, logger);
186
- const pluginTypes = parsePluginTypes(config);
187
- const importPath = toImportPath(output, file);
188
- const prev = routeMap.get(route);
189
- if (!prev || priority > prev.priority)
190
- routeMap.set(route, {
191
- route,
192
- importPath,
193
- pluginTypes,
194
- priority
195
- });
196
- }
197
- const items = Array.from(routeMap.values()).sort(
198
- (a, b) => a.route.localeCompare(b.route)
199
- );
200
- const content = formatTypes(items);
201
- let changed = true;
202
- try {
203
- const previous = await readFile(output, "utf8");
204
- if (previous === content) changed = false;
205
- } catch (_error) {
206
- }
207
- if (changed) {
208
- await mkdir(dirname(output), {
209
- recursive: true
210
- });
211
- await writeFile(output, content);
212
- }
213
- return {
214
- output,
215
- changed,
216
- fileCount: files.length,
217
- routeCount: items.length
218
- };
219
- }
220
-
221
- export { __export, __reExport, generateFaasTypes, isTypegenSourceFile, resolveServerConfig };