Skip to content

Commit fa9fb1e

Browse files
authored
Merge pull request #29 from enormora/config-merging
Merge common settings into per package settings when both exist
2 parents 8acaddd + 301e645 commit fa9fb1e

File tree

6 files changed

+414
-68
lines changed

6 files changed

+414
-68
lines changed

packtory.config.js

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,48 +20,37 @@ export async function buildConfig() {
2020
registrySettings: { token: npmToken },
2121
commonPackageSettings: {
2222
sourcesFolder,
23-
mainPackageJson: packageJson
23+
mainPackageJson: packageJson,
24+
includeSourceMapFiles: true,
25+
additionalFiles: [
26+
{
27+
sourceFilePath: path.join(projectFolder, 'LICENSE'),
28+
targetFilePath: 'LICENSE'
29+
},
30+
{
31+
sourceFilePath: path.join(projectFolder, 'README.md'),
32+
targetFilePath: 'readme.md'
33+
}
34+
]
2435
},
2536
packages: [
2637
{
2738
name: 'packtory',
28-
includeSourceMapFiles: true,
2939
entryPoints: [
3040
{
3141
js: 'packages/packtory/packtory.entry-point.js',
3242
declarationFile: 'packages/packtory/packtory.entry-point.d.ts'
3343
}
34-
],
35-
additionalFiles: [
36-
{
37-
sourceFilePath: path.join(projectFolder, 'LICENSE'),
38-
targetFilePath: 'LICENSE'
39-
},
40-
{
41-
sourceFilePath: path.join(projectFolder, 'README.md'),
42-
targetFilePath: 'readme.md'
43-
}
4444
]
4545
},
4646
{
4747
name: '@packtory/cli',
48-
includeSourceMapFiles: true,
4948
entryPoints: [
5049
{
5150
js: 'packages/command-line-interface/command-line-interface.entry-point.js',
5251
declarationFile: 'packages/command-line-interface/command-line-interface.entry-point.d.ts'
5352
}
5453
],
55-
additionalFiles: [
56-
{
57-
sourceFilePath: path.join(projectFolder, 'LICENSE'),
58-
targetFilePath: 'LICENSE'
59-
},
60-
{
61-
sourceFilePath: path.join(projectFolder, 'README.md'),
62-
targetFilePath: 'readme.md'
63-
}
64-
],
6554
additionalPackageJsonAttributes: {
6655
bin: {
6756
packtory: './command-line-interface.entry-point.js'

source/bundler/bundle-build-options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export type BundleBuildOptions = {
1111
readonly name: string;
1212
readonly version: string;
1313
readonly mainPackageJson: MainPackageJson;
14-
readonly includeSourceMapFiles?: boolean;
14+
readonly includeSourceMapFiles?: boolean | undefined;
1515
readonly bundleDependencies?: readonly BundleDescription[];
1616
readonly bundlePeerDependencies?: readonly BundleDescription[];
1717
readonly additionalFiles?: readonly (AdditionalFileDescription | string)[];

source/config/config.test.ts

Lines changed: 53 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import test from 'ava';
22
import { checkValidationFailure, checkValidationSuccess } from '../test-libraries/verify-schema-validation.js';
33
import { packtoryConfigSchema } from './config.js';
44

5-
test('validation succeeds when commonPackageSettings is undefined', checkValidationSuccess, {
5+
test('validation succeeds when commonPackageSettings is defined but empty', checkValidationSuccess, {
66
schema: packtoryConfigSchema,
77
data: {
88
registrySettings: { token: 'foo' },
9-
commonPackageSettings: undefined,
9+
commonPackageSettings: {},
1010
packages: [
1111
{
1212
sourcesFolder: 'source',
@@ -18,6 +18,25 @@ test('validation succeeds when commonPackageSettings is undefined', checkValidat
1818
}
1919
});
2020

21+
test('validation succeeds when commonPackageSettings is defined with all optional values', checkValidationSuccess, {
22+
schema: packtoryConfigSchema,
23+
data: {
24+
registrySettings: { token: 'foo' },
25+
commonPackageSettings: {
26+
includeSourceMapFiles: true,
27+
additionalFiles: [],
28+
additionalPackageJsonAttributes: {}
29+
},
30+
packages: [
31+
{
32+
sourcesFolder: 'source',
33+
mainPackageJson: {},
34+
name: 'foo',
35+
entryPoints: [{ js: 'foo' }]
36+
}
37+
]
38+
}
39+
});
2140
test(
2241
'validation succeeds when commonPackageSettings is not given and a package contains all required settings',
2342
checkValidationSuccess,
@@ -204,13 +223,12 @@ test('validation fails when an empty object is given', checkValidationFailure, {
204223
});
205224

206225
test(
207-
'validation fails when commonPackageSettings is undefined and mainPackageJson is missing in a package',
226+
'validation fails when commonPackageSettings is not defined and mainPackageJson is missing in a package',
208227
checkValidationFailure,
209228
{
210229
schema: packtoryConfigSchema,
211230
data: {
212231
registrySettings: { token: 'foo' },
213-
commonPackageSettings: undefined,
214232
packages: [
215233
{
216234
sourcesFolder: 'source',
@@ -221,7 +239,7 @@ test(
221239
},
222240
expectedMessages: [
223241
'At packages.0.mainPackageJson: missing key or index',
224-
'At commonPackageSettings: expected object; but got undefined'
242+
'At commonPackageSettings: missing key or index'
225243
]
226244
}
227245
);
@@ -235,26 +253,29 @@ test('validation fails when packages is an empty array', checkValidationFailure,
235253
expectedMessages: ['At packages.0: missing key or index', 'At commonPackageSettings: missing key or index']
236254
});
237255

238-
test('validation fails when commonPackageSettings is empty', checkValidationFailure, {
239-
schema: packtoryConfigSchema,
240-
data: {
241-
registrySettings: { token: 'foo' },
242-
commonPackageSettings: {},
243-
packages: [
244-
{
245-
name: 'foo',
246-
entryPoints: [{ js: 'foo' }]
247-
}
256+
test(
257+
'validation fails when commonPackageSettings is empty and packages don’t define required properties',
258+
checkValidationFailure,
259+
{
260+
schema: packtoryConfigSchema,
261+
data: {
262+
registrySettings: { token: 'foo' },
263+
commonPackageSettings: {},
264+
packages: [
265+
{
266+
name: 'foo',
267+
entryPoints: [{ js: 'foo' }]
268+
}
269+
]
270+
},
271+
expectedMessages: [
272+
'At packages.0.sourcesFolder: missing key or index',
273+
'At packages.0.mainPackageJson: missing key or index',
274+
'At commonPackageSettings.sourcesFolder: missing key or index',
275+
'At commonPackageSettings.mainPackageJson: missing key or index'
248276
]
249-
},
250-
expectedMessages: [
251-
'At commonPackageSettings: expected undefined; but got object',
252-
'At packages.0.sourcesFolder: missing key or index',
253-
'At packages.0.mainPackageJson: missing key or index',
254-
'At commonPackageSettings.sourcesFolder: missing key or index',
255-
'At commonPackageSettings.mainPackageJson: missing key or index'
256-
]
257-
});
277+
}
278+
);
258279

259280
test('validation fails when commonPackageSettings contains only mainPackageJson', checkValidationFailure, {
260281
schema: packtoryConfigSchema,
@@ -271,7 +292,6 @@ test('validation fails when commonPackageSettings contains only mainPackageJson'
271292
]
272293
},
273294
expectedMessages: [
274-
'At commonPackageSettings: expected undefined; but got object',
275295
'At packages.0.sourcesFolder: missing key or index',
276296
'At packages.0.mainPackageJson: missing key or index',
277297
'At commonPackageSettings.sourcesFolder: missing key or index'
@@ -295,8 +315,8 @@ test(
295315
]
296316
},
297317
expectedMessages: [
298-
'At packages.0.sourcesFolder: missing key or index',
299-
'At commonPackageSettings: expected object; but got undefined'
318+
'At commonPackageSettings: expected object; but got undefined',
319+
'At packages.0.sourcesFolder: missing key or index'
300320
]
301321
}
302322
);
@@ -316,7 +336,6 @@ test('validation fails when commonPackageSettings contains only sourcesFolder',
316336
]
317337
},
318338
expectedMessages: [
319-
'At commonPackageSettings: expected undefined; but got object',
320339
'At packages.0.sourcesFolder: missing key or index',
321340
'At packages.0.mainPackageJson: missing key or index',
322341
'At commonPackageSettings.mainPackageJson: missing key or index'
@@ -450,10 +469,9 @@ test('validation fails when additionalFiles is not an array', checkValidationFai
450469
]
451470
},
452471
expectedMessages: [
453-
'At commonPackageSettings: expected undefined; but got object',
472+
'At commonPackageSettings.additionalFiles: expected array; but got string',
454473
'At packages.0.sourcesFolder: missing key or index',
455-
'At packages.0.mainPackageJson: missing key or index',
456-
'At commonPackageSettings.additionalFiles: expected array; but got string'
474+
'At packages.0.mainPackageJson: missing key or index'
457475
]
458476
});
459477

@@ -474,10 +492,9 @@ test('validation fails when includeSourceMapFiles is not a boolean', checkValida
474492
]
475493
},
476494
expectedMessages: [
477-
'At commonPackageSettings: expected undefined; but got object',
495+
'At commonPackageSettings.includeSourceMapFiles: expected boolean; but got string',
478496
'At packages.0.sourcesFolder: missing key or index',
479-
'At packages.0.mainPackageJson: missing key or index',
480-
'At commonPackageSettings.includeSourceMapFiles: expected boolean; but got string'
497+
'At packages.0.mainPackageJson: missing key or index'
481498
]
482499
});
483500

@@ -498,10 +515,9 @@ test('validation fails when additionalPackageJsonAttributes is not an object', c
498515
]
499516
},
500517
expectedMessages: [
501-
'At commonPackageSettings: expected undefined; but got object',
518+
'At commonPackageSettings.additionalPackageJsonAttributes: expected object; but got string',
502519
'At packages.0.sourcesFolder: missing key or index',
503-
'At packages.0.mainPackageJson: missing key or index',
504-
'At commonPackageSettings.additionalPackageJsonAttributes: expected object; but got string'
520+
'At packages.0.mainPackageJson: missing key or index'
505521
]
506522
});
507523

@@ -522,7 +538,6 @@ test('validation fails when versioning is not an object', checkValidationFailure
522538
]
523539
},
524540
expectedMessages: [
525-
'At commonPackageSettings: expected undefined; but got object',
526541
'At packages.0.sourcesFolder: missing key or index',
527542
'At packages.0.mainPackageJson: missing key or index',
528543
'At packages.0.versioning: expected object; but got string'
@@ -546,7 +561,6 @@ test('validation fails when bundleDependencies is not an array', checkValidation
546561
]
547562
},
548563
expectedMessages: [
549-
'At commonPackageSettings: expected undefined; but got object',
550564
'At packages.0.sourcesFolder: missing key or index',
551565
'At packages.0.mainPackageJson: missing key or index',
552566
'At packages.0.bundleDependencies: expected array; but got string'
@@ -570,7 +584,6 @@ test('validation fails when bundlePeerDependencies is not an array', checkValida
570584
]
571585
},
572586
expectedMessages: [
573-
'At commonPackageSettings: expected undefined; but got object',
574587
'At packages.0.sourcesFolder: missing key or index',
575588
'At packages.0.mainPackageJson: missing key or index',
576589
'At packages.0.bundlePeerDependencies: expected array; but got string'

source/config/config.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
nonEmptyArray,
99
rest,
1010
type Schema,
11-
undefined as undefined_,
1211
partial,
1312
extend
1413
} from '@effect/schema/Schema';
@@ -29,6 +28,11 @@ const $perPackageSettingsSchema = struct({
2928
type PerPackageSettings = Schema.To<typeof $perPackageSettingsSchema>;
3029
const perPackageSettingsSchema: Schema<PerPackageSettings> = $perPackageSettingsSchema;
3130

31+
const optionalCommonPackageSettingsSchema = struct({
32+
sourcesFolder: optional(nonEmptyStringSchema, { exact: true }),
33+
mainPackageJson: optional(mainPackageJsonSchema, { exact: true })
34+
});
35+
3236
const requiredCommonPackageSettingsSchema = struct({
3337
sourcesFolder: nonEmptyStringSchema,
3438
mainPackageJson: mainPackageJsonSchema
@@ -52,7 +56,9 @@ const optionalPackageSettingsSchema = struct({
5256

5357
const configWithOptionalCommonSettingsSchema = struct({
5458
registrySettings: registrySettingsSchema,
55-
commonPackageSettings: optional(undefined_, { exact: true }),
59+
commonPackageSettings: optional(optionalCommonPackageSettingsSchema.pipe(extend(optionalPackageSettingsSchema)), {
60+
exact: true
61+
}),
5662
packages: nonEmptyArray(
5763
requiredCommonPackageSettingsSchema
5864
.pipe(extend(optionalPackageSettingsSchema))

0 commit comments

Comments
 (0)