Skip to content

Commit 16ae1bd

Browse files
committed
refactor static methods
1 parent a5b3b5f commit 16ae1bd

12 files changed

+112
-217
lines changed

readme.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ $ npm install yeoman-environment
1717
Full documentation available [here](http://yeoman.io/authoring/integrating-yeoman.html).
1818

1919
```js
20-
import yeoman from 'yeoman-environment';
21-
const env = yeoman.createEnv();
20+
import { createEnv } from 'yeoman-environment';
2221

2322
// The #lookup() method will search the user computer for installed generators
2423
// The search if done from the current working directory

src/cli/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import process from 'node:process';
33
import YeomanCommand, { addEnvironmentOptions } from '../util/command.js';
44
import packageJson from '../package.json';
5-
import Env from '../index.js';
5+
import { createEnv } from '../index.js';
66
import { printGroupedGenerator, environmentAction } from './utils.js';
77

88
const program = new YeomanCommand();
@@ -25,7 +25,7 @@ program
2525
.command('find')
2626
.description('Find installed generators')
2727
.action(async () => {
28-
const env = Env.createEnv();
28+
const env = createEnv();
2929
const generators = await env.lookup();
3030
printGroupedGenerator(generators, env);
3131
});
@@ -34,7 +34,7 @@ program
3434
.command('list')
3535
.description('List generators available to be used')
3636
.action(async () => {
37-
const env = Env.createEnv();
37+
const env = createEnv();
3838
await env.lookup();
3939
printGroupedGenerator(Object.values(env.getGeneratorsMeta()), env);
4040
});

src/cli/utils.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { requireNamespace } from '@yeoman/namespace';
22
import { groupBy } from 'lodash-es';
33
import createLogger from 'debug';
4-
import Environment from '../index.js';
4+
import Environment, { createEnv } from '../index.js';
55

66
const debug = createLogger('yeoman:yoe');
77

@@ -33,7 +33,7 @@ export const environmentAction = async function (this: any, generatorNamespace:
3333
return;
3434
}
3535

36-
this.env = Environment.createEnv([], { ...options, command: this });
36+
this.env = createEnv({ ...options, command: this });
3737
await this.env.lookupLocalPackages();
3838

3939
return this.env.execute(generatorNamespace, command.args.splice(1));

src/commands.ts

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import YeomanCommand, { addEnvironmentOptions } from './util/command.js';
2+
import { createEnv } from './index.js';
3+
4+
/**
5+
* Prepare a commander instance for cli support.
6+
*
7+
* @param {Command} command - Command to be prepared
8+
* @param generatorPath - Generator to create Command
9+
* @return {Command} return command
10+
*/
11+
export const prepareGeneratorCommand = async (command: YeomanCommand, generatorPath: string, namespace?: string) => {
12+
const env = createEnv();
13+
const meta = env.register(generatorPath, { namespace });
14+
command.env = env;
15+
command.registerGenerator(await meta.instantiateHelp());
16+
command.action(async function (this: YeomanCommand) {
17+
// eslint-disable-next-line @typescript-eslint/no-this-alias
18+
let rootCommand: YeomanCommand = this;
19+
while (rootCommand.parent) {
20+
rootCommand = rootCommand.parent as YeomanCommand;
21+
}
22+
23+
const generator = await meta.instantiate(this.args, this.opts());
24+
await env.runGenerator(generator);
25+
});
26+
return command;
27+
};
28+
29+
/**
30+
* Prepare a commander instance for cli support.
31+
*
32+
* @param generatorPaht - Generator to create Command
33+
* @return Return a Command instance
34+
*/
35+
export const prepareCommand = async (generatorPath: string, command = new YeomanCommand()) => {
36+
command = addEnvironmentOptions(command);
37+
return prepareGeneratorCommand(command, generatorPath);
38+
};

src/environment-base.ts

+23-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import EventEmitter from 'node:events';
21
import { createRequire } from 'node:module';
32
import { basename, isAbsolute, join, relative, resolve } from 'node:path';
43
import process from 'node:process';
@@ -35,13 +34,13 @@ import { type ConflicterOptions } from '@yeoman/conflicter';
3534
import { defaults, pick } from 'lodash-es';
3635
import { ComposedStore } from './composed-store.js';
3736
import Store from './store.js';
37+
import Environment from './environment.js';
3838
import type YeomanCommand from './util/command.js';
3939
import { asNamespace, defaultLookups } from './util/namespace.js';
4040
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
4544
import { packageManagerInstallTask } from './package-manager.js';
4645

4746
const require = createRequire(import.meta.url);
@@ -51,7 +50,7 @@ const ENVIRONMENT_VERSION = require('../package.json').version;
5150

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

54-
type EnvironmentOptions = BaseEnvironmentOptions &
53+
export type EnvironmentOptions = BaseEnvironmentOptions &
5554
Omit<TerminalAdapterOptions, 'promptModule'> & {
5655
adapter?: InputOutputAdapter;
5756
logCwd?: string;
@@ -116,7 +115,7 @@ const getInstantiateOptions = (args?: any, options?: any): InstantiateOptions =>
116115
return { generatorOptions: options };
117116
};
118117

119-
export default class EnvironmentBase extends EventEmitter implements BaseEnvironment {
118+
export default class EnvironmentBase extends Environment implements BaseEnvironment {
120119
cwd: string;
121120
adapter: QueuedAdapter;
122121
sharedFs: MemFs<MemFsEditorFile>;
@@ -137,7 +136,12 @@ export default class EnvironmentBase extends EventEmitter implements BaseEnviron
137136
protected _rootGenerator?: BaseGenerator;
138137
protected compatibilityMode?: false | 'v4';
139138

140-
constructor(options: EnvironmentOptions = {}) {
139+
constructor(options?: EnvironmentOptions);
140+
constructor(options: EnvironmentOptions = {}, adapterCompat?: InputOutputAdapter) {
141+
if (adapterCompat) {
142+
options.adapter = adapterCompat;
143+
}
144+
141145
super();
142146

143147
this.setMaxListeners(100);
@@ -198,6 +202,11 @@ export default class EnvironmentBase extends EventEmitter implements BaseEnviron
198202
this.experimental = experimental || process.argv.includes('--experimental');
199203

200204
this.alias(/^([^:]+)$/, '$1:app');
205+
206+
this.loadSharedOptions(this.options);
207+
if (this.sharedOptions.skipLocalCache === undefined) {
208+
this.sharedOptions.skipLocalCache = true;
209+
}
201210
}
202211

203212
async applyTransforms(transformStreams: Transform[], options: ApplyTransformsOptions = {}): Promise<void> {
@@ -546,9 +555,9 @@ export default class EnvironmentBase extends EventEmitter implements BaseEnviron
546555
* @param packagePath - PackagePath to the generator npm package (optional)
547556
* @return environment - This environment
548557
*/
549-
register(filePath: string, meta?: Partial<BaseGeneratorMeta> | undefined): void;
550-
register(generator: unknown, meta: BaseGeneratorMeta): void;
551-
register(pathOrStub: unknown, meta?: Partial<BaseGeneratorMeta> | BaseGeneratorMeta, ...args: any[]): this {
558+
register(filePath: string, meta?: Partial<BaseGeneratorMeta> | undefined): GeneratorMeta;
559+
register(generator: unknown, meta: BaseGeneratorMeta): GeneratorMeta;
560+
register(pathOrStub: unknown, meta?: Partial<BaseGeneratorMeta> | BaseGeneratorMeta, ...args: any[]): GeneratorMeta {
552561
if (typeof pathOrStub === 'string') {
553562
if (typeof meta === 'object') {
554563
return this.registerGeneratorPath(pathOrStub, meta.namespace, meta.packagePath);
@@ -832,7 +841,7 @@ export default class EnvironmentBase extends EventEmitter implements BaseEnviron
832841
* @param packagePath - PackagePath to the generator npm package (optional)
833842
* @return environment - This environment
834843
*/
835-
protected registerGeneratorPath(generatorPath: string, namespace?: string, packagePath?: string): this {
844+
protected registerGeneratorPath(generatorPath: string, namespace?: string, packagePath?: string): GeneratorMeta {
836845
if (typeof generatorPath !== 'string') {
837846
throw new TypeError('You must provide a generator name to register.');
838847
}
@@ -850,13 +859,13 @@ export default class EnvironmentBase extends EventEmitter implements BaseEnviron
850859
// Generator is already registered and matches the current namespace.
851860
const generatorMeta = this.store.getMeta(namespace);
852861
if (generatorMeta && generatorMeta.resolved === generatorPath) {
853-
return this;
862+
return generatorMeta;
854863
}
855864

856865
const meta = this.store.add({ namespace, resolved: generatorPath, packagePath });
857866

858867
debug('Registered %s (%s) on package %s (%s)', namespace, generatorPath, meta.packageNamespace, packagePath);
859-
return this;
868+
return meta;
860869
}
861870

862871
/**
@@ -869,7 +878,7 @@ export default class EnvironmentBase extends EventEmitter implements BaseEnviron
869878
* @param resolved - The file path to the generator
870879
* @param packagePath - The generator's package path
871880
*/
872-
protected registerStub(Generator: any, namespace: string, resolved = UNKNOWN_RESOLVED, packagePath?: string): this {
881+
protected registerStub(Generator: any, namespace: string, resolved = UNKNOWN_RESOLVED, packagePath?: string): GeneratorMeta {
873882
if (typeof Generator !== 'function' && typeof Generator.createGenerator !== 'function') {
874883
throw new TypeError('You must provide a stub function to register.');
875884
}
@@ -878,9 +887,9 @@ export default class EnvironmentBase extends EventEmitter implements BaseEnviron
878887
throw new TypeError('You must provide a namespace to register.');
879888
}
880889

881-
this.store.add({ namespace, resolved, packagePath }, Generator);
890+
const meta = this.store.add({ namespace, resolved, packagePath }, Generator);
882891

883892
debug('Registered %s (%s) on package (%s)', namespace, resolved, packagePath);
884-
return this;
893+
return meta;
885894
}
886895
}

src/environment-full.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import { join } from 'node:path';
33
import { flyImport } from 'fly-import';
44
import semver from 'semver';
55
import { type YeomanNamespace, requireNamespace } from '@yeoman/namespace';
6-
import Environment from './environment.js';
76
import { type LookupOptions } from './generator-lookup.js';
87
import YeomanCommand from './util/command.js';
8+
import EnvironmentBase from './environment-base.js';
99

10-
class FullEnvironment extends Environment {
10+
class FullEnvironment extends EnvironmentBase {
1111
/**
1212
* Generate a command for the generator and execute.
1313
*

0 commit comments

Comments
 (0)