Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove getPackageType deps, fix for Bun #335

Merged
merged 3 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
'use strict'

const fs = require('node:fs/promises')
ObscuritySRL marked this conversation as resolved.
Show resolved Hide resolved
const path = require('node:path')
const url = require('node:url')
const { readdir } = require('node:fs').promises
const pkgUp = require('pkg-up')

const isFastifyAutoloadTypescriptOverride = !!process.env.FASTIFY_AUTOLOAD_TYPESCRIPT
const isTsNode = (Symbol.for('ts-node.register.instance') in process) || !!process.env.TS_NODE_DEV
Expand Down Expand Up @@ -116,9 +115,23 @@ const fastifyAutoload = async function autoload (fastify, options) {
}

async function getPackageType (cwd) {
const nearestPackage = await pkgUp({ cwd })
if (nearestPackage) {
return require(nearestPackage).type
const directories = cwd.split(path.sep)

// required for paths that begin with the sep, such as linux root
directories[0] = directories[0] !== '' ? directories[0] : path.sep

while (directories.length > 0) {
const file = path.join(...directories, 'package.json')

const fileExists = await fs.stat(file)
.then(() => true)
.catch(() => false)

if (fileExists) {
return require(file).type
ObscuritySRL marked this conversation as resolved.
Show resolved Hide resolved
}

directories.pop()
}
}

Expand All @@ -132,7 +145,7 @@ function getScriptType (fname, packageType) {
// eslint-disable-next-line default-param-last
async function findPlugins (dir, options, hookedAccumulator = {}, prefix, depth = 0, hooks = []) {
const { indexPattern, ignorePattern, ignoreFilter, matchFilter, scriptPattern, dirNameRoutePrefix, maxDepth, autoHooksPattern } = options
const list = await readdir(dir, { withFileTypes: true })
const list = await fs.readdir(dir, { withFileTypes: true })
let currentHooks = []

// check to see if hooks or plugins have been added to this prefix, initialize if not
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@
"vite": "^4.0.0",
"vitest": "^0.34.1"
},
"dependencies": {
"pkg-up": "^3.1.0"
},
"dependencies": {},
"standard": {
"ignore": [
"test/*/*-error/lib/a.js"
Expand Down