Skip to content

Commit

Permalink
fix: run dep check in prod mode (#964)
Browse files Browse the repository at this point in the history
Run dep check command twice - once to check all deps of a project are
present and once again to only check production code for production
deps in order to catch the instance when we add a production dep to
the dev deps list.
  • Loading branch information
achingbrain committed Apr 19, 2022
1 parent 95429d6 commit 43f20bf
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 23 deletions.
9 changes: 1 addition & 8 deletions src/cmds/dependency-check.js
Expand Up @@ -30,14 +30,7 @@ export default {
.positional('input', {
describe: 'Files to check',
type: 'string',
array: true,
default: userConfig.dependencyCheck.input
})
.option('p', {
alias: 'production-only',
describe: 'Check production dependencies and paths only',
type: 'boolean',
default: userConfig.dependencyCheck.productionOnly
array: true
})
.option('i', {
alias: 'ignore',
Expand Down
5 changes: 4 additions & 1 deletion src/config/user.js
Expand Up @@ -111,7 +111,10 @@ const defaults = {
productionInput: [
'package.json',
'src/**/*.js',
'src/**/*.cjs'
'src/**/*.cjs',
'dist/src/**/*.js',
'utils/**/*.js',
'utils/**/*.cjs'
],
ignore: [
'@types/*'
Expand Down
51 changes: 37 additions & 14 deletions src/dependency-check.js
Expand Up @@ -13,13 +13,6 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url))
* @typedef {import("./types").DependencyCheckOptions} DependencyCheckOptions
*/

/**
* @param {any} arr1
* @param {any} arr2
*/
const isDefaultInput = (arr1, arr2) =>
JSON.stringify(arr1) === JSON.stringify(arr2)

const tasks = new Listr(
[
{
Expand All @@ -30,17 +23,47 @@ const tasks = new Listr(
*/
task: async (ctx, task) => {
const forwardOptions = ctx['--'] ? ctx['--'] : []
const input =
ctx.productionOnly &&
isDefaultInput(ctx.fileConfig.dependencyCheck.input, ctx.input)
? ctx.fileConfig.dependencyCheck.productionInput
: ctx.input
const noDev = ctx.productionOnly ? ['--no-dev'] : []
const input = ctx.input.length > 0 ? ctx.input : ctx.fileConfig.dependencyCheck.input
const ignore = ctx.ignore
.concat(ctx.fileConfig.dependencyCheck.ignore)
.reduce((acc, i) => acc.concat('-i', i), /** @type {string[]} */ ([]))

const args = [...input, '--missing', ...ignore]

if (pkg.type === 'module') {
// use detective-es6 for js, regular detective for cjs
args.push(
'--extensions', 'cjs:detective-cjs',
'--extensions', 'js:detective-es6'
)
}

await execa(
'dependency-check',
[...args, ...forwardOptions],
merge(
{
localDir: path.join(__dirname, '..'),
preferLocal: true
}
)
)
}
},
{
title: 'dependency-check (production only)',
/**
* @param {GlobalOptions & DependencyCheckOptions} ctx
* @param {Task} task
*/
task: async (ctx, task) => {
const forwardOptions = ctx['--'] ? ctx['--'] : []
const input = ctx.input.length > 0 ? ctx.input : ctx.fileConfig.dependencyCheck.productionInput
const ignore = ctx.ignore
.concat(ctx.fileConfig.dependencyCheck.ignore)
.reduce((acc, i) => acc.concat('-i', i), /** @type {string[]} */ ([]))

const args = [...input, '--missing', ...noDev, ...ignore]
const args = [...input, '--missing', '--no-dev', ...ignore]

if (pkg.type === 'module') {
// use detective-es6 for js, regular detective for cjs
Expand Down

0 comments on commit 43f20bf

Please sign in to comment.