sdnext 0.0.8 → 0.0.9

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.
@@ -9,7 +9,15 @@ function getHookTypeFromName(name) {
9
9
  if (/^query[^a-z]/.test(name)) return "query";
10
10
  return "mutation";
11
11
  }
12
- function getHookTypeFromContent(content) {
12
+ let hook_setting = {};
13
+ async function getSetting() {
14
+ hook_setting = await readSdNextSetting();
15
+ return hook_setting;
16
+ }
17
+ async function getHookTypeFromContent(path, content) {
18
+ const setting = await getSetting();
19
+ const type = setting.hook?.[path];
20
+ if (void 0 !== type && "skip" !== type) return type;
13
21
  if (content.includes("useMutation")) return "mutation";
14
22
  if (content.includes("IdOrParams")) return "get";
15
23
  if (content.includes("useQuery")) return "query";
@@ -24,6 +32,7 @@ async function createHook(path, hookMap) {
24
32
  const upName = name.replace(/^./, (char)=>char.toUpperCase());
25
33
  const key = name.replace(/[A-Z]/g, (char)=>`-${char.toLowerCase()}`);
26
34
  const mutationHook = `import { useId } from "react"
35
+
27
36
  import { useMutation, UseMutationOptions } from "@tanstack/react-query"
28
37
  import { createRequestFn } from "deepsea-tools"
29
38
 
@@ -126,13 +135,14 @@ export const use${upName} = createUseQuery({
126
135
  query: queryHook,
127
136
  mutation: mutationHook
128
137
  };
129
- const hookPath = join("hooks", dir, base.replace(/^./, (char)=>`use${char.toUpperCase()}`));
138
+ const hookName = base.replace(/^./, (char)=>`use${char.toUpperCase()}`);
139
+ const hookPath = join("hooks", dir, hookName);
130
140
  let hookType = getHookTypeFromName(name);
131
141
  let overwrite = true;
132
142
  try {
133
143
  const current = await readFile(hookPath, "utf-8");
134
144
  if (current.trim()) overwrite = false;
135
- const contentType = getHookTypeFromContent(current);
145
+ const contentType = await getHookTypeFromContent(join(cwd(), hookPath), current);
136
146
  if (contentType) hookType = contentType;
137
147
  if (map[hookType] === current) return;
138
148
  } catch (error) {
@@ -165,8 +175,9 @@ async function hook(options, { args }) {
165
175
  const newEntires = entires.filter(([path, { overwrite }])=>overwrite);
166
176
  const oldEntires = entires.filter(([path, { overwrite }])=>!overwrite);
167
177
  const root = cwd();
168
- const setting = await readSdNextSetting();
178
+ const setting = await getSetting();
169
179
  for await (const [path, { overwrite, type, ...map }] of newEntires){
180
+ const resolved = join(root, "hooks", path);
170
181
  const answer = await prompts_select({
171
182
  message: path,
172
183
  choices: [
@@ -175,11 +186,10 @@ async function hook(options, { args }) {
175
186
  "get",
176
187
  "skip"
177
188
  ],
178
- default: setting.hook?.[root]?.[path] ?? type
189
+ default: setting.hook?.[resolved] ?? type
179
190
  });
180
191
  setting.hook ??= {};
181
- setting.hook[root] ??= {};
182
- setting.hook[root][path] = answer;
192
+ setting.hook[resolved] = answer;
183
193
  if ("skip" === answer) continue;
184
194
  const { dir, base } = parse(path);
185
195
  await mkdir(join("hooks", dir), {
@@ -192,7 +202,7 @@ async function hook(options, { args }) {
192
202
  message: "Please check the hooks you want to overwrite",
193
203
  choices: oldEntires.map(([key])=>key)
194
204
  });
195
- for await (const [path, { overwrite, type, ...map }] of oldEntires){
205
+ for (const [path, { overwrite, type, ...map }] of oldEntires){
196
206
  if (!overwrites.includes(path)) continue;
197
207
  const { dir, base } = parse(path);
198
208
  await mkdir(join("hooks", dir), {
@@ -1,5 +1,5 @@
1
1
  import { OperationType } from "./hook";
2
2
  export interface SdNextSetting {
3
- hook?: Record<string, Record<string, OperationType>>;
3
+ hook?: Record<string, OperationType>;
4
4
  }
5
5
  export declare function readSdNextSetting(): Promise<SdNextSetting>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sdnext",
3
- "version": "0.0.8",
3
+ "version": "0.0.9",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "exports": {
package/src/utils/hook.ts CHANGED
@@ -5,7 +5,7 @@ import { cwd } from "process"
5
5
  import { checkbox, select } from "@inquirer/prompts"
6
6
  import { Command } from "commander"
7
7
 
8
- import { readSdNextSetting } from "./readSdNextSetting"
8
+ import { readSdNextSetting, SdNextSetting } from "./readSdNextSetting"
9
9
  import { writeSdNextSetting } from "./writeSdNextSetting"
10
10
 
11
11
  export type HookType = "get" | "query" | "mutation"
@@ -20,7 +20,17 @@ function getHookTypeFromName(name: string): HookType {
20
20
  return "mutation"
21
21
  }
22
22
 
23
- function getHookTypeFromContent(content: string): HookType | undefined {
23
+ let setting: SdNextSetting = {}
24
+
25
+ async function getSetting() {
26
+ setting = await readSdNextSetting()
27
+ return setting
28
+ }
29
+
30
+ async function getHookTypeFromContent(path: string, content: string): Promise<HookType | undefined> {
31
+ const setting = await getSetting()
32
+ const type = setting.hook?.[path]
33
+ if (type !== undefined && type !== "skip") return type
24
34
  if (content.includes("useMutation")) return "mutation"
25
35
  if (content.includes("IdOrParams")) return "get"
26
36
  if (content.includes("useQuery")) return "query"
@@ -43,6 +53,7 @@ export async function createHook(path: string, hookMap: Record<string, HookData>
43
53
  const key = name.replace(/[A-Z]/g, char => `-${char.toLowerCase()}`)
44
54
 
45
55
  const mutationHook = `import { useId } from "react"
56
+
46
57
  import { useMutation, UseMutationOptions } from "@tanstack/react-query"
47
58
  import { createRequestFn } from "deepsea-tools"
48
59
 
@@ -173,11 +184,9 @@ export const use${upName} = createUseQuery({
173
184
  mutation: mutationHook,
174
185
  }
175
186
 
176
- const hookPath = join(
177
- "hooks",
178
- dir,
179
- base.replace(/^./, char => `use${char.toUpperCase()}`),
180
- )
187
+ const hookName = base.replace(/^./, char => `use${char.toUpperCase()}`)
188
+
189
+ const hookPath = join("hooks", dir, hookName)
181
190
 
182
191
  let hookType = getHookTypeFromName(name)
183
192
  let overwrite = true
@@ -185,7 +194,7 @@ export const use${upName} = createUseQuery({
185
194
  try {
186
195
  const current = await readFile(hookPath, "utf-8")
187
196
  if (current.trim()) overwrite = false
188
- const contentType = getHookTypeFromContent(current)
197
+ const contentType = await getHookTypeFromContent(join(cwd(), hookPath), current)
189
198
  if (contentType) hookType = contentType
190
199
  if (map[hookType] === current) return
191
200
  } catch (error) {
@@ -235,18 +244,19 @@ export async function hook(options: Record<string, string>, { args }: Command) {
235
244
 
236
245
  const root = cwd()
237
246
 
238
- const setting = await readSdNextSetting()
247
+ const setting = await getSetting()
239
248
 
240
249
  for await (const [path, { overwrite, type, ...map }] of newEntires) {
250
+ const resolved = join(root, "hooks", path)
251
+
241
252
  const answer = await select<OperationType>({
242
253
  message: path,
243
254
  choices: ["mutation", "query", "get", "skip"],
244
- default: setting.hook?.[root]?.[path] ?? type,
255
+ default: setting.hook?.[resolved] ?? type,
245
256
  })
246
257
 
247
258
  setting.hook ??= {}
248
- setting.hook[root] ??= {}
249
- setting.hook[root][path] = answer
259
+ setting.hook[resolved] = answer
250
260
 
251
261
  if (answer === "skip") continue
252
262
 
@@ -269,7 +279,7 @@ export async function hook(options: Record<string, string>, { args }: Command) {
269
279
  choices: oldEntires.map(([key]) => key),
270
280
  })
271
281
 
272
- for await (const [path, { overwrite, type, ...map }] of oldEntires) {
282
+ for (const [path, { overwrite, type, ...map }] of oldEntires) {
273
283
  if (!overwrites.includes(path)) continue
274
284
 
275
285
  const { dir, base } = parse(path)
@@ -6,7 +6,7 @@ import { join } from "node:path"
6
6
  import { OperationType } from "./hook"
7
7
 
8
8
  export interface SdNextSetting {
9
- hook?: Record<string, Record<string, OperationType>>
9
+ hook?: Record<string, OperationType>
10
10
  }
11
11
 
12
12
  export async function readSdNextSetting(): Promise<SdNextSetting> {