Skip to content

Commit 1081a77

Browse files
committed
temp show
1 parent 49a8860 commit 1081a77

File tree

2 files changed

+194
-5
lines changed

2 files changed

+194
-5
lines changed

packages/yarnpkg-core/sources/Configuration.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -662,13 +662,16 @@ export type ConfigurationDefinitionMap<V = ConfigurationValueMap> = {
662662
[K in keyof V]: DefinitionForType<V[K]>;
663663
};
664664

665-
function parseValue(configuration: Configuration, path: string, value: unknown, definition: SettingsDefinition, folder: PortablePath, {overwrite, reset}: {overwrite: boolean, reset: boolean}) {
665+
function parseValue(configuration: Configuration, path: string, valueBase: any, definition: SettingsDefinition, folder: PortablePath, {overwrite, reset: resetBase}: {overwrite: boolean, reset: boolean}) {
666666
const current = getCurrentValue(configuration, path);
667667
const defaultValue = getDefaultValue(configuration, definition);
668668

669-
if (current === undefined && value === undefined)
669+
if (current === undefined && resetBase === undefined)
670670
return defaultValue;
671671

672+
const reset = valueBase?.strategy === `reset` || resetBase;
673+
const value = valueBase?.strategy !== undefined ? valueBase?.value : valueBase;
674+
672675
if (definition.isArray || (definition.type === SettingsType.ANY && Array.isArray(value))) {
673676
const currentValue = reset
674677
? []
@@ -1488,7 +1491,7 @@ export class Configuration {
14881491
}
14891492
}
14901493

1491-
useWithSource(source: string, data: {[key: string]: unknown}, folder: PortablePath, opts?: {strict?: boolean, overwrite?: boolean, reset?: boolean}) {
1494+
useWithSource(source: string, data: {[key: string]: unknown}, folder: PortablePath, opts?: {strict?: boolean, overwrite?: boolean}) {
14921495
try {
14931496
this.use(source, data, folder, opts);
14941497
} catch (error) {
@@ -1497,7 +1500,7 @@ export class Configuration {
14971500
}
14981501
}
14991502

1500-
use(source: string, data: {[key: string]: unknown}, folder: PortablePath, {strict = true, overwrite = false, reset = false}: {strict?: boolean, overwrite?: boolean, reset?: boolean} = {}) {
1503+
use(source: string, data: {[key: string]: unknown}, folder: PortablePath, {strict = true, overwrite = false}: {strict?: boolean, overwrite?: boolean} = {}) {
15011504
strict = strict && this.get(`enableStrictSettings`);
15021505

15031506
for (const key of [`enableStrictSettings`, ...Object.keys(data)]) {
@@ -1528,7 +1531,7 @@ export class Configuration {
15281531
let parsed;
15291532
try {
15301533
parsed = parseValue(this, key, data[key], definition, folder, {
1531-
overwrite, reset,
1534+
overwrite, reset: false,
15321535
});
15331536
} catch (error) {
15341537
error.message += ` in ${formatUtils.pretty(this, source, formatUtils.Type.PATH)}`;

packages/yarnpkg-core/tests/Configuration.test.ts

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,4 +473,190 @@ describe(`Configuration`, () => {
473473
expect(array[1]).toBe(`2`);
474474
});
475475
});
476+
477+
it(`it should correctly extend or reset the values`, async() => {
478+
const {plugins, pluginConfiguration} = await initConfigurationPlugin(`{
479+
string: {
480+
description: "",
481+
type: "STRING",
482+
default: "",
483+
},
484+
string2: {
485+
description: "",
486+
type: "STRING",
487+
default: "",
488+
},
489+
stringArray: {
490+
description: "",
491+
type: "STRING",
492+
isArray: true,
493+
default: [],
494+
},
495+
stringArray2: {
496+
description: "",
497+
type: "STRING",
498+
isArray: true,
499+
default: [],
500+
},
501+
shape: {
502+
description: "",
503+
type: "SHAPE",
504+
properties: {
505+
number: {
506+
description: "",
507+
type: "NUMBER",
508+
default: 0,
509+
},
510+
string: {
511+
description: "",
512+
type: "STRING",
513+
default: "default",
514+
},
515+
},
516+
},
517+
shape2: {
518+
description: "",
519+
type: "SHAPE",
520+
properties: {
521+
number: {
522+
description: "",
523+
type: "NUMBER",
524+
default: 0,
525+
},
526+
string: {
527+
description: "",
528+
type: "STRING",
529+
default: "default",
530+
},
531+
},
532+
},
533+
map: {
534+
description: "",
535+
type: "MAP",
536+
valueDefinition: {
537+
description: "",
538+
type: "SHAPE",
539+
properties: {
540+
number: {
541+
description: "",
542+
type: "NUMBER",
543+
default: 0,
544+
},
545+
string: {
546+
description: "",
547+
type: "STRING",
548+
default: "default",
549+
},
550+
},
551+
},
552+
},
553+
map2: {
554+
description: "",
555+
type: "MAP",
556+
valueDefinition: {
557+
description: "",
558+
type: "SHAPE",
559+
properties: {
560+
number: {
561+
description: "",
562+
type: "NUMBER",
563+
default: 0,
564+
},
565+
string: {
566+
description: "",
567+
type: "STRING",
568+
default: "default",
569+
},
570+
},
571+
},
572+
},
573+
}`);
574+
await initializeConfiguration({
575+
plugins,
576+
string: `bar`,
577+
string2: {value: `bar2`, strategy: `reset`},
578+
stringArray: [`bar`],
579+
stringArray2: {value: [`bar2`], strategy: `reset`},
580+
shape: {
581+
number: 2,
582+
string: `bar`,
583+
},
584+
shape2: {
585+
value: {
586+
number: 22,
587+
string: `bar2`,
588+
},
589+
strategy: `reset`,
590+
},
591+
map: {
592+
bar: {number: 2, string: `barbar`},
593+
},
594+
map2: {
595+
value: {
596+
bar: {number: 22, string: `barbar`},
597+
},
598+
strategy: `reset`,
599+
},
600+
}, async dir => {
601+
const workspaceDirectory = `${dir}/workspace` as PortablePath;
602+
603+
await xfs.mkdirPromise(workspaceDirectory);
604+
await xfs.writeFilePromise(`${workspaceDirectory}/.yarnrc.yml` as PortablePath, stringifySyml({
605+
string: `foo`,
606+
string2: `foo2`,
607+
stringArray: [`foo`],
608+
stringArray2: [`foo2`],
609+
shape: {
610+
number: 1, string: `foo`,
611+
},
612+
shape2: {
613+
number: 11, string: `foo2`,
614+
},
615+
map: {
616+
foo: {number: 1, string: `foo`},
617+
},
618+
map2: {
619+
foo: {number: 11, string: `foo2`},
620+
},
621+
}));
622+
623+
const configuration = await Configuration.find(workspaceDirectory, pluginConfiguration);
624+
625+
expect(configuration.get(`string`)).toBe(`foo`);
626+
expect(configuration.get(`string2`)).toBe(`bar2`);
627+
expect(configuration.get(`stringArray`)).toEqual([`foo`, `bar`]);
628+
expect(configuration.get(`stringArray2`)).toEqual([`bar2`]);
629+
expect(configuration.get(`shape`)).toEqual(new Map<string, any>([[`number`, 1], [`string`, `foo`]])); // the number is default value
630+
expect(configuration.get(`shape2`)).toEqual(new Map<string, any>([[`number`, 22], [`string`, `bar2`]])); // the number is default value
631+
expect(configuration.get(`map`)).toEqual(
632+
new Map([
633+
[
634+
`foo`,
635+
new Map<string, any>([
636+
[`number`, 1],
637+
[`string`, `foo`],
638+
]),
639+
],
640+
[
641+
`bar`,
642+
new Map<string, any>([
643+
[`number`, 2],
644+
[`string`, `barbar`],
645+
]),
646+
],
647+
]),
648+
);
649+
expect(configuration.get(`map2`)).toEqual(
650+
new Map([
651+
[
652+
`bar`,
653+
new Map<string, any>([
654+
[`number`, 22],
655+
[`string`, `barbar`],
656+
]),
657+
],
658+
]),
659+
);
660+
});
661+
});
476662
});

0 commit comments

Comments
 (0)