@common-stack/generate-plugin 6.0.6-alpha.32 → 6.0.6-alpha.34

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.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,16 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [6.0.6-alpha.34](https://github.com/cdmbase/common-stack/compare/v6.0.6-alpha.33...v6.0.6-alpha.34) (2024-09-27)
7
+
8
+ ### Bug Fixes
9
+
10
+ - **server-stack:** apollo server redis cache ([2ef1df3](https://github.com/cdmbase/common-stack/commit/2ef1df30176616baf9816c47168687577f7aa8ff))
11
+
12
+ ## [6.0.6-alpha.33](https://github.com/cdmbase/common-stack/compare/v6.0.6-alpha.32...v6.0.6-alpha.33) (2024-09-23)
13
+
14
+ **Note:** Version bump only for package @common-stack/generate-plugin
15
+
6
16
  ## [6.0.6-alpha.32](https://github.com/cdmbase/common-stack/compare/v6.0.6-alpha.31...v6.0.6-alpha.32) (2024-09-22)
7
17
 
8
18
  **Note:** Version bump only for package @common-stack/generate-plugin
@@ -62,7 +62,7 @@
62
62
  "dependencies": {
63
63
  "@apollo/client": "^3.9.0",
64
64
  "@babel/runtime": "^7.20.1",
65
- "@common-stack/server-stack": "6.0.6-alpha.31",
65
+ "@common-stack/server-stack": "6.0.6-alpha.33",
66
66
  "@container-stack/mailing-api-server": "^5.2.1-alpha.2",
67
67
  "@container-stack/territory": "^5.2.1-alpha.2",
68
68
  "@remix-run/node": "~2.10.1",
@@ -10,12 +10,6 @@ scalar Observable
10
10
  scalar JSON
11
11
  scalar JSONObject
12
12
 
13
- directive @cacheControl(maxAge: Int, scope: CacheControlScope) on FIELD_DEFINITION | OBJECT | INTERFACE
14
- enum CacheControlScope {
15
- PUBLIC
16
- PRIVATE
17
- }
18
-
19
13
  """
20
14
  An object with an ID.
21
15
  """
@@ -84,7 +78,7 @@ interface IResourceUtilizationSettings {
84
78
  adminApiNamespace: String
85
79
  }
86
80
 
87
- """
81
+ """
88
82
  Input geometry of the location.
89
83
  List the `longitude` first and then `latitude`
90
84
  - Validate longitude values are between `-180` and `180`
@@ -0,0 +1,192 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import glob from'glob';
4
+
5
+ // Regex to find `use<QueryName>Query` calls, capturing query name and arguments
6
+ const queryPattern = /use([A-Za-z0-9]+Query)\(([^)]*)\)/g;
7
+ const fileExtensions = ['.js', '.ts', '.jsx', '.tsx'];
8
+ const excludeFiles = ['react-hooks', 'generated'];
9
+ const basePath = process.cwd();
10
+
11
+ const findPackageJsonFiles = () => {
12
+ const monorepoRoot = path.join(basePath, '..', '..');
13
+ return new Promise((resolve, reject) => {
14
+ glob(
15
+ `${monorepoRoot}/+(servers|portable-devices|packages|packages-modules)/**/package.json`,
16
+ { nodir: true, ignore: '**/node_modules/**' },
17
+ (err, files) => {
18
+ if (err) reject(`Unable to scan directory: ${err}`);
19
+ resolve(files);
20
+ },
21
+ );
22
+ });
23
+ };
24
+
25
+ // Function to transform componentPath from `lib` to `src` path and handle package names
26
+ const transformComponentPath = (componentPath, packageName) => {
27
+ return componentPath
28
+ .replace('/lib/', '/src/')
29
+ .replace(packageName + '/', '')
30
+ .replace('.js', '');
31
+ };
32
+
33
+ // Normalize file paths for consistent comparison (remove extensions and resolve absolute paths)
34
+ const normalizePath = (filePath) => {
35
+ const resolvedPath = path.resolve(filePath).replace(/\.[jt]sx?$/, ''); // Remove .js, .ts, .jsx, .tsx extensions
36
+ return resolvedPath;
37
+ };
38
+
39
+ // Function to extract component paths and queries from routes.json
40
+ async function extractComponentPathsAndQueries(routesFile, packageName) {
41
+ let componentQueries = {};
42
+
43
+ try {
44
+ const content = await fs.promises.readFile(routesFile, 'utf-8');
45
+ const routes = JSON.parse(content);
46
+
47
+ // Traverse routes to extract componentPath and queries
48
+ routes.forEach(routeObj => {
49
+ Object.keys(routeObj).forEach(routeKey => {
50
+ const route = routeObj[routeKey];
51
+ if (route.hasComponent && route.componentPath) {
52
+ const transformedPath = normalizePath(transformComponentPath(route.componentPath, packageName));
53
+ const queries = Object.keys(route.queries || {});
54
+ if (queries.length > 0) {
55
+ componentQueries[transformedPath] = queries;
56
+ }
57
+ }
58
+ });
59
+ });
60
+
61
+ console.log('Extracted components and queries from routes.json:', componentQueries);
62
+ } catch (err) {
63
+ console.error(`Error reading routes.json file: ${routesFile}`, err);
64
+ }
65
+
66
+ return componentQueries;
67
+ }
68
+
69
+ // Function to recursively search for useQuery in files and validate if the file is a data component
70
+ async function checkFiles(dir, componentQueries) {
71
+ const files = await fs.promises.readdir(dir, { withFileTypes: true });
72
+
73
+ for (const file of files) {
74
+ const fullPath = path.join(dir, file.name);
75
+
76
+ if (file.isDirectory()) {
77
+ // Recurse into subdirectories
78
+ await checkFiles(fullPath, componentQueries);
79
+ } else if (fileExtensions.includes(path.extname(file.name)) && !excludeFiles.includes(path.parse(file.name).name)) {
80
+ await checkForInvalidQueryUsage(fullPath, componentQueries);
81
+ }
82
+ }
83
+ }
84
+
85
+ // Function to extract query details from a match
86
+ const extractQueryDetails = (queryMatch) => {
87
+ const [fullMatch, queryName, queryArgs] = queryMatch;
88
+ return {
89
+ queryName,
90
+ fullMatch,
91
+ queryArgs: queryArgs.trim(),
92
+ isLazyQuery: queryName.endsWith('LazyQuery'),
93
+ hasCacheOnlyFetchPolicy: queryArgs.includes("fetchPolicy: 'cache-only'"),
94
+ };
95
+ };
96
+
97
+ // Function to check for query usage in a file and validate if it is allowed
98
+ async function checkForInvalidQueryUsage(filePath, componentQueries) {
99
+ try {
100
+ const content = await fs.promises.readFile(filePath, 'utf-8');
101
+ let match;
102
+ const usedQueries = [];
103
+ const queriesMissingCacheOnly = [];
104
+ const queriesWithNoArguments = [];
105
+ const queryNames = [];
106
+
107
+ while ((match = queryPattern.exec(content)) !== null) {
108
+ const queryDetails = extractQueryDetails(match);
109
+ usedQueries.push(queryDetails);
110
+ queryNames.push(queryDetails.queryName);
111
+
112
+ // Ignore LazyQuery for fetchPolicy check
113
+ if (!queryDetails.isLazyQuery && !queryDetails.hasCacheOnlyFetchPolicy) {
114
+ queriesMissingCacheOnly.push(queryDetails.queryName);
115
+ }
116
+
117
+ // Check for queries without arguments
118
+ if (queryDetails.queryArgs === '') {
119
+ queriesWithNoArguments.push(queryDetails.queryName);
120
+ }
121
+ }
122
+
123
+ if (usedQueries.length > 0) {
124
+ const normalizedFilePath = normalizePath(filePath);
125
+ const isDataComponent = componentQueries[normalizedFilePath];
126
+
127
+ const warnings = [];
128
+
129
+ // Warning if a data component has more than 3 queries
130
+ if (isDataComponent && usedQueries.length > 3) {
131
+ warnings.push(`Warning: Data component "${filePath}" has more than 3 queries. Consider splitting into multiple data components with a layout component as the parent.`);
132
+ }
133
+
134
+ // Consolidate missing cache-only fetchPolicy warnings
135
+ if (queriesMissingCacheOnly.length > 0) {
136
+ warnings.push(`Warning: The following queries are missing fetchPolicy: "cache-only":\n - ${queriesMissingCacheOnly.join('\n - ')}`);
137
+ }
138
+
139
+ // Warn for queries without arguments (which could fetch all data)
140
+ if (queriesWithNoArguments.length > 0) {
141
+ warnings.push(`Warning: The following queries have no arguments and could fetch all data. Consider narrowing the search and using pagination:\n - ${queriesWithNoArguments.join('\n - ')}`);
142
+ }
143
+
144
+ // Warning for non-data components with queries
145
+ if (!isDataComponent) {
146
+ warnings.push(`Warning: File "${filePath}" is a non-data component (not listed in routes.json) but is using queries.`);
147
+ }
148
+
149
+ // Display queries first, then display all warnings together
150
+ if (warnings.length > 0) {
151
+ console.log('----------------------------------------------------');
152
+ console.log(`File: ${filePath}`);
153
+ console.log('Queries:');
154
+ usedQueries.forEach(query => console.log(` - ${query.fullMatch}`)); // Display full query details
155
+ console.log('\nWarnings:');
156
+ warnings.forEach(warning => console.log(` - ${warning}`));
157
+ console.log('----------------------------------------------------\n');
158
+ }
159
+ }
160
+ } catch (err) {
161
+ console.error(`Error reading file: ${filePath}`, err);
162
+ }
163
+ }
164
+
165
+ // Check in a single package
166
+ async function checkPackage(packageDir, packageName) {
167
+ const routesFile = path.resolve(path.join(packageDir, './lib/routes.json'));
168
+ const sourceDir = path.resolve(path.join(packageDir, './src'));
169
+ let componentQueries = {};
170
+
171
+ if (fs.existsSync(routesFile)) {
172
+ console.log(`Extracting component paths and queries from ${routesFile}...`);
173
+ componentQueries = await extractComponentPathsAndQueries(routesFile, packageName);
174
+ }
175
+
176
+ console.log(`Searching for query usage in files under ${sourceDir}...`);
177
+ await checkFiles(sourceDir, componentQueries);
178
+ }
179
+
180
+ // Run the script
181
+ (async () => {
182
+ console.log('Checking useQuery usage ...');
183
+
184
+ const packageJsonFiles = await findPackageJsonFiles();
185
+ for (const file of packageJsonFiles) {
186
+ const packageJson = JSON.parse(fs.readFileSync(file, 'utf8'));
187
+ const packageDir = path.dirname(file);
188
+ await checkPackage(packageDir, packageJson.name);
189
+ }
190
+
191
+ console.log('Completed checking useQuery usage.');
192
+ })();
@@ -147,7 +147,7 @@
147
147
  "@babel/register": "^7.18.9",
148
148
  "@babel/runtime": "^7.20.1",
149
149
  "@common-stack/env-list-loader": "6.0.6-alpha.5",
150
- "@common-stack/generate-plugin": "6.0.6-alpha.31",
150
+ "@common-stack/generate-plugin": "6.0.6-alpha.33",
151
151
  "@emotion/babel-plugin": "^11.11.0",
152
152
  "@graphql-codegen/add": "^5.0.2",
153
153
  "@graphql-codegen/cli": "^5.0.2",
@@ -28,7 +28,7 @@
28
28
  "@common-stack/client-core": "6.0.6-alpha.5",
29
29
  "@common-stack/core": "6.0.6-alpha.5",
30
30
  "@common-stack/server-core": "6.0.6-alpha.5",
31
- "@common-stack/server-stack": "6.0.6-alpha.31",
31
+ "@common-stack/server-stack": "6.0.6-alpha.33",
32
32
  "@common-stack/store-mongo": "6.0.6-alpha.5",
33
33
  "@container-stack/mailing-api": "5.2.1-alpha.1",
34
34
  "helmet": "^3.21.2",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@common-stack/generate-plugin",
3
- "version": "6.0.6-alpha.32",
3
+ "version": "6.0.6-alpha.34",
4
4
  "type": "module",
5
5
  "main": "./lib/index.mjs",
6
6
  "typings": "./lib/index.d.ts",
@@ -25,5 +25,5 @@
25
25
  },
26
26
  "executors": "./executors.json",
27
27
  "generators": "./generators.json",
28
- "gitHead": "dbc730a62e3bae08f50cc035918d6e4d6664470a"
28
+ "gitHead": "a18390ead2b175b764970e162055142a2f694e9e"
29
29
  }
@@ -147,7 +147,7 @@
147
147
  "@babel/register": "^7.18.9",
148
148
  "@babel/runtime": "^7.20.1",
149
149
  "@common-stack/env-list-loader": "6.0.6-alpha.5",
150
- "@common-stack/generate-plugin": "6.0.6-alpha.31",
150
+ "@common-stack/generate-plugin": "6.0.6-alpha.33",
151
151
  "@emotion/babel-plugin": "^11.11.0",
152
152
  "@graphql-codegen/add": "^5.0.2",
153
153
  "@graphql-codegen/cli": "^5.0.2",
@@ -28,7 +28,7 @@
28
28
  "@common-stack/client-core": "6.0.6-alpha.5",
29
29
  "@common-stack/core": "6.0.6-alpha.5",
30
30
  "@common-stack/server-core": "6.0.6-alpha.5",
31
- "@common-stack/server-stack": "6.0.6-alpha.31",
31
+ "@common-stack/server-stack": "6.0.6-alpha.33",
32
32
  "@common-stack/store-mongo": "6.0.6-alpha.5",
33
33
  "@container-stack/mailing-api": "5.2.1-alpha.1",
34
34
  "helmet": "^3.21.2",