@cedarjs/api-server 6.0.0-rc.189 → 6.0.0-rc.241

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.
Files changed (44) hide show
  1. package/README.md +0 -1
  2. package/dist/apiCLIConfig.d.ts.map +1 -1
  3. package/dist/apiCLIConfig.js +1 -1
  4. package/dist/apiCLIConfigHandler.d.ts.map +1 -1
  5. package/dist/apiCLIConfigHandler.js +5 -0
  6. package/dist/bin.js +135 -40
  7. package/dist/bothCLIConfig.d.ts.map +1 -1
  8. package/dist/bothCLIConfig.js +2 -2
  9. package/dist/bothCLIConfigHandler.d.ts.map +1 -1
  10. package/dist/bothCLIConfigHandler.js +8 -2
  11. package/dist/cliHelpers.d.ts +21 -4
  12. package/dist/cliHelpers.d.ts.map +1 -1
  13. package/dist/cliHelpers.js +39 -11
  14. package/dist/createServer.d.ts.map +1 -1
  15. package/dist/createServer.js +4 -4
  16. package/dist/createServerHelpers.d.ts.map +1 -1
  17. package/dist/createServerHelpers.js +6 -2
  18. package/dist/plugins/api.d.ts +1 -1
  19. package/dist/plugins/api.d.ts.map +1 -1
  20. package/dist/plugins/api.js +1 -1
  21. package/dist/plugins/graphql.d.ts +19 -3
  22. package/dist/plugins/graphql.d.ts.map +1 -1
  23. package/dist/plugins/graphql.js +26 -11
  24. package/dist/serverFile.d.ts +23 -0
  25. package/dist/serverFile.d.ts.map +1 -0
  26. package/dist/serverFile.js +35 -0
  27. package/dist/types.d.ts +3 -3
  28. package/dist/types.d.ts.map +1 -1
  29. package/dist/utils.d.ts +0 -3
  30. package/dist/utils.d.ts.map +1 -1
  31. package/dist/utils.js +0 -20
  32. package/package.json +10 -20
  33. package/dist/buildManager.d.ts +0 -15
  34. package/dist/buildManager.d.ts.map +0 -1
  35. package/dist/buildManager.js +0 -55
  36. package/dist/serverManager.d.ts +0 -8
  37. package/dist/serverManager.d.ts.map +0 -1
  38. package/dist/serverManager.js +0 -102
  39. package/dist/watch.d.ts +0 -10
  40. package/dist/watch.d.ts.map +0 -1
  41. package/dist/watch.js +0 -375
  42. package/dist/watchPaths.d.ts +0 -3
  43. package/dist/watchPaths.d.ts.map +0 -1
  44. package/dist/watchPaths.js +0 -109
package/dist/watch.js DELETED
@@ -1,375 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- // src/watch.ts
4
- import path3 from "node:path";
5
- import ansis2 from "ansis";
6
- import chokidar from "chokidar";
7
- import { config } from "dotenv-defaults";
8
- import {
9
- buildApi,
10
- cleanApiBuild,
11
- rebuildApi
12
- } from "@cedarjs/internal/dist/build/api";
13
- import { loadAndValidateSdls } from "@cedarjs/internal/dist/validateSchema";
14
- import { getPaths as getPaths3 } from "@cedarjs/project-config";
15
-
16
- // src/utils.ts
17
- function debounce(func, wait) {
18
- let timeoutId = null;
19
- const debounced = ((...args) => {
20
- if (timeoutId !== null) {
21
- clearTimeout(timeoutId);
22
- }
23
- timeoutId = setTimeout(() => {
24
- timeoutId = null;
25
- func(...args);
26
- }, wait);
27
- });
28
- debounced.cancel = () => {
29
- if (timeoutId !== null) {
30
- clearTimeout(timeoutId);
31
- timeoutId = null;
32
- }
33
- };
34
- return debounced;
35
- }
36
-
37
- // src/buildManager.ts
38
- var BuildManager = class {
39
- shouldRebuild;
40
- shouldClean;
41
- debouncedBuild;
42
- buildFn;
43
- constructor(buildFn) {
44
- this.shouldRebuild = true;
45
- this.shouldClean = false;
46
- this.buildFn = buildFn;
47
- if (process.env.RWJS_DELAY_RESTART) {
48
- console.warn(
49
- "[DEPRECATED] RWJS_DELAY_RESTART is deprecated and will be removed in the next major release. Please rename it to CEDAR_DELAY_API_RESTART in your .env file."
50
- );
51
- }
52
- const delay = process.env.CEDAR_DELAY_API_RESTART || process.env.RWJS_DELAY_RESTART;
53
- this.debouncedBuild = debounce(
54
- async (options) => {
55
- try {
56
- await this.buildFn({
57
- ...options,
58
- rebuild: this.shouldRebuild,
59
- clean: this.shouldClean
60
- });
61
- } finally {
62
- this.shouldRebuild = true;
63
- this.shouldClean = false;
64
- }
65
- },
66
- // We want to delay execution when multiple files are modified on the
67
- // filesystem. This usually happens when running Cedar generator commands.
68
- // Local writes are very fast, but writes in e2e environments are not, so
69
- // allow the default to be adjusted with an env-var.
70
- delay ? parseInt(delay, 10) : 500
71
- );
72
- }
73
- // Wrapper method to handle options and set precedence flags.
74
- // If we ever see a `rebuild: false` option while debouncing, we never want to rebuild.
75
- // If we ever see a `clean: true` option, we always want to clean.
76
- async run(options) {
77
- if (options.rebuild === false) {
78
- this.shouldRebuild = false;
79
- }
80
- if (options.clean) {
81
- this.shouldClean = true;
82
- }
83
- await this.debouncedBuild(options);
84
- }
85
- cancelScheduledBuild() {
86
- this.debouncedBuild.cancel();
87
- }
88
- };
89
-
90
- // src/serverManager.ts
91
- import { fork } from "child_process";
92
- import fs from "node:fs";
93
- import path from "path";
94
- import ansis from "ansis";
95
- import yargs from "yargs";
96
- import { hideBin } from "yargs/helpers";
97
- import { getConfig, getPaths, resolveFile } from "@cedarjs/project-config";
98
- var argv = yargs(hideBin(process.argv)).option("debugPort", {
99
- description: "Port on which to expose API server debugger",
100
- type: "number",
101
- alias: ["debug-port", "dp"]
102
- }).option("port", {
103
- description: "The port to listen at",
104
- type: "number",
105
- alias: "p"
106
- }).parseSync();
107
- var rwjsPaths = getPaths();
108
- var ServerManager = class {
109
- httpServerProcess = null;
110
- async startApiServer() {
111
- const forkOpts = {
112
- execArgv: process.execArgv
113
- };
114
- if (getConfig().experimental.opentelemetry.enabled) {
115
- const opentelemetrySDKScriptPath = path.join(
116
- rwjsPaths.api.dist,
117
- "opentelemetry.js"
118
- );
119
- const opentelemetrySDKScriptPathRelative = path.relative(
120
- rwjsPaths.base,
121
- opentelemetrySDKScriptPath
122
- );
123
- console.log(
124
- `Setting up OpenTelemetry using the setup file: ${opentelemetrySDKScriptPathRelative}`
125
- );
126
- if (fs.existsSync(opentelemetrySDKScriptPath)) {
127
- forkOpts.execArgv = forkOpts.execArgv.concat([
128
- `--require=${opentelemetrySDKScriptPath}`
129
- ]);
130
- } else {
131
- console.error(
132
- `OpenTelemetry setup file does not exist at ${opentelemetrySDKScriptPathRelative}`
133
- );
134
- }
135
- }
136
- const debugPort = argv["debug-port"];
137
- if (debugPort) {
138
- forkOpts.execArgv = forkOpts.execArgv.concat([`--inspect=${debugPort}`]);
139
- }
140
- const port = argv.port ?? getConfig().api.port;
141
- const serverFile = resolveFile(`${rwjsPaths.api.dist}/server`);
142
- if (serverFile) {
143
- this.httpServerProcess = fork(
144
- serverFile,
145
- ["--apiPort", port.toString()],
146
- forkOpts
147
- );
148
- } else {
149
- const dirname = import.meta.dirname;
150
- const binPath = path.join(dirname, "bin.js");
151
- const args = ["api", "--port", port.toString()];
152
- this.httpServerProcess = fork(binPath, args, forkOpts);
153
- }
154
- }
155
- async restartApiServer() {
156
- await this.killApiServer();
157
- await this.startApiServer();
158
- }
159
- async killApiServer() {
160
- if (!this.httpServerProcess) {
161
- return;
162
- }
163
- await new Promise((resolve) => {
164
- console.log(ansis.yellow("Shutting down API server."));
165
- const cleanup = () => {
166
- this.httpServerProcess?.removeAllListeners("exit");
167
- clearTimeout(forceKillTimeout);
168
- };
169
- this.httpServerProcess?.on("exit", () => {
170
- console.log(ansis.yellow("API server exited."));
171
- cleanup();
172
- resolve();
173
- });
174
- const forceKillTimeout = setTimeout(() => {
175
- console.log(
176
- ansis.yellow(
177
- "API server did not exit within 2 seconds, forcefully closing it."
178
- )
179
- );
180
- cleanup();
181
- this.httpServerProcess?.kill("SIGKILL");
182
- resolve();
183
- }, 2e3);
184
- this.httpServerProcess?.kill();
185
- });
186
- }
187
- };
188
- var serverManager = new ServerManager();
189
-
190
- // src/watchPaths.ts
191
- import fs2 from "node:fs";
192
- import path2 from "node:path";
193
- import {
194
- getDbDir,
195
- getPaths as getPaths2,
196
- importStatementPath
197
- } from "@cedarjs/project-config";
198
- async function workspacePackagesPaths() {
199
- const cedarPaths2 = getPaths2();
200
- const packagesDir = path2.join(cedarPaths2.packages);
201
- const packages = [];
202
- try {
203
- const rootPackageJsonPath = path2.join(cedarPaths2.base, "package.json");
204
- const rootPackageJson = JSON.parse(
205
- fs2.readFileSync(rootPackageJsonPath, "utf8")
206
- );
207
- const hasPackageJsonWorkspaces = Array.isArray(rootPackageJson.workspaces) && rootPackageJson.workspaces.some((w) => w.startsWith("packages/"));
208
- if (!hasPackageJsonWorkspaces || !fs2.existsSync(packagesDir)) {
209
- return [];
210
- }
211
- const globPattern = path2.join(packagesDir, "*").replaceAll("\\", "/");
212
- const packageDirs = await Array.fromAsync(fs2.promises.glob(globPattern));
213
- const apiPackageJsonPath = path2.join(cedarPaths2.api.base, "package.json");
214
- const apiPackageJson = JSON.parse(
215
- fs2.readFileSync(apiPackageJsonPath, "utf8")
216
- );
217
- const deps = {
218
- ...apiPackageJson.dependencies ?? {},
219
- ...apiPackageJson.devDependencies ?? {},
220
- ...apiPackageJson.peerDependencies ?? {}
221
- };
222
- const workspaceDepNames = /* @__PURE__ */ new Set();
223
- for (const [name, version] of Object.entries(deps)) {
224
- if (String(version).startsWith("workspace:")) {
225
- workspaceDepNames.add(name);
226
- }
227
- }
228
- for (const packageDir of packageDirs) {
229
- const packageJsonPath = path2.join(packageDir, "package.json");
230
- if (!fs2.existsSync(packageJsonPath)) {
231
- continue;
232
- }
233
- const pkgJson = JSON.parse(fs2.readFileSync(packageJsonPath, "utf8"));
234
- if (workspaceDepNames.has(pkgJson.name)) {
235
- packages.push(path2.join(packageDir, "dist"));
236
- }
237
- }
238
- } catch {
239
- }
240
- return packages;
241
- }
242
- async function apiIgnorePaths() {
243
- const cedarPaths2 = getPaths2();
244
- const dbDir = await getDbDir(cedarPaths2.api.prismaConfig);
245
- if (dbDir === cedarPaths2.api.base) {
246
- throw new Error(
247
- "Database directory cannot be the same as the API directory"
248
- );
249
- }
250
- const ignoredApiPaths = [
251
- // TODO: Is this still true?
252
- // use this, because using cedarPaths.api.dist seems to not ignore on first
253
- // build
254
- "api/dist",
255
- cedarPaths2.api.types,
256
- dbDir
257
- ];
258
- return ignoredApiPaths;
259
- }
260
- async function getIgnoreFunction() {
261
- const cedarPaths2 = getPaths2();
262
- const ignoredApiPaths = await apiIgnorePaths();
263
- const ignoredExtensions = [
264
- ".DS_Store",
265
- ".db",
266
- ".sqlite",
267
- "-journal",
268
- ".test.js",
269
- ".test.ts",
270
- ".scenarios.ts",
271
- ".scenarios.js",
272
- ".d.ts",
273
- ".log"
274
- ];
275
- return (file) => {
276
- if (file.includes("node_modules")) {
277
- return true;
278
- }
279
- if (ignoredExtensions.some((ext) => file.endsWith(ext))) {
280
- return true;
281
- }
282
- if (file.includes(importStatementPath(cedarPaths2.packages)) && file.includes("/src/")) {
283
- return true;
284
- }
285
- if (ignoredApiPaths.some((ignoredPath) => file.includes(ignoredPath))) {
286
- return true;
287
- }
288
- return false;
289
- };
290
- }
291
- async function pathsToWatch() {
292
- const cedarPaths2 = getPaths2();
293
- const watchPaths = [cedarPaths2.api.src, ...await workspacePackagesPaths()];
294
- return watchPaths.map((p) => importStatementPath(p));
295
- }
296
-
297
- // src/watch.ts
298
- var cedarPaths = getPaths3();
299
- if (!process.env.CEDAR_ENV_FILES_LOADED) {
300
- config({
301
- path: path3.join(cedarPaths.base, ".env"),
302
- defaults: path3.join(cedarPaths.base, ".env.defaults"),
303
- multiline: true
304
- });
305
- process.env.CEDAR_ENV_FILES_LOADED = "true";
306
- }
307
- async function buildAndServe(options) {
308
- const buildTs = Date.now();
309
- console.log(ansis2.dim.italic("Building..."));
310
- if (options.clean) {
311
- await cleanApiBuild();
312
- }
313
- if (options.rebuild) {
314
- await rebuildApi();
315
- } else {
316
- await buildApi();
317
- }
318
- await serverManager.restartApiServer();
319
- console.log(ansis2.dim.italic("Took " + (Date.now() - buildTs) + " ms"));
320
- }
321
- var buildManager = new BuildManager(buildAndServe);
322
- async function validateSdls() {
323
- try {
324
- await loadAndValidateSdls();
325
- return true;
326
- } catch (e) {
327
- serverManager.killApiServer();
328
- console.error(
329
- ansis2.redBright(`[GQL Server Error] - Schema validation failed`)
330
- );
331
- console.error(ansis2.red(e?.message));
332
- console.error(ansis2.redBright("-".repeat(40)));
333
- buildManager.cancelScheduledBuild();
334
- return false;
335
- }
336
- }
337
- async function startWatch() {
338
- const patterns = await pathsToWatch();
339
- const watcher = chokidar.watch(patterns, {
340
- persistent: true,
341
- ignoreInitial: true,
342
- ignored: await getIgnoreFunction()
343
- });
344
- watcher.on("ready", async () => {
345
- await buildManager.run({ clean: true, rebuild: false });
346
- await validateSdls();
347
- });
348
- watcher.on("all", async (eventName, filePath) => {
349
- if (eventName === "addDir" && filePath === cedarPaths.api.base) {
350
- return;
351
- }
352
- if (eventName) {
353
- if (filePath.includes(".sdl")) {
354
- const isValid = await validateSdls();
355
- if (!isValid) {
356
- return;
357
- }
358
- }
359
- }
360
- const displayPath = path3.relative(cedarPaths.base, filePath);
361
- console.log(ansis2.dim(`[${eventName}] ${displayPath}`));
362
- buildManager.cancelScheduledBuild();
363
- if (eventName === "add" || eventName === "unlink") {
364
- await buildManager.run({ rebuild: false });
365
- } else {
366
- await buildManager.run({ rebuild: true });
367
- }
368
- });
369
- }
370
- if (import.meta.url === `file://${process.argv[1]}`) {
371
- startWatch();
372
- }
373
- export {
374
- startWatch
375
- };
@@ -1,3 +0,0 @@
1
- export declare function getIgnoreFunction(): Promise<(file: string) => boolean>;
2
- export declare function pathsToWatch(): Promise<string[]>;
3
- //# sourceMappingURL=watchPaths.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"watchPaths.d.ts","sourceRoot":"","sources":["../src/watchPaths.ts"],"names":[],"mappings":"AAmGA,wBAAsB,iBAAiB,mBAqBvB,MAAM,cAwBrB;AAED,wBAAsB,YAAY,sBAQjC"}
@@ -1,109 +0,0 @@
1
- import fs from "node:fs";
2
- import path from "node:path";
3
- import {
4
- getDbDir,
5
- getPaths,
6
- importStatementPath
7
- } from "@cedarjs/project-config";
8
- async function workspacePackagesPaths() {
9
- const cedarPaths = getPaths();
10
- const packagesDir = path.join(cedarPaths.packages);
11
- const packages = [];
12
- try {
13
- const rootPackageJsonPath = path.join(cedarPaths.base, "package.json");
14
- const rootPackageJson = JSON.parse(
15
- fs.readFileSync(rootPackageJsonPath, "utf8")
16
- );
17
- const hasPackageJsonWorkspaces = Array.isArray(rootPackageJson.workspaces) && rootPackageJson.workspaces.some((w) => w.startsWith("packages/"));
18
- if (!hasPackageJsonWorkspaces || !fs.existsSync(packagesDir)) {
19
- return [];
20
- }
21
- const globPattern = path.join(packagesDir, "*").replaceAll("\\", "/");
22
- const packageDirs = await Array.fromAsync(fs.promises.glob(globPattern));
23
- const apiPackageJsonPath = path.join(cedarPaths.api.base, "package.json");
24
- const apiPackageJson = JSON.parse(
25
- fs.readFileSync(apiPackageJsonPath, "utf8")
26
- );
27
- const deps = {
28
- ...apiPackageJson.dependencies ?? {},
29
- ...apiPackageJson.devDependencies ?? {},
30
- ...apiPackageJson.peerDependencies ?? {}
31
- };
32
- const workspaceDepNames = /* @__PURE__ */ new Set();
33
- for (const [name, version] of Object.entries(deps)) {
34
- if (String(version).startsWith("workspace:")) {
35
- workspaceDepNames.add(name);
36
- }
37
- }
38
- for (const packageDir of packageDirs) {
39
- const packageJsonPath = path.join(packageDir, "package.json");
40
- if (!fs.existsSync(packageJsonPath)) {
41
- continue;
42
- }
43
- const pkgJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
44
- if (workspaceDepNames.has(pkgJson.name)) {
45
- packages.push(path.join(packageDir, "dist"));
46
- }
47
- }
48
- } catch {
49
- }
50
- return packages;
51
- }
52
- async function apiIgnorePaths() {
53
- const cedarPaths = getPaths();
54
- const dbDir = await getDbDir(cedarPaths.api.prismaConfig);
55
- if (dbDir === cedarPaths.api.base) {
56
- throw new Error(
57
- "Database directory cannot be the same as the API directory"
58
- );
59
- }
60
- const ignoredApiPaths = [
61
- // TODO: Is this still true?
62
- // use this, because using cedarPaths.api.dist seems to not ignore on first
63
- // build
64
- "api/dist",
65
- cedarPaths.api.types,
66
- dbDir
67
- ];
68
- return ignoredApiPaths;
69
- }
70
- async function getIgnoreFunction() {
71
- const cedarPaths = getPaths();
72
- const ignoredApiPaths = await apiIgnorePaths();
73
- const ignoredExtensions = [
74
- ".DS_Store",
75
- ".db",
76
- ".sqlite",
77
- "-journal",
78
- ".test.js",
79
- ".test.ts",
80
- ".scenarios.ts",
81
- ".scenarios.js",
82
- ".d.ts",
83
- ".log"
84
- ];
85
- return (file) => {
86
- if (file.includes("node_modules")) {
87
- return true;
88
- }
89
- if (ignoredExtensions.some((ext) => file.endsWith(ext))) {
90
- return true;
91
- }
92
- if (file.includes(importStatementPath(cedarPaths.packages)) && file.includes("/src/")) {
93
- return true;
94
- }
95
- if (ignoredApiPaths.some((ignoredPath) => file.includes(ignoredPath))) {
96
- return true;
97
- }
98
- return false;
99
- };
100
- }
101
- async function pathsToWatch() {
102
- const cedarPaths = getPaths();
103
- const watchPaths = [cedarPaths.api.src, ...await workspacePackagesPaths()];
104
- return watchPaths.map((p) => importStatementPath(p));
105
- }
106
- export {
107
- getIgnoreFunction,
108
- pathsToWatch
109
- };