Skip to content

Commit

Permalink
refactor: improve type safety
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Nov 9, 2024
1 parent e4e144a commit 137416a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ jobs:
- name: Lint
run: pnpm lint

- name: Test types
run: pnpm tsc --noEmit

- name: Sync
run: pnpm sync

Expand Down
16 changes: 9 additions & 7 deletions lib/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { categories } from './categories'
import type { ModuleInfo } from './types'
import { fetchGithubPkg, modulesDir, distDir, distFile, rootDir } from './utils'

export async function sync(name, repo?: string, isNew: boolean = false) {
export async function sync(name: string, repo?: string, isNew: boolean = false) {
const mod = await getModule(name)

// Repo
Expand All @@ -23,7 +23,7 @@ export async function sync(name, repo?: string, isNew: boolean = false) {
}

// Defaults
if (!mod.repo) {
if (!mod.repo && repo) {
mod.repo = repo
}
if (!mod.github) {
Expand Down Expand Up @@ -58,7 +58,7 @@ export async function sync(name, repo?: string, isNew: boolean = false) {
}
}
else if (!categories.includes(mod.category)) {
let newCat = mod.category[0].toUpperCase() + mod.category.substr(1)
let newCat = mod.category[0]!.toUpperCase() + mod.category.substr(1)
if (newCat.length <= 3) {
newCat = newCat.toUpperCase()
}
Expand All @@ -72,7 +72,7 @@ export async function sync(name, repo?: string, isNew: boolean = false) {

// ci is flaky with external links
if (!isCI) {
for (const key of ['website', 'learn_more']) {
for (const key of ['website', 'learn_more'] as const) {
if (mod[key] && !mod[key].includes('github.com')) {
// we just need to test that we get a 200 response (or a valid redirect)
await $fetch(mod[key]).catch((err) => {
Expand Down Expand Up @@ -111,6 +111,8 @@ export async function sync(name, repo?: string, isNew: boolean = false) {
for (const key in mod) {
if (!validFields.includes(key)) {
invalidFields.push(key)

// @ts-expect-error dynamic delete
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete mod[key]
}
Expand All @@ -137,7 +139,7 @@ export async function sync(name, repo?: string, isNew: boolean = false) {
// TODO: Sync with maintainers.app
if (!mod.maintainers.length) {
const owner = mod.repo.split('/')[0]
if (owner !== 'nuxt-community' && owner !== 'nuxt') {
if (owner && owner !== 'nuxt-community' && owner !== 'nuxt') {
mod.maintainers.push({
name: owner,
github: owner,
Expand All @@ -164,7 +166,7 @@ export async function sync(name, repo?: string, isNew: boolean = false) {
return mod
}

export async function getModule(name): Promise<ModuleInfo> {
export async function getModule(name: string): Promise<ModuleInfo> {
let mod: ModuleInfo = {
name,
description: '',
Expand All @@ -191,7 +193,7 @@ export async function getModule(name): Promise<ModuleInfo> {
return mod
}

export async function writeModule(module) {
export async function writeModule(module: ModuleInfo) {
const file = resolve(modulesDir, `${module.name}.yml`)
await fsp.writeFile(file, yml.dump(module), 'utf8')
}
Expand Down
8 changes: 4 additions & 4 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ export const modulesDir = resolve(rootDir, 'modules')
export const distDir = resolve(rootDir)
export const distFile = resolve(distDir, 'modules.json')

export function fetchPKG(name) {
export function fetchPKG(name: string) {
return ofetch('http://registry.npmjs.org/' + name)
}

export function fetchRawGithub(path) {
export function fetchRawGithub(path: string) {
return ofetch('https://raw.githubusercontent.com/' + path, { responseType: 'json' })
}

export function fetchGithubPkg(repo) {
export function fetchGithubPkg(repo: string) {
let path: string
// HEAD will be the default branch
[repo, path = 'HEAD'] = repo.split('#')
[repo, path = 'HEAD'] = repo.split('#') as [string, string?]

return fetchRawGithub(repo + '/' + path + '/' + 'package.json')
}
Expand Down
20 changes: 19 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
{
"extends": "./website/.nuxt/tsconfig.json"
"compilerOptions": {
"target": "es2022",
"lib": [
"es2022"
],
"moduleDetection": "force",
"module": "preserve",
"resolveJsonModule": true,
"allowJs": true,
"strict": true,
"noImplicitOverride": true,
"noUncheckedIndexedAccess": true,
"noEmit": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"isolatedModules": true,
"verbatimModuleSyntax": true,
"skipLibCheck": true
}
}

0 comments on commit 137416a

Please sign in to comment.