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

Support excluding specific file patterns in nested directories #29

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,10 @@ const npmWalker = Class => class Walker extends Class {

if (Array.isArray(pkg.files))
super.onReadIgnoreFile('package.json', '*\n' + pkg.files.map(
f => '!' + f + '\n!' + f.replace(/\/+$/, '') + '/**'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be a better solution if we change this to always return '!' + f, and then update ignore-walker to handle directories when the /** is missing? Currently if I do this, the tests for the package-json-bin-x.js tests fail, e.g.

const json = {
name: 'test-package',
version: '1.6.2',
bin: '__bin',
files: [
'lib'
]
}

// to support excluding specific file patterns, don't append "/**" if f ends in "**/!(pattern)"
f => /\*\*\/!\([^/]+\)$/.test(f)
? '!' + f
: '!' + f + '\n!' + f.replace(/\/+$/, '') + '/**'
).join('\n') + '\n', then)
else
then()
Expand Down
73 changes: 73 additions & 0 deletions test/package-json-glob.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'use strict'

const fs = require('fs')
const path = require('path')

const mkdirp = require('mkdirp')
const rimraf = require('rimraf')
const t = require('tap')

const pack = require('../')

const pkg = path.join(__dirname, path.basename(__filename, '.js'))
t.teardown(_ => rimraf.sync(pkg))

const json = {
name: 'test-package',
version: '1.6.2',
files: [
'dist/**/!(*.test.*)'
]
}

const expect = [
'package.json',
'dist/index.d.ts',
'dist/index.js',
'dist/lib/util.d.ts',
'dist/lib/util.js'
]

t.test('setup', t => {
rimraf.sync(pkg)
mkdirp.sync(pkg)
fs.writeFileSync(path.join(pkg, 'package.json'), JSON.stringify(json, null, 2))

const srcDir = path.join(pkg, 'src')
mkdirp.sync(srcDir)
fs.writeFileSync(path.join(srcDir, 'index.ts'), 'export const x = () => console.log("index")')
fs.writeFileSync(path.join(srcDir, 'index.test.ts'), 'import { x } from "./index"')

const srcLibDir = path.join(srcDir, 'lib')
mkdirp.sync(srcLibDir)
fs.writeFileSync(path.join(srcLibDir, 'util.ts'), 'export const y = () => console.log("util")')
fs.writeFileSync(path.join(srcLibDir, 'util.test.ts'), 'import { y } from "./util"')

const distDir = path.join(pkg, 'dist')
mkdirp.sync(distDir)
fs.writeFileSync(path.join(distDir, 'index.d.ts'), 'export declare function x(): void')
fs.writeFileSync(path.join(distDir, 'index.js'), 'exports.x = function () { console.log("index") }')
fs.writeFileSync(path.join(distDir, 'index.test.d.ts'), '')
fs.writeFileSync(path.join(distDir, 'index.test.js'), 'var x = require("./index").x')

const distLibDir = path.join(distDir, 'lib')
mkdirp.sync(distLibDir)
fs.writeFileSync(path.join(distLibDir, 'util.d.ts'), 'export declare function y(): void')
fs.writeFileSync(path.join(distLibDir, 'util.js'), 'exports.y = function () { console.log("util") }')
fs.writeFileSync(path.join(distLibDir, 'util.test.d.ts'), '')
fs.writeFileSync(path.join(distLibDir, 'util.test.js'), 'var y = require("./util").y')

t.end()
})

t.test('follows npm package ignoring rules', t => {
const check = (files, t) => {
t.same(files, expect)
t.end()
}

t.test('sync', t => check(pack.sync({ path: pkg }), t))
t.test('async', t => pack({ path: pkg }).then(files => check(files, t)))

t.end()
})