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

nlf/workspace fixes #102

Merged
merged 2 commits into from
May 19, 2022
Merged
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
51 changes: 50 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,33 @@ const fs = require('fs')
const glob = require('glob')
const globify = pattern => pattern.split('\\').join('/')

const readOutOfTreeIgnoreFiles = (root, rel, result = '') => {
for (const file of ['.gitignore', '.npmignore']) {
try {
const ignoreContent = fs.readFileSync(path.join(root, file), { encoding: 'utf8' })
result += ignoreContent + '\n'
} catch (err) {
// we ignore ENOENT errors completely because we don't care if the file doesn't exist
// but we throw everything else because failing to read a file that does exist is
// something that the user likely wants to know about. we don't need to test this.
/* istanbul ignore next */
if (err.code !== 'ENOENT') {
throw err
}
}
}

if (!rel) {
return result
}

const firstRel = rel.split(path.sep)[0]
const newRoot = path.join(root, firstRel)
const newRel = path.relative(newRoot, path.join(root, rel))

return readOutOfTreeIgnoreFiles(newRoot, newRel, result)
}

const pathHasPkg = (input) => {
if (!input.startsWith('node_modules/')) {
return false
Expand Down Expand Up @@ -119,9 +146,31 @@ class Walker extends IgnoreWalker {
this.bundledScopes = Array.from(new Set(
this.bundled.filter(f => /^@/.test(f))
.map(f => f.split('/')[0])))
const rules = defaultRules.join('\n') + '\n'
this.packageJsonCache = this.parent ? this.parent.packageJsonCache
: (opt.packageJsonCache || new Map())
let rules = defaultRules.join('\n') + '\n'

if (opt.prefix && opt.workspaces) {
const gPath = globify(opt.path)
const gPrefix = globify(opt.prefix)
const gWorkspaces = opt.workspaces.map((ws) => globify(ws))
// if opt.path and opt.prefix are not the same directory, and opt.workspaces has opt.path
// in it, then we know that opt.path is a workspace directory. in order to not drop ignore
// rules from directories between the workspace root (opt.prefix) and the workspace itself
// (opt.path), we need to find and read those now
/* istanbul ignore else */
if (gPath !== gPrefix && gWorkspaces.includes(gPath)) {
// relpath is the relative path between the prefix and the parent of opt.path
// we use the parent because ignore-walk will read the files in opt.path already
const relpath = path.relative(opt.prefix, path.dirname(opt.path))
rules += readOutOfTreeIgnoreFiles(opt.prefix, relpath)
} else if (gPath === gPrefix) {
// on the other hand, if the path and the prefix are the same, then we ignore workspaces
// so that we don't pack workspaces inside of a root project
rules += opt.workspaces.map((ws) => globify(path.relative(opt.path, ws))).join('\n')
}
}

super.onReadIgnoreFile(rootBuiltinRules, rules, _ => _)
} else {
this.bundled = []
Expand Down
111 changes: 111 additions & 0 deletions test/workspace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
'use strict'

const path = require('path')
const t = require('tap')

const packlist = require('../')

t.test('respects workspace root ignore files', async (t) => {
const root = t.testdir({
'package.json': JSON.stringify({
name: 'workspace-root',
version: '1.0.0',
main: 'root.js',
workspaces: ['./workspaces/foo'],
}),
'root.js': `console.log('hello')`,
'.gitignore': 'ignore-me',
'ignore-me': 'should be ignored',
workspaces: {
'.gitignore': 'ignore-me-also',
'ignore-me': 'should be ignored',
'ignore-me-also': 'should also be ignored',
foo: {
'package.json': JSON.stringify({
name: 'workspace-child',
version: '1.0.0',
main: 'child.js',
}),
'child.js': `console.log('hello')`,
'ignore-me': 'should be ignored',
'ignore-me-also': 'should also be ignored',
},
},
})

const workspacePath = path.join(root, 'workspaces', 'foo')
// this simulates what it looks like when a user does i.e. npm pack -w ./workspaces/foo
const files = await packlist({
path: workspacePath,
prefix: root,
workspaces: [workspacePath],
})
t.same(files, [
'child.js',
'package.json',
])

// here we leave off workspaces to satisfy coverage
const secondFiles = await packlist({
path: workspacePath,
prefix: root,
})
t.same(secondFiles, [
'ignore-me',
'ignore-me-also',
'child.js',
'package.json',
])
})

t.test('packing a workspace root does not include children', async (t) => {
const root = t.testdir({
'package.json': JSON.stringify({
name: 'workspace-root',
version: '1.0.0',
main: 'root.js',
workspaces: ['./workspaces/foo'],
}),
'root.js': `console.log('hello')`,
'.gitignore': 'ignore-me',
'ignore-me': 'should be ignored',
workspaces: {
'.gitignore': 'ignore-me-also',
'ignore-me': 'should be ignored',
'ignore-me-also': 'should also be ignored',
foo: {
'package.json': JSON.stringify({
name: 'workspace-child',
version: '1.0.0',
main: 'child.js',
}),
'child.js': `console.log('hello')`,
'ignore-me': 'should be ignored',
'ignore-me-also': 'should also be ignored',
},
},
})

const workspacePath = path.join(root, 'workspaces', 'foo')
// this simulates what it looks like when a user does i.e. npm pack from a workspace root
const files = await packlist({
path: root,
prefix: root,
workspaces: [workspacePath],
})
t.same(files, [
'root.js',
'package.json',
])

const secondFiles = await packlist({
path: root,
prefix: root,
})
t.same(secondFiles, [
'workspaces/foo/child.js',
'root.js',
'package.json',
'workspaces/foo/package.json',
])
})