Skip to content

Commit 74ff742

Browse files
authored
feat: add getContextData (#1630)
1 parent 3b7c0af commit 74ff742

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"doc:fix": "sed -i -e 's:^[[:space:]]*<!--[[:space:]]*$::g' -e 's:^[[:space:]]*-->[[:space:]]*$::g' $npm_package_config_doc_path$DOC_FOLDER/global.html || true",
4444
"doc:generate": "jsdoc -c jsdoc.json -d $npm_package_config_doc_path$DOC_FOLDER",
4545
"doc:prettier": "prettier $npm_package_config_doc_path$DOC_FOLDER --write --ignore-path .prettierignore-doc",
46+
"fix": "eslint . --fix && prettier . --write",
4647
"prepare": "npm run build",
4748
"pretest": "eslint . && prettier . --check && npm run build",
4849
"test": "vitest run --coverage"

src/generator.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export class BaseGenerator<O extends BaseOptions = BaseOptions, F extends BaseFe
6565
/** @deprecated */
6666
arguments!: string[];
6767
_destinationRoot!: string;
68+
_contextMap?: Map<string, any>;
6869
_sourceRoot!: string;
6970

7071
generatorConfig?: Storage;
@@ -744,6 +745,9 @@ export class BaseGenerator<O extends BaseOptions = BaseOptions, F extends BaseFe
744745
destinationRoot(rootPath?: string) {
745746
if (typeof rootPath === 'string') {
746747
this._destinationRoot = pathResolve(rootPath);
748+
if ('getContextMap' in this.env) {
749+
this._contextMap = (this.env as any).getContextMap(this._destinationRoot);
750+
}
747751

748752
if (!fs.existsSync(this._destinationRoot)) {
749753
fs.mkdirSync(this._destinationRoot, { recursive: true });
@@ -814,6 +818,24 @@ export class BaseGenerator<O extends BaseOptions = BaseOptions, F extends BaseFe
814818
const appName: string = this.packageJson.get('name') ?? path.basename(this.destinationRoot());
815819
return appName.replaceAll(/[^\s\w]+?/g, ' ');
816820
}
821+
822+
getContextData<const T = any>(key: string, factory?: () => T): T {
823+
if (!this._contextMap) {
824+
throw new Error('getContextMap is not implemented in the environment');
825+
}
826+
827+
if (this._contextMap.has(key)) {
828+
return this._contextMap.get(key);
829+
}
830+
831+
if (factory) {
832+
const value = factory();
833+
this._contextMap.set(key, value);
834+
return value;
835+
}
836+
837+
throw new Error(`Context data ${key} not found and no factory provided`);
838+
}
817839
}
818840

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

test/generators.test.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import EventEmitter from 'node:events';
22
import path from 'node:path';
33
import os from 'node:os';
4-
import { beforeEach, describe, it } from 'vitest';
4+
import { beforeEach, describe, expect, it, vitest } from 'vitest';
55
import { TestAdapter } from '@yeoman/adapter/testing';
66
import Environment from 'yeoman-environment';
77
import assert from 'yeoman-assert';
@@ -120,6 +120,33 @@ describe('Generators module', () => {
120120
});
121121
});
122122

123+
describe('#getContextData', () => {
124+
beforeEach(() => {
125+
generator = new Base({
126+
env: env,
127+
resolved: 'test',
128+
localConfigOnly: true,
129+
});
130+
});
131+
132+
it('non existing key should throw', () => {
133+
expect(() => generator.getContextData('foo')).toThrow('Context data foo not found and no factory provided');
134+
});
135+
136+
it('non existing key should use factory if provided', () => {
137+
const data = 'bar';
138+
const factory: () => string = vitest.fn().mockReturnValue(data);
139+
expect(generator.getContextData('foo', factory)).toBe(data);
140+
expect(factory).toHaveBeenCalled();
141+
});
142+
143+
it('retrieves the data', () => {
144+
const data = 'bar';
145+
generator._contextMap.set('foo', data);
146+
expect(generator.getContextData('foo')).toBe(data);
147+
});
148+
});
149+
123150
it('running standalone', () =>
124151
new Promise(done => {
125152
const Generator = class extends Base {};

0 commit comments

Comments
 (0)