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

test #170

Closed
wants to merge 3 commits into from
Closed

test #170

Show file tree
Hide file tree
Changes from all 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
34 changes: 13 additions & 21 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const path = require('path')

const getName = require('@npmcli/name-from-folder')
const { minimatch } = require('minimatch')
const rpj = require('read-package-json-fast')
const pkgJson = require('@npmcli/package-json')
const { glob } = require('glob')

function appendNegatedPatterns (allPatterns) {
Expand Down Expand Up @@ -67,15 +67,7 @@ function getPatterns (workspaces) {
}

function getPackageName (pkg, pathname) {
const { name } = pkg
return name || getName(pathname)
}

function pkgPathmame (opts) {
return (...args) => {
const cwd = opts.cwd ? opts.cwd : process.cwd()
return path.join.apply(null, [cwd, ...args])
}
return pkg.name || getName(pathname)
}

// make sure glob pattern only matches folders
Expand All @@ -101,16 +93,19 @@ async function mapWorkspaces (opts = {}) {
code: 'EMAPWORKSPACESPKG',
})
}
if (!opts.cwd) {
opts.cwd = process.cwd()
}

const { workspaces = [] } = opts.pkg
const { patterns, negatedPatterns } = getPatterns(workspaces)
const results = new Map()
const seen = new Map()

if (!patterns.length && !negatedPatterns.length) {
return results
}

const seen = new Map()
const getGlobOpts = () => ({
...opts,
ignore: [
Expand All @@ -121,8 +116,6 @@ async function mapWorkspaces (opts = {}) {
],
})

const getPackagePathname = pkgPathmame(opts)

let matches = await glob(patterns.map((p) => getGlobPattern(p)), getGlobOpts())
// preserves glob@8 behavior
matches = matches.sort((a, b) => a.localeCompare(b, 'en'))
Expand All @@ -138,10 +131,8 @@ async function mapWorkspaces (opts = {}) {

for (const match of orderedMatches) {
let pkg
const packageJsonPathname = getPackagePathname(match, 'package.json')

try {
pkg = await rpj(packageJsonPathname)
pkg = await pkgJson.normalize(path.join(opts.cwd, match))
} catch (err) {
if (err.code === 'ENOENT') {
continue
Expand All @@ -150,15 +141,14 @@ async function mapWorkspaces (opts = {}) {
}
}

const packagePathname = path.dirname(packageJsonPathname)
const name = getPackageName(pkg, packagePathname)
const name = getPackageName(pkg.content, pkg.path)

let seenPackagePathnames = seen.get(name)
if (!seenPackagePathnames) {
seenPackagePathnames = new Set()
seen.set(name, seenPackagePathnames)
}
seenPackagePathnames.add(packagePathname)
seenPackagePathnames.add(pkg.path)
}

const errorMessageArray = ['must not have multiple workspaces with the same name']
Expand Down Expand Up @@ -200,6 +190,9 @@ mapWorkspaces.virtual = function (opts = {}) {
code: 'EMAPWORKSPACESLOCKFILE',
})
}
if (!opts.cwd) {
opts.cwd = process.cwd()
}

const { packages = {} } = opts.lockfile
const { workspaces = [] } = packages[''] || {}
Expand All @@ -218,10 +211,9 @@ mapWorkspaces.virtual = function (opts = {}) {
}
}

const getPackagePathname = pkgPathmame(opts)
for (const pattern of patterns) {
for (const packageKey of minimatch.match(packageKeys, pattern)) {
const packagePathname = getPackagePathname(packageKey)
const packagePathname = path.join(opts.cwd, packageKey)
const name = getPackageName(packages[packageKey], packagePathname)
results.set(packagePathname, name)
}
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@
"tap": "^16.0.1"
},
"dependencies": {
"@npmcli/name-from-folder": "^3.0.0",
"@npmcli/name-from-folder": "^2.0.0",
"@npmcli/package-json": "^5.2.0",
"glob": "^10.2.2",
"minimatch": "^9.0.0",
"read-package-json-fast": "^4.0.0"
"minimatch": "^9.0.0"
},
"templateOSS": {
"//@npmcli/template-oss": "This file is partially managed by @npmcli/template-oss. Edits may be overwritten.",
Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ test('unexpected rpj errors', t => {
err.code = 'ERR'

const mapW = t.mock('../', {
'read-package-json-fast': () => Promise.reject(err),
'@npmcli/package-json': { normalize: () => Promise.reject(err) },
})

return t.rejects(
Expand Down