Skip to content

Commit 751d3d5

Browse files
committed
drop namespaceToName
1 parent 16ae1bd commit 751d3d5

File tree

3 files changed

+25
-40
lines changed

3 files changed

+25
-40
lines changed

src/environment-base.ts

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import { type LookupOptions, lookupGenerators } from './generator-lookup.js';
4141
import { UNKNOWN_NAMESPACE, UNKNOWN_RESOLVED, defaultQueues } from './constants.js';
4242
import { resolveModulePath } from './util/resolve.js';
4343
import { commitSharedFsTask } from './commit.js';
44+
// eslint-disable-next-line import/order
4445
import { packageManagerInstallTask } from './package-manager.js';
4546

4647
const require = createRequire(import.meta.url);

src/environment.js

+24-33
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import EventEmitter from 'node:events';
22
import path, { isAbsolute } from 'node:path';
33
import { pathToFileURL } from 'node:url';
4-
import { toNamespace } from '@yeoman/namespace';
4+
import { requireNamespace, toNamespace } from '@yeoman/namespace';
55
import { defaults, pick, uniq } from 'lodash-es';
66
import createdLogger from 'debug';
77
import { findPackagesIn, getNpmPaths, moduleLookupSync } from './module-lookup.js';
@@ -31,32 +31,22 @@ function splitArgsFromString(argsString) {
3131
return result;
3232
}
3333

34-
/**
35-
* @classdesc `Environment` object is responsible of handling the lifecyle and bootstrap
36-
* of generators in a specific environment (your app).
37-
*
38-
* It provides a high-level API to create and run generators, as well as further
39-
* tuning where and how a generator is resolved.
40-
*
41-
* An environment is created using a list of `arguments` and a Hash of
42-
* `options`. Usually, this is the list of arguments you get back from your CLI
43-
* options parser.
44-
*
45-
* An optional adapter can be passed to provide interaction in non-CLI environment
46-
* (e.g. IDE plugins), otherwise a `QueuedAdapter` is instantiated by default
47-
*
48-
*/
49-
export default class Environment extends EventEmitter {
50-
/**
51-
* Convert a generators namespace to its name
52-
*
53-
* @param {String} namespace
54-
* @return {String}
55-
*/
56-
static namespaceToName(namespace) {
57-
return namespace.split(':')[0];
58-
}
59-
34+
/**
35+
* @classdesc `Environment` object is responsible of handling the lifecyle and bootstrap
36+
* of generators in a specific environment (your app).
37+
*
38+
* It provides a high-level API to create and run generators, as well as further
39+
* tuning where and how a generator is resolved.
40+
*
41+
* An environment is created using a list of `arguments` and a Hash of
42+
* `options`. Usually, this is the list of arguments you get back from your CLI
43+
* options parser.
44+
*
45+
* An optional adapter can be passed to provide interaction in non-CLI environment
46+
* (e.g. IDE plugins), otherwise a `QueuedAdapter` is instantiated by default
47+
*
48+
*/
49+
export default class Environment extends EventEmitter {
6050
/**
6151
* Lookup for a specific generator.
6252
*
@@ -76,9 +66,7 @@ function splitArgsFromString(argsString) {
7666
: { singleResult: !(options && options.multiple), ...options };
7767

7868
options.filePatterns = options.filePatterns || defaultLookups.map(prefix => path.join(prefix, '*/index.{js,ts}'));
79-
80-
const name = Environment.namespaceToName(namespace);
81-
options.packagePatterns = options.packagePatterns || toNamespace(name)?.generatorHint;
69+
options.packagePatterns = options.packagePatterns || toNamespace(namespace)?.generatorHint;
8270

8371
options.npmPaths = options.npmPaths || getNpmPaths({ localOnly: options.localOnly, filePaths: false }).reverse();
8472
options.packagePatterns = options.packagePatterns || 'generator-*';
@@ -88,7 +76,8 @@ function splitArgsFromString(argsString) {
8876
moduleLookupSync(options, ({ files, packagePath }) => {
8977
for (const filename of files) {
9078
const fileNS = asNamespace(filename, { lookups: defaultLookups });
91-
if (namespace === fileNS || (options.packagePath && namespace === Environment.namespaceToName(fileNS))) {
79+
const ns = toNamespace(fileNS);
80+
if (namespace === fileNS || (options.packagePath && namespace === ns?.packageNamespace)) {
9281
// Version 2.6.0 returned pattern instead of modulePath for options.packagePath
9382
const returnPath = options.packagePath ? packagePath : options.generatorPath ? path.posix.join(filename, '../../') : filename;
9483
if (options.singleResult) {
@@ -213,7 +202,7 @@ function splitArgsFromString(argsString) {
213202
* @return {Array}
214203
*/
215204
getGeneratorNames() {
216-
return uniq(Object.keys(this.getGeneratorsMeta()).map(namespace => Environment.namespaceToName(namespace)));
205+
return uniq(Object.keys(this.getGeneratorsMeta()).map(namespace => toNamespace(namespace)?.packageNamespace));
217206
}
218207

219208
/**
@@ -241,7 +230,9 @@ function splitArgsFromString(argsString) {
241230
* @return {Array} - array of paths.
242231
*/
243232
getPackagePaths(namespace) {
244-
return this.store.getPackagesPaths()[namespace] || this.store.getPackagesPaths()[Environment.namespaceToName(this.alias(namespace))];
233+
return (
234+
this.store.getPackagesPaths()[namespace] || this.store.getPackagesPaths()[requireNamespace(this.alias(namespace)).packageNamespace]
235+
);
245236
}
246237

247238
/**

test/environment.js

-7
Original file line numberDiff line numberDiff line change
@@ -949,11 +949,4 @@ describe('Environment', () => {
949949
assert(env);
950950
});
951951
});
952-
953-
describe('.namespaceToName()', () => {
954-
it('convert a namespace to a name', () => {
955-
const name = Environment.namespaceToName('mocha:generator');
956-
assert.equal(name, 'mocha');
957-
});
958-
});
959952
});

0 commit comments

Comments
 (0)