@query-key-gen/used-generator 0.3.0 → 0.4.0

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/package.json CHANGED
@@ -1,36 +1,36 @@
1
1
  {
2
- "name": "@query-key-gen/used-generator",
3
- "version": "0.3.0",
4
- "description": "",
5
- "type": "module",
6
- "exports": {
7
- ".": {
8
- "types": "./lib/index.d.ts",
9
- "require": "./lib/index.mjs",
10
- "import": "./lib/index.js"
2
+ "name": "@query-key-gen/used-generator",
3
+ "version": "0.4.0",
4
+ "description": "",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./lib/index.d.ts",
9
+ "require": "./lib/index.mjs",
10
+ "import": "./lib/index.js"
11
+ }
12
+ },
13
+ "scripts": {
14
+ "dev": "tsup --watch",
15
+ "build": "tsup --clean",
16
+ "start": "node lib/index.js",
17
+ "watch:build": "nodemon --watch './src' --ext '*' --exec 'pnpm build' "
18
+ },
19
+ "keywords": [],
20
+ "author": "",
21
+ "license": "ISC",
22
+ "dependencies": {
23
+ "fast-glob": "^3.3.3",
24
+ "tsup": "^8.3.6",
25
+ "typescript": "~5.7.2",
26
+ "vite": "^6.1.0",
27
+ "zod": "^3.24.2"
28
+ },
29
+ "devDependencies": {
30
+ "@query-key-gen/generator-common": "workspace:*",
31
+ "@types/node": "^22.13.14"
32
+ },
33
+ "publishConfig": {
34
+ "access": "public"
11
35
  }
12
- },
13
- "keywords": [],
14
- "author": "",
15
- "license": "ISC",
16
- "dependencies": {
17
- "fast-glob": "^3.3.3",
18
- "tsup": "^8.3.6",
19
- "typescript": "~5.7.2",
20
- "vite": "^6.1.0",
21
- "zod": "^3.24.2"
22
- },
23
- "devDependencies": {
24
- "@types/node": "^22.13.14",
25
- "@query-key-gen/generator-common": "0.0.0"
26
- },
27
- "publishConfig": {
28
- "access": "public"
29
- },
30
- "scripts": {
31
- "dev": "tsup --watch",
32
- "build": "tsup --clean",
33
- "start": "node lib/index.js",
34
- "watch:build": "nodemon --watch './src' --ext '*' --exec 'pnpm build' "
35
- }
36
- }
36
+ }
package/lib/index.cjs DELETED
@@ -1,374 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __defProps = Object.defineProperties;
5
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
7
- var __getOwnPropNames = Object.getOwnPropertyNames;
8
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
9
- var __getProtoOf = Object.getPrototypeOf;
10
- var __hasOwnProp = Object.prototype.hasOwnProperty;
11
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
12
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
13
- var __spreadValues = (a, b) => {
14
- for (var prop in b || (b = {}))
15
- if (__hasOwnProp.call(b, prop))
16
- __defNormalProp(a, prop, b[prop]);
17
- if (__getOwnPropSymbols)
18
- for (var prop of __getOwnPropSymbols(b)) {
19
- if (__propIsEnum.call(b, prop))
20
- __defNormalProp(a, prop, b[prop]);
21
- }
22
- return a;
23
- };
24
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
25
- var __export = (target, all) => {
26
- for (var name in all)
27
- __defProp(target, name, { get: all[name], enumerable: true });
28
- };
29
- var __copyProps = (to, from, except, desc) => {
30
- if (from && typeof from === "object" || typeof from === "function") {
31
- for (let key of __getOwnPropNames(from))
32
- if (!__hasOwnProp.call(to, key) && key !== except)
33
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
34
- }
35
- return to;
36
- };
37
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
38
- // If the importer is in node compatibility mode or this is not an ESM
39
- // file that has been converted to a CommonJS file using a Babel-
40
- // compatible transform (i.e. "__esModule" has not been set), then set
41
- // "default" to the CommonJS "module.exports" for node compatibility.
42
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
43
- mod
44
- ));
45
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
46
- var __async = (__this, __arguments, generator) => {
47
- return new Promise((resolve, reject) => {
48
- var fulfilled = (value) => {
49
- try {
50
- step(generator.next(value));
51
- } catch (e) {
52
- reject(e);
53
- }
54
- };
55
- var rejected = (value) => {
56
- try {
57
- step(generator.throw(value));
58
- } catch (e) {
59
- reject(e);
60
- }
61
- };
62
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
63
- step((generator = generator.apply(__this, __arguments)).next());
64
- });
65
- };
66
-
67
- // src/index.ts
68
- var index_exports = {};
69
- __export(index_exports, {
70
- default: () => QueryKeyUsedPlugin
71
- });
72
- module.exports = __toCommonJS(index_exports);
73
- var import_path4 = __toESM(require("path"), 1);
74
-
75
- // ../common/src/core/Programmer.ts
76
- var import_fast_glob = __toESM(require("fast-glob"), 1);
77
- var import_path = __toESM(require("path"), 1);
78
- var import_typescript = __toESM(require("typescript"), 1);
79
- var Programmer = class {
80
- constructor(config) {
81
- // getCompilerOptions = () => {
82
- // const { options: compilerOptions } = ts.parseJsonConfigFileContent(
83
- // ts.readConfigFile('tsconfig.node.json', ts.sys.readFile).config,
84
- // {
85
- // fileExists: ts.sys.fileExists,
86
- // readFile: ts.sys.readFile,
87
- // readDirectory: ts.sys.readDirectory,
88
- // useCaseSensitiveFileNames: ts.sys.useCaseSensitiveFileNames
89
- // },
90
- // path.dirname('.')
91
- // );
92
- // return compilerOptions;
93
- // };
94
- this.findFiles = (program) => __async(this, null, function* () {
95
- const config = this.config;
96
- const sourceFiles = program.getSourceFiles().filter((item) => {
97
- return !item.isDeclarationFile;
98
- });
99
- const outputFile = import_typescript.default.createSourceFile(config.output, "", this.compilerOptions.target);
100
- const projectFiles = sourceFiles.filter((item) => {
101
- const relativePath = import_path.default.relative(process.cwd(), item.fileName);
102
- const outputPath = import_path.default.relative(process.cwd(), config.output);
103
- return !relativePath.includes(outputPath);
104
- });
105
- return {
106
- outputFile,
107
- projectFiles
108
- };
109
- });
110
- this.config = config;
111
- this.compilerOptions = {
112
- target: import_typescript.default.ScriptTarget.ES2015
113
- };
114
- }
115
- // Getter로 createProgram 정의
116
- get create() {
117
- return () => __async(this, null, function* () {
118
- const files = yield (0, import_fast_glob.default)(this.config.path, {
119
- onlyFiles: true
120
- });
121
- const program = import_typescript.default.createProgram({
122
- rootNames: files,
123
- options: this.compilerOptions
124
- });
125
- return {
126
- program,
127
- checker: program.getTypeChecker()
128
- };
129
- });
130
- }
131
- };
132
-
133
- // ../common/src/core/GenericRunner.ts
134
- var GenericRunner = class {
135
- constructor(programmer, config) {
136
- this.programmer = programmer;
137
- this.config = config;
138
- }
139
- process() {
140
- return __async(this, null, function* () {
141
- console.log("\u{1F527} Base process logic");
142
- });
143
- }
144
- execute() {
145
- this.process();
146
- }
147
- };
148
-
149
- // src/core/QueryKeyUsedInfoGenerator.ts
150
- var import_fs2 = __toESM(require("fs"), 1);
151
- var import_typescript3 = __toESM(require("typescript"), 1);
152
-
153
- // src/queryFinder/QueryKeyUsedFinder.ts
154
- var import_typescript2 = __toESM(require("typescript"), 1);
155
- var QueryKeyUsedFinder;
156
- ((QueryKeyUsedFinder2) => {
157
- QueryKeyUsedFinder2.find = (sourceFiles, globalQueryKeyName) => {
158
- const globalQueryKeys = sourceFiles.map((sourceFile) => {
159
- return findGlobalQueryKeys(sourceFile, globalQueryKeyName);
160
- });
161
- const queryKeyByDeclaration = globalQueryKeys.flatMap((keys) => {
162
- const usedDeclarations = keys.findNode.map((key) => {
163
- const declaration = findUsedDeclaration(key.node);
164
- return __spreadProps(__spreadValues({}, key), {
165
- declaration,
166
- sourceFile: keys.sourceFile
167
- });
168
- });
169
- return usedDeclarations;
170
- });
171
- return queryKeyByDeclaration;
172
- };
173
- const findGlobalQueryKeys = (node, globalQueryKeyName) => {
174
- const findNode = [];
175
- const findText = [globalQueryKeyName];
176
- const visitor = (node2) => {
177
- if (import_typescript2.default.isImportDeclaration(node2)) {
178
- return;
179
- }
180
- if (import_typescript2.default.isCallExpression(node2) || import_typescript2.default.isIdentifier(node2)) {
181
- if (import_typescript2.default.isIdentifier(node2) && findText.includes(node2.getText())) {
182
- const grandParent = node2.parent.parent;
183
- if (import_typescript2.default.isPropertyAccessExpression(node2.parent)) {
184
- const isGlobalQueryKey = findText.some(
185
- (text) => grandParent.getText().includes(text)
186
- );
187
- findNode.push({
188
- node: node2,
189
- parent: isGlobalQueryKey ? grandParent : null
190
- });
191
- }
192
- }
193
- }
194
- import_typescript2.default.forEachChild(node2, visitor);
195
- };
196
- visitor(node);
197
- return { findNode, sourceFile: node };
198
- };
199
- const findUsedDeclaration = (node) => {
200
- let parentNode = node.parent;
201
- let declaration = null;
202
- while (parentNode) {
203
- if (import_typescript2.default.isFunctionDeclaration(parentNode)) {
204
- declaration = parentNode;
205
- break;
206
- }
207
- if (import_typescript2.default.isVariableDeclaration(parentNode) && parentNode.initializer && import_typescript2.default.isArrowFunction(parentNode.initializer)) {
208
- declaration = parentNode;
209
- break;
210
- }
211
- parentNode = parentNode.parent;
212
- }
213
- return declaration;
214
- };
215
- })(QueryKeyUsedFinder || (QueryKeyUsedFinder = {}));
216
-
217
- // src/utils/index.ts
218
- var import_child_process = require("child_process");
219
- var import_fs = __toESM(require("fs"), 1);
220
- var import_path2 = __toESM(require("path"), 1);
221
- var prettify = (output) => {
222
- const prettier = import_path2.default.resolve("./node_modules/.bin/prettier");
223
- if (import_fs.default.existsSync(prettier)) (0, import_child_process.execSync)(`${prettier} --write --cache ${output}`);
224
- };
225
- var logger = (() => {
226
- const prefix = "[QUERY-KEY-USED-GENERATOR]: ";
227
- const log = () => {
228
- return {
229
- warn: (msg) => {
230
- console.warn(`[WARN]${prefix}${msg}`);
231
- },
232
- error: (msg) => {
233
- console.error(`[ERROR]${prefix}${msg}`);
234
- },
235
- info: (msg) => {
236
- console.log(`[INFO]${prefix}${msg}`);
237
- }
238
- };
239
- };
240
- return log();
241
- })();
242
-
243
- // src/core/QueryKeyUsedInfoGenerator.ts
244
- var import_path3 = __toESM(require("path"), 1);
245
- var QueryKeyUsedInfoGenerator = class extends GenericRunner {
246
- constructor() {
247
- super(...arguments);
248
- this.process = () => __async(this, null, function* () {
249
- const { program } = yield this.programmer.create();
250
- const { projectFiles } = yield this.programmer.findFiles(program);
251
- const queryKeyUsedInfo = QueryKeyUsedFinder.find(
252
- projectFiles,
253
- this.config.globalQueryKeyName
254
- );
255
- const result = this.makeQueryKeyUsedInfo(queryKeyUsedInfo);
256
- this.writeInfo(result);
257
- prettify(this.config.output);
258
- });
259
- this.makeQueryKeyUsedInfo = (queryKeyUsedInfo) => {
260
- const result = queryKeyUsedInfo.map((item) => {
261
- var _a, _b;
262
- const { declaration, sourceFile, parent } = item;
263
- const declarationName = function() {
264
- var _a2;
265
- if (!declaration) {
266
- return;
267
- }
268
- if (import_typescript3.default.isVariableDeclaration(declaration)) {
269
- return declaration.name.getText();
270
- }
271
- if (import_typescript3.default.isFunctionDeclaration(declaration)) {
272
- return (_a2 = declaration.name) == null ? void 0 : _a2.getText();
273
- }
274
- return;
275
- }();
276
- const fileName = import_path3.default.relative(process.cwd(), sourceFile.fileName).replace("src/", "");
277
- return {
278
- sourceFile: {
279
- name: fileName
280
- },
281
- ["query-key"]: {
282
- name: parent == null ? void 0 : parent.getText(),
283
- pos: parent == null ? void 0 : parent.pos,
284
- end: parent == null ? void 0 : parent.end
285
- },
286
- func: {
287
- name: declarationName,
288
- pos: (_a = declaration == null ? void 0 : declaration.pos) != null ? _a : 0,
289
- end: (_b = declaration == null ? void 0 : declaration.end) != null ? _b : 0
290
- }
291
- };
292
- });
293
- return result;
294
- };
295
- this.writeType = () => {
296
- return `
297
- interface QueryKeyUsedInfo {
298
- sourceFile: {
299
- name: string;
300
-
301
- };
302
- ["query-key"]: {
303
- name: string;
304
- pos: number;
305
- end: number;
306
- };
307
- func: {
308
- name: string;
309
- pos: number;
310
- end: number;
311
- };
312
- }
313
- `;
314
- };
315
- this.writeInfo = (result) => {
316
- const content = `
317
- ${this.writeType()}
318
- export const queryKeyUsedInfo : QueryKeyUsedInfo[] = ${JSON.stringify(result)};
319
- `;
320
- import_fs2.default.writeFileSync(this.config.output, content);
321
- };
322
- }
323
- };
324
-
325
- // src/types/config.ts
326
- var import_zod = require("zod");
327
- var configSchema = import_zod.z.object({
328
- output: import_zod.z.string().default("src/query-key-used-info.ts"),
329
- path: import_zod.z.string().default("./src/**/*.{jsx,tsx,ts}"),
330
- globalQueryKeyName: import_zod.z.string().default("globalQueryKeys"),
331
- ignoreFiles: import_zod.z.array(import_zod.z.string()).default([])
332
- });
333
- var defaultConfig = {
334
- output: "./src/query-key-used-info.ts",
335
- path: "./src/**/*.{jsx,tsx,ts}",
336
- ignoreFiles: [".d.ts", "queryKeys.ts"],
337
- globalQueryKeyName: "globalQueryKeys"
338
- };
339
-
340
- // src/index.ts
341
- function QueryKeyUsedPlugin(_config) {
342
- var _a;
343
- const config = configSchema.parse(__spreadProps(__spreadValues({}, defaultConfig), {
344
- ignoreFiles: [...defaultConfig.ignoreFiles, ...(_a = _config == null ? void 0 : _config.ignoreFiles) != null ? _a : []]
345
- }));
346
- const rootDir = process.cwd();
347
- const ignoreFiles = config.ignoreFiles;
348
- const program = new Programmer(config);
349
- const generator = new QueryKeyUsedInfoGenerator(program, config);
350
- return {
351
- name: "query-key-used-generator",
352
- enforce: "pre",
353
- configureServer(server) {
354
- const listener = (absolutePath = "") => {
355
- const filePath = import_path4.default.relative(rootDir, absolutePath);
356
- const outputPath = import_path4.default.relative(rootDir, config.output);
357
- if (!filePath.startsWith("src") || filePath.startsWith(outputPath)) return;
358
- if (ignoreFiles.some((item) => {
359
- const ignorePath = import_path4.default.relative(process.cwd(), item);
360
- return filePath.includes(ignorePath);
361
- })) {
362
- return;
363
- }
364
- generator.execute();
365
- };
366
- server.watcher.on("add", listener);
367
- server.watcher.on("change", listener);
368
- server.watcher.on("unlink", listener);
369
- },
370
- buildStart() {
371
- generator.execute();
372
- }
373
- };
374
- }
package/lib/index.d.cts DELETED
@@ -1,24 +0,0 @@
1
- import { Plugin } from 'vite';
2
- import { z } from 'zod';
3
-
4
- declare const configSchema: z.ZodObject<{
5
- output: z.ZodDefault<z.ZodString>;
6
- path: z.ZodDefault<z.ZodString>;
7
- globalQueryKeyName: z.ZodDefault<z.ZodString>;
8
- ignoreFiles: z.ZodDefault<z.ZodArray<z.ZodString, "many">>;
9
- }, "strip", z.ZodTypeAny, {
10
- output: string;
11
- path: string;
12
- globalQueryKeyName: string;
13
- ignoreFiles: string[];
14
- }, {
15
- output?: string | undefined;
16
- path?: string | undefined;
17
- globalQueryKeyName?: string | undefined;
18
- ignoreFiles?: string[] | undefined;
19
- }>;
20
- type ConfigOptions = z.infer<typeof configSchema>;
21
-
22
- declare function QueryKeyUsedPlugin(_config?: Omit<ConfigOptions, 'path'>): Plugin;
23
-
24
- export { QueryKeyUsedPlugin as default };
package/lib/index.d.ts DELETED
@@ -1,24 +0,0 @@
1
- import { Plugin } from 'vite';
2
- import { z } from 'zod';
3
-
4
- declare const configSchema: z.ZodObject<{
5
- output: z.ZodDefault<z.ZodString>;
6
- path: z.ZodDefault<z.ZodString>;
7
- globalQueryKeyName: z.ZodDefault<z.ZodString>;
8
- ignoreFiles: z.ZodDefault<z.ZodArray<z.ZodString, "many">>;
9
- }, "strip", z.ZodTypeAny, {
10
- output: string;
11
- path: string;
12
- globalQueryKeyName: string;
13
- ignoreFiles: string[];
14
- }, {
15
- output?: string | undefined;
16
- path?: string | undefined;
17
- globalQueryKeyName?: string | undefined;
18
- ignoreFiles?: string[] | undefined;
19
- }>;
20
- type ConfigOptions = z.infer<typeof configSchema>;
21
-
22
- declare function QueryKeyUsedPlugin(_config?: Omit<ConfigOptions, 'path'>): Plugin;
23
-
24
- export { QueryKeyUsedPlugin as default };
package/lib/index.js DELETED
@@ -1,346 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __defProps = Object.defineProperties;
3
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
- var __spreadValues = (a, b) => {
9
- for (var prop in b || (b = {}))
10
- if (__hasOwnProp.call(b, prop))
11
- __defNormalProp(a, prop, b[prop]);
12
- if (__getOwnPropSymbols)
13
- for (var prop of __getOwnPropSymbols(b)) {
14
- if (__propIsEnum.call(b, prop))
15
- __defNormalProp(a, prop, b[prop]);
16
- }
17
- return a;
18
- };
19
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
- var __async = (__this, __arguments, generator) => {
21
- return new Promise((resolve, reject) => {
22
- var fulfilled = (value) => {
23
- try {
24
- step(generator.next(value));
25
- } catch (e) {
26
- reject(e);
27
- }
28
- };
29
- var rejected = (value) => {
30
- try {
31
- step(generator.throw(value));
32
- } catch (e) {
33
- reject(e);
34
- }
35
- };
36
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
37
- step((generator = generator.apply(__this, __arguments)).next());
38
- });
39
- };
40
-
41
- // src/index.ts
42
- import path4 from "path";
43
-
44
- // ../common/src/core/Programmer.ts
45
- import fg from "fast-glob";
46
- import path from "path";
47
- import ts from "typescript";
48
- var Programmer = class {
49
- constructor(config) {
50
- // getCompilerOptions = () => {
51
- // const { options: compilerOptions } = ts.parseJsonConfigFileContent(
52
- // ts.readConfigFile('tsconfig.node.json', ts.sys.readFile).config,
53
- // {
54
- // fileExists: ts.sys.fileExists,
55
- // readFile: ts.sys.readFile,
56
- // readDirectory: ts.sys.readDirectory,
57
- // useCaseSensitiveFileNames: ts.sys.useCaseSensitiveFileNames
58
- // },
59
- // path.dirname('.')
60
- // );
61
- // return compilerOptions;
62
- // };
63
- this.findFiles = (program) => __async(this, null, function* () {
64
- const config = this.config;
65
- const sourceFiles = program.getSourceFiles().filter((item) => {
66
- return !item.isDeclarationFile;
67
- });
68
- const outputFile = ts.createSourceFile(config.output, "", this.compilerOptions.target);
69
- const projectFiles = sourceFiles.filter((item) => {
70
- const relativePath = path.relative(process.cwd(), item.fileName);
71
- const outputPath = path.relative(process.cwd(), config.output);
72
- return !relativePath.includes(outputPath);
73
- });
74
- return {
75
- outputFile,
76
- projectFiles
77
- };
78
- });
79
- this.config = config;
80
- this.compilerOptions = {
81
- target: ts.ScriptTarget.ES2015
82
- };
83
- }
84
- // Getter로 createProgram 정의
85
- get create() {
86
- return () => __async(this, null, function* () {
87
- const files = yield fg(this.config.path, {
88
- onlyFiles: true
89
- });
90
- const program = ts.createProgram({
91
- rootNames: files,
92
- options: this.compilerOptions
93
- });
94
- return {
95
- program,
96
- checker: program.getTypeChecker()
97
- };
98
- });
99
- }
100
- };
101
-
102
- // ../common/src/core/GenericRunner.ts
103
- var GenericRunner = class {
104
- constructor(programmer, config) {
105
- this.programmer = programmer;
106
- this.config = config;
107
- }
108
- process() {
109
- return __async(this, null, function* () {
110
- console.log("\u{1F527} Base process logic");
111
- });
112
- }
113
- execute() {
114
- this.process();
115
- }
116
- };
117
-
118
- // src/core/QueryKeyUsedInfoGenerator.ts
119
- import fs2 from "fs";
120
- import ts3 from "typescript";
121
-
122
- // src/queryFinder/QueryKeyUsedFinder.ts
123
- import ts2 from "typescript";
124
- var QueryKeyUsedFinder;
125
- ((QueryKeyUsedFinder2) => {
126
- QueryKeyUsedFinder2.find = (sourceFiles, globalQueryKeyName) => {
127
- const globalQueryKeys = sourceFiles.map((sourceFile) => {
128
- return findGlobalQueryKeys(sourceFile, globalQueryKeyName);
129
- });
130
- const queryKeyByDeclaration = globalQueryKeys.flatMap((keys) => {
131
- const usedDeclarations = keys.findNode.map((key) => {
132
- const declaration = findUsedDeclaration(key.node);
133
- return __spreadProps(__spreadValues({}, key), {
134
- declaration,
135
- sourceFile: keys.sourceFile
136
- });
137
- });
138
- return usedDeclarations;
139
- });
140
- return queryKeyByDeclaration;
141
- };
142
- const findGlobalQueryKeys = (node, globalQueryKeyName) => {
143
- const findNode = [];
144
- const findText = [globalQueryKeyName];
145
- const visitor = (node2) => {
146
- if (ts2.isImportDeclaration(node2)) {
147
- return;
148
- }
149
- if (ts2.isCallExpression(node2) || ts2.isIdentifier(node2)) {
150
- if (ts2.isIdentifier(node2) && findText.includes(node2.getText())) {
151
- const grandParent = node2.parent.parent;
152
- if (ts2.isPropertyAccessExpression(node2.parent)) {
153
- const isGlobalQueryKey = findText.some(
154
- (text) => grandParent.getText().includes(text)
155
- );
156
- findNode.push({
157
- node: node2,
158
- parent: isGlobalQueryKey ? grandParent : null
159
- });
160
- }
161
- }
162
- }
163
- ts2.forEachChild(node2, visitor);
164
- };
165
- visitor(node);
166
- return { findNode, sourceFile: node };
167
- };
168
- const findUsedDeclaration = (node) => {
169
- let parentNode = node.parent;
170
- let declaration = null;
171
- while (parentNode) {
172
- if (ts2.isFunctionDeclaration(parentNode)) {
173
- declaration = parentNode;
174
- break;
175
- }
176
- if (ts2.isVariableDeclaration(parentNode) && parentNode.initializer && ts2.isArrowFunction(parentNode.initializer)) {
177
- declaration = parentNode;
178
- break;
179
- }
180
- parentNode = parentNode.parent;
181
- }
182
- return declaration;
183
- };
184
- })(QueryKeyUsedFinder || (QueryKeyUsedFinder = {}));
185
-
186
- // src/utils/index.ts
187
- import { execSync } from "child_process";
188
- import fs from "fs";
189
- import path2 from "path";
190
- var prettify = (output) => {
191
- const prettier = path2.resolve("./node_modules/.bin/prettier");
192
- if (fs.existsSync(prettier)) execSync(`${prettier} --write --cache ${output}`);
193
- };
194
- var logger = (() => {
195
- const prefix = "[QUERY-KEY-USED-GENERATOR]: ";
196
- const log = () => {
197
- return {
198
- warn: (msg) => {
199
- console.warn(`[WARN]${prefix}${msg}`);
200
- },
201
- error: (msg) => {
202
- console.error(`[ERROR]${prefix}${msg}`);
203
- },
204
- info: (msg) => {
205
- console.log(`[INFO]${prefix}${msg}`);
206
- }
207
- };
208
- };
209
- return log();
210
- })();
211
-
212
- // src/core/QueryKeyUsedInfoGenerator.ts
213
- import path3 from "path";
214
- var QueryKeyUsedInfoGenerator = class extends GenericRunner {
215
- constructor() {
216
- super(...arguments);
217
- this.process = () => __async(this, null, function* () {
218
- const { program } = yield this.programmer.create();
219
- const { projectFiles } = yield this.programmer.findFiles(program);
220
- const queryKeyUsedInfo = QueryKeyUsedFinder.find(
221
- projectFiles,
222
- this.config.globalQueryKeyName
223
- );
224
- const result = this.makeQueryKeyUsedInfo(queryKeyUsedInfo);
225
- this.writeInfo(result);
226
- prettify(this.config.output);
227
- });
228
- this.makeQueryKeyUsedInfo = (queryKeyUsedInfo) => {
229
- const result = queryKeyUsedInfo.map((item) => {
230
- var _a, _b;
231
- const { declaration, sourceFile, parent } = item;
232
- const declarationName = function() {
233
- var _a2;
234
- if (!declaration) {
235
- return;
236
- }
237
- if (ts3.isVariableDeclaration(declaration)) {
238
- return declaration.name.getText();
239
- }
240
- if (ts3.isFunctionDeclaration(declaration)) {
241
- return (_a2 = declaration.name) == null ? void 0 : _a2.getText();
242
- }
243
- return;
244
- }();
245
- const fileName = path3.relative(process.cwd(), sourceFile.fileName).replace("src/", "");
246
- return {
247
- sourceFile: {
248
- name: fileName
249
- },
250
- ["query-key"]: {
251
- name: parent == null ? void 0 : parent.getText(),
252
- pos: parent == null ? void 0 : parent.pos,
253
- end: parent == null ? void 0 : parent.end
254
- },
255
- func: {
256
- name: declarationName,
257
- pos: (_a = declaration == null ? void 0 : declaration.pos) != null ? _a : 0,
258
- end: (_b = declaration == null ? void 0 : declaration.end) != null ? _b : 0
259
- }
260
- };
261
- });
262
- return result;
263
- };
264
- this.writeType = () => {
265
- return `
266
- interface QueryKeyUsedInfo {
267
- sourceFile: {
268
- name: string;
269
-
270
- };
271
- ["query-key"]: {
272
- name: string;
273
- pos: number;
274
- end: number;
275
- };
276
- func: {
277
- name: string;
278
- pos: number;
279
- end: number;
280
- };
281
- }
282
- `;
283
- };
284
- this.writeInfo = (result) => {
285
- const content = `
286
- ${this.writeType()}
287
- export const queryKeyUsedInfo : QueryKeyUsedInfo[] = ${JSON.stringify(result)};
288
- `;
289
- fs2.writeFileSync(this.config.output, content);
290
- };
291
- }
292
- };
293
-
294
- // src/types/config.ts
295
- import { z } from "zod";
296
- var configSchema = z.object({
297
- output: z.string().default("src/query-key-used-info.ts"),
298
- path: z.string().default("./src/**/*.{jsx,tsx,ts}"),
299
- globalQueryKeyName: z.string().default("globalQueryKeys"),
300
- ignoreFiles: z.array(z.string()).default([])
301
- });
302
- var defaultConfig = {
303
- output: "./src/query-key-used-info.ts",
304
- path: "./src/**/*.{jsx,tsx,ts}",
305
- ignoreFiles: [".d.ts", "queryKeys.ts"],
306
- globalQueryKeyName: "globalQueryKeys"
307
- };
308
-
309
- // src/index.ts
310
- function QueryKeyUsedPlugin(_config) {
311
- var _a;
312
- const config = configSchema.parse(__spreadProps(__spreadValues({}, defaultConfig), {
313
- ignoreFiles: [...defaultConfig.ignoreFiles, ...(_a = _config == null ? void 0 : _config.ignoreFiles) != null ? _a : []]
314
- }));
315
- const rootDir = process.cwd();
316
- const ignoreFiles = config.ignoreFiles;
317
- const program = new Programmer(config);
318
- const generator = new QueryKeyUsedInfoGenerator(program, config);
319
- return {
320
- name: "query-key-used-generator",
321
- enforce: "pre",
322
- configureServer(server) {
323
- const listener = (absolutePath = "") => {
324
- const filePath = path4.relative(rootDir, absolutePath);
325
- const outputPath = path4.relative(rootDir, config.output);
326
- if (!filePath.startsWith("src") || filePath.startsWith(outputPath)) return;
327
- if (ignoreFiles.some((item) => {
328
- const ignorePath = path4.relative(process.cwd(), item);
329
- return filePath.includes(ignorePath);
330
- })) {
331
- return;
332
- }
333
- generator.execute();
334
- };
335
- server.watcher.on("add", listener);
336
- server.watcher.on("change", listener);
337
- server.watcher.on("unlink", listener);
338
- },
339
- buildStart() {
340
- generator.execute();
341
- }
342
- };
343
- }
344
- export {
345
- QueryKeyUsedPlugin as default
346
- };