@cedarjs/cli 3.0.0-canary.13367 → 3.0.0-canary.13370

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,68 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import { isTypeScriptProject } from "@cedarjs/cli-helpers";
4
+ import { getPaths } from "@cedarjs/project-config";
5
+ function addRealtimeToGraphqlHandler(ctx, task, force) {
6
+ const graphqlHandlerPath = path.join(
7
+ getPaths().api.functions,
8
+ `graphql.${isTypeScriptProject() ? "ts" : "js"}`
9
+ );
10
+ if (!fs.existsSync(graphqlHandlerPath)) {
11
+ ctx.realtimeHandlerSkipped = true;
12
+ task.skip("GraphQL handler not found");
13
+ return;
14
+ }
15
+ const contentLines = fs.readFileSync(graphqlHandlerPath).toString().split("\n");
16
+ const importLineRegex = /^import {.*realtime.*} from ['"]src\/lib\/realtime['"];?$/;
17
+ const multilineImportRegex = /^} from ['"]src\/lib\/realtime['"];?$/;
18
+ const hasRealtimeImport = contentLines.some((line) => {
19
+ return importLineRegex.test(line) || multilineImportRegex.test(line);
20
+ });
21
+ if (hasRealtimeImport && !force) {
22
+ ctx.realtimeHandlerSkipped = true;
23
+ task.skip("Realtime import already exists");
24
+ return;
25
+ }
26
+ const handlerIndex = contentLines.findLastIndex(
27
+ (line) => line === "export const handler = createGraphQLHandler({"
28
+ );
29
+ if (handlerIndex === -1) {
30
+ ctx.realtimeHandlerSkipped = true;
31
+ task.skip("Unexpected syntax. Handler not found");
32
+ return;
33
+ }
34
+ const handlerLines = contentLines.slice(handlerIndex);
35
+ const hasRealtimeOption = handlerLines.some(
36
+ (line) => /^\s*realtime\b/.test(line)
37
+ );
38
+ if (hasRealtimeOption && !force) {
39
+ ctx.realtimeHandlerSkipped = true;
40
+ task.skip("Realtime option already exists");
41
+ return;
42
+ }
43
+ const lastImportIndex = contentLines.slice(0, handlerIndex).findLastIndex((line) => line.startsWith("import "));
44
+ if (lastImportIndex === -1) {
45
+ ctx.realtimeHandlerSkipped = true;
46
+ task.skip("Unexpected syntax. No imports found");
47
+ return;
48
+ }
49
+ contentLines.splice(
50
+ lastImportIndex + 1,
51
+ 0,
52
+ "import { realtime } from 'src/lib/realtime'"
53
+ );
54
+ const handlerIndexAfterSplice = handlerIndex + 1;
55
+ const sdlsIndex = handlerLines.findLastIndex(
56
+ (line) => /^\s*sdls,$/.test(line)
57
+ );
58
+ if (sdlsIndex === -1) {
59
+ ctx.realtimeHandlerSkipped = true;
60
+ task.skip("Unexpected syntax. `sdls` option not found");
61
+ return;
62
+ }
63
+ contentLines.splice(handlerIndexAfterSplice + sdlsIndex, 0, " realtime,");
64
+ fs.writeFileSync(graphqlHandlerPath, contentLines.join("\n"));
65
+ }
66
+ export {
67
+ addRealtimeToGraphqlHandler
68
+ };
@@ -1,5 +1,6 @@
1
1
  import fs from "node:fs";
2
2
  import path from "path";
3
+ import execa from "execa";
3
4
  import { Listr } from "listr2";
4
5
  import { addApiPackages } from "@cedarjs/cli-helpers";
5
6
  import { generate as generateTypes } from "@cedarjs/internal/dist/generate/generate";
@@ -9,6 +10,7 @@ import c from "../../../lib/colors.js";
9
10
  import { getPaths, transformTSToJS, writeFile } from "../../../lib/index.js";
10
11
  import { isTypeScriptProject, serverFileExists } from "../../../lib/project.js";
11
12
  import { setupServerFileTasks } from "../server-file/serverFileHandler.js";
13
+ import { addRealtimeToGraphqlHandler } from "./addRealtimeToGraphql.js";
12
14
  const { version } = JSON.parse(
13
15
  fs.readFileSync(
14
16
  path.resolve(import.meta.dirname, "../../../../package.json"),
@@ -26,7 +28,7 @@ async function handler({ force, includeExamples, verbose }) {
26
28
  [
27
29
  addApiPackages(["ioredis@^5", `@cedarjs/realtime@${version}`]),
28
30
  {
29
- title: "Adding the realtime api lib ...",
31
+ title: "Adding the realtime api lib...",
30
32
  task: async () => {
31
33
  const serverFileTemplateContent = fs.readFileSync(
32
34
  path.resolve(
@@ -48,7 +50,13 @@ async function handler({ force, includeExamples, verbose }) {
48
50
  }
49
51
  },
50
52
  {
51
- title: "Adding Countdown example subscription ...",
53
+ title: "Enabling realtime support in the GraphQL handler...",
54
+ task: (ctx, task) => {
55
+ addRealtimeToGraphqlHandler(ctx, task, force);
56
+ }
57
+ },
58
+ {
59
+ title: "Adding Countdown example subscription...",
52
60
  enabled: () => includeExamples,
53
61
  task: async () => {
54
62
  let exampleSubscriptionTemplateContent = fs.readFileSync(
@@ -84,7 +92,7 @@ async function handler({ force, includeExamples, verbose }) {
84
92
  }
85
93
  },
86
94
  {
87
- title: "Adding NewMessage example subscription ...",
95
+ title: "Adding NewMessage example subscription...",
88
96
  enabled: () => includeExamples,
89
97
  task: async () => {
90
98
  const exampleSdlTemplateContent = fs.readFileSync(
@@ -157,7 +165,7 @@ async function handler({ force, includeExamples, verbose }) {
157
165
  }
158
166
  },
159
167
  {
160
- title: "Adding Auctions example live query ...",
168
+ title: "Adding Auctions example live query...",
161
169
  enabled: () => includeExamples,
162
170
  task: async () => {
163
171
  const exampleSdlTemplateContent = fs.readFileSync(
@@ -202,7 +210,7 @@ async function handler({ force, includeExamples, verbose }) {
202
210
  }
203
211
  },
204
212
  {
205
- title: "Adding Defer example queries ...",
213
+ title: "Adding Defer example queries...",
206
214
  enabled: () => includeExamples,
207
215
  task: async () => {
208
216
  const exampleSdlTemplateContent = fs.readFileSync(
@@ -247,7 +255,7 @@ async function handler({ force, includeExamples, verbose }) {
247
255
  }
248
256
  },
249
257
  {
250
- title: "Adding Stream example queries ...",
258
+ title: "Adding Stream example queries...",
251
259
  enabled: () => includeExamples,
252
260
  task: async () => {
253
261
  const exampleSdlTemplateContent = fs.readFileSync(
@@ -292,13 +300,31 @@ async function handler({ force, includeExamples, verbose }) {
292
300
  }
293
301
  },
294
302
  {
295
- title: `Generating types ...`,
303
+ title: `Generating types...`,
296
304
  task: async () => {
297
305
  await generateTypes();
298
306
  console.log(
299
307
  "Note: You may need to manually restart GraphQL in VSCode to see the new types take effect.\n\n"
300
308
  );
301
309
  }
310
+ },
311
+ {
312
+ title: "Cleaning up...",
313
+ task: () => {
314
+ const graphqlHandlerPath = path.join(
315
+ getPaths().api.functions,
316
+ `graphql.${isTypeScriptProject() ? "ts" : "js"}`
317
+ );
318
+ execa.sync(
319
+ "yarn",
320
+ ["cedar", "lint", "--fix", graphqlHandlerPath, realtimeLibFilePath],
321
+ {
322
+ cwd: getPaths().base,
323
+ // Silently ignore errors
324
+ reject: false
325
+ }
326
+ );
327
+ }
302
328
  }
303
329
  ],
304
330
  {
@@ -311,6 +337,22 @@ async function handler({ force, includeExamples, verbose }) {
311
337
  tasks.add(setupServerFileTasks({ force }));
312
338
  }
313
339
  await tasks.run();
340
+ if (tasks.ctx?.realtimeHandlerSkipped) {
341
+ const graphqlHandlerPath = path.join(
342
+ getPaths().api.functions,
343
+ `graphql.${isTypeScriptProject() ? "ts" : "js"}`
344
+ );
345
+ const relativePath = path.relative(getPaths().base, graphqlHandlerPath);
346
+ console.log();
347
+ console.log(
348
+ c.warning(
349
+ `Note: The setup command skipped adding realtime to your GraphQL handler. Please review ${relativePath}, and manually add it if needed.`
350
+ )
351
+ );
352
+ console.log(
353
+ "You want to make sure you have an import like `import { realtime } from '@cedarjs/realtime'`, and that you pass `realtime` to the call to `createGraphQLHandler`."
354
+ );
355
+ }
314
356
  } catch (e) {
315
357
  errorTelemetry(process.argv, e.message);
316
358
  console.error(c.error(e.message));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cedarjs/cli",
3
- "version": "3.0.0-canary.13367+4edbb1ade",
3
+ "version": "3.0.0-canary.13370+b8841f057",
4
4
  "description": "The CedarJS Command Line",
5
5
  "repository": {
6
6
  "type": "git",
@@ -31,17 +31,18 @@
31
31
  "test:watch": "vitest watch"
32
32
  },
33
33
  "dependencies": {
34
+ "@babel/parser": "7.29.0",
34
35
  "@babel/preset-typescript": "7.28.5",
35
36
  "@babel/runtime-corejs3": "7.29.0",
36
- "@cedarjs/api-server": "3.0.0-canary.13367",
37
- "@cedarjs/cli-helpers": "3.0.0-canary.13367",
38
- "@cedarjs/fastify-web": "3.0.0-canary.13367",
39
- "@cedarjs/internal": "3.0.0-canary.13367",
40
- "@cedarjs/prerender": "3.0.0-canary.13367",
41
- "@cedarjs/project-config": "3.0.0-canary.13367",
42
- "@cedarjs/structure": "3.0.0-canary.13367",
43
- "@cedarjs/telemetry": "3.0.0-canary.13367",
44
- "@cedarjs/web-server": "3.0.0-canary.13367",
37
+ "@cedarjs/api-server": "3.0.0-canary.13370",
38
+ "@cedarjs/cli-helpers": "3.0.0-canary.13370",
39
+ "@cedarjs/fastify-web": "3.0.0-canary.13370",
40
+ "@cedarjs/internal": "3.0.0-canary.13370",
41
+ "@cedarjs/prerender": "3.0.0-canary.13370",
42
+ "@cedarjs/project-config": "3.0.0-canary.13370",
43
+ "@cedarjs/structure": "3.0.0-canary.13370",
44
+ "@cedarjs/telemetry": "3.0.0-canary.13370",
45
+ "@cedarjs/web-server": "3.0.0-canary.13370",
45
46
  "@listr2/prompt-adapter-enquirer": "2.0.16",
46
47
  "@opentelemetry/api": "1.9.0",
47
48
  "@opentelemetry/core": "1.30.1",
@@ -78,6 +79,7 @@
78
79
  "prettier": "3.8.1",
79
80
  "prisma": "6.19.2",
80
81
  "prompts": "2.4.2",
82
+ "recast": "0.23.11",
81
83
  "rimraf": "6.1.2",
82
84
  "semver": "7.7.3",
83
85
  "smol-toml": "1.6.0",
@@ -104,5 +106,5 @@
104
106
  "publishConfig": {
105
107
  "access": "public"
106
108
  },
107
- "gitHead": "4edbb1ade15442a45ce47e6b721ae12d5b5ec8be"
109
+ "gitHead": "b8841f057dc5f352fecc928685b20796636a10b0"
108
110
  }