@cedarjs/cli 6.0.0 → 7.0.0-canary.2982

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.
@@ -183,7 +183,7 @@ You'll manually need to merge it with your existing entry.client${ext} file.`;
183
183
  fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, 2));
184
184
  }
185
185
  },
186
- addWebPackages(["@apollo/client-react-streaming@0.10.0"]),
186
+ addWebPackages(["@apollo/client-react-streaming@0.14.5"]),
187
187
  {
188
188
  task: () => {
189
189
  printTaskEpilogue(command, description, EXPERIMENTAL_TOPIC_ID);
@@ -22,6 +22,26 @@ const builder = createBuilder({
22
22
  default: "",
23
23
  description: "Use to enforce a specific query name within the generated cell - must be unique.",
24
24
  type: "string"
25
+ },
26
+ beforeQuery: {
27
+ default: false,
28
+ description: "Include a typed `beforeQuery` stub for configuring the query (e.g. variables, fetch policy).",
29
+ type: "boolean"
30
+ },
31
+ afterQuery: {
32
+ default: false,
33
+ description: "Include a typed `afterQuery` stub for sanitizing data returned from the query.",
34
+ type: "boolean"
35
+ },
36
+ isEmpty: {
37
+ default: false,
38
+ description: "Include a typed `isEmpty` stub for overriding the default check for the Empty component.",
39
+ type: "boolean"
40
+ },
41
+ fragment: {
42
+ default: "",
43
+ description: "Generate a fragment cell (exports FRAGMENT instead of QUERY) that reads its data from a parent Cell, given the GraphQL type it selects from - e.g. --fragment User.",
44
+ type: "string"
25
45
  }
26
46
  };
27
47
  }
@@ -26,7 +26,11 @@ const files = async ({
26
26
  list = false,
27
27
  query,
28
28
  stories,
29
- tests
29
+ tests,
30
+ beforeQuery = false,
31
+ afterQuery = false,
32
+ isEmpty = false,
33
+ fragment = ""
30
34
  }) => {
31
35
  let cellName = removeGeneratorName(name, "cell");
32
36
  let idName = "id";
@@ -35,22 +39,49 @@ const files = async ({
35
39
  let model = null;
36
40
  let templateNameSuffix = "";
37
41
  let typeName = cellName;
38
- const shouldGenerateList = (isWordPluralizable(cellName) ? isPlural(cellName) : list) || list;
39
- try {
40
- model = await getSchema(pascalcase(singularize(cellName)));
41
- idName = getIdName(model);
42
- idType = getIdType(model);
43
- typeName = model.name;
44
- mockIdValues = idType === "String" ? mockIdValues.map((value) => `'${value}'`) : mockIdValues;
45
- } catch {
46
- idType = "Int";
42
+ if (fragment) {
43
+ if (list) {
44
+ throw new Error(
45
+ "The --list flag cannot be combined with --fragment; fragment cells always render a single item."
46
+ );
47
+ }
48
+ if (beforeQuery) {
49
+ throw new Error(
50
+ "The --before-query flag cannot be combined with --fragment; fragment cells never fire a query of their own."
51
+ );
52
+ }
53
+ if (query) {
54
+ throw new Error(
55
+ "The --query flag cannot be combined with --fragment; fragment cells don't have an operation name."
56
+ );
57
+ }
58
+ }
59
+ const shouldGenerateList = !fragment && ((isWordPluralizable(cellName) ? isPlural(cellName) : list) || list);
60
+ if (!fragment) {
61
+ try {
62
+ model = await getSchema(pascalcase(singularize(cellName)));
63
+ idName = getIdName(model);
64
+ idType = getIdType(model);
65
+ typeName = model.name;
66
+ mockIdValues = idType === "String" ? mockIdValues.map((value) => `'${value}'`) : mockIdValues;
67
+ } catch {
68
+ idType = "Int";
69
+ }
47
70
  }
48
71
  if (shouldGenerateList) {
49
72
  cellName = forcePluralizeWord(cellName);
50
73
  templateNameSuffix = "List";
51
74
  }
52
75
  let operationName = query;
53
- if (operationName) {
76
+ let fragmentName;
77
+ let fragmentOnType;
78
+ let fragmentPropName;
79
+ if (fragment) {
80
+ const fragmentTypeVariants = nameVariants(fragment);
81
+ fragmentOnType = fragmentTypeVariants.pascalName;
82
+ fragmentPropName = fragmentTypeVariants.camelName;
83
+ fragmentName = `${nameVariants(cellName).pascalName}Cell_${fragmentPropName}`;
84
+ } else if (operationName) {
54
85
  const userSpecifiedOperationNameIsUnique = await operationNameIsUnique(operationName);
55
86
  if (!userSpecifiedOperationNameIsUnique) {
56
87
  throw new Error(`Specified query name: "${operationName}" is not unique!`);
@@ -67,11 +98,20 @@ const files = async ({
67
98
  extension,
68
99
  webPathSection: CEDAR_WEB_PATH_NAME,
69
100
  generator: "cell",
70
- templatePath: `cell${templateNameSuffix}.tsx.template`,
71
- templateVars: {
101
+ templatePath: fragment ? "cellFragment.tsx.template" : `cell${templateNameSuffix}.tsx.template`,
102
+ templateVars: fragment ? {
103
+ fragmentName,
104
+ fragmentOnType,
105
+ camelName: fragmentPropName,
106
+ afterQuery,
107
+ isEmpty
108
+ } : {
72
109
  operationName,
73
110
  idName,
74
- idType
111
+ idType,
112
+ beforeQuery,
113
+ afterQuery,
114
+ isEmpty
75
115
  }
76
116
  });
77
117
  const testFile = await templateForComponentFile({
@@ -80,8 +120,8 @@ const files = async ({
80
120
  extension: `.test${extension}`,
81
121
  webPathSection: CEDAR_WEB_PATH_NAME,
82
122
  generator: "cell",
83
- templatePath: "test.js.template",
84
- templateVars: {
123
+ templatePath: fragment ? "testFragment.js.template" : "test.js.template",
124
+ templateVars: fragment ? { camelName: fragmentPropName } : {
85
125
  idName: shouldGenerateList ? void 0 : idName,
86
126
  mockIdValues: shouldGenerateList ? void 0 : mockIdValues
87
127
  }
@@ -92,7 +132,7 @@ const files = async ({
92
132
  extension: `.stories${extension}`,
93
133
  webPathSection: CEDAR_WEB_PATH_NAME,
94
134
  generator: "cell",
95
- templatePath: "stories.tsx.template"
135
+ templatePath: fragment ? "storiesFragment.tsx.template" : "stories.tsx.template"
96
136
  });
97
137
  const mockFile = await templateForComponentFile({
98
138
  name: cellName,
@@ -101,7 +141,12 @@ const files = async ({
101
141
  webPathSection: CEDAR_WEB_PATH_NAME,
102
142
  generator: "cell",
103
143
  templatePath: `mock${templateNameSuffix}.ts.template`,
104
- templateVars: {
144
+ templateVars: fragment ? {
145
+ idName,
146
+ mockIdValues,
147
+ typeName: fragmentOnType,
148
+ camelName: fragmentPropName
149
+ } : {
105
150
  idName,
106
151
  mockIdValues,
107
152
  typeName
@@ -122,7 +167,10 @@ const files = async ({
122
167
  const handler = createHandler({
123
168
  componentName: "cell",
124
169
  filesFn: files,
125
- includeAdditionalTasks: ({ name: cellName }) => {
170
+ includeAdditionalTasks: ({
171
+ name: cellName,
172
+ fragment
173
+ }) => {
126
174
  return [
127
175
  {
128
176
  title: `Generating types ...`,
@@ -130,7 +178,7 @@ const handler = createHandler({
130
178
  const queryFieldName = nameVariants(
131
179
  removeGeneratorName(cellName, "cell")
132
180
  ).camelName;
133
- const projectHasSdl = await checkProjectForQueryField(queryFieldName);
181
+ const projectHasSdl = Boolean(fragment) || await checkProjectForQueryField(queryFieldName);
134
182
  if (projectHasSdl) {
135
183
  const { errors } = await generateTypes();
136
184
  for (const { message, error } of errors) {
@@ -6,7 +6,9 @@ import type {
6
6
  import type {
7
7
  CellSuccessProps,
8
8
  CellFailureProps,
9
- TypedDocumentNode,
9
+ TypedDocumentNode,<% if (beforeQuery) { %>
10
+ CellBeforeQueryResult,<% } %><% if (afterQuery || isEmpty) { %>
11
+ DataObject,<% } %>
10
12
  } from '@cedarjs/web'
11
13
 
12
14
  export const QUERY: TypedDocumentNode<
@@ -19,7 +21,24 @@ export const QUERY: TypedDocumentNode<
19
21
  }
20
22
  }
21
23
  `
22
-
24
+ <% if (beforeQuery) { %>
25
+ export const beforeQuery = (
26
+ props: ${operationName}Variables,
27
+ ): CellBeforeQueryResult<${operationName}Variables> => {
28
+ return { variables: props, fetchPolicy: 'cache-and-network' }
29
+ }
30
+ <% } %><% if (isEmpty) { %>
31
+ export const isEmpty = (
32
+ data: DataObject,
33
+ { isDataEmpty }: { isDataEmpty: (data: DataObject) => boolean },
34
+ ) => {
35
+ return isDataEmpty(data)
36
+ }
37
+ <% } %><% if (afterQuery) { %>
38
+ export const afterQuery = (data: DataObject): DataObject => {
39
+ return data
40
+ }
41
+ <% } %>
23
42
  export const Loading = () => <div>Loading...</div>
24
43
 
25
44
  export const Empty = () => <div>Empty</div>
@@ -3,7 +3,9 @@ import type { ${operationName}, ${operationName}Variables } from 'types/graphql
3
3
  import type {
4
4
  CellSuccessProps,
5
5
  CellFailureProps,
6
- TypedDocumentNode,
6
+ TypedDocumentNode,<% if (beforeQuery) { %>
7
+ CellBeforeQueryResult,<% } %><% if (afterQuery || isEmpty) { %>
8
+ DataObject,<% } %>
7
9
  } from '@cedarjs/web'
8
10
 
9
11
  export const QUERY: TypedDocumentNode<
@@ -16,7 +18,24 @@ export const QUERY: TypedDocumentNode<
16
18
  }
17
19
  }
18
20
  `
19
-
21
+ <% if (beforeQuery) { %>
22
+ export const beforeQuery = (
23
+ props: ${operationName}Variables,
24
+ ): CellBeforeQueryResult<${operationName}Variables> => {
25
+ return { variables: props, fetchPolicy: 'cache-and-network' }
26
+ }
27
+ <% } %><% if (isEmpty) { %>
28
+ export const isEmpty = (
29
+ data: DataObject,
30
+ { isDataEmpty }: { isDataEmpty: (data: DataObject) => boolean },
31
+ ) => {
32
+ return isDataEmpty(data)
33
+ }
34
+ <% } %><% if (afterQuery) { %>
35
+ export const afterQuery = (data: DataObject): DataObject => {
36
+ return data
37
+ }
38
+ <% } %>
20
39
  export const Loading = () => <div>Loading...</div>
21
40
 
22
41
  export const Empty = () => <div>Empty</div>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cedarjs/cli",
3
- "version": "6.0.0",
3
+ "version": "7.0.0-canary.2982",
4
4
  "description": "The CedarJS Command Line",
5
5
  "repository": {
6
6
  "type": "git",
@@ -36,17 +36,17 @@
36
36
  "@babel/preset-typescript": "7.29.7",
37
37
  "@babel/traverse": "7.29.8",
38
38
  "@babel/types": "7.29.8",
39
- "@cedarjs/api-server": "6.0.0",
40
- "@cedarjs/babel-config": "6.0.0",
41
- "@cedarjs/cli-helpers": "6.0.0",
42
- "@cedarjs/internal": "6.0.0",
43
- "@cedarjs/prerender": "6.0.0",
44
- "@cedarjs/project-config": "6.0.0",
45
- "@cedarjs/structure": "6.0.0",
46
- "@cedarjs/telemetry": "6.0.0",
47
- "@cedarjs/utils": "6.0.0",
48
- "@cedarjs/vite": "6.0.0",
49
- "@cedarjs/web-server": "6.0.0",
39
+ "@cedarjs/api-server": "7.0.0-canary.2982",
40
+ "@cedarjs/babel-config": "7.0.0-canary.2982",
41
+ "@cedarjs/cli-helpers": "7.0.0-canary.2982",
42
+ "@cedarjs/internal": "7.0.0-canary.2982",
43
+ "@cedarjs/prerender": "7.0.0-canary.2982",
44
+ "@cedarjs/project-config": "7.0.0-canary.2982",
45
+ "@cedarjs/structure": "7.0.0-canary.2982",
46
+ "@cedarjs/telemetry": "7.0.0-canary.2982",
47
+ "@cedarjs/utils": "7.0.0-canary.2982",
48
+ "@cedarjs/vite": "7.0.0-canary.2982",
49
+ "@cedarjs/web-server": "7.0.0-canary.2982",
50
50
  "@listr2/prompt-adapter-enquirer": "4.3.0",
51
51
  "@opentelemetry/api": "1.9.1",
52
52
  "@opentelemetry/core": "1.30.1",
@@ -94,7 +94,7 @@
94
94
  "yargs": "17.7.3"
95
95
  },
96
96
  "devDependencies": {
97
- "@cedarjs/framework-tools": "6.0.0",
97
+ "@cedarjs/framework-tools": "7.0.0-canary.2982",
98
98
  "@prisma/dmmf": "7.8.0",
99
99
  "@types/archiver": "^7.0.0",
100
100
  "memfs": "4.68.1",
@@ -1,133 +0,0 @@
1
- import fs from "node:fs";
2
- import path from "node:path";
3
- import { colors } from "@cedarjs/cli-helpers";
4
- import { formatAddRootPackagesCommand } from "@cedarjs/cli-helpers/packageManager/display";
5
- import { getPaths } from "@cedarjs/project-config";
6
- const FLAT_CONFIG_FILENAMES = [
7
- "eslint.config.js",
8
- "eslint.config.mjs",
9
- "eslint.config.cjs",
10
- "eslint.config.ts",
11
- "eslint.config.mts",
12
- "eslint.config.cts"
13
- ];
14
- const LEGACY_CONFIG_FILENAMES = [
15
- ".eslintrc",
16
- ".eslintrc.js",
17
- ".eslintrc.cjs",
18
- ".eslintrc.mjs",
19
- ".eslintrc.json",
20
- ".eslintrc.yaml",
21
- ".eslintrc.yml"
22
- ];
23
- const MIGRATION_GUIDE_URL = "https://github.com/cedarjs/cedar/blob/main/packages/eslint-config/README.md#migrating-from-legacy-eslintrcjs-config";
24
- function findFlatConfig(base) {
25
- return FLAT_CONFIG_FILENAMES.map(
26
- (filename) => path.join(base, filename)
27
- ).find((configPath) => fs.existsSync(configPath));
28
- }
29
- function readRootPackageJson(base) {
30
- try {
31
- const contents = fs.readFileSync(path.join(base, "package.json"), "utf-8");
32
- const parsed = JSON.parse(contents);
33
- if (parsed && typeof parsed === "object") {
34
- return parsed;
35
- }
36
- return void 0;
37
- } catch {
38
- return void 0;
39
- }
40
- }
41
- function hasLegacyConfig(base, packageJson) {
42
- const hasLegacyConfigFile = LEGACY_CONFIG_FILENAMES.some(
43
- (filename) => fs.existsSync(path.join(base, filename))
44
- );
45
- return hasLegacyConfigFile || Boolean(packageJson?.["eslintConfig"]);
46
- }
47
- function listsEslintConfigPackage(packageJson) {
48
- return ["dependencies", "devDependencies"].some((field) => {
49
- const deps = packageJson[field];
50
- return deps !== null && typeof deps === "object" && "@cedarjs/eslint-config" in deps;
51
- });
52
- }
53
- async function formatAddConfigPackageCommand() {
54
- let version = "";
55
- try {
56
- const packageJson = await import("../../package.json", { with: { type: "json" } });
57
- version = "@" + packageJson.default.version;
58
- } catch {
59
- }
60
- try {
61
- return formatAddRootPackagesCommand(
62
- ["@cedarjs/eslint-config" + version],
63
- true
64
- );
65
- } catch {
66
- return "yarn add -D @cedarjs/eslint-config" + version;
67
- }
68
- }
69
- async function legacyConfigMessage(packageJson) {
70
- const needsConfigPackage = !packageJson || !listsEslintConfigPackage(packageJson);
71
- return [
72
- colors.error("Cedar no longer supports ESLint's legacy config format"),
73
- "",
74
- "Support for `.eslintrc.*` and the `eslintConfig` field in package.json",
75
- "was removed in v6. Create an `eslint.config.mjs` in your project root",
76
- '(`eslint.config.js` if your project is ESM, i.e. has `"type": "module"`):',
77
- "",
78
- colors.tip(" import cedarConfig from '@cedarjs/eslint-config'"),
79
- "",
80
- colors.tip(" export default await cedarConfig()"),
81
- "",
82
- "Then delete your `.eslintrc.*` file and remove the `eslintConfig` field",
83
- "from package.json, moving any custom rules you had into an extra config",
84
- "object after `cedarConfig()`.",
85
- ...needsConfigPackage ? [
86
- "",
87
- "You'll also need `@cedarjs/eslint-config` itself, which is no longer",
88
- "installed for you:",
89
- "",
90
- colors.tip(" " + await formatAddConfigPackageCommand())
91
- ] : [],
92
- "",
93
- "Full migration guide: " + colors.link(MIGRATION_GUIDE_URL)
94
- ].join("\n");
95
- }
96
- async function missingConfigPackageMessage(flatConfigPath) {
97
- return [
98
- colors.error("Cannot find `@cedarjs/eslint-config`"),
99
- "",
100
- `Your ${path.basename(flatConfigPath)} uses \`@cedarjs/eslint-config\`, but`,
101
- "the package isn't listed in your project's package.json. As of v6,",
102
- "`@cedarjs/core` no longer depends on it, so projects have to declare it",
103
- "themselves:",
104
- "",
105
- colors.tip(" " + await formatAddConfigPackageCommand()),
106
- "",
107
- colors.info(
108
- "`@cedarjs/eslint-config` brings its own ESLint, so you do not need to add `eslint` separately."
109
- )
110
- ].join("\n");
111
- }
112
- async function getEslintSetupError() {
113
- const base = getPaths().base;
114
- const packageJson = readRootPackageJson(base);
115
- const flatConfigPath = findFlatConfig(base);
116
- if (!flatConfigPath) {
117
- if (!hasLegacyConfig(base, packageJson)) {
118
- return void 0;
119
- }
120
- return legacyConfigMessage(packageJson);
121
- }
122
- if (!packageJson || listsEslintConfigPackage(packageJson)) {
123
- return void 0;
124
- }
125
- const flatConfig = fs.readFileSync(flatConfigPath, "utf-8");
126
- if (!flatConfig.includes("@cedarjs/eslint-config")) {
127
- return void 0;
128
- }
129
- return missingConfigPackageMessage(flatConfigPath);
130
- }
131
- export {
132
- getEslintSetupError
133
- };