@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.
Files changed (2) hide show
  1. package/bin/index.ts +63 -22
  2. 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[2]
23
+ const [_, __, dirFlag, withAuthArg] = proc.argv as Partial<string[]>
23
24
 
24
- const withAuth = proc.argv[3] === '--auth'
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 || '.bagelink')
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
- const { types, code } = await openAPI(
37
- baseURL || '',
38
- 'import.meta.env.VITE_BAGEL_BASE_URL'
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
- if (!fs.existsSync(bagelinkDir)) fs.mkdirSync(bagelinkDir)
69
+ const typesPath = join(bagelinkDir, 'types.d.ts')
70
+ await formatAndWriteCode(typesPath, types)
42
71
 
43
- const typesPath = join(bagelinkDir, 'types.d.ts')
44
- fs.writeFileSync(typesPath, await prettier.format(types, { parser: 'typescript' }))
72
+ const apiPath = join(bagelinkDir, 'api.ts')
73
+ await formatAndWriteCode(apiPath, code)
45
74
 
46
- const apiPath = join(bagelinkDir, 'api.ts')
47
- fs.writeFileSync(apiPath, await prettier.format(code, { parser: 'typescript' }))
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
- if (withAuth) {
50
- const authCode = fs.readFileSync(join(cwd, 'auth.txt'), 'utf-8')
51
- const authPath = join(bagelinkDir, 'auth.ts')
52
- fs.writeFileSync(authPath, authCode)
87
+ await runEsLintOnDir(bagelinkDir)
88
+ } catch (error) {
89
+ proc.exitCode = 1
90
+ console.error(error)
53
91
  }
54
92
  }
55
93
 
56
- loadTypes().catch(console.error)
94
+ loadTypes().catch((error: unknown) => {
95
+ proc.exitCode = 1
96
+ console.error(error)
97
+ })
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@bagelink/sdk",
3
3
  "type": "module",
4
- "version": "0.0.631",
4
+ "version": "0.0.635",
5
5
  "description": "Bagel core sdk packages",
6
6
  "author": {
7
7
  "name": "Neveh Allon",