@bagelink/sdk 0.0.631 → 0.0.635
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 +63 -22
- package/package.json +1 -1
package/bin/index.ts
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
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
|
-
const proc = _require('process')
|
|
12
|
+
const proc = _require('process') as typeof import('process')
|
|
13
|
+
|
|
13
14
|
const cwd = proc.cwd()
|
|
14
15
|
const env = loadEnv('', cwd)
|
|
15
16
|
|
|
@@ -19,38 +20,78 @@ 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 formatAndWriteCode(outPath: string, code: string) {
|
|
37
|
+
const prettyCode = await prettier.format(code, { parser: 'typescript' })
|
|
38
|
+
fs.writeFileSync(outPath, prettyCode)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async function runEsLintOnDir(dir: string) {
|
|
42
|
+
// ! only run if eslint.config.js exists
|
|
43
|
+
if (!fs.existsSync(join(cwd, 'eslint.config.js'))) {
|
|
44
|
+
console.log('no eslint.config.js found, skipping eslint')
|
|
45
|
+
return
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const eslint = new ESLint({ fix: true, overrideConfigFile: join(cwd, 'eslint.config.js'), cwd })
|
|
49
|
+
const results = await eslint.lintFiles(`${dir}/**/*.ts`)
|
|
50
|
+
|
|
51
|
+
await ESLint.outputFixes(results)
|
|
52
|
+
|
|
53
|
+
// const formatter = await eslint.loadFormatter('stylish')
|
|
54
|
+
// const rulesMeta = eslint.getRulesMetaForResults(results)
|
|
55
|
+
// await formatter.format(results, { cwd, rulesMeta })
|
|
56
|
+
// const resultText =
|
|
57
|
+
// console.log(resultText)
|
|
58
|
+
}
|
|
34
59
|
|
|
35
60
|
export async function loadTypes() {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
61
|
+
try {
|
|
62
|
+
const { types, code } = await openAPI(
|
|
63
|
+
baseURL || '',
|
|
64
|
+
'import.meta.env.VITE_BAGEL_BASE_URL'
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
if (!fs.existsSync(bagelinkDir)) fs.mkdirSync(bagelinkDir)
|
|
40
68
|
|
|
41
|
-
|
|
69
|
+
const typesPath = join(bagelinkDir, 'types.d.ts')
|
|
70
|
+
await formatAndWriteCode(typesPath, types)
|
|
42
71
|
|
|
43
|
-
|
|
44
|
-
|
|
72
|
+
const apiPath = join(bagelinkDir, 'api.ts')
|
|
73
|
+
await formatAndWriteCode(apiPath, code)
|
|
45
74
|
|
|
46
|
-
|
|
47
|
-
|
|
75
|
+
if (withAuth) {
|
|
76
|
+
const authCode = fs.readFileSync(
|
|
77
|
+
join(cwd, 'auth.ts'),
|
|
78
|
+
'utf-8'
|
|
79
|
+
).replace(
|
|
80
|
+
'',
|
|
81
|
+
''
|
|
82
|
+
)
|
|
83
|
+
const authPath = join(bagelinkDir, 'auth.ts')
|
|
84
|
+
await formatAndWriteCode(authPath, authCode)
|
|
85
|
+
}
|
|
48
86
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
87
|
+
await runEsLintOnDir(bagelinkDir)
|
|
88
|
+
} catch (error) {
|
|
89
|
+
proc.exitCode = 1
|
|
90
|
+
console.error(error)
|
|
53
91
|
}
|
|
54
92
|
}
|
|
55
93
|
|
|
56
|
-
loadTypes().catch(
|
|
94
|
+
loadTypes().catch((error: unknown) => {
|
|
95
|
+
proc.exitCode = 1
|
|
96
|
+
console.error(error)
|
|
97
|
+
})
|