Skip to content

Commit 7fac329

Browse files
authored
Merge pull request #44 from webdeveric/dev
Added `--dev-condition`
2 parents 9620582 + 6af03a9 commit 7fac329

7 files changed

Lines changed: 96 additions & 15 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"engines": {
3333
"node": ">=20.17.0"
3434
},
35-
"packageManager": "pnpm@10.31.0+sha512.e3927388bfaa8078ceb79b748ffc1e8274e84d75163e67bc22e06c0d3aed43dd153151cbf11d7f8301ff4acb98c68bdc5cadf6989532801ffafe3b3e4a63c268",
35+
"packageManager": "pnpm@10.32.0+sha512.9b2634bb3fed5601c33633f2d92593f506270a3963b8c51d2b2d6a828da615ce4e9deebef9614ccebbc13ac8d3c0f9c9ccceb583c69c8578436fa477dbb20d70",
3636
"scripts": {
3737
"prevalidate": "pnpm build:dev",
3838
"validate": "./dist/cli.mjs",

pnpm-lock.yaml

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

readme.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ yarn add validate-package-exports -D
2424
| --- | --- | --- |
2525
| `--check` / `-s` | Check syntax of JS files | `false` |
2626
| `--concurrency` / `-c` | Concurrency | `availableParallelism()` |
27+
| `--dev-condition` | Custom condition name only used during dev | none |
2728
| `--bail` / `-b` | Stop after the first error | `process.env.CI === 'true'` |
2829
| `--no-bail` | Turn off `--bail` | `false` |
2930
| `--info` / `-i` | Show `info` messages.<br>The default behavior is to only show `error`. | `process.env.RUNNER_DEBUG === '1'` |
@@ -59,6 +60,18 @@ OR
5960
}
6061
```
6162

63+
#### Dev Condition
64+
65+
If you use a [`customCondition` in your `tsconfig.json`](https://www.typescriptlang.org/tsconfig/#customConditions), like when using [Nx](https://nx.dev/docs/technologies/test-tools/vitest/guides/testing-without-building-dependencies#step-1-add-customconditions-to-tsconfigbasejson), you can use the `--dev-condition` flag so that those entry points are skipped when validating which files are packed.
66+
67+
```json
68+
{
69+
"scripts": {
70+
"prepublishOnly": "validate-package-exports --check --info --dev-condition @example/monorepo"
71+
}
72+
}
73+
```
74+
6275
### Using `npx`
6376

6477
```shell

src/lib/Validator.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ export class Validator extends EventEmitter {
157157
const files = new Set(await getPacklist(packageContext.directory));
158158

159159
const results: Result[] = await Readable.from(unique(entryPoints, (entryPoint) => entryPoint.relativePath))
160+
// Remove entry points that are matching a dev condition.
161+
// The assumption is that files for dev conditions will not be packed.
162+
.filter(
163+
(entryPoint: EntryPoint) =>
164+
!(entryPoint.condition !== undefined && this.options.devCondition.includes(entryPoint.condition)),
165+
)
160166
.map(
161167
(entryPoint): Result => {
162168
const willBePacked = files.has(entryPoint.relativePath);

src/types.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,16 @@ export const logLevelMapping = {
3636
} as const satisfies Record<LogLevelName, LogLevel>;
3737

3838
export type CliArguments = {
39-
packages: string[];
4039
bail: boolean;
4140
check: boolean;
4241
concurrency: number;
43-
json: boolean;
42+
devCondition: string[];
4443
info: boolean;
44+
json: boolean;
45+
packages: string[];
4546
};
4647

47-
export type ValidatorOptions = Pick<CliArguments, 'bail' | 'check' | 'concurrency'> & {
48+
export type ValidatorOptions = Omit<CliArguments, 'json' | 'info' | 'packages'> & {
4849
package: PackageJsonPath;
4950
controller: AbortController;
5051
};

src/utils/getCliArguments.test.ts

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ describe('getCliArguments()', () => {
1111
expect(getCliArguments([])).toEqual({
1212
bail: process.env.CI === 'true',
1313
check: false,
14+
concurrency: availableParallelism(),
15+
devCondition: [],
1416
info: process.env.RUNNER_DEBUG === '1',
1517
json: false,
16-
concurrency: availableParallelism(),
1718
packages: ['./package.json'],
1819
} satisfies CliArguments);
1920

@@ -22,9 +23,10 @@ describe('getCliArguments()', () => {
2223
).toEqual({
2324
bail: true,
2425
check: true,
26+
concurrency: 1,
27+
devCondition: [],
2528
info: true,
2629
json: true,
27-
concurrency: 1,
2830
packages: ['./some-path/package.json'],
2931
} satisfies CliArguments);
3032

@@ -41,6 +43,54 @@ describe('getCliArguments()', () => {
4143
);
4244
});
4345

46+
it('--dev-condition flag', () => {
47+
// Single value
48+
expect(getCliArguments(['--dev-condition', '@webdeveric/example'])).toEqual({
49+
bail: process.env.CI === 'true',
50+
check: false,
51+
concurrency: availableParallelism(),
52+
devCondition: ['@webdeveric/example'],
53+
info: process.env.RUNNER_DEBUG === '1',
54+
json: false,
55+
packages: ['./package.json'],
56+
} satisfies CliArguments);
57+
58+
// Multiple values
59+
expect(
60+
getCliArguments(['--dev-condition', '@webdeveric/example1', '--dev-condition', '@webdeveric/example2']),
61+
).toEqual({
62+
bail: process.env.CI === 'true',
63+
check: false,
64+
concurrency: availableParallelism(),
65+
devCondition: ['@webdeveric/example1', '@webdeveric/example2'],
66+
info: process.env.RUNNER_DEBUG === '1',
67+
json: false,
68+
packages: ['./package.json'],
69+
} satisfies CliArguments);
70+
71+
// CSV string
72+
expect(getCliArguments(['--dev-condition', '@webdeveric/example1,@webdeveric/example2'])).toEqual({
73+
bail: process.env.CI === 'true',
74+
check: false,
75+
concurrency: availableParallelism(),
76+
devCondition: ['@webdeveric/example1', '@webdeveric/example2'],
77+
info: process.env.RUNNER_DEBUG === '1',
78+
json: false,
79+
packages: ['./package.json'],
80+
} satisfies CliArguments);
81+
82+
// CSV string with whitespace padding
83+
expect(getCliArguments(['--dev-condition', ' @webdeveric/example1 , @webdeveric/example2 '])).toEqual({
84+
bail: process.env.CI === 'true',
85+
check: false,
86+
concurrency: availableParallelism(),
87+
devCondition: ['@webdeveric/example1', '@webdeveric/example2'],
88+
info: process.env.RUNNER_DEBUG === '1',
89+
json: false,
90+
packages: ['./package.json'],
91+
} satisfies CliArguments);
92+
});
93+
4494
it('Throws when given incorrect arguments', () => {
4595
expect(() => {
4696
getCliArguments(['--not-a-real-flag']);

src/utils/getCliArguments.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,17 @@ export function getCliArguments(args?: NodeJS.Process['argv']): CliArguments {
1212
strict: true,
1313
tokens: false,
1414
options: {
15+
// This value is used in `Readable` options.
1516
concurrency: {
1617
type: 'string',
1718
short: 'c',
1819
},
20+
// Specify which custom conditions are used only during development.
21+
'dev-condition': {
22+
type: 'string',
23+
multiple: true,
24+
default: [],
25+
},
1926
// Stop processing at the first error.
2027
bail: {
2128
type: 'boolean',
@@ -30,7 +37,7 @@ export function getCliArguments(args?: NodeJS.Process['argv']): CliArguments {
3037
short: 's',
3138
},
3239
/**
33-
* @deprecated This is done automatically and this flag will be removed in the next version.
40+
* @deprecated This is done automatically and this flag will be removed in the next major version.
3441
*/
3542
verify: {
3643
type: 'boolean',
@@ -74,13 +81,17 @@ export function getCliArguments(args?: NodeJS.Process['argv']): CliArguments {
7481

7582
const noBail = values['no-bail'] ?? config.options['no-bail'].default;
7683
const noInfo = values['no-info'] ?? config.options['no-info'].default;
84+
const devCondition = values['dev-condition'] ?? config.options['dev-condition'].default;
7785

7886
return {
79-
packages: positionals.length ? positionals : ['./package.json'],
80-
concurrency: parseConcurrency(values.concurrency),
8187
bail: noBail ? false : (values.bail ?? config.options.bail.default),
8288
check: values.check ?? config.options.check.default,
83-
json: values.json ?? false,
89+
concurrency: parseConcurrency(values.concurrency),
90+
devCondition: devCondition.flatMap((item) => {
91+
return item.split(',').map((singleDevCondition) => singleDevCondition.trim());
92+
}),
8493
info: noInfo ? false : (values.info ?? config.options.info.default),
94+
json: values.json ?? false,
95+
packages: positionals.length ? positionals : ['./package.json'],
8596
};
8697
}

0 commit comments

Comments
 (0)