Skip to content

Commit

Permalink
fix(deps): update dependency read-pkg-up to v7 (#1176)
Browse files Browse the repository at this point in the history
  • Loading branch information
renovate[bot] authored Aug 31, 2020
1 parent b6b11b1 commit b83885e
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 201 deletions.
48 changes: 0 additions & 48 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,18 +125,14 @@
"netlify-redirect-parser": "^2.5.0",
"netlify-redirector": "^0.2.0",
"node-fetch": "^2.6.0",
"npm-packlist": "^1.4.4",
"open": "^6.4.0",
"ora": "^4.1.1",
"p-wait-for": "^3.0.0",
"parse-github-url": "^1.0.2",
"parse-gitignore": "^1.0.1",
"precinct": "^6.1.2",
"prettyjson": "^1.2.1",
"random-item": "^1.0.0",
"raw-body": "^2.4.1",
"read-pkg-up": "^6.0.0",
"require-package-name": "^2.0.1",
"resolve": "^1.12.0",
"safe-join": "^0.1.3",
"static-server": "^2.2.1",
Expand Down
150 changes: 1 addition & 149 deletions src/utils/finders.js
Original file line number Diff line number Diff line change
@@ -1,153 +1,5 @@
const path = require('path')
const fs = require('fs')
const packList = require('npm-packlist')
const precinct = require('precinct')
const resolve = require('resolve')
const readPkgUp = require('read-pkg-up')
const requirePackageName = require('require-package-name')
const alwaysIgnored = new Set(['aws-sdk'])
const debug = require('debug')('netlify-dev-plugin:src/utils/finders')

const ignoredExtensions = new Set([
'.log',
'.lock',
'.html',
'.md',
'.map',
'.ts',
'.png',
'.jpeg',
'.jpg',
'.gif',
'.css',
'.patch',
])

function ignoreMissing(dependency, optional) {
return alwaysIgnored.has(dependency) || (optional && dependency in optional)
}

function includeModuleFile(packageJson, moduleFilePath) {
if (packageJson.files) {
return true
}

return !ignoredExtensions.has(path.extname(moduleFilePath))
}

function getDependencies(filename, basedir) {
const servicePath = basedir

const filePaths = new Set()
const modulePaths = new Set()
const pkgs = {}

const modulesToProcess = []
const localFilesToProcess = [filename]

function handle(name, basedir, optionalDependencies) {
const moduleName = requirePackageName(name.replace(/\\/g, '/'))

if (alwaysIgnored.has(moduleName)) {
return
}

try {
const pathToModule = resolve.sync(path.join(moduleName, 'package.json'), {
basedir,
})
const pkg = readPkgUp.sync({ cwd: pathToModule })

if (pkg) {
modulesToProcess.push(pkg)
}
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
if (ignoreMissing(moduleName, optionalDependencies)) {
debug(`WARNING missing optional dependency: ${moduleName}`)
return null
}
try {
// this resolves the requested import also against any set up NODE_PATH extensions, etc.
const resolved = require.resolve(name)
localFilesToProcess.push(resolved)
return
} catch (e) {
throw new Error(`Could not find "${moduleName}" module in file: ${filename.replace(
path.dirname(basedir),
''
)}.
Please ensure "${moduleName}" is installed in the project.`)
}
}
throw e
}
}

while (localFilesToProcess.length) {
const currentLocalFile = localFilesToProcess.pop()

if (filePaths.has(currentLocalFile)) {
continue
}

filePaths.add(currentLocalFile)
precinct.paperwork(currentLocalFile, { includeCore: false }).forEach(dependency => {
if (dependency.indexOf('.') === 0) {
const abs = resolve.sync(dependency, {
basedir: path.dirname(currentLocalFile),
})
localFilesToProcess.push(abs)
} else {
handle(dependency, servicePath)
}
})
}

while (modulesToProcess.length) {
const currentModule = modulesToProcess.pop()
const currentModulePath = path.join(currentModule.path, '..')
const packageJson = currentModule.pkg

if (modulePaths.has(currentModulePath)) {
continue
}
modulePaths.add(currentModulePath)
pkgs[currentModulePath] = packageJson
;['dependencies', 'peerDependencies', 'optionalDependencies'].forEach(key => {
const dependencies = packageJson[key]

if (dependencies) {
Object.keys(dependencies).forEach(dependency => {
handle(dependency, currentModulePath, packageJson.optionalDependencies)
})
}
})
}

modulePaths.forEach(modulePath => {
const packageJson = pkgs[modulePath]
const moduleFilePaths = packList.sync({ path: modulePath })

moduleFilePaths.forEach(moduleFilePath => {
if (includeModuleFile(packageJson, moduleFilePath)) {
filePaths.add(path.join(modulePath, moduleFilePath))
}
})
})

// TODO: get rid of this
const sizes = {}
filePaths.forEach(filepath => {
const stat = fs.lstatSync(filepath)
const ext = path.extname(filepath)
sizes[ext] = (sizes[ext] || 0) + stat.size
})
debug('Sizes per extension: ', sizes)

return [...filePaths]
}

function findModuleDir(dir) {
let basedir = dir
Expand All @@ -173,4 +25,4 @@ function findHandler(functionPath) {
return handlerPath
}

module.exports = { getDependencies, findModuleDir, findHandler }
module.exports = { findModuleDir, findHandler }

0 comments on commit b83885e

Please sign in to comment.