Skip to content

Commit

Permalink
Refactor++
Browse files Browse the repository at this point in the history
  • Loading branch information
tinvaan committed May 27, 2024
1 parent 3f4b02f commit ecec6ab
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 28 deletions.
32 changes: 4 additions & 28 deletions provider/techstack/index.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,16 @@

import fs from 'fs'
import YAML from 'yaml'
import load from './settings.js'

import type {
Annotation,
AnnotationsParams,
AnnotationsResult,
MetaParams,
MetaResult,
Provider,
Annotation
Provider
} from '@openctx/provider'

import type TSF from './techstack.schema.ts'
export type Settings = { yaml: string }

/**
* Read the techstack file configuration for project
*
* @param fileUri - absolute path of techstack yml file
* @returns parsed yaml object
*/
async function load(fileUri: string): Promise<TSF> {
let content
if (typeof window === 'undefined') {
// Server
content = await fs.promises.readFile(fileUri, 'utf-8')
} else {
// Browser
const r: Response = await fetch(fileUri)
if (r.status !== 200) {
console.error(`Techstack: failed to fetch settings from ${fileUri}`)
return {} as TSF
}
content = await r.text()
}
return YAML.parse(content)
}

const techstack: Provider<Settings> = {
meta(params: MetaParams, settings: Settings): MetaResult {
Expand Down
49 changes: 49 additions & 0 deletions provider/techstack/settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

import YAML from 'yaml'
import { readFileSync } from 'fs'
import { fileURLToPath } from 'url'


import type TSF from './techstack.schema.js'


export function urlread(uri: string): string {
const url = new URL(uri)
if (url.protocol === 'file:') {
return fileURLToPath(url)
}

if (url.protocol === 'http:' || url.protocol === 'https:') {
if (typeof window === 'undefined') {
throw new URIError(`${url.protocol} urls are not supported in current environment`)
}
return uri
}

throw new URIError(`${url.protocol} urls not supported`)
}

/**
* Read the techstack file configuration for project
*
* @param fileUri - absolute path of techstack yml file
* @returns parsed yaml object
*/
export default async function load(fileUri: string): Promise<TSF> {
let content

Check failure on line 33 in provider/techstack/settings.ts

View workflow job for this annotation

GitHub Actions / build

This variable has implicitly the any type.
const uri = urlread(fileUri)

if (typeof window === 'undefined') {
content = readFileSync(uri, 'utf-8')
} else {
// Browser
const r: Response = await fetch(fileUri)
if (r.status !== 200) {
console.error(`Techstack: failed to fetch settings from ${fileUri}`)
return {} as TSF
}
content = await r.text()
}

return YAML.parse(content)
}

0 comments on commit ecec6ab

Please sign in to comment.