-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy path.pnpmfile.cjs
More file actions
268 lines (231 loc) · 9.12 KB
/
.pnpmfile.cjs
File metadata and controls
268 lines (231 loc) · 9.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/**
* pnpm hook for config-driven local development.
*
* This hook reads `.openzeppelin-dev.json` from the repository root and rewrites
* configured dependency families to either packed tarballs or direct repo paths
* when their corresponding LOCAL_* flags are enabled.
*/
const fs = require('fs');
const path = require('path');
const CONFIG_FILE = '.openzeppelin-dev.json';
const STANDARD_FAMILIES = {
ui: {
repoName: 'openzeppelin-ui',
envFlag: 'LOCAL_UI',
envNames: ['LOCAL_UI_PATH'],
defaultPath: '../openzeppelin-ui',
packageMap: {
'@openzeppelin/ui-types': 'packages/types',
'@openzeppelin/ui-utils': 'packages/utils',
'@openzeppelin/ui-styles': 'packages/styles',
'@openzeppelin/ui-components': 'packages/components',
'@openzeppelin/ui-renderer': 'packages/renderer',
'@openzeppelin/ui-react': 'packages/react',
'@openzeppelin/ui-storage': 'packages/storage',
},
},
adapters: {
repoName: 'openzeppelin-adapters',
envFlag: 'LOCAL_ADAPTERS',
envNames: ['LOCAL_ADAPTERS_PATH'],
defaultPath: '../openzeppelin-adapters',
packageMap: {
'@openzeppelin/adapters-vite': 'packages/adapters-vite',
'@openzeppelin/adapter-evm-core': 'packages/adapter-evm-core',
'@openzeppelin/adapter-evm': 'packages/adapter-evm',
'@openzeppelin/adapter-midnight': 'packages/adapter-midnight',
'@openzeppelin/adapter-polkadot': 'packages/adapter-polkadot',
'@openzeppelin/adapter-runtime-utils': 'packages/adapter-runtime-utils',
'@openzeppelin/adapter-solana': 'packages/adapter-solana',
'@openzeppelin/adapter-stellar': 'packages/adapter-stellar',
},
},
};
function isObject(value) {
return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
}
function getRealPath(targetPath) {
return typeof fs.realpathSync.native === 'function'
? fs.realpathSync.native(targetPath)
: fs.realpathSync(targetPath);
}
function resolveCacheDir(workspaceRoot, cacheDir) {
const resolvedWorkspaceRoot = path.resolve(workspaceRoot);
const resolvedCacheDir = path.resolve(resolvedWorkspaceRoot, cacheDir);
const relativeCacheDir = path.relative(resolvedWorkspaceRoot, resolvedCacheDir);
if (
relativeCacheDir === '' ||
relativeCacheDir.startsWith('..') ||
path.isAbsolute(relativeCacheDir)
) {
throw new Error(`${CONFIG_FILE} "cacheDir" must be a subdirectory of the workspace root.`);
}
return resolvedCacheDir;
}
function isAnyLocalFamilyEnabled() {
return Object.values(STANDARD_FAMILIES).some((family) => process.env[family.envFlag] === 'true');
}
function readProjectConfig(workspaceRoot) {
const configPath = path.join(workspaceRoot, CONFIG_FILE);
if (!fs.existsSync(configPath)) {
throw new Error(`Missing ${CONFIG_FILE} in ${workspaceRoot}.`);
}
const parsed = JSON.parse(fs.readFileSync(configPath, 'utf8'));
if (!isObject(parsed) || parsed.version !== 1 || !isObject(parsed.families)) {
throw new Error(`${CONFIG_FILE} must declare "version": 1 and a "families" object.`);
}
const families = Object.create(null);
for (const [familyKey, overrides] of Object.entries(parsed.families)) {
if (!Object.prototype.hasOwnProperty.call(STANDARD_FAMILIES, familyKey)) {
throw new Error(`Unsupported family "${familyKey}" in ${CONFIG_FILE}.`);
}
const familyOverrides = isObject(overrides) ? overrides : {};
const baseFamily = STANDARD_FAMILIES[familyKey];
const filteredEnvNames =
Array.isArray(familyOverrides.envNames) && familyOverrides.envNames.length > 0
? familyOverrides.envNames.filter((value) => typeof value === 'string' && value.length > 0)
: null;
families[familyKey] = {
...baseFamily,
defaultPath:
typeof familyOverrides.defaultPath === 'string' && familyOverrides.defaultPath.length > 0
? familyOverrides.defaultPath
: baseFamily.defaultPath,
envNames:
filteredEnvNames && filteredEnvNames.length > 0
? filteredEnvNames
: [...baseFamily.envNames],
};
}
const cacheDirFromConfig =
typeof parsed.cacheDir === 'string' && parsed.cacheDir.trim().length > 0
? parsed.cacheDir
: '.packed-packages/local-dev';
return {
cacheDir: resolveCacheDir(workspaceRoot, cacheDirFromConfig),
families,
};
}
function getConfiguredPath(envNames, defaultPath) {
for (const envName of envNames) {
if (process.env[envName]) {
return {
envName,
relativePath: process.env[envName],
};
}
}
return {
envName: null,
relativePath: defaultPath,
};
}
function resolveRepoRoot(baseDir, family) {
const { envName, relativePath } = getConfiguredPath(family.envNames, family.defaultPath);
const resolvedPath = path.resolve(baseDir, relativePath);
if (!fs.existsSync(resolvedPath)) {
const envHelp = family.envNames.join(' or ');
const envSource = envName ? `${envName}=${relativePath}` : `default path ${family.defaultPath}`;
throw new Error(
`[local-dev] ${family.repoName} checkout not found at ${resolvedPath} (${envSource}). Set ${envHelp} to a valid ${family.repoName} checkout.`
);
}
return getRealPath(resolvedPath);
}
function resolvePackageDirectory(workspaceRoot, family, packageName, packagePath) {
const repoRoot = resolveRepoRoot(workspaceRoot, family);
const resolvedPath = path.resolve(repoRoot, packagePath);
const expectedPackageJsonPath = path.join(resolvedPath, 'package.json');
if (!fs.existsSync(resolvedPath)) {
throw new Error(
`[local-dev] Expected ${packageName} to have a package.json at ${expectedPackageJsonPath}, but it was not found. Check that ${family.repoName} matches a compatible checkout and contains this package.`
);
}
const absolutePath = getRealPath(resolvedPath);
const packageJsonPath = path.join(absolutePath, 'package.json');
if (!fs.existsSync(packageJsonPath)) {
throw new Error(
`[local-dev] Expected ${packageName} to have a package.json at ${packageJsonPath}, but it was not found. Check that ${family.repoName} matches a compatible checkout and contains this package.`
);
}
return absolutePath;
}
function readPackedManifest(cacheDir, familyKey) {
const manifestPath = path.join(cacheDir, `${familyKey}.json`);
if (!fs.existsSync(manifestPath)) {
return null;
}
try {
const parsed = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
return isObject(parsed) && isObject(parsed.packages) ? parsed.packages : null;
} catch {
return null;
}
}
function rewriteDependencies(pkg, context, cacheDir, familyKey, family) {
const packedPackages = readPackedManifest(cacheDir, familyKey);
const workspaceRoot = __dirname;
for (const depType of ['dependencies', 'devDependencies']) {
if (!pkg[depType]) continue;
for (const [npmName, packagePath] of Object.entries(family.packageMap)) {
if (!pkg[depType][npmName]) continue;
const packedTarballPath = packedPackages && packedPackages[npmName];
if (packedTarballPath && fs.existsSync(packedTarballPath)) {
pkg[depType][npmName] = `file:${packedTarballPath}`;
context.log(`[local-dev] ${npmName} → ${packedTarballPath} (packed)`);
continue;
}
const absolutePath = resolvePackageDirectory(workspaceRoot, family, npmName, packagePath);
pkg[depType][npmName] = `file:${absolutePath}`;
context.log(`[local-dev] ${npmName} → ${absolutePath}`);
}
}
}
/**
* Widen `^X.Y.Z` ranges on `@openzeppelin/adapter*` packages to also include
* pre-release versions (`>=X.Y.Z-0 <(X+1).0.0`).
*
* This lets `pnpm install` resolve RC packages (e.g. 2.0.0-rc.1) when the
* stable version (e.g. 2.0.0) hasn't been published yet, without changing
* the declared range in package.json. Once the stable version ships, pnpm
* naturally resolves to it (highest match wins). The rewrite is harmless
* when stable versions are available — it's effectively a permanent no-op.
*
* Skips deps already rewritten to `file:` paths by local-dev mode.
*/
function allowAdapterPrereleases(pkg) {
for (const depType of ['dependencies', 'devDependencies']) {
if (!pkg[depType]) continue;
for (const [name, range] of Object.entries(pkg[depType])) {
if (!name.startsWith('@openzeppelin/adapter') && !name.startsWith('@openzeppelin/adapters-')) continue;
const m = range.match(/^\^(\d+)\.(\d+)\.(\d+)$/);
if (!m) continue;
const maj = Number(m[1]), min = Number(m[2]), pat = Number(m[3]);
const upper = maj > 0
? `${maj + 1}.0.0`
: min > 0
? `0.${min + 1}.0`
: `0.0.${pat + 1}`;
pkg[depType][name] = `>=${maj}.${min}.${pat}-0 <${upper}`;
}
}
}
function readPackage(pkg, context) {
if (isAnyLocalFamilyEnabled()) {
const workspaceRoot = __dirname;
const projectConfig = readProjectConfig(workspaceRoot);
for (const [familyKey, family] of Object.entries(projectConfig.families)) {
if (process.env[family.envFlag] !== 'true') {
continue;
}
rewriteDependencies(pkg, context, projectConfig.cacheDir, familyKey, family);
}
}
allowAdapterPrereleases(pkg);
return pkg;
}
module.exports = {
hooks: {
readPackage,
},
};