@bagelink/sdk 0.0.631 → 0.0.633
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/bin/index.ts +31 -12
- package/package.json +1 -1
package/bin/index.ts
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
import fs from 'node:fs'
|
|
4
|
-
import { createRequire } from 'node:module'
|
|
5
4
|
import { join } from 'node:path'
|
|
5
|
+
import { createRequire } from 'node:module'
|
|
6
|
+
import * as prettier from 'prettier'
|
|
7
|
+
import { ESLint } from 'eslint'
|
|
6
8
|
import { loadEnv } from 'vite'
|
|
7
9
|
import { openAPI } from '@bagelink/sdk'
|
|
8
10
|
|
|
9
|
-
import * as prettier from 'prettier'
|
|
10
|
-
|
|
11
11
|
const _require = createRequire(import.meta.url)
|
|
12
12
|
const proc = _require('process')
|
|
13
|
-
|
|
13
|
+
|
|
14
|
+
const cwd: string = proc.cwd()
|
|
14
15
|
const env = loadEnv('', cwd)
|
|
15
16
|
|
|
16
17
|
if (!env.VITE_BAGEL_BASE_URL) {
|
|
@@ -19,18 +20,30 @@ if (!env.VITE_BAGEL_BASE_URL) {
|
|
|
19
20
|
|
|
20
21
|
const baseURL = `${env.VITE_BAGEL_BASE_URL}/openapi.json`
|
|
21
22
|
|
|
22
|
-
const dirFlag = proc.argv[
|
|
23
|
+
const [_, __, dirFlag, withAuthArg] = proc.argv as Partial<string[]>
|
|
23
24
|
|
|
24
|
-
const withAuth =
|
|
25
|
+
const withAuth = withAuthArg === '--auth'
|
|
25
26
|
|
|
26
|
-
if (dirFlag) {
|
|
27
|
+
if (dirFlag !== undefined) {
|
|
27
28
|
console.log(`using passed in dir to generate sdk client: ${dirFlag}`)
|
|
28
29
|
} else {
|
|
29
30
|
console.log(`using default dir to generate sdk client: .bagelink`)
|
|
30
31
|
console.log(`to use a different dir, pass it as an argument to the command`)
|
|
31
32
|
}
|
|
32
33
|
|
|
33
|
-
const bagelinkDir = join(cwd, dirFlag
|
|
34
|
+
const bagelinkDir = join(cwd, dirFlag ?? '.bagelink')
|
|
35
|
+
|
|
36
|
+
async function formatCode(code: string) {
|
|
37
|
+
const prettyCode = await prettier.format(code, { parser: 'typescript' })
|
|
38
|
+
|
|
39
|
+
const eslint = new ESLint({ fix: true, overrideConfigFile: join(cwd, 'eslint.config.js') })
|
|
40
|
+
const results = await eslint.lintText(prettyCode)
|
|
41
|
+
const formatter = await eslint.loadFormatter('stylish')
|
|
42
|
+
const rulesMeta = eslint.getRulesMetaForResults(results)
|
|
43
|
+
const resultText = await formatter.format(results, { cwd, rulesMeta })
|
|
44
|
+
|
|
45
|
+
return resultText
|
|
46
|
+
}
|
|
34
47
|
|
|
35
48
|
export async function loadTypes() {
|
|
36
49
|
const { types, code } = await openAPI(
|
|
@@ -41,15 +54,21 @@ export async function loadTypes() {
|
|
|
41
54
|
if (!fs.existsSync(bagelinkDir)) fs.mkdirSync(bagelinkDir)
|
|
42
55
|
|
|
43
56
|
const typesPath = join(bagelinkDir, 'types.d.ts')
|
|
44
|
-
fs.writeFileSync(typesPath, await
|
|
57
|
+
fs.writeFileSync(typesPath, await formatCode(types))
|
|
45
58
|
|
|
46
59
|
const apiPath = join(bagelinkDir, 'api.ts')
|
|
47
|
-
fs.writeFileSync(apiPath, await
|
|
60
|
+
fs.writeFileSync(apiPath, await formatCode(code))
|
|
48
61
|
|
|
49
62
|
if (withAuth) {
|
|
50
|
-
const authCode = fs.readFileSync(
|
|
63
|
+
const authCode = fs.readFileSync(
|
|
64
|
+
join(cwd, 'auth.ts'),
|
|
65
|
+
'utf-8'
|
|
66
|
+
).replace(
|
|
67
|
+
'// @ts-expect-error - required for axios to work',
|
|
68
|
+
''
|
|
69
|
+
)
|
|
51
70
|
const authPath = join(bagelinkDir, 'auth.ts')
|
|
52
|
-
fs.writeFileSync(authPath, authCode)
|
|
71
|
+
fs.writeFileSync(authPath, await formatCode(authCode))
|
|
53
72
|
}
|
|
54
73
|
}
|
|
55
74
|
|