@uiwwsw/react-query-helper 1.2.0 → 1.2.2

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/README.md CHANGED
@@ -146,7 +146,7 @@ npm exec react-query-helper -- --generate
146
146
  | --- | --- | --- |
147
147
  | `sourceDir` | ✅ | API 함수가 포함된 TypeScript 디렉토리 (루트 기준 경로) |
148
148
  | `outputDir` | ✅ | 생성된 훅과 옵션 파일이 저장될 디렉토리 |
149
- | `ignoredFiles` | ❌ | 코드 생성에서 제외할 파일 이름 배열 |
149
+ | `ignoredFiles` | ❌ | 코드 생성에서 제외할 파일/경로 패턴 배열 (`domain.ts`, `**/*.spec.ts`, `admin/internal/**`) |
150
150
  | `templateDir` | ❌ | 생성된 코드에서 `queryOption` 계열을 import 할 모듈 경로 또는 상대 디렉토리 |
151
151
  | `analyzer` | ❌ | 어떤 함수를 읽을지 필터링하는 설정 (`exported-only`, `async-only`, include/exclude 등) |
152
152
  | `template` | ❌ | 어떤 아티팩트를 생성할지와 이름 규칙을 제어하는 설정 |
@@ -251,6 +251,41 @@ const config: AutoQueryConfig = {
251
251
  - `customAnalyzerPath`: 파일을 읽고 원하는 함수 메타 목록을 직접 반환
252
252
  - `customTemplatePath`: 최종 생성 코드 문자열을 직접 반환
253
253
 
254
+ 예시 `rqh.analyzer.mjs`:
255
+
256
+ ```js
257
+ export function analyzeFile(filePath, config) {
258
+ // 여기서 AST를 직접 파싱하거나,
259
+ // 파일명 규칙 / 함수명 규칙을 자유롭게 적용할 수 있습니다.
260
+ return [
261
+ {
262
+ name: "getUser",
263
+ parameters: ["params"],
264
+ isAsync: true,
265
+ isExported: true,
266
+ },
267
+ ];
268
+ }
269
+ ```
270
+
271
+ 예시 `rqh.template.mjs`:
272
+
273
+ ```js
274
+ export function generateOptionsCode({ functionInfos, importPath }) {
275
+ const names = functionInfos.map((info) => info.name).join(", ");
276
+ return `import { ${names} } from "${importPath}";\nexport const customGenerated = true;\n`;
277
+ }
278
+ ```
279
+
280
+ 언제 custom analyzer를 쓰면 되냐면:
281
+ - built-in analyzer 설정만으로 충분한 경우
282
+ - `exported-only`, `async-only`, 이름 include/exclude, 아티팩트 on/off 정도
283
+ - custom analyzer가 필요한 경우
284
+ - 함수 선언이 래핑되어 있거나
285
+ - factory/higher-order 구조를 쓰거나
286
+ - 팀 전용 규칙으로 함수 후보를 뽑아야 하거나
287
+ - 기본 AST 규칙보다 더 복잡한 generic 패턴을 강제하고 싶을 때
288
+
254
289
  즉 이제는 기본 규칙을 조금 바꾸는 수준이 아니라, 팀 전용 생성기처럼 확장할 수 있습니다.
255
290
 
256
291
  ```ts
package/dist/cli.js CHANGED
@@ -1605,6 +1605,26 @@ function shouldIncludeFunction(info, config = {}) {
1605
1605
  }
1606
1606
  return true;
1607
1607
  }
1608
+ function unwrapFunctionLike(node) {
1609
+ if (!node) {
1610
+ return;
1611
+ }
1612
+ if (ts.isArrowFunction(node) || ts.isFunctionExpression(node)) {
1613
+ return node;
1614
+ }
1615
+ if (ts.isParenthesizedExpression(node) || ts.isAsExpression(node) || ts.isTypeAssertionExpression(node) || ts.isSatisfiesExpression(node) || ts.isNonNullExpression(node)) {
1616
+ return unwrapFunctionLike(node.expression);
1617
+ }
1618
+ return;
1619
+ }
1620
+ function getFunctionInfo(name, parameters, isAsync, isExported, sourceFile) {
1621
+ return {
1622
+ name,
1623
+ parameters: parameters.map((param) => param.name.getText(sourceFile)),
1624
+ isAsync,
1625
+ isExported
1626
+ };
1627
+ }
1608
1628
  function analyzeFile(filePath, config = {}) {
1609
1629
  const sourceCode = readFileSync(filePath, "utf8");
1610
1630
  const scriptKind = filePath.endsWith(".tsx") ? ts.ScriptKind.TSX : ts.ScriptKind.TS;
@@ -1616,23 +1636,14 @@ function analyzeFile(filePath, config = {}) {
1616
1636
  const isExported = hasExportModifier(node);
1617
1637
  node.declarationList.declarations.forEach((declaration) => {
1618
1638
  if (ts.isVariableDeclaration(declaration) && ts.isIdentifier(declaration.name)) {
1619
- const initializer = declaration.initializer;
1620
- if (initializer && (ts.isArrowFunction(initializer) || ts.isFunctionExpression(initializer))) {
1621
- const name = declaration.name.text;
1622
- const parameters = initializer.parameters.map((param) => param.name.getText(sourceFile));
1623
- const isAsync = initializer.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword) || false;
1624
- functionInfos.push({ name, parameters, isAsync, isExported });
1639
+ const initializer = unwrapFunctionLike(declaration.initializer);
1640
+ if (initializer) {
1641
+ functionInfos.push(getFunctionInfo(declaration.name.text, initializer.parameters, !!initializer.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword), isExported, sourceFile));
1625
1642
  }
1626
1643
  }
1627
1644
  });
1628
- } else if (ts.isFunctionDeclaration(node)) {
1629
- if (node.name) {
1630
- const name = node.name.text;
1631
- const parameters = node.parameters.map((param) => param.name.getText(sourceFile));
1632
- const isAsync = node.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword) || false;
1633
- const isExported = hasExportModifier(node);
1634
- functionInfos.push({ name, parameters, isAsync, isExported });
1635
- }
1645
+ } else if (ts.isFunctionDeclaration(node) && node.name) {
1646
+ functionInfos.push(getFunctionInfo(node.name.text, node.parameters, !!node.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword), hasExportModifier(node), sourceFile));
1636
1647
  }
1637
1648
  });
1638
1649
  return functionInfos.filter((info) => shouldIncludeFunction(info, config));
@@ -1858,6 +1869,26 @@ function shouldProcess(filePath) {
1858
1869
  const extension = extname3(filePath).toLowerCase();
1859
1870
  return extension === ".ts" || extension === ".tsx";
1860
1871
  }
1872
+ function escapeRegExp(value) {
1873
+ return value.replace(/[|\\{}()[\]^$+?.]/g, "\\$&");
1874
+ }
1875
+ function globToRegExp(pattern) {
1876
+ const normalized = normalizeModulePath(pattern);
1877
+ const placeholder = "__DOUBLE_STAR__";
1878
+ const escaped = escapeRegExp(normalized).replace(/\*\*/g, placeholder).replace(/\*/g, "[^/]*").replace(new RegExp(placeholder, "g"), ".*");
1879
+ return new RegExp(`^${escaped}$`);
1880
+ }
1881
+ function isIgnoredFile(filePath) {
1882
+ const relativePath = normalizeModulePath(relative3(config.resolvedSourceDir, filePath));
1883
+ const fileName = basename3(filePath);
1884
+ return config.ignoredFiles?.some((pattern) => {
1885
+ const normalizedPattern = normalizeModulePath(pattern);
1886
+ if (!normalizedPattern.includes("/") && !normalizedPattern.includes("*")) {
1887
+ return fileName === normalizedPattern;
1888
+ }
1889
+ return globToRegExp(normalizedPattern).test(relativePath);
1890
+ }) ?? false;
1891
+ }
1861
1892
  function printUsage() {
1862
1893
  console.log("Usage: react-query-helper [init | generate | watch | help]");
1863
1894
  console.log(" react-query-helper [--init | --generate | --watch | --help]");
@@ -1938,7 +1969,7 @@ async function processFile(filePath) {
1938
1969
  }
1939
1970
  const relativeDir = dirname4(relativePath);
1940
1971
  const fileName = basename3(filePath, extname3(filePath));
1941
- if (config.ignoredFiles?.includes(basename3(filePath))) {
1972
+ if (isIgnoredFile(filePath)) {
1942
1973
  console.log(`Skipping ignored file: ${filePath}`);
1943
1974
  return;
1944
1975
  }
@@ -1984,7 +2015,7 @@ async function runGenerate() {
1984
2015
  const files = await new Promise((resolve5, reject) => {
1985
2016
  const results = [];
1986
2017
  const watcher = watch(sourcePath, {
1987
- ignored: config.ignoredFiles?.map((file) => join4(sourcePath, "**", file)) || [],
2018
+ ignored: (watchedPath) => isIgnoredFile(String(watchedPath)),
1988
2019
  ignoreInitial: false,
1989
2020
  depth: 99
1990
2021
  }).on("add", (path) => results.push(path)).on("ready", () => {
@@ -2013,23 +2044,23 @@ async function runWatch() {
2013
2044
  ensureDirectory(outputRootPath);
2014
2045
  console.log(`\uD83D\uDCC1 Ensured output directory exists at ${outputRootPath}`);
2015
2046
  watch(sourcePath, {
2016
- ignored: config.ignoredFiles?.map((file) => join4(sourcePath, "**", file)) || [],
2047
+ ignored: (watchedPath) => isIgnoredFile(String(watchedPath)),
2017
2048
  ignoreInitial: false,
2018
2049
  depth: 99
2019
2050
  }).on("add", async (filePath) => {
2020
- if (!shouldProcess(filePath)) {
2051
+ if (!shouldProcess(filePath) || isIgnoredFile(filePath)) {
2021
2052
  return;
2022
2053
  }
2023
2054
  console.log(`\uD83D\uDCC4 File added: ${filePath}`);
2024
2055
  await processFile(filePath);
2025
2056
  }).on("change", async (filePath) => {
2026
- if (!shouldProcess(filePath)) {
2057
+ if (!shouldProcess(filePath) || isIgnoredFile(filePath)) {
2027
2058
  return;
2028
2059
  }
2029
2060
  console.log(`\uD83D\uDD04 File changed: ${filePath}`);
2030
2061
  await processFile(filePath);
2031
2062
  }).on("unlink", async (filePath) => {
2032
- if (!shouldProcess(filePath)) {
2063
+ if (!shouldProcess(filePath) || isIgnoredFile(filePath)) {
2033
2064
  return;
2034
2065
  }
2035
2066
  console.log(`\uD83D\uDDD1️ File unlinked: ${filePath}`);
@@ -1 +1 @@
1
- {"version":3,"file":"analyzer.d.ts","sourceRoot":"","sources":["../../src/core/analyzer.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,YAAY,GAAG,eAAe,GAAG,KAAK,CAAC;AACnD,MAAM,MAAM,iBAAiB,GAAG,KAAK,GAAG,YAAY,GAAG,WAAW,CAAC;AAEnE,MAAM,WAAW,cAAc;IAC7B,YAAY,CAAC,EAAE,YAAY,CAAC;IAC5B,iBAAiB,CAAC,EAAE,iBAAiB,CAAC;IACtC,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;IACxB,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;CACzB;AAED,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,MAAM,EAAE,CAAC;IACrB,OAAO,EAAE,OAAO,CAAC;IACjB,UAAU,EAAE,OAAO,CAAC;CACrB;AAgCD,wBAAgB,WAAW,CACzB,QAAQ,EAAE,MAAM,EAChB,MAAM,GAAE,cAAmB,GAC1B,YAAY,EAAE,CAwDhB"}
1
+ {"version":3,"file":"analyzer.d.ts","sourceRoot":"","sources":["../../src/core/analyzer.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,YAAY,GAAG,eAAe,GAAG,KAAK,CAAC;AACnD,MAAM,MAAM,iBAAiB,GAAG,KAAK,GAAG,YAAY,GAAG,WAAW,CAAC;AAEnE,MAAM,WAAW,cAAc;IAC7B,YAAY,CAAC,EAAE,YAAY,CAAC;IAC5B,iBAAiB,CAAC,EAAE,iBAAiB,CAAC;IACtC,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;IACxB,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;CACzB;AAED,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,MAAM,EAAE,CAAC;IACrB,OAAO,EAAE,OAAO,CAAC;IACjB,UAAU,EAAE,OAAO,CAAC;CACrB;AAuED,wBAAgB,WAAW,CACzB,QAAQ,EAAE,MAAM,EAChB,MAAM,GAAE,cAAmB,GAC1B,YAAY,EAAE,CAyDhB"}
package/dist/index.js CHANGED
@@ -21,6 +21,26 @@ function shouldIncludeFunction(info, config = {}) {
21
21
  }
22
22
  return true;
23
23
  }
24
+ function unwrapFunctionLike(node) {
25
+ if (!node) {
26
+ return;
27
+ }
28
+ if (ts.isArrowFunction(node) || ts.isFunctionExpression(node)) {
29
+ return node;
30
+ }
31
+ if (ts.isParenthesizedExpression(node) || ts.isAsExpression(node) || ts.isTypeAssertionExpression(node) || ts.isSatisfiesExpression(node) || ts.isNonNullExpression(node)) {
32
+ return unwrapFunctionLike(node.expression);
33
+ }
34
+ return;
35
+ }
36
+ function getFunctionInfo(name, parameters, isAsync, isExported, sourceFile) {
37
+ return {
38
+ name,
39
+ parameters: parameters.map((param) => param.name.getText(sourceFile)),
40
+ isAsync,
41
+ isExported
42
+ };
43
+ }
24
44
  function analyzeFile(filePath, config = {}) {
25
45
  const sourceCode = readFileSync(filePath, "utf8");
26
46
  const scriptKind = filePath.endsWith(".tsx") ? ts.ScriptKind.TSX : ts.ScriptKind.TS;
@@ -32,23 +52,14 @@ function analyzeFile(filePath, config = {}) {
32
52
  const isExported = hasExportModifier(node);
33
53
  node.declarationList.declarations.forEach((declaration) => {
34
54
  if (ts.isVariableDeclaration(declaration) && ts.isIdentifier(declaration.name)) {
35
- const initializer = declaration.initializer;
36
- if (initializer && (ts.isArrowFunction(initializer) || ts.isFunctionExpression(initializer))) {
37
- const name = declaration.name.text;
38
- const parameters = initializer.parameters.map((param) => param.name.getText(sourceFile));
39
- const isAsync = initializer.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword) || false;
40
- functionInfos.push({ name, parameters, isAsync, isExported });
55
+ const initializer = unwrapFunctionLike(declaration.initializer);
56
+ if (initializer) {
57
+ functionInfos.push(getFunctionInfo(declaration.name.text, initializer.parameters, !!initializer.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword), isExported, sourceFile));
41
58
  }
42
59
  }
43
60
  });
44
- } else if (ts.isFunctionDeclaration(node)) {
45
- if (node.name) {
46
- const name = node.name.text;
47
- const parameters = node.parameters.map((param) => param.name.getText(sourceFile));
48
- const isAsync = node.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword) || false;
49
- const isExported = hasExportModifier(node);
50
- functionInfos.push({ name, parameters, isAsync, isExported });
51
- }
61
+ } else if (ts.isFunctionDeclaration(node) && node.name) {
62
+ functionInfos.push(getFunctionInfo(node.name.text, node.parameters, !!node.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword), hasExportModifier(node), sourceFile));
52
63
  }
53
64
  });
54
65
  return functionInfos.filter((info) => shouldIncludeFunction(info, config));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uiwwsw/react-query-helper",
3
- "version": "1.2.0",
3
+ "version": "1.2.2",
4
4
  "module": "dist/index.js",
5
5
  "type": "module",
6
6
  "types": "dist/index.d.ts",