|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. |
| 2 | +// See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +import * as path from 'node:path'; |
| 5 | + |
| 6 | +import type { HeftConfiguration, IHeftTaskSession } from '@rushstack/heft'; |
| 7 | +import { FileSystem } from '@rushstack/node-core-library'; |
| 8 | + |
| 9 | +import type { INapiRsPluginOptions } from './NapiRsPlugin'; |
| 10 | +import { |
| 11 | + type INapiRsConfiguration, |
| 12 | + type INapiRsPluginAccessorHooks, |
| 13 | + STAGE_LOAD_LOCAL_CONFIG, |
| 14 | + PLUGIN_NAME, |
| 15 | + INapiRsConfigurationFnOptions, |
| 16 | + NapiRsCliImport |
| 17 | +} from './shared'; |
| 18 | + |
| 19 | +type INapiRsConfigJsExport = |
| 20 | + | INapiRsConfiguration |
| 21 | + | Promise<INapiRsConfiguration> |
| 22 | + | ((options: INapiRsConfigurationFnOptions) => INapiRsConfiguration) |
| 23 | + | ((options: INapiRsConfigurationFnOptions) => Promise<INapiRsConfiguration>); |
| 24 | +type INapiRsConfigJs = INapiRsConfigJsExport | { default: INapiRsConfigJsExport }; |
| 25 | + |
| 26 | +/** |
| 27 | + * @internal |
| 28 | + */ |
| 29 | +export interface ILoadNapiRsConfigurationOptions { |
| 30 | + taskSession: IHeftTaskSession; |
| 31 | + heftConfiguration: HeftConfiguration; |
| 32 | + loadNapiRsAsyncFn: () => Promise<NapiRsCliImport>; |
| 33 | + hooks: Pick<INapiRsPluginAccessorHooks, 'onLoadConfiguration' | 'onConfigure' | 'onAfterConfigure'>; |
| 34 | + |
| 35 | + _tryLoadConfigFileAsync?: typeof tryLoadNapiRsConfigurationFileAsync; |
| 36 | +} |
| 37 | + |
| 38 | +const DEFAULT_NAPI_RS_CONFIG_PATH: './napi-rs.config.mjs' = './napi-rs.config.mjs'; |
| 39 | + |
| 40 | +/** |
| 41 | + * @internal |
| 42 | + */ |
| 43 | +export async function tryLoadNapiRsConfigurationAsync( |
| 44 | + options: ILoadNapiRsConfigurationOptions, |
| 45 | + pluginOptions: INapiRsPluginOptions |
| 46 | +): Promise<INapiRsConfiguration | undefined> { |
| 47 | + const { taskSession, hooks, _tryLoadConfigFileAsync = tryLoadNapiRsConfigurationFileAsync } = options; |
| 48 | + const { logger } = taskSession; |
| 49 | + const { terminal } = logger; |
| 50 | + |
| 51 | + // Apply default behavior. Due to the state of `this._napiRsConfiguration`, this code |
| 52 | + // will execute exactly once. |
| 53 | + hooks.onLoadConfiguration.tapPromise( |
| 54 | + { |
| 55 | + name: PLUGIN_NAME, |
| 56 | + stage: STAGE_LOAD_LOCAL_CONFIG |
| 57 | + }, |
| 58 | + async () => { |
| 59 | + terminal.writeVerboseLine(`Attempting to load NAPI-RS configuration from local file`); |
| 60 | + const napiRsConfiguration: INapiRsConfiguration | undefined = await _tryLoadConfigFileAsync( |
| 61 | + options, |
| 62 | + pluginOptions |
| 63 | + ); |
| 64 | + |
| 65 | + if (napiRsConfiguration) { |
| 66 | + terminal.writeVerboseLine(`Loaded NAPI-RS configuration from local file.`); |
| 67 | + } |
| 68 | + |
| 69 | + return napiRsConfiguration; |
| 70 | + } |
| 71 | + ); |
| 72 | + |
| 73 | + // Obtain the NAPI-RS configuration by calling into the hook. |
| 74 | + // The local configuration is loaded at STAGE_LOAD_LOCAL_CONFIG |
| 75 | + terminal.writeVerboseLine('Attempting to load NAPI-RS configuration'); |
| 76 | + let napiRsConfiguration: INapiRsConfiguration | false | undefined = |
| 77 | + await hooks.onLoadConfiguration.promise(); |
| 78 | + |
| 79 | + if (napiRsConfiguration === false) { |
| 80 | + terminal.writeLine('NAPI-RS disabled by external plugin'); |
| 81 | + napiRsConfiguration = undefined; |
| 82 | + } else if ( |
| 83 | + napiRsConfiguration === undefined || |
| 84 | + (Array.isArray(napiRsConfiguration) && napiRsConfiguration.length === 0) |
| 85 | + ) { |
| 86 | + terminal.writeLine('No NAPI-RS configuration found'); |
| 87 | + napiRsConfiguration = undefined; |
| 88 | + } else { |
| 89 | + if (hooks.onConfigure.isUsed()) { |
| 90 | + // Allow for plugins to customize the configuration |
| 91 | + await hooks.onConfigure.promise(napiRsConfiguration); |
| 92 | + } |
| 93 | + if (hooks.onAfterConfigure.isUsed()) { |
| 94 | + // Provide the finalized configuration |
| 95 | + await hooks.onAfterConfigure.promise(napiRsConfiguration); |
| 96 | + } |
| 97 | + } |
| 98 | + return napiRsConfiguration as INapiRsConfiguration | undefined; |
| 99 | +} |
| 100 | + |
| 101 | +/** |
| 102 | + * @internal |
| 103 | + */ |
| 104 | +export async function tryLoadNapiRsConfigurationFileAsync( |
| 105 | + options: ILoadNapiRsConfigurationOptions, |
| 106 | + pluginOptions: INapiRsPluginOptions |
| 107 | +): Promise<INapiRsConfiguration | undefined> { |
| 108 | + const { taskSession, heftConfiguration, loadNapiRsAsyncFn } = options; |
| 109 | + const { logger } = taskSession; |
| 110 | + const { terminal } = logger; |
| 111 | + const { configurationPath } = pluginOptions; |
| 112 | + let napiRsConfigJs: INapiRsConfigJs | undefined; |
| 113 | + |
| 114 | + try { |
| 115 | + const buildFolderPath: string = heftConfiguration.buildFolderPath; |
| 116 | + const configPath: string = path.resolve( |
| 117 | + buildFolderPath, |
| 118 | + configurationPath || DEFAULT_NAPI_RS_CONFIG_PATH |
| 119 | + ); |
| 120 | + terminal.writeVerboseLine(`Attempting to load NAPI-RS configuration from "${configPath}".`); |
| 121 | + napiRsConfigJs = await _tryLoadNapiRsConfigurationFileInnerAsync(configPath); |
| 122 | + } catch (error) { |
| 123 | + logger.emitError(error as Error); |
| 124 | + } |
| 125 | + |
| 126 | + if (napiRsConfigJs) { |
| 127 | + const napiRsConfig: INapiRsConfigJsExport = |
| 128 | + (napiRsConfigJs as { default: INapiRsConfigJsExport }).default || |
| 129 | + (napiRsConfigJs as INapiRsConfigJsExport); |
| 130 | + |
| 131 | + if (typeof napiRsConfig === 'function') { |
| 132 | + // Defer loading of napiRs until we know for sure that we will need it |
| 133 | + return napiRsConfig({ |
| 134 | + taskSession, |
| 135 | + heftConfiguration, |
| 136 | + napiRs: await loadNapiRsAsyncFn() |
| 137 | + }); |
| 138 | + } else { |
| 139 | + return napiRsConfig; |
| 140 | + } |
| 141 | + } else { |
| 142 | + return undefined; |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +/** |
| 147 | + * @internal |
| 148 | + */ |
| 149 | +export async function _tryLoadNapiRsConfigurationFileInnerAsync( |
| 150 | + configurationPath: string |
| 151 | +): Promise<INapiRsConfigJs | undefined> { |
| 152 | + const configExists: boolean = await FileSystem.existsAsync(configurationPath); |
| 153 | + if (configExists) { |
| 154 | + try { |
| 155 | + return await import(configurationPath); |
| 156 | + } catch (e) { |
| 157 | + const error: NodeJS.ErrnoException = e as NodeJS.ErrnoException; |
| 158 | + if (error.code === 'ERR_MODULE_NOT_FOUND') { |
| 159 | + // No configuration found, return undefined. |
| 160 | + return undefined; |
| 161 | + } |
| 162 | + throw new Error(`Error loading NAPI-RS configuration at "${configurationPath}": ${e}`); |
| 163 | + } |
| 164 | + } else { |
| 165 | + return undefined; |
| 166 | + } |
| 167 | +} |
0 commit comments