Skip to content

Commit

Permalink
feat(rules): no-missing-pkg-files checks "unpkg" and "module" fields …
Browse files Browse the repository at this point in the history
…by default
  • Loading branch information
boneskull committed Aug 29, 2023
1 parent 3c4fe17 commit 6df6087
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
8 changes: 7 additions & 1 deletion __snapshots__/cli.spec.ts.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,14 @@ exports['midnight-smoker smoker CLI option --json when the script succeeds shoul
"bin": true,
"browser": true,
"types": true,
"unpkg": true,
"module": true,
"fields": [
"bin",
"browser",
"types"
"types",
"unpkg",
"module"
]
}
},
Expand Down Expand Up @@ -282,6 +286,8 @@ exports['midnight-smoker smoker CLI option --json when the script fails should p
"bin": true,
"browser": true,
"types": true,
"unpkg": true,
"module": true,
"fields": []
}
},
Expand Down
28 changes: 20 additions & 8 deletions src/rules/builtin/no-missing-pkg-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,28 @@ import {zNonEmptyStringArray, zTrue} from '../../schema-util';

const debug = createDebug('midnight-smoker:rule:no-missing-pkg-files');

/**
* Each of these is a flag in the options object, which can be set to `false` to
* avoid checking the value (relative filepath) exists.
*/
const EXPLICIT_FIELD_FLAGS = [
'bin',
'browser',
'types',
'unpkg',
'module',
] as const;

const noMissingPkgFiles = createRule({
async check({pkgJson: pkg, pkgPath, fail}, opts) {
let fieldsToCheck = opts.fields ?? [];
if (opts.bin !== false) {
fieldsToCheck.push('bin');
}
if (opts.browser !== false) {
fieldsToCheck.push('browser');
}
if (opts.types !== false) {
fieldsToCheck.push('types');

for (const field of EXPLICIT_FIELD_FLAGS) {
if (opts[field] !== false) {
fieldsToCheck.push(field);
}
}

fieldsToCheck = [...new Set(fieldsToCheck)];

/**
Expand Down Expand Up @@ -75,6 +85,8 @@ const noMissingPkgFiles = createRule({
bin: zTrue.describe('Check the "bin" field (if it exists)'),
browser: zTrue.describe('Check the "browser" field (if it exists)'),
types: zTrue.describe('Check the "types" field (if it exists)'),
unpkg: zTrue.describe('Check the "unpkg" field (if it exists)'),
module: zTrue.describe('Check the "module" field (if it exists)'),
fields: zNonEmptyStringArray.describe(
'Check files referenced by these additional top-level fields',
),
Expand Down

0 comments on commit 6df6087

Please sign in to comment.