Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
BREAKING: Remove --entry, instead add directly
Browse files Browse the repository at this point in the history
  • Loading branch information
voxpelli committed Jul 28, 2019
1 parent 151bc29 commit 8657129
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 23 deletions.
3 changes: 1 addition & 2 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ if (args.help || args._.length === 0) {
console.log("--no-dev Won't tell you about devDependencies that are missing or unused")
console.log("--no-peer Won't tell you about peerDependencies that are missing or unused")
console.log("--ignore-module, -i Won't tell you about these module names when missing or unused. Supports globbing")
console.log('--entry If a package.json or module folder was set, then by default the main and bin entries in the package.json will be parsed, but you can add more the list of entries by passing them in as --entry. Supports globbing')
console.log("--no-default-entries Won't parse your main and bin entries from package.json even when a package.json or module folder has been defined")
console.log('--detective Requireable path containing an alternative implementation of the detective module that supports alternate syntaxes')
console.log("--extensions, -e List of file extensions with detective to use when resolving require paths. Eg. 'js,jsx:detective-es6'")
Expand Down Expand Up @@ -73,7 +72,7 @@ function extensions (arg) {

check({
path: args._.shift(),
entries: args._.concat(args.entry || []),
entries: args._,
noDefaultEntries: !args['default-entries'],
extensions: extensions(args.e),
detective: args.detective
Expand Down
80 changes: 60 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,43 +42,83 @@ async function resolveGlobbedPath (entries, cwd) {
return paths
}

module.exports = async function (opts) {
let targetPath = opts.path
let pkgPath = targetPath
let entries = []
let pkg
async function resolveModuleTarget (targetPath) {
let pkgPath, pkg

try {
pkg = await promisedReadPackage(targetPath)
pkgPath = targetPath
} catch (err) {
if (targetPath.endsWith('/package.json') || targetPath === 'package.json') {
throw new Error('Failed to read package.json: ' + err.message)
}

if (err && err.code === 'EISDIR') {
// We were given a path to a module folder
pkgPath = path.join(targetPath, 'package.json')
} else {
// We've likely been given entries rather than a package.json or module path, try resolving that instead
entries = await resolveGlobbedPath(pkgPath)
pkg = await promisedReadPackage(pkgPath)
}
}

if (!entries[0]) {
throw new Error('Failed to find package.json, no files found')
}
if (!pkg) return undefined

opts.noDefaultEntries = true
pkgPath = await pkgUp({ cwd: path.dirname(entries[0]) })
}
return {
pkgPath,
pkg
}
}

async function resolveEntryTarget (targetPath) {
// We've been given an entry path pattern as the target rather than a package.json or module folder
// We'll resolve those entries and then finds us the package.json from the location of those
const targetEntries = await resolveGlobbedPath(targetPath)

if (!targetEntries[0]) {
throw new Error('Failed to find package.json, no file to resolve it from')
}

const pkgPath = await pkgUp({ cwd: path.dirname(targetEntries[0]) })

if (!pkgPath) {
throw new Error('Failed to find a package.json')
}

pkg = await promisedReadPackage(pkgPath)
const pkg = await promisedReadPackage(pkgPath)

return {
pkgPath,
pkg,
targetEntries
}
}

module.exports = async function ({
builtins,
detective,
entries,
extensions,
noDefaultEntries,
path: targetPath
}) {
if (!targetPath) throw new Error('Requires a path to be set')

const {
pkgPath,
pkg,
targetEntries
} = await resolveModuleTarget(targetPath) || await resolveEntryTarget(targetPath)

entries = targetEntries ? [...targetEntries, ...entries] : entries
extensions = getExtensions(extensions, detective)
noDefaultEntries = noDefaultEntries || (targetEntries && targetEntries.length !== 0)

return parse({
path: pkgPath,
builtins,
entries,
extensions,
noDefaultEntries,
package: pkg,
entries: entries.concat(opts.entries),
noDefaultEntries: opts.noDefaultEntries,
builtins: opts.builtins,
extensions: getExtensions(opts.extensions, opts.detective)
path: pkgPath
})
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"check:node-versions": "installed-check --engine-check --no-version-check",
"lint": "standard",
"test-cli:custom-detective": "node cli.js test/ -e js:detective-cjs",
"test-cli:glob": "node cli.js test/ --entry '**/*.js' --no-default-entries",
"test-cli:glob": "node cli.js 'test/**/*.js' --no-default-entries",
"test-cli:main-as-file": "node cli.js test/index.js",
"test-cli:simple": "node cli.js test/",
"test-cli": "npm-run-all --parallel test-cli:*",
Expand Down

0 comments on commit 8657129

Please sign in to comment.