|
1 | | -import { isAbsolute, join, resolve as resolvePath } from 'node:path'; |
2 | | -import { pathToFileURL } from 'node:url'; |
| 1 | +import { readFile, realpath } from 'node:fs/promises'; |
| 2 | +import { isAbsolute, join, relative, resolve as resolvePath, sep } from 'node:path'; |
| 3 | +import { fileURLToPath, pathToFileURL } from 'node:url'; |
3 | 4 | import { moduleResolve } from 'import-meta-resolve'; |
4 | 5 | import type { Options as PrettierOptions } from 'prettier'; |
| 6 | +import { toPosixPath } from './pathHelpers.ts'; |
5 | 7 | import type { FmtPluginSpecifier, ResolvedFmtOptions } from './types.ts'; |
6 | 8 |
|
7 | 9 | type FmtPlugin = NonNullable<PrettierOptions['plugins']>[number]; |
8 | 10 | type FmtPluginResolver = (options: ResolvedFmtOptions) => ResolvedFmtOptions; |
| 11 | +type FingerprintResolver = (plugin: FmtPluginSpecifier) => Promise<string | undefined>; |
9 | 12 |
|
10 | 13 | const resolveModuleUrl = (specifier: string, parentUrl: URL): string => |
11 | 14 | moduleResolve(specifier, parentUrl).href; |
12 | 15 |
|
13 | 16 | const isFmtPluginSpecifier = (plugin: FmtPlugin): plugin is FmtPluginSpecifier => |
14 | 17 | typeof plugin === 'string' || plugin instanceof URL; |
15 | 18 |
|
| 19 | +const getPackageRoot = (entryPath: string): string | undefined => { |
| 20 | + const marker = `${sep}node_modules${sep}`; |
| 21 | + const index = entryPath.lastIndexOf(marker); |
| 22 | + if (index === -1) { |
| 23 | + return undefined; |
| 24 | + } |
| 25 | + const start = index + marker.length; |
| 26 | + |
| 27 | + let end = entryPath.indexOf(sep, start); |
| 28 | + if (end === -1) { |
| 29 | + return undefined; |
| 30 | + } |
| 31 | + if (entryPath[start] === '@') { |
| 32 | + end = entryPath.indexOf(sep, end + 1); |
| 33 | + if (end === -1) { |
| 34 | + return undefined; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + return entryPath.slice(0, end); |
| 39 | +}; |
| 40 | + |
| 41 | +const fingerprintPlugin = async (pluginUrl: string): Promise<string | undefined> => { |
| 42 | + try { |
| 43 | + const url = new URL(pluginUrl); |
| 44 | + if (url.protocol !== 'file:') { |
| 45 | + return undefined; |
| 46 | + } |
| 47 | + |
| 48 | + // Resolving symlinks keeps workspace and other local plugins out of the |
| 49 | + // package-version fast path when their implementation can change in place. |
| 50 | + const entryPath = await realpath(fileURLToPath(url)); |
| 51 | + const packageRoot = getPackageRoot(entryPath); |
| 52 | + if (packageRoot === undefined) { |
| 53 | + return undefined; |
| 54 | + } |
| 55 | + |
| 56 | + const pkg: unknown = JSON.parse(await readFile(join(packageRoot, 'package.json'), 'utf8')); |
| 57 | + if ( |
| 58 | + typeof pkg !== 'object' || |
| 59 | + pkg === null || |
| 60 | + !('name' in pkg) || |
| 61 | + typeof pkg.name !== 'string' || |
| 62 | + !('version' in pkg) || |
| 63 | + typeof pkg.version !== 'string' |
| 64 | + ) { |
| 65 | + return undefined; |
| 66 | + } |
| 67 | + |
| 68 | + return JSON.stringify([ |
| 69 | + 'package', |
| 70 | + pkg.name, |
| 71 | + pkg.version, |
| 72 | + toPosixPath(relative(packageRoot, entryPath)), |
| 73 | + ]); |
| 74 | + } catch { |
| 75 | + return undefined; |
| 76 | + } |
| 77 | +}; |
| 78 | + |
| 79 | +/** Creates a memoized identity resolver for installed package plugins. */ |
| 80 | +const createFingerprintResolver = (): FingerprintResolver => { |
| 81 | + const cache = new Map<string, Promise<string | undefined>>(); |
| 82 | + |
| 83 | + return (plugin) => { |
| 84 | + const pluginUrl = plugin instanceof URL ? plugin.href : plugin; |
| 85 | + let fingerprint = cache.get(pluginUrl); |
| 86 | + if (fingerprint === undefined) { |
| 87 | + fingerprint = fingerprintPlugin(pluginUrl); |
| 88 | + cache.set(pluginUrl, fingerprint); |
| 89 | + } |
| 90 | + return fingerprint; |
| 91 | + }; |
| 92 | +}; |
| 93 | + |
16 | 94 | /** Creates a project-root resolver for plugins in final per-file options. */ |
17 | 95 | const createFmtPluginResolver = (rootPath: string): FmtPluginResolver => { |
18 | 96 | const parentUrl = pathToFileURL(join(rootPath, 'index.js')); |
@@ -65,5 +143,5 @@ const createFmtPluginResolver = (rootPath: string): FmtPluginResolver => { |
65 | 143 | }; |
66 | 144 | }; |
67 | 145 |
|
68 | | -export { createFmtPluginResolver }; |
69 | | -export type { FmtPluginResolver }; |
| 146 | +export { createFingerprintResolver, createFmtPluginResolver }; |
| 147 | +export type { FingerprintResolver, FmtPluginResolver }; |
0 commit comments