Skip to content
This repository has been archived by the owner on May 10, 2021. It is now read-only.

Commit

Permalink
respect no-optional argument
Browse files Browse the repository at this point in the history
PR-URL: #2
Credit: @cruzdanilo
Close: #2
Reviewed-by: @isaacs
  • Loading branch information
cruzdanilo authored and claudiahdz committed Oct 8, 2019
1 parent 3fa8efb commit 425669a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,10 @@ class Installer {
/^dev(elopment)?$/.test(this.opts.also)
)
const includeProd = !/^dev(elopment)?$/.test(this.opts.only)
return (dep.dev && includeDev) || (!dep.dev && includeProd)
const includeOptional = includeProd && this.opts.optional
return (dep.dev && includeDev) ||
(dep.optional && includeOptional) ||
(!dep.dev && !dep.optional && includeProd)
}

updateJson (tree) {
Expand Down
37 changes: 37 additions & 0 deletions test/specs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,43 @@ test('removes failed optional dependencies', t => {
})
})

test('don\'t install optional dependencies with no-optional argument', t => {
const fixture = new Tacks(Dir({
'package.json': File({
name: pkgName,
version: pkgVersion,
dependencies: { a: '^1' },
optionalDependencies: { b: '^1' }
}),
'package-lock.json': File({
lockfileVersion: 1,
dependencies: {
a: { version: '1.0.0' },
b: { version: '1.0.0', optional: true }
}
})
}))
fixture.create(prefix)

extract = (name, child, childPath, opts) => {
const files = new Tacks(Dir({
'package.json': File({
name,
version: '1.0.0'
})
}))
files.create(childPath)
}

return run({ optional: false }).then(details => {
t.equal(details.pkgCount, 1, 'only prod deps counted')
t.ok(fs.statSync(path.join(prefix, 'node_modules', 'a')), 'dep a is there')
t.throws(() => {
fs.statSync(path.join(prefix, 'node_modules', 'b'))
}, 'optional dep b is not there')
})
})

test('runs lifecycle hooks of packages with env variables', t => {
const originalConsoleLog = console.log
console.log = () => {}
Expand Down

0 comments on commit 425669a

Please sign in to comment.