Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/environment-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ const ENVIRONMENT_VERSION = require('../package.json').version;

const debug = createdLogger('yeoman:environment');

export type EnvironmentLookupOptions = LookupOptions & {
/** Add a scope to the namespace if there is no scope */
registerToScope?: string;
/** Customize the namespace to be registered */
customizeNamespace?: (ns?: string) => string;
};

export type EnvironmentOptions = BaseEnvironmentOptions &
Omit<TerminalAdapterOptions, 'promptModule'> & {
adapter?: InputOutputAdapter;
Expand Down Expand Up @@ -600,8 +607,13 @@ export default class EnvironmentBase extends EventEmitter implements BaseEnviron
* So this index file `node_modules/generator-dummy/lib/generators/yo/index.js` would be
* registered as `dummy:yo` generator.
*/
async lookup(options?: LookupOptions & { registerToScope?: string }): Promise<LookupGeneratorMeta[]> {
const { registerToScope, lookups = this.lookups, ...remainingOptions } = options ?? { localOnly: false };
async lookup(options?: EnvironmentLookupOptions): Promise<LookupGeneratorMeta[]> {
const {
registerToScope,
customizeNamespace = (ns: string) => ns,
lookups = this.lookups,
...remainingOptions
} = options ?? { localOnly: false };
options = {
...remainingOptions,
lookups,
Expand All @@ -615,11 +627,11 @@ export default class EnvironmentBase extends EventEmitter implements BaseEnviron
repositoryPath = join(repositoryPath, '..');
}

let namespace = asNamespace(relative(repositoryPath, filePath), { lookups });
let namespace = customizeNamespace(asNamespace(relative(repositoryPath, filePath), { lookups }));
try {
const resolved = realpathSync(filePath);
if (!namespace) {
namespace = asNamespace(resolved, { lookups });
namespace = customizeNamespace(asNamespace(resolved, { lookups }));
}

if (registerToScope && !namespace.startsWith('@')) {
Expand Down
15 changes: 15 additions & 0 deletions test/__snapshots__/resolver.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,21 @@ exports[`Environment Resolver #lookup() with options with 3 packagePaths 1`] = `
}
`;

exports[`Environment Resolver #lookup() with options with customizeNamespace 1`] = `
{
"custom:app": {
"importGenerator": [Function],
"importModule": [Function],
"instantiate": [Function],
"instantiateHelp": [Function],
"namespace": "custom:app",
"packageNamespace": "custom",
"packagePath": "fixtures/lookup-custom/node_modules/generator-module",
"resolved": "fixtures/generator-module/generators/app/index.js",
},
}
`;

exports[`Environment Resolver #lookup() with options with npmPaths 1`] = `
{
"@scoped/scoped:app": {
Expand Down
13 changes: 13 additions & 0 deletions test/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,19 @@ describe('Environment Resolver', async function () {
assert.ok(this.env.getRegisteredPackages().length === 1);
});

it('with customizeNamespace', async function () {
await this.env.lookup({
localOnly: true,
packagePaths: ['node_modules/generator-module'],
customizeNamespace: ns => ns.replace('module', 'custom'),
});

expect(toRelativeMeta(this.env.getGeneratorsMeta())).toMatchSnapshot();

assert.ok(await this.env.get('custom:app'));
assert.ok(this.env.getRegisteredPackages().length === 1);
});

it('with scope and packagePaths', async function () {
await this.env.lookup({
localOnly: true,
Expand Down