Skip to content

Commit 1bab4dc

Browse files
committed
temp show
1 parent 82ce5b3 commit 1bab4dc

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

0 commit comments

Comments
 (0)