Skip to content

Commit

Permalink
chore: migrate to eslint v9 (nuxt#955)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored May 7, 2024
1 parent cbfd092 commit 1c773a0
Show file tree
Hide file tree
Showing 10 changed files with 1,019 additions and 204 deletions.
5 changes: 0 additions & 5 deletions .eslintrc

This file was deleted.

9 changes: 9 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// @ts-check
import { createConfigForNuxt } from '@nuxt/eslint-config/flat'

export default createConfigForNuxt({
features: {
tooling: true,
stylistic: true,
},
})
2 changes: 1 addition & 1 deletion lib/categories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ export const categories = [
'Request',
'SEO',
'Security',
'UI'
'UI',
]
5 changes: 3 additions & 2 deletions lib/cli.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { sync, syncAll, build } from './modules'
import { version } from './version'

async function main () {
async function main() {
const [command, ...args] = process.argv.splice(2)

switch (command) {
Expand All @@ -12,7 +12,8 @@ async function main () {
console.log('Syncing ' + (name === '-' ? repo : name))
const module = await sync(name, repo, true)
console.log('Synced', module.name)
} else {
}
else {
console.log('Syncing all modules')
const { count, success } = await syncAll()
console.log('Sync ' + count + ' modules')
Expand Down
53 changes: 30 additions & 23 deletions lib/modules.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { resolve, join, basename, extname } from 'path'
import { promises as fsp, existsSync } from 'fs'
import { resolve, join, basename, extname } from 'node:path'
import { promises as fsp, existsSync } from 'node:fs'
import * as yml from 'js-yaml'
import { globby } from 'globby'
import defu from 'defu'
import pLimit from 'p-limit'
import { categories } from './categories'
import { ModuleInfo } from './types'
import { fetchGithubPkg, modulesDir, distDir, distFile, rootDir } from './utils'
import { $fetch } from 'ofetch'
import { isCI } from 'std-env'
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, repo?: string, isNew: boolean = false) {
const mod = await getModule(name)

// Repo
Expand Down Expand Up @@ -40,27 +40,32 @@ export async function sync (name, repo?: string, isNew: boolean = false) {
// Type
if (mod.repo.startsWith('nuxt-community/') || mod.repo.startsWith('nuxt-modules/')) {
mod.type = 'community'
} else if (mod.repo.startsWith('nuxt/')) {
}
else if (mod.repo.startsWith('nuxt/')) {
mod.type = 'official'
} else {
}
else {
mod.type = '3rd-party'
}

// Category
if (!mod.category) {
if (!isNew) {
throw new Error(`No category for ${name}`)
} else {
}
else {
console.log(`[TODO] Add a category to ./modules/${name}.yml`)
}
} else if (!categories.includes(mod.category)) {
}
else if (!categories.includes(mod.category)) {
let newCat = mod.category[0].toUpperCase() + mod.category.substr(1)
if (newCat.length <= 3) {
newCat = newCat.toUpperCase()
}
if (categories.includes(newCat)) {
mod.category = newCat
} else {
}
else {
throw new Error(`Unknown category ${mod.category} for ${mod.name}.\nSupported categories: ${categories.join(', ')}`)
}
}
Expand Down Expand Up @@ -100,12 +105,13 @@ export async function sync (name, repo?: string, isNew: boolean = false) {
'maintainers',
'compatibility',
'sponsor',
'aliases'
'aliases',
]
const invalidFields = []
for (const key in mod) {
if (!validFields.includes(key)) {
invalidFields.push(key)
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete mod[key]
}
}
Expand Down Expand Up @@ -134,12 +140,13 @@ export async function sync (name, repo?: string, isNew: boolean = false) {
if (owner !== 'nuxt-community' && owner !== 'nuxt') {
mod.maintainers.push({
name: owner,
github: owner
github: owner,
})
} else if (!isNew) {
}
else if (!isNew) {
throw new Error(`No maintainer for ${mod.name}`)
} else {
// eslint-disable-next-line no-console
}
else {
console.log(`[TODO] Add a maintainer to ./modules/${name}.yml`)
}
}
Expand All @@ -157,7 +164,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): Promise<ModuleInfo> {
let mod: ModuleInfo = {
name,
description: '',
Expand All @@ -172,8 +179,8 @@ export async function getModule (name): Promise<ModuleInfo> {
maintainers: [],
compatibility: {
nuxt: '^2.0.0',
requires: {}
}
requires: {},
},
}

const file = resolve(modulesDir, name + '.yml')
Expand All @@ -184,20 +191,20 @@ export async function getModule (name): Promise<ModuleInfo> {
return mod
}

export async function writeModule (module) {
export async function writeModule(module) {
const file = resolve(modulesDir, `${module.name}.yml`)
await fsp.writeFile(file, yml.dump(module), 'utf8')
}

export async function readModules () {
export async function readModules() {
const globPattern = join(modulesDir, '*.yml').replace(/\\/g, '/')
const names = (await globby(globPattern)).map(p => basename(p, extname(p))).filter(_ => _)

return Promise.all(names.map(n => getModule(n)))
.then(modules => modules.filter(m => m.name))
}

export async function syncAll () {
export async function syncAll() {
const modules = await readModules()
const limit = pLimit(10)
let success = true
Expand All @@ -212,7 +219,7 @@ export async function syncAll () {
return { count: updatedModules.length, success }
}

export async function build () {
export async function build() {
const modules = await readModules()
await fsp.mkdir(distDir, { recursive: true })
await fsp.writeFile(distFile, JSON.stringify(modules, null, 2))
Expand Down
16 changes: 8 additions & 8 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import type { categories } from './categories'
// TODO: Move to @nuxt/kit
// TODO: Support version matrix
export interface ModuleCompatibility {
nuxt: string
devtools?: string
versionMap?: {
[nuxtVersion: string]: string
}
requires: { bridge?: boolean | 'optional' },
nuxt: string
devtools?: string
versionMap?: {
[nuxtVersion: string]: string
}
requires: { bridge?: boolean | 'optional' }
}

export interface MaintainerInfo {
Expand All @@ -28,12 +28,12 @@ export type ModuleType = 'community' | 'official' | '3rd-party'

export interface ModuleInfo {
name: string
description:string
description: string
repo: string
npm: string
icon?: string
github: string
website:string
website: string
learn_more: string
category: (typeof categories)[number]
type: ModuleType
Expand Down
13 changes: 7 additions & 6 deletions lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import { resolve } from 'path'
import { resolve } from 'node:path'
import { ofetch } from 'ofetch'

export const rootDir = resolve(__dirname, '..')
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) {
return ofetch('http://registry.npmjs.org/' + name)
}

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

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

return fetchRawGithub(repo + '/' + path + '/' + 'package.json')
}

export function uniq (items: any[]) {
export function uniq<T>(items: T[]) {
return Array.from(new Set(items))
}
6 changes: 3 additions & 3 deletions lib/version.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import path from 'path'
import { promises as fsp } from 'fs'
import path from 'node:path'
import { promises as fsp } from 'node:fs'
import hasha from 'hasha'
import { rootDir, distFile } from './utils'
import { build } from './modules'

export async function version () {
export async function version() {
await build()

const pkgFile = path.resolve(rootDir, 'package.json')
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@
"cli": "jiti ./lib/cli",
"website:build": "pnpm build && nuxi build website",
"website:dev": "pnpm build && nuxi dev website",
"lint": "eslint --ext .",
"lint": "eslint",
"release": "pnpm cli version && npm publish",
"sync": "pnpm cli sync",
"test": "pnpm lint && pnpm sync"
},
"devDependencies": {
"@nuxt/eslint-config": "^0.2.0",
"@nuxt/eslint-config": "^0.3.10",
"@types/js-yaml": "^4.0.5",
"@types/node": "^18.17.15",
"defu": "^6.1.2",
"eslint": "^8.49.0",
"eslint": "^9.2.0",
"globby": "^13.2.2",
"hasha": "^5.2.2",
"jiti": "^1.20.0",
Expand All @@ -36,4 +36,4 @@
"typescript": "^5.2.2"
},
"packageManager": "[email protected]"
}
}
Loading

0 comments on commit 1c773a0

Please sign in to comment.