|
| 1 | +import { spawnSync } from 'node:child_process'; |
1 | 2 | import { createRequire } from 'node:module'; |
2 | | -import { fileURLToPath } from 'node:url'; |
| 3 | +import { join } from 'node:path'; |
| 4 | +import { fileURLToPath, pathToFileURL } from 'node:url'; |
3 | 5 |
|
4 | 6 | import { importModule } from '../../../core/src/shared/utils/module.ts'; |
5 | 7 | import type { PostinstallOptions } from './add.ts'; |
6 | 8 |
|
7 | 9 | const DIR_CWD = process.cwd(); |
8 | | -const require = createRequire(DIR_CWD); |
| 10 | +// createRequire treats a bare directory as a file path and resolves from its parent, |
| 11 | +// so anchor it to a file inside the project instead. |
| 12 | +const require = createRequire(join(DIR_CWD, 'package.json')); |
9 | 13 |
|
10 | 14 | export const postinstallAddon = async (addonName: string, options: PostinstallOptions) => { |
11 | 15 | const hookPath = `${addonName}/postinstall`; |
12 | | - let modulePath: string; |
| 16 | + const logger = options.logger; |
| 17 | + let modulePath: string | undefined; |
13 | 18 | try { |
14 | | - modulePath = import.meta.resolve(hookPath, DIR_CWD); |
| 19 | + // Resolve from the user's project: import.meta.resolve would resolve relative to THIS file, |
| 20 | + // which points at the wrong copy (or none at all) when the CLI runs from a different tree |
| 21 | + // than the project, e.g. via npx or in a monorepo. |
| 22 | + modulePath = require.resolve(hookPath, { paths: [DIR_CWD] }); |
15 | 23 | } catch (e) { |
| 24 | + // When the addon was installed while this process was already running (the upgrade command |
| 25 | + // installs dependencies mid-run), Node's module resolution has cached the earlier negative |
| 26 | + // lookup and keeps failing. A fresh child process resolves from a clean cache. |
| 27 | + const result = spawnSync( |
| 28 | + process.execPath, |
| 29 | + ['-p', `require.resolve(${JSON.stringify(hookPath)}, { paths: [process.cwd()] })`], |
| 30 | + { cwd: DIR_CWD, encoding: 'utf8', timeout: 30_000 } |
| 31 | + ); |
| 32 | + if (result.status === 0 && result.stdout.trim()) { |
| 33 | + modulePath = result.stdout.trim(); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + if (!modulePath) { |
16 | 38 | try { |
17 | | - modulePath = require.resolve(hookPath, { paths: [DIR_CWD] }); |
| 39 | + modulePath = import.meta.resolve(hookPath); |
18 | 40 | } catch (e) { |
| 41 | + logger.warn( |
| 42 | + `Could not resolve the postinstall hook of ${addonName}, skipping its configuration. Run \`npx storybook add ${addonName}\` to set it up manually.` |
| 43 | + ); |
19 | 44 | return; |
20 | 45 | } |
21 | 46 | } |
22 | 47 |
|
| 48 | + // Prefer the module resolved from the user's project (modulePath): a bare `import(hookPath)` |
| 49 | + // resolves relative to THIS file, which points at the wrong copy (or none at all) when the CLI |
| 50 | + // runs from a different tree than the project, e.g. via npx or in a monorepo. |
| 51 | + const moduleFilePath = modulePath.startsWith('file:') ? fileURLToPath(modulePath) : modulePath; |
| 52 | + |
23 | 53 | let moduledLoaded: any; |
24 | 54 | try { |
25 | | - moduledLoaded = await import(hookPath) |
| 55 | + moduledLoaded = await import(pathToFileURL(moduleFilePath).href) |
| 56 | + .catch(() => importModule(moduleFilePath)) |
| 57 | + .catch(() => require(moduleFilePath)) |
| 58 | + .catch(() => import(hookPath)) |
26 | 59 | .catch(() => importModule(hookPath)) |
27 | | - .catch(() => importModule(modulePath)) |
28 | | - .catch(() => importModule(fileURLToPath(modulePath))) |
29 | | - .catch(() => require(hookPath)) |
30 | | - .catch(() => require(modulePath)) |
31 | | - .catch(() => require(fileURLToPath(modulePath))); |
| 60 | + .catch(() => require(hookPath)); |
32 | 61 | } catch (e) { |
| 62 | + logger.warn( |
| 63 | + `Could not load the postinstall hook of ${addonName} (${String( |
| 64 | + e |
| 65 | + )}), skipping its configuration. Run \`npx storybook add ${addonName}\` to set it up manually.` |
| 66 | + ); |
33 | 67 | return; |
34 | 68 | } |
35 | 69 |
|
36 | 70 | const postinstall = moduledLoaded?.default || moduledLoaded?.postinstall || moduledLoaded; |
37 | | - const logger = options.logger; |
38 | 71 |
|
39 | 72 | if (!postinstall || typeof postinstall !== 'function') { |
40 | 73 | logger.error(`Error finding postinstall function for ${addonName}`); |
|
0 commit comments