Skip to content

Commit 96810f6

Browse files
authored
feat: replace setContextData with improved getContextData api (#1643)
1 parent fa34261 commit 96810f6

File tree

2 files changed

+21
-44
lines changed

2 files changed

+21
-44
lines changed

src/generator.ts

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,10 @@ export class BaseGenerator<O extends BaseOptions = BaseOptions, F extends BaseFe
824824
* Get a value from the context map.
825825
* Fallback to factory function if the value is not set.
826826
*/
827-
getContextData<const T = any>(context: string | { context: string; key: string }, factory?: () => T): T {
827+
getContextData<const T = any>(
828+
context: string | { context: string; key: string },
829+
opts: { override?: T } | { factory?: () => T } = {},
830+
): T {
828831
if (!('getContextMap' in this.env)) {
829832
throw new Error('getContextMap is not implemented in the environment');
830833
}
@@ -833,40 +836,24 @@ export class BaseGenerator<O extends BaseOptions = BaseOptions, F extends BaseFe
833836
const map: Map<string, any> =
834837
typeof context === 'object' ? (this.env as any).getContextMap(context.context) : this._contextMap;
835838

839+
const value = map.get(key);
840+
if ('override' in opts) {
841+
map.set(key, opts.override);
842+
return value;
843+
}
844+
836845
if (map.has(key)) {
837-
return map.get(key);
846+
return value;
838847
}
839848

840-
if (factory) {
841-
const value = factory();
849+
if ('factory' in opts && opts.factory) {
850+
const value = opts.factory();
842851
map.set(key, value);
843852
return value;
844853
}
845854

846855
throw new Error(`Context data ${key} not found and no factory provided`);
847856
}
848-
849-
/**
850-
* @experimental
851-
* Set a value to the context map.
852-
* Returns the old value.
853-
*
854-
* @example
855-
* const oldValue = this.setContextData('runOnce', true); // returns undefined
856-
*/
857-
setContextData<const T = any>(context: string | { context: string; key: string }, value: T): T | undefined {
858-
if (!('getContextMap' in this.env)) {
859-
throw new Error('getContextMap is not implemented in the environment');
860-
}
861-
862-
const key = typeof context === 'string' ? context : context.key;
863-
const map: Map<string, any> =
864-
typeof context === 'object' ? (this.env as any).getContextMap(context.context) : this._contextMap;
865-
866-
const oldValue = map.get(key);
867-
map.set(key, value);
868-
return oldValue;
869-
}
870857
}
871858

872859
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging

test/generators.test.ts

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ describe('Generators module', () => {
136136
it('non existing key should use factory if provided', () => {
137137
const data = 'bar';
138138
const factory: () => string = vitest.fn().mockReturnValue(data);
139-
expect(generator.getContextData('foo', factory)).toBe(data);
139+
expect(generator.getContextData('foo', { factory })).toBe(data);
140140
expect(factory).toHaveBeenCalled();
141141
});
142142

@@ -151,34 +151,24 @@ describe('Generators module', () => {
151151
const key = 'foo';
152152
const data = 'bar';
153153
const factory: () => string = vitest.fn().mockReturnValue(data);
154-
expect(generator.getContextData({ context, key }, factory)).toBe(data);
154+
expect(generator.getContextData({ context, key }, { factory })).toBe(data);
155155
expect(factory).toHaveBeenCalled();
156156
expect(env.getContextMap(context).get(key)).toBe(data);
157157
});
158-
});
159-
160-
describe('#setContextData', () => {
161-
beforeEach(() => {
162-
generator = new Base({
163-
env: env,
164-
resolved: 'test',
165-
localConfigOnly: true,
166-
});
167-
});
168158

169-
it('sets new data and retrieves old data', () => {
159+
it('using overrides option sets new data and retrieves old data', () => {
170160
const key = 'foo';
171161
const data = 'bar';
172-
expect(generator.setContextData(key, data)).toBe(undefined);
173-
expect(generator.setContextData(key, 'new')).toBe(data);
162+
expect(generator.getContextData(key, { override: data })).toBe(undefined);
163+
expect(generator.getContextData(key, { override: 'new' })).toBe(data);
174164
});
175165

176-
it('supports custon context', () => {
166+
it('supports overrides option with custon context', () => {
177167
const context = 'ctx';
178168
const key = 'foo';
179169
const data = 'bar';
180-
expect(generator.setContextData({ context, key }, data)).toBe(undefined);
181-
expect(generator.setContextData({ context, key }, 'new')).toBe(data);
170+
expect(generator.getContextData({ context, key }, { override: data })).toBe(undefined);
171+
expect(generator.getContextData({ context, key }, { override: 'new' })).toBe(data);
182172
expect(env.getContextMap(context).get(key)).toBe('new');
183173
});
184174
});

0 commit comments

Comments
 (0)