@query-key-gen/used-generator 0.0.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/lib/index.cjs +374 -0
- package/lib/index.d.cts +24 -0
- package/lib/index.d.ts +24 -0
- package/lib/index.js +346 -0
- package/package.json +36 -0
- package/src/core/QueryKeyUsedInfoGenerator.ts +101 -0
- package/src/core/index.ts +1 -0
- package/src/index.ts +48 -0
- package/src/queryFinder/QueryKeyUsedFinder.ts +97 -0
- package/src/queryFinder/index.ts +1 -0
- package/src/types/config.ts +17 -0
- package/src/types/index.ts +1 -0
- package/src/utils/index.ts +33 -0
- package/tsconfig.json +107 -0
- package/tsup.config.ts +10 -0
package/lib/index.cjs
ADDED
@@ -0,0 +1,374 @@
|
|
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
ADDED
@@ -0,0 +1,24 @@
|
|
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
ADDED
@@ -0,0 +1,24 @@
|
|
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
ADDED
@@ -0,0 +1,346 @@
|
|
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
|
+
};
|
package/package.json
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
{
|
2
|
+
"name": "@query-key-gen/used-generator",
|
3
|
+
"version": "0.0.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
|
+
"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
|
+
}
|
@@ -0,0 +1,101 @@
|
|
1
|
+
import fs from 'fs';
|
2
|
+
import ts from 'typescript';
|
3
|
+
|
4
|
+
import { QueryKeyUsedFinder } from '../queryFinder/QueryKeyUsedFinder';
|
5
|
+
import { prettify } from '../utils';
|
6
|
+
import { GenericRunner } from '@query-key-gen/generator-common';
|
7
|
+
import path from 'path';
|
8
|
+
import { ConfigOptions } from '../types/config';
|
9
|
+
|
10
|
+
/**
|
11
|
+
* Function used in the query key generator to find file information
|
12
|
+
* and write it to the query-key-used-info.ts file
|
13
|
+
*/
|
14
|
+
class QueryKeyUsedInfoGenerator extends GenericRunner<ConfigOptions> {
|
15
|
+
protected override process = async () => {
|
16
|
+
const { program } = await this.programmer.create();
|
17
|
+
const { projectFiles } = await this.programmer.findFiles(program);
|
18
|
+
const queryKeyUsedInfo = QueryKeyUsedFinder.find(
|
19
|
+
projectFiles,
|
20
|
+
this.config.globalQueryKeyName
|
21
|
+
);
|
22
|
+
const result = this.makeQueryKeyUsedInfo(queryKeyUsedInfo);
|
23
|
+
this.writeInfo(result);
|
24
|
+
prettify(this.config.output);
|
25
|
+
};
|
26
|
+
|
27
|
+
makeQueryKeyUsedInfo = (queryKeyUsedInfo: QueryKeyUsedFinder.QueryKeyUsedFinderReturnType) => {
|
28
|
+
const result = queryKeyUsedInfo.map(item => {
|
29
|
+
const { declaration, sourceFile, parent } = item;
|
30
|
+
|
31
|
+
const declarationName = (function () {
|
32
|
+
if (!declaration) {
|
33
|
+
return;
|
34
|
+
}
|
35
|
+
|
36
|
+
if (ts.isVariableDeclaration(declaration)) {
|
37
|
+
return declaration.name.getText();
|
38
|
+
}
|
39
|
+
|
40
|
+
if (ts.isFunctionDeclaration(declaration)) {
|
41
|
+
return declaration.name?.getText();
|
42
|
+
}
|
43
|
+
|
44
|
+
return;
|
45
|
+
})();
|
46
|
+
|
47
|
+
const fileName = path.relative(process.cwd(), sourceFile.fileName).replace('src/', '');
|
48
|
+
|
49
|
+
return {
|
50
|
+
sourceFile: {
|
51
|
+
name: fileName
|
52
|
+
},
|
53
|
+
|
54
|
+
['query-key']: {
|
55
|
+
name: parent?.getText(),
|
56
|
+
pos: parent?.pos,
|
57
|
+
end: parent?.end
|
58
|
+
},
|
59
|
+
func: {
|
60
|
+
name: declarationName,
|
61
|
+
pos: declaration?.pos ?? 0,
|
62
|
+
end: declaration?.end ?? 0
|
63
|
+
}
|
64
|
+
};
|
65
|
+
});
|
66
|
+
|
67
|
+
return result;
|
68
|
+
};
|
69
|
+
|
70
|
+
writeType = () => {
|
71
|
+
return `
|
72
|
+
interface QueryKeyUsedInfo {
|
73
|
+
sourceFile: {
|
74
|
+
name: string;
|
75
|
+
|
76
|
+
};
|
77
|
+
["query-key"]: {
|
78
|
+
name: string;
|
79
|
+
pos: number;
|
80
|
+
end: number;
|
81
|
+
};
|
82
|
+
func: {
|
83
|
+
name: string;
|
84
|
+
pos: number;
|
85
|
+
end: number;
|
86
|
+
};
|
87
|
+
}
|
88
|
+
`;
|
89
|
+
};
|
90
|
+
|
91
|
+
writeInfo = (result: object) => {
|
92
|
+
const content = `
|
93
|
+
${this.writeType()}
|
94
|
+
export const queryKeyUsedInfo : QueryKeyUsedInfo[] = ${JSON.stringify(result)};
|
95
|
+
`;
|
96
|
+
|
97
|
+
fs.writeFileSync(this.config.output, content);
|
98
|
+
};
|
99
|
+
}
|
100
|
+
|
101
|
+
export { QueryKeyUsedInfoGenerator };
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './QueryKeyUsedInfoGenerator';
|
package/src/index.ts
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
import { Plugin } from 'vite';
|
2
|
+
|
3
|
+
import path from 'path';
|
4
|
+
|
5
|
+
import { Programmer } from '../../common/src';
|
6
|
+
import { QueryKeyUsedInfoGenerator } from './core';
|
7
|
+
import { ConfigOptions, configSchema, defaultConfig } from './types/config';
|
8
|
+
|
9
|
+
export default function QueryKeyUsedPlugin(_config?: Omit<ConfigOptions, 'path'>): Plugin {
|
10
|
+
const config = configSchema.parse({
|
11
|
+
...defaultConfig,
|
12
|
+
ignoreFiles: [...defaultConfig.ignoreFiles, ...(_config?.ignoreFiles ?? [])]
|
13
|
+
});
|
14
|
+
const rootDir = process.cwd(); // 프로젝트 루트 경로
|
15
|
+
const ignoreFiles = config.ignoreFiles;
|
16
|
+
const program = new Programmer(config);
|
17
|
+
|
18
|
+
const generator = new QueryKeyUsedInfoGenerator(program, config);
|
19
|
+
|
20
|
+
return {
|
21
|
+
name: 'query-key-used-generator',
|
22
|
+
enforce: 'pre',
|
23
|
+
configureServer(server) {
|
24
|
+
const listener = (absolutePath = '') => {
|
25
|
+
const filePath = path.relative(rootDir, absolutePath);
|
26
|
+
const outputPath = path.relative(rootDir, config.output);
|
27
|
+
|
28
|
+
if (!filePath.startsWith('src') || filePath.startsWith(outputPath)) return;
|
29
|
+
if (
|
30
|
+
ignoreFiles.some(item => {
|
31
|
+
const ignorePath = path.relative(process.cwd(), item);
|
32
|
+
return filePath.includes(ignorePath);
|
33
|
+
})
|
34
|
+
) {
|
35
|
+
return;
|
36
|
+
}
|
37
|
+
|
38
|
+
generator.execute();
|
39
|
+
};
|
40
|
+
server.watcher.on('add', listener);
|
41
|
+
server.watcher.on('change', listener);
|
42
|
+
server.watcher.on('unlink', listener);
|
43
|
+
},
|
44
|
+
buildStart() {
|
45
|
+
generator.execute();
|
46
|
+
}
|
47
|
+
};
|
48
|
+
}
|
@@ -0,0 +1,97 @@
|
|
1
|
+
import ts from 'typescript';
|
2
|
+
|
3
|
+
namespace QueryKeyUsedFinder {
|
4
|
+
export type QueryKeyUsedFinderReturnType = ReturnType<typeof find>;
|
5
|
+
|
6
|
+
export const find = (sourceFiles: ts.SourceFile[], globalQueryKeyName: string) => {
|
7
|
+
// find globalQueryKeys using place
|
8
|
+
const globalQueryKeys = sourceFiles.map(sourceFile => {
|
9
|
+
return findGlobalQueryKeys(sourceFile, globalQueryKeyName);
|
10
|
+
});
|
11
|
+
|
12
|
+
const queryKeyByDeclaration = globalQueryKeys.flatMap(keys => {
|
13
|
+
const usedDeclarations = keys.findNode.map(key => {
|
14
|
+
// 해당 key에서 사용중인 함수를 찾는다.
|
15
|
+
const declaration = findUsedDeclaration(key.node);
|
16
|
+
|
17
|
+
return {
|
18
|
+
...key,
|
19
|
+
declaration,
|
20
|
+
sourceFile: keys.sourceFile
|
21
|
+
};
|
22
|
+
});
|
23
|
+
|
24
|
+
return usedDeclarations;
|
25
|
+
});
|
26
|
+
|
27
|
+
return queryKeyByDeclaration;
|
28
|
+
};
|
29
|
+
|
30
|
+
const findGlobalQueryKeys = (node: ts.SourceFile, globalQueryKeyName: string) => {
|
31
|
+
const findNode: {
|
32
|
+
node: ts.Node;
|
33
|
+
parent: ts.Node | null;
|
34
|
+
}[] = [];
|
35
|
+
|
36
|
+
const findText = [globalQueryKeyName];
|
37
|
+
|
38
|
+
const visitor = (node: ts.Node) => {
|
39
|
+
// import 구문은 무시
|
40
|
+
if (ts.isImportDeclaration(node)) {
|
41
|
+
return;
|
42
|
+
}
|
43
|
+
|
44
|
+
if (ts.isCallExpression(node) || ts.isIdentifier(node)) {
|
45
|
+
if (ts.isIdentifier(node) && findText.includes(node.getText())) {
|
46
|
+
const grandParent = node.parent.parent;
|
47
|
+
|
48
|
+
if (ts.isPropertyAccessExpression(node.parent)) {
|
49
|
+
const isGlobalQueryKey = findText.some(text =>
|
50
|
+
grandParent.getText().includes(text)
|
51
|
+
);
|
52
|
+
|
53
|
+
findNode.push({
|
54
|
+
node,
|
55
|
+
parent: isGlobalQueryKey ? grandParent : null
|
56
|
+
});
|
57
|
+
}
|
58
|
+
}
|
59
|
+
}
|
60
|
+
|
61
|
+
ts.forEachChild(node, visitor);
|
62
|
+
};
|
63
|
+
|
64
|
+
visitor(node);
|
65
|
+
|
66
|
+
return { findNode, sourceFile: node };
|
67
|
+
};
|
68
|
+
|
69
|
+
const findUsedDeclaration = (node: ts.Node) => {
|
70
|
+
let parentNode = node.parent; // 부모 노드 탐색
|
71
|
+
|
72
|
+
let declaration: ts.FunctionDeclaration | ts.VariableDeclaration | null = null;
|
73
|
+
|
74
|
+
// 사용중인 함수를 찾을 때까지 부모를 계속 탐색
|
75
|
+
while (parentNode) {
|
76
|
+
if (ts.isFunctionDeclaration(parentNode)) {
|
77
|
+
declaration = parentNode;
|
78
|
+
break;
|
79
|
+
}
|
80
|
+
|
81
|
+
if (
|
82
|
+
ts.isVariableDeclaration(parentNode) &&
|
83
|
+
parentNode.initializer &&
|
84
|
+
ts.isArrowFunction(parentNode.initializer)
|
85
|
+
) {
|
86
|
+
declaration = parentNode;
|
87
|
+
break;
|
88
|
+
}
|
89
|
+
|
90
|
+
parentNode = parentNode.parent;
|
91
|
+
}
|
92
|
+
|
93
|
+
return declaration;
|
94
|
+
};
|
95
|
+
}
|
96
|
+
|
97
|
+
export { QueryKeyUsedFinder };
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './QueryKeyUsedFinder';
|
@@ -0,0 +1,17 @@
|
|
1
|
+
import { z } from 'zod';
|
2
|
+
|
3
|
+
export const configSchema = z.object({
|
4
|
+
output: z.string().default('src/query-key-used-info.ts'),
|
5
|
+
path: z.string().default('./src/**/*.{jsx,tsx,ts}'),
|
6
|
+
globalQueryKeyName: z.string().default('globalQueryKeys'),
|
7
|
+
ignoreFiles: z.array(z.string()).default([])
|
8
|
+
});
|
9
|
+
|
10
|
+
export type ConfigOptions = z.infer<typeof configSchema>;
|
11
|
+
|
12
|
+
export const defaultConfig: Required<ConfigOptions> = {
|
13
|
+
output: './src/query-key-used-info.ts',
|
14
|
+
path: './src/**/*.{jsx,tsx,ts}',
|
15
|
+
ignoreFiles: ['.d.ts', 'queryKeys.ts'],
|
16
|
+
globalQueryKeyName: 'globalQueryKeys'
|
17
|
+
};
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './config';
|
@@ -0,0 +1,33 @@
|
|
1
|
+
import { execSync } from 'child_process';
|
2
|
+
import fs from 'fs';
|
3
|
+
import path from 'path';
|
4
|
+
|
5
|
+
const toCamelCase = (str: string): string => {
|
6
|
+
return str.replace(/[^a-zA-Z0-9]+(.)/g, (_, letter) => letter.toUpperCase());
|
7
|
+
};
|
8
|
+
|
9
|
+
const prettify = (output: string) => {
|
10
|
+
const prettier = path.resolve('./node_modules/.bin/prettier');
|
11
|
+
if (fs.existsSync(prettier)) execSync(`${prettier} --write --cache ${output}`);
|
12
|
+
};
|
13
|
+
|
14
|
+
const logger = (() => {
|
15
|
+
const prefix = '[QUERY-KEY-USED-GENERATOR]: ';
|
16
|
+
const log = () => {
|
17
|
+
return {
|
18
|
+
warn: (msg: string) => {
|
19
|
+
console.warn(`[WARN]${prefix}${msg}`);
|
20
|
+
},
|
21
|
+
error: (msg: string) => {
|
22
|
+
console.error(`[ERROR]${prefix}${msg}`);
|
23
|
+
},
|
24
|
+
info: (msg: string) => {
|
25
|
+
console.log(`[INFO]${prefix}${msg}`);
|
26
|
+
}
|
27
|
+
};
|
28
|
+
};
|
29
|
+
|
30
|
+
return log();
|
31
|
+
})();
|
32
|
+
|
33
|
+
export { logger, prettify, toCamelCase };
|
package/tsconfig.json
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
{
|
2
|
+
"compilerOptions": {
|
3
|
+
/* Visit https://aka.ms/tsconfig.json to read more about this file */
|
4
|
+
|
5
|
+
/* Projects */
|
6
|
+
// "incremental": true, /* Enable incremental compilation */
|
7
|
+
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
|
8
|
+
// "tsBuildInfoFile": "./", /* Specify the folder for .tsbuildinfo incremental compilation files. */
|
9
|
+
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects */
|
10
|
+
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
|
11
|
+
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
|
12
|
+
|
13
|
+
/* Language and Environment */
|
14
|
+
"target": "ES2015" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
|
15
|
+
"lib": [
|
16
|
+
"DOM",
|
17
|
+
"ES2020"
|
18
|
+
] /* Specify a set of bundled library declaration files that describe the target runtime environment. */,
|
19
|
+
// "jsx": "preserve", /* Specify what JSX code is generated. */
|
20
|
+
"experimentalDecorators": true /* Enable experimental support for TC39 stage 2 draft decorators. */,
|
21
|
+
"emitDecoratorMetadata": true /* Emit design-type metadata for decorated declarations in source files. */,
|
22
|
+
|
23
|
+
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h' */
|
24
|
+
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
|
25
|
+
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using `jsx: react-jsx*`.` */
|
26
|
+
// "reactNamespace": "", /* Specify the object invoked for `createElement`. This only applies when targeting `react` JSX emit. */
|
27
|
+
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
|
28
|
+
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
|
29
|
+
|
30
|
+
/* Modules */
|
31
|
+
"module": "commonjs" /* Specify what module code is generated. */,
|
32
|
+
// "rootDir": "./", /* Specify the root folder within your source files. */
|
33
|
+
"moduleResolution": "node" /* Specify how TypeScript looks up a file from a given module specifier. */,
|
34
|
+
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
|
35
|
+
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
|
36
|
+
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
|
37
|
+
// "typeRoots": [], /* Specify multiple folders that act like `./node_modules/@types`. */
|
38
|
+
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
|
39
|
+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
40
|
+
// "resolveJsonModule": true, /* Enable importing .json files */
|
41
|
+
// "noResolve": true, /* Disallow `import`s, `require`s or `<reference>`s from expanding the number of files TypeScript should add to a project. */
|
42
|
+
|
43
|
+
/* JavaScript Support */
|
44
|
+
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */
|
45
|
+
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
|
46
|
+
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from `node_modules`. Only applicable with `allowJs`. */
|
47
|
+
|
48
|
+
/* Emit */
|
49
|
+
"declaration": true /* Generate .d.ts files from TypeScript and JavaScript files in your project. */,
|
50
|
+
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
|
51
|
+
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
52
|
+
"sourceMap": true /* Create source map files for emitted JavaScript files. */,
|
53
|
+
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */
|
54
|
+
"outDir": "./lib" /* Specify an output folder for all emitted files. */,
|
55
|
+
// "removeComments": true, /* Disable emitting comments. */
|
56
|
+
// "noEmit": true, /* Disable emitting files from a compilation. */
|
57
|
+
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
|
58
|
+
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types */
|
59
|
+
"downlevelIteration": true /* Emit more compliant, but verbose and less performant JavaScript for iteration. */,
|
60
|
+
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
|
61
|
+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
62
|
+
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
|
63
|
+
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
|
64
|
+
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
|
65
|
+
"newLine": "lf" /* Set the newline character for emitting files. */,
|
66
|
+
"stripInternal": true /* Disable emitting declarations that have `@internal` in their JSDoc comments. */,
|
67
|
+
// "noEmitHelpers": true, /* Disable generating custom helper functions like `__extends` in compiled output. */
|
68
|
+
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
|
69
|
+
// "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */
|
70
|
+
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
|
71
|
+
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
|
72
|
+
|
73
|
+
/* Interop Constraints */
|
74
|
+
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
75
|
+
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
76
|
+
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */,
|
77
|
+
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
78
|
+
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
|
79
|
+
|
80
|
+
/* Type Checking */
|
81
|
+
"strict": true /* Enable all strict type-checking options. */,
|
82
|
+
"noImplicitAny": true /* Enable error reporting for expressions and declarations with an implied `any` type.. */,
|
83
|
+
"strictNullChecks": true /* When type checking, take into account `null` and `undefined`. */,
|
84
|
+
"strictFunctionTypes": true /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */,
|
85
|
+
"strictBindCallApply": true /* Check that the arguments for `bind`, `call`, and `apply` methods match the original function. */,
|
86
|
+
"strictPropertyInitialization": true /* Check for class properties that are declared but not set in the constructor. */,
|
87
|
+
"noImplicitThis": true /* Enable error reporting when `this` is given the type `any`. */,
|
88
|
+
"useUnknownInCatchVariables": true /* Type catch clause variables as 'unknown' instead of 'any'. */,
|
89
|
+
"alwaysStrict": true /* Ensure 'use strict' is always emitted. */,
|
90
|
+
"noUnusedLocals": true /* Enable error reporting when a local variables aren't read. */,
|
91
|
+
"noUnusedParameters": true /* Raise an error when a function parameter isn't read */,
|
92
|
+
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
|
93
|
+
"noImplicitReturns": true /* Enable error reporting for codepaths that do not explicitly return in a function. */,
|
94
|
+
"noFallthroughCasesInSwitch": true /* Enable error reporting for fallthrough cases in switch statements. */,
|
95
|
+
"noUncheckedIndexedAccess": true /* Include 'undefined' in index signature results */,
|
96
|
+
"noImplicitOverride": true /* Ensure overriding members in derived classes are marked with an override modifier. */,
|
97
|
+
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type */
|
98
|
+
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
|
99
|
+
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
|
100
|
+
|
101
|
+
/* Completeness */
|
102
|
+
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
|
103
|
+
"skipLibCheck": true /* Skip type checking all .d.ts files. */,
|
104
|
+
"types": ["node"]
|
105
|
+
},
|
106
|
+
"include": ["src"]
|
107
|
+
}
|