@effect-app/cli 1.23.1 → 1.23.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/src/old.ts ADDED
@@ -0,0 +1,283 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ /* eslint-disable @typescript-eslint/no-unsafe-argument */
3
+ import cp from "child_process"
4
+ import fs from "fs"
5
+ import w from "node-watch"
6
+ import path from "path"
7
+ import readline from "readline/promises"
8
+ import { sync } from "./sync.js"
9
+
10
+ function askQuestion(query: string) {
11
+ const rl = readline.createInterface({
12
+ input: process.stdin,
13
+ output: process.stdout
14
+ })
15
+
16
+ return rl.question(query)
17
+ }
18
+
19
+ const _cmd = process.argv[2]
20
+ const supportedCommands = [
21
+ "watch",
22
+ "index",
23
+ "index-multi",
24
+ "packagejson",
25
+ "packagejson-target",
26
+ "packagejson-packages",
27
+ "link",
28
+ "unlink",
29
+ "sync",
30
+ "ncu:effect",
31
+ "ncu:effect-app"
32
+ ] as const
33
+ if (
34
+ !supportedCommands.includes(_cmd as any)
35
+ ) {
36
+ console.log("unknown command: ", _cmd, "supported commands: ", supportedCommands.join(", "))
37
+ process.exit(1)
38
+ }
39
+
40
+ const cmd = _cmd as typeof supportedCommands[number]
41
+
42
+ const debug = process.argv.includes("--debug")
43
+
44
+ function touch(path: string) {
45
+ const time = new Date()
46
+ try {
47
+ fs.utimesSync(path, time, time)
48
+ } catch (err) {
49
+ fs.closeSync(fs.openSync(path, "w"))
50
+ }
51
+ }
52
+
53
+ function* monitorIndexes_(path: string) {
54
+ yield monitorChildIndexes(path)
55
+ const indexFile = path + "/index.ts"
56
+ if (fs.existsSync(indexFile)) {
57
+ yield monitorRootIndexes(path, indexFile)
58
+ }
59
+ }
60
+
61
+ function monitorIndexes(path: string) {
62
+ return [...monitorIndexes_(path)]
63
+ }
64
+
65
+ function monitorChildIndexes(path: string) {
66
+ return w.default(path, { recursive: true }, (evt, path) => {
67
+ const pathParts = path.split("/")
68
+ const isController = pathParts[pathParts.length - 1]?.toLowerCase().includes(".controllers.")
69
+ if (!isController) return
70
+
71
+ let i = 1
72
+ const r = pathParts.toReversed()
73
+ while (i < r.length) {
74
+ const files = ["controllers.ts", "routes.ts"]
75
+ .map((f) => [...pathParts.slice(0, pathParts.length - i), f].join("/"))
76
+ .filter((f) => fs.existsSync(f))
77
+ if (files.length) {
78
+ if (debug) {
79
+ console.log("change!", evt, path, files)
80
+ }
81
+ cp.execSync(`cd api && pnpm eslint --fix ${files.map((_) => `"../${_}"`).join(" ")}`)
82
+ break
83
+ }
84
+ i++
85
+ }
86
+ })
87
+ }
88
+
89
+ function monitorRootIndexes(path: string, indexFile: string) {
90
+ return w.default(path, (_, path) => {
91
+ if (path.endsWith(indexFile)) return
92
+ // const dirName = pathParts[pathParts.length - 2]!
93
+ // console.log("change!", evt, path, dirName, indexFile)
94
+ cp.execSync(`pnpm eslint --fix "${indexFile}"`)
95
+ })
96
+ }
97
+
98
+ // TODO: cache, don't do things when it already existed before, so only file is updated, not created.
99
+
100
+ const startDir = process.cwd()
101
+
102
+ function packagejson(p: string, levels = 0) {
103
+ const curDir = process.cwd()
104
+ let r = ""
105
+ // TODO: no chdir!
106
+ try {
107
+ process.chdir(path.resolve(startDir, p))
108
+ r = cp.execSync(`sh ${p === "." ? "../.." : startDir}/scripts/extract.sh`, { encoding: "utf-8" })
109
+ } finally {
110
+ process.chdir(curDir)
111
+ }
112
+
113
+ const s = r.split("\n").sort((a, b) => a < b ? -1 : 1).join("\n")
114
+ const items = JSON.parse(`{${s.substring(0, s.length - 1)} }`) as Record<string, unknown>
115
+
116
+ const pkg = JSON.parse(fs.readFileSync(p + "/package.json", "utf-8"))
117
+ const t = levels
118
+ ? Object
119
+ .keys(items)
120
+ .filter((_) => _.split("/").length <= (levels + 1 /* `./` */))
121
+ .reduce((prev, cur) => {
122
+ prev[cur] = items[cur]
123
+ return prev
124
+ }, {} as Record<string, unknown>)
125
+ : items
126
+
127
+ const exps = {
128
+ ...(fs.existsSync(p + "/src/index.ts")
129
+ ? {
130
+ ".": {
131
+ "types": "./dist/index.d.ts",
132
+ "default": "./dist/index.js"
133
+ }
134
+ }
135
+ : undefined),
136
+ ...Object
137
+ .keys(t)
138
+ .reduce((prev, cur) => {
139
+ if (cur !== "./index" && !cur.includes("/internal/")) prev[cur] = t[cur]
140
+ return prev
141
+ }, {} as Record<string, unknown>)
142
+ // ...pkg.name === "effect-app" ? {
143
+ // "./types/awesome": { "types": "./types/awesome.d.ts" }
144
+ // } : {},
145
+ }
146
+ pkg.exports = exps
147
+ fs.writeFileSync(p + "/package.json", JSON.stringify(pkg, null, 2))
148
+ }
149
+
150
+ function monitorPackagejson(path: string, levels = 0) {
151
+ packagejson(path, levels)
152
+ w.default(path + "/src", { recursive: true }, (_, __) => {
153
+ packagejson(path, levels)
154
+ })
155
+ }
156
+
157
+ function updateEffectAppPackages() {
158
+ const filters = ["effect-app", "@effect-app/*"]
159
+ for (const filter of filters) {
160
+ cp.execSync(`pnpm exec ncu -u --filter "${filter}"`, { stdio: "inherit" })
161
+ cp.execSync(`pnpm -r exec ncu -u --filter "${filter}"`, { stdio: "inherit" })
162
+ }
163
+ }
164
+
165
+ function updateEffectPackages() {
166
+ const effectFilters = ["effect", "@effect/*", "@effect-atom/*"]
167
+ for (const filter of effectFilters) {
168
+ cp.execSync(`pnpm exec ncu -u --filter "${filter}"`, { stdio: "inherit" })
169
+ cp.execSync(`pnpm -r exec ncu -u --filter "${filter}"`, { stdio: "inherit" })
170
+ }
171
+
172
+ updateEffectAppPackages()
173
+ }
174
+
175
+ ;(async () => {
176
+ let cmds = process.argv.slice(3).filter((_) => _ !== "--debug")
177
+ switch (cmd) {
178
+ case "link":
179
+ await import("./link.js")
180
+ break
181
+ case "unlink":
182
+ await import("./unlink.js")
183
+ break
184
+ case "watch": {
185
+ const dirs = ["../api/src/resources", "../api/src/models"]
186
+ const viteConfigFile = "./vite.config.ts"
187
+ const viteConfigExists = fs.existsSync(viteConfigFile)
188
+ dirs.forEach((d) => {
189
+ if (fs.existsSync(d)) {
190
+ const files: string[] = []
191
+ w.default(d, { recursive: true }, (t, f) => {
192
+ // console.log("change!", d)
193
+ touch("./tsconfig.json")
194
+ if (viteConfigExists && t === "update" && !files.includes(f)) {
195
+ // TODO: only on new files
196
+ touch(viteConfigFile)
197
+ files.push(f)
198
+ }
199
+ })
200
+ }
201
+ })
202
+
203
+ break
204
+ }
205
+
206
+ case "index-multi": {
207
+ ;[
208
+ "./api/src"
209
+ ]
210
+ .filter(
211
+ (_) => fs.existsSync(_)
212
+ )
213
+ .forEach(monitorIndexes)
214
+ break
215
+ }
216
+
217
+ case "index": {
218
+ monitorIndexes("./src")
219
+ break
220
+ }
221
+
222
+ case "packagejson": {
223
+ monitorPackagejson(".")
224
+ break
225
+ }
226
+
227
+ case "packagejson-target": {
228
+ const target = process.argv[3]!
229
+ target.split(",").forEach((_) => monitorPackagejson(_, 1))
230
+ cmds = process.argv.slice(4)
231
+ break
232
+ }
233
+
234
+ case "packagejson-packages": {
235
+ fs
236
+ .readdirSync(startDir + "/packages")
237
+ .map((_) => startDir + "/packages/" + _)
238
+ .filter((_) =>
239
+ fs.existsSync(_ + "/package.json")
240
+ && fs.existsSync(_ + "/src")
241
+ && !_.endsWith("eslint-codegen-model")
242
+ && !_.endsWith("vue-components")
243
+ )
244
+ .forEach((_) => monitorPackagejson(_))
245
+ break
246
+ }
247
+
248
+ case "sync": {
249
+ console.log("Sync all snippets?")
250
+
251
+ await askQuestion("Are you sure you want to sync snippets")
252
+ await sync()
253
+ return process.exit(0)
254
+ }
255
+
256
+ case "ncu:effect": {
257
+ console.log("Updating effect & effect-app dependencies...")
258
+
259
+ updateEffectPackages()
260
+
261
+ cp.execSync("pnpm i", { stdio: "inherit" })
262
+
263
+ break
264
+ }
265
+
266
+ case "ncu:effect-app": {
267
+ console.log("Updating effect-app dependencies...")
268
+
269
+ updateEffectAppPackages()
270
+
271
+ cp.execSync("pnpm i", { stdio: "inherit" })
272
+
273
+ break
274
+ }
275
+ }
276
+
277
+ if (cmds.length) {
278
+ const p = cp.spawn(cmds[0]!, cmds.slice(1), { stdio: "inherit" })
279
+ p.on("close", (code) => process.exit(code ?? 0))
280
+ p.on("exit", (code) => process.exit(code ?? 0))
281
+ p.on("disconnect", () => process.exit(1))
282
+ }
283
+ })()