yeoman-environment 8.3.0 → 8.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/store.d.ts CHANGED
@@ -37,6 +37,10 @@ export type StoreLookupGeneratorMeta = FoundGenerator & ((StoreGeneratorMeta & {
37
37
  export type StoreGeneratorMeta = Omit<GeneratorMeta, 'importGenerator' | 'instantiate' | 'instantiateHelp'> & {
38
38
  /** Require the module `require(meta.resolved)`, throws if the module cannot be required synchronously. */
39
39
  requireModule?: () => unknown;
40
+ /** Require the sibling module of the name, like `command.ts` next to `index.ts`, `undefined` if missing. */
41
+ requireSibling?: (name: string) => unknown;
42
+ /** Import the sibling module of the name, like `requireSibling` for a module that can not be required. */
43
+ importSibling?: (name: string) => Promise<unknown>;
40
44
  importGenerator: <G extends BaseGenerator = BaseGenerator>(options?: StoreEnvironmentOptions) => Promise<GetGeneratorConstructor<G> & BaseGeneratorConstructorMeta> | (GetGeneratorConstructor<G> & BaseGeneratorConstructorMeta);
41
45
  instantiate: <G extends BaseGenerator = BaseGenerator>(arguments_?: string[], options?: any, environmentOptions?: StoreEnvironmentOptions) => Promise<G>;
42
46
  instantiateHelp: <G extends BaseGenerator = BaseGenerator>(options?: StoreEnvironmentOptions) => Promise<G>;
package/dist/store.js CHANGED
@@ -7,9 +7,9 @@ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExte
7
7
  return path;
8
8
  };
9
9
  import { pathToFileURL } from 'node:url';
10
- import { basename, extname, join, relative } from 'node:path';
10
+ import { basename, dirname, extname, join, relative } from 'node:path';
11
11
  import { createRequire } from 'node:module';
12
- import { readFileSync, realpathSync } from 'node:fs';
12
+ import { existsSync, readFileSync, realpathSync } from 'node:fs';
13
13
  import { namespaceFromPackageName, toNamespace } from '@yeoman/namespace';
14
14
  import createDebug from 'debug';
15
15
  import { lookupGeneratorsSync } from "./generator-lookup.js";
@@ -20,6 +20,38 @@ const require = createRequire(import.meta.url);
20
20
  const operationsKey = Symbol('operations');
21
21
  const setOperations = (meta, operations) => Object.defineProperty(meta, operationsKey, { value: operations });
22
22
  const getOperations = (meta) => meta[operationsKey];
23
+ /** The functions loading the modules next to a generator module, with its extension. */
24
+ const createSiblingFunctions = (resolved) => {
25
+ if (typeof resolved !== 'string' || !extname(resolved)) {
26
+ return {};
27
+ }
28
+ const folder = dirname(resolved);
29
+ const extension = extname(resolved);
30
+ const siblingPath = (name) => {
31
+ const filePath = join(folder, `${name}${extension}`);
32
+ return existsSync(filePath) ? filePath : undefined;
33
+ };
34
+ const requireSibling = (name) => {
35
+ const filePath = siblingPath(name);
36
+ return filePath ? require(filePath) : undefined;
37
+ };
38
+ const importSibling = async (name) => {
39
+ const filePath = siblingPath(name);
40
+ if (!filePath) {
41
+ return undefined;
42
+ }
43
+ try {
44
+ return require(filePath);
45
+ }
46
+ catch (error) {
47
+ if (error.code === 'ERR_REQUIRE_ESM' || error.code === 'ERR_REQUIRE_ASYNC_MODULE') {
48
+ return import(__rewriteRelativeImportExtension(pathToFileURL(filePath).href));
49
+ }
50
+ throw error;
51
+ }
52
+ };
53
+ return { requireSibling, importSibling };
54
+ };
23
55
  /** The namespace of a generator package, like `@scope/foo` for `@scope/generator-foo`, undefined for another name. */
24
56
  const namespaceOfPackage = (packageName) => {
25
57
  try {
@@ -222,6 +254,7 @@ export default class Store {
222
254
  generatorMeta = {
223
255
  ...meta,
224
256
  ...this.createMetaFunctions(operations, () => this.environment),
257
+ ...createSiblingFunctions(meta.resolved),
225
258
  requireModule,
226
259
  importModule,
227
260
  getPackageJson,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yeoman-environment",
3
- "version": "8.3.0",
3
+ "version": "8.4.0",
4
4
  "description": "Handles the lifecyle and bootstrapping of generators in a specific environment",
5
5
  "keywords": [
6
6
  "development",