-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathupdateVersion.mjs
More file actions
463 lines (441 loc) · 16 KB
/
Copy pathupdateVersion.mjs
File metadata and controls
463 lines (441 loc) · 16 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import fs from 'fs-extra';
import {glob} from 'glob';
import path from 'node:path';
import prettier from 'prettier';
import {PackageMetadata} from './shared/PackageMetadata.mjs';
import {packagesManager} from './shared/packagesManager.mjs';
import readMonorepoPackageJson from './shared/readMonorepoPackageJson.mjs';
import {
MIN_TYPESCRIPT_VERSION,
tooOldStubPath,
tooOldTypesVersions,
TYPESCRIPT_TOO_OLD_CONDITION,
} from './shared/typescriptTooOld.mjs';
import npmToWwwName from './www/npmToWwwName.mjs';
const monorepoPackageJson = readMonorepoPackageJson();
// get version from monorepo package.json version
const version = monorepoPackageJson.version;
const publicNpmNames = new Set(
packagesManager.getPublicPackages().map(pkg => pkg.getNpmName()),
);
/**
* @typedef {{source?: string; import: Record<string, string>; require: Record<string, string>}} ImportRequireExports
*/
/**
* - Set the version to the monorepo ./package.json version
* - Update dependencies, devDependencies, and peerDependencies
* - Update the exports map and set other required default fields
* @param {PackageMetadata} pkg
*/
function updatePackage(pkg) {
pkg.packageJson.version = version;
updateDependencies(pkg);
if (!pkg.isPrivate()) {
updatePublicPackage(pkg);
}
pkg.writeSync();
}
/**
* Update every package.json in the packages/ and examples/ directories
*
* - Set the version to the monorepo ./package.json version
* - Update the versions of monorepo dependencies, devDependencies, and peerDependencies
* - Update the exports map and set other required default fields
*
*/
async function updateVersion() {
await regenerateInternalVersionModule();
packagesManager.getPackages().forEach(updatePackage);
glob
.sync([
'./examples/*/package.json',
'./scripts/__tests__/integration/fixtures/*/package.json',
])
.forEach(packageJsonPath =>
updatePackage(new PackageMetadata(packageJsonPath)),
);
}
const INTERNAL_PACKAGE_NAME = '@lexical/internal';
/**
* Rewrite the generated literal in @lexical/internal's version module to the
* current monorepo version. In a Rollup build `process.env.LEXICAL_VERSION`
* is statically replaced with a build-specific string; this literal is the
* fallback used when the source is consumed without that build step.
*/
async function regenerateInternalVersionModule() {
const versionPath = path.resolve('packages/lexical-internal/src/version.ts');
if (!fs.existsSync(versionPath)) {
return;
}
const next = fs
.readFileSync(versionPath, 'utf8')
.replace(/'[^']*\+source'/, `'${version}+source'`);
const prettierConfig = (await prettier.resolveConfig(versionPath)) || {};
fs.writeFileSync(
versionPath,
await prettier.format(next, {...prettierConfig, filepath: versionPath}),
);
}
/**
* Return true if any non-test source file in the package imports from
* `@lexical/internal/...`, meaning the package needs it as a runtime
* dependency (so the `source` export condition resolves for npm consumers).
*
* @param {PackageMetadata} pkg
* @returns {boolean}
*/
function srcImportsInternalPackage(pkg) {
const srcDir = pkg.resolve('src');
if (!fs.existsSync(srcDir)) {
return false;
}
const stack = [srcDir];
while (stack.length > 0) {
const dir = /** @type {string} */ (stack.pop());
for (const ent of fs.readdirSync(dir, {withFileTypes: true})) {
if (ent.isDirectory()) {
if (ent.name !== '__tests__' && ent.name !== '__bench__') {
stack.push(path.join(dir, ent.name));
}
} else if (/\.tsx?$/.test(ent.name)) {
const contents = fs.readFileSync(path.join(dir, ent.name), 'utf8');
if (contents.includes(`from '${INTERNAL_PACKAGE_NAME}/`)) {
return true;
}
}
}
}
return false;
}
/**
* Replace the extension in a .js or .mjs filename with another extension.
* Used to convert between the two or to add a prefix before the extension.
*
* `ext` may contain $1 to use the original extension in the
* replacement, e.g. replaceExtension('foo.js', '.bar$1') -> 'foo.bar.js'
*
* @param {string} fileName
* @param {string} ext
* @returns {string} fileName with ext as the new extension
*/
function replaceExtension(fileName, ext) {
return fileName.replace(/(\.m?js)$/, ext);
}
/**
* webpack can use these conditions to choose a dev or prod
* build without a fork module, which is especially helpful
* in the ESM build.
*
* @param {string} fileName may have .js or .mjs extension
* @returns {Record<'development'|'production', string>}
*/
function withEnvironments(fileName) {
return {
development: replaceExtension(fileName, '.dev$1'),
production: replaceExtension(fileName, '.prod$1'),
};
}
/**
* The subdirectory of a package that holds built artifacts (and is what
* the public exports/main/types fields resolve into). Keeping this in one
* place makes the package directory itself a publishable npm package and
* allows `pnpm link` / `file:` consumers to point at the package root.
*/
const DIST_DIR = 'dist';
/**
* Build an export map for a particular entry point in the package.json
*
* @param {string} basename the name of the entry point module without an extension (e.g. 'index')
* @param {string} [typesBasename]
* @param {string} [sourceRelPath] path relative to the package root for the
* TypeScript (or CJS) source backing this entry. When provided, a
* `source` condition is added so bundlers configured with
* `resolve.conditions: ['source', ...]` can consume the package
* without a build step (useful for `pnpm link` / `file:` consumers).
* @returns {ImportRequireExports} The export map for this file
*/
function exportEntry(
basename,
typesBasename = `${basename}.d.ts`,
sourceRelPath,
) {
// Bundlers such as webpack require 'types' to be first and 'default' to be
// last per #5731. Keys are in descending priority order.
const prefix = `./${DIST_DIR}/${basename}`;
const types = `./${DIST_DIR}/${typesBasename}`;
// Redirect consumers that read "exports" but are below the minimum supported
// TypeScript version (TypeScript 4.9 understands `types@`, up to but not
// including the minimum) to the "too old" stub. Must precede `types`.
const tooOld = tooOldStubPath(DIST_DIR);
return {
/* eslint-disable sort-keys-fix/sort-keys-fix */
...(sourceRelPath ? {source: `./${sourceRelPath}`} : null),
import: {
[TYPESCRIPT_TOO_OLD_CONDITION]: tooOld,
types,
...withEnvironments(`${prefix}.mjs`),
node: `${prefix}.node.mjs`,
default: `${prefix}.mjs`,
},
require: {
[TYPESCRIPT_TOO_OLD_CONDITION]: tooOld,
types,
...withEnvironments(`${prefix}.js`),
default: `${prefix}.js`,
},
/* eslint-enable sort-keys-fix/sort-keys-fix */
};
}
/**
* Add a browser condition for a particular entry point in the package.json
*
* @param {ImportRequireExports} exports
* @returns {Record<'browser'|'import'|'require', Record<string,string>>} The export map for this file
*/
function withBrowser(exports) {
const browser = Object.fromEntries(
Object.entries(exports.import).flatMap(([k, v]) => {
if (k === 'node') {
return [];
} else if (k === 'types' || k.startsWith('types@')) {
// `types` and the versioned `types@<min>` condition point at .d.ts
// files, never a browser bundle; pass them through unchanged.
return [[k, v]];
}
return [[k, v.replace(/((?:\.dev|\.prod)?\.m?js)$/, '.browser$1')]];
}),
);
// Keep `source` first so a consumer that opts in with
// `resolve.conditions: ['source', ...]` always wins over `browser`.
const {source, ...rest} = exports;
return {...(source ? {source} : null), browser, ...rest};
}
/**
* Strip any leading './' or 'dist/' segment from a path stored in
* package.json. Existing package.json fields may be `Lexical.js`,
* `./Lexical.js`, or `./dist/Lexical.js` depending on when they were
* last written; normalize to the bare basename for derivation.
*
* @param {string} value
* @returns {string}
*/
function stripDistPrefix(value) {
return value.replace(/^(\.\/)?(dist\/)?/, '');
}
/**
* Files that should be present alongside package.json in every public
* package directory so it ships as a complete npm package without any
* separate copy-into-`npm/` step. `src` is included (minus tests and
* benchmarks) so the `source` export condition (added by `exportEntry`)
* resolves for npm consumers that opt in via
* `resolve.conditions: ['source', ...]`.
*/
const PUBLIC_FILES_FIELD = [
'dist',
'src',
'!src/__tests__',
'!src/__bench__',
'!src/__mocks__',
'!src/**/*.test.ts',
'!src/**/*.test.tsx',
'!src/**/*.bench.ts',
'!src/**/*.bench.tsx',
'README.md',
'LICENSE',
];
/**
* Copy the monorepo LICENSE into the package directory. The published
* tarball must contain the LICENSE next to package.json; doing this
* during `update-packages` keeps the working tree publishable without
* relying on the previous prepare-release copy step.
*
* @param {PackageMetadata} pkg
*/
function ensureLicense(pkg) {
const dest = pkg.resolve('LICENSE');
fs.copySync(path.resolve('LICENSE'), dest);
}
/**
* Find the source file backing a `main`-style entry. The build always
* compiles `src/index.{ts,tsx}` into the `main` output regardless of
* what `main` is named, so check for index first; fall back to
* `src/<basename>.{ts,tsx,js}` to cover any custom layout.
*
* @param {PackageMetadata} pkg
* @param {string} basename
* @returns {string | undefined}
*/
function findMainSourceRelPath(pkg, basename) {
const candidates = [
'index.ts',
'index.tsx',
'index.js',
`${basename}.ts`,
`${basename}.tsx`,
`${basename}.js`,
];
for (const fn of candidates) {
if (fs.existsSync(pkg.resolve('src', fn))) {
return `src/${fn}`;
}
}
return undefined;
}
/**
* Update the public package's packageJson in-place to add default configurations
* for `sideEffects` and `module` as well as to maintain the `exports` map.
*
* @param {PackageMetadata} pkg
*/
function updatePublicPackage(pkg) {
const {packageJson} = pkg;
if (packageJson.sideEffects === undefined) {
packageJson.sideEffects = false;
}
// If there's a main we expect a single entry point
if (packageJson.main) {
const mainBase = stripDistPrefix(packageJson.main);
packageJson.main = `./${DIST_DIR}/${mainBase}`;
packageJson.module = `./${DIST_DIR}/${replaceExtension(mainBase, '.mjs')}`;
const sourceRelPath = findMainSourceRelPath(
pkg,
replaceExtension(mainBase, ''),
);
// Derive the declaration basename from the entry source (the build emits
// e.g. src/index.ts -> dist/index.d.ts) rather than from the existing
// `types` field: `types` is rewritten to the "too old" stub below, so
// reading it here would corrupt the real types on a second run.
const typesBase = sourceRelPath
? `${path.basename(sourceRelPath).replace(/\.(tsx?|js)$/, '')}.d.ts`
: 'index.d.ts';
packageJson.exports = {
'.': exportEntry(
replaceExtension(mainBase, ''),
typesBase,
sourceRelPath,
),
};
} else {
/** @type {Record<string, unknown>} */
const exports = {};
// Export all src/*.tsx? files that do not have a prefix extension (e.g. no .d.ts)
for (const fn of fs.readdirSync(pkg.resolve('src'))) {
if (/^[^.]+\.tsx?$/.test(fn)) {
const basename = fn.replace(/\.tsx?$/, '');
const hasBrowser = fs.existsSync(
pkg.resolve('src', fn.replace(/(\.tsx?)$/, '.browser$1')),
);
const packageName = pkg.getNpmName();
const isIndex = basename === 'index';
const entryNameInput = isIndex
? packageName
: `${packageName}/${basename}`;
const entryName = npmToWwwName(entryNameInput);
let entry = exportEntry(entryName, `${basename}.d.ts`, `src/${fn}`);
if (hasBrowser) {
entry = withBrowser(entry);
}
// support for import "@lexical/react/LexicalComposer"
exports[isIndex ? '.' : `./${basename}`] = entry;
if (!hasBrowser && !isIndex) {
// support for import "@lexical/react/LexicalComposer.js"
// @mdxeditor/editor uses this at least as of v3.46.0
exports[`./${basename}.js`] = entry;
}
}
}
packageJson.exports = exports;
}
// Tombstone the legacy type-resolution fields. A consumer whose TypeScript
// cannot read "exports" (classic moduleResolution, at any version) resolves
// `types` for the package root and `typesVersions` for every subpath; point
// both at the "too old" stub so they get a clear upgrade message instead of
// a misleading "Cannot find module". Modern resolvers ignore both fields
// because "exports" takes priority over them (TypeScript >= 4.9).
packageJson.types = tooOldStubPath(DIST_DIR);
packageJson.typesVersions = tooOldTypesVersions(DIST_DIR);
// Advise (but do not require) a supported TypeScript at install time. npm and
// pnpm surface a peer-dependency warning when an out-of-range `typescript` is
// present; `optional` keeps it from being installed or hard-required for
// consumers that don't use TypeScript. This complements the type-check-time
// guard above with an earlier, install-time signal.
packageJson.peerDependencies = {
...packageJson.peerDependencies,
typescript: `>=${MIN_TYPESCRIPT_VERSION}`,
};
packageJson.peerDependenciesMeta = {
...packageJson.peerDependenciesMeta,
typescript: {optional: true},
};
pkg.sortDependencies('peerDependencies');
// Whitelist what ships to npm. The package root is the publish root, so
// we no longer need a separate `npm/` copy step.
packageJson.files = [...PUBLIC_FILES_FIELD];
ensureLicense(pkg);
}
/**
* Update dependencies and peerDependencies in pkg in-place.
* All entries for monorepo packages will be updated to version.
* All peerDependencies for monorepo packages will be moved to dependencies.
*
* @param {PackageMetadata} pkg
*/
function updateDependencies(pkg) {
// examples should use exact versions since they
// are not currently in the workspace
const depVersion =
path.basename(pkg.resolve('..')) !== 'packages' ? version : 'workspace:*';
const {packageJson} = pkg;
const {
dependencies = {},
peerDependencies = {},
devDependencies = {},
} = packageJson;
// Pinned-locally deps (link:..., file:...) are intentional — a fixture
// or example may want to resolve through pnpm's link protocol against
// the local checkout. Leave those alone; only normalize semver-style
// pins to the canonical monorepo version.
const isLocalProtocol = (/** @type {unknown} */ v) =>
typeof v === 'string' && /^(link|file|portal):/.test(v);
[dependencies, devDependencies].forEach(deps => {
Object.keys(deps).forEach(dep => {
if (publicNpmNames.has(dep) && !isLocalProtocol(deps[dep])) {
deps[dep] = depVersion;
}
});
});
// Move peerDependencies on lexical monorepo packages to dependencies
// per #5783
Object.keys(peerDependencies).forEach(peerDep => {
if (publicNpmNames.has(peerDep)) {
delete peerDependencies[peerDep];
dependencies[peerDep] = depVersion;
}
});
// Reconcile the @lexical/internal dependency: a package needs it iff its
// source imports it (so the `source` export condition resolves for npm
// consumers). Add it when imported, remove it when no longer imported.
// (@lexical/internal must not depend on itself; leave local-protocol pins.)
if (
pkg.getNpmName() !== INTERNAL_PACKAGE_NAME &&
!isLocalProtocol(dependencies[INTERNAL_PACKAGE_NAME])
) {
if (srcImportsInternalPackage(pkg)) {
dependencies[INTERNAL_PACKAGE_NAME] = depVersion;
} else {
delete dependencies[INTERNAL_PACKAGE_NAME];
}
}
pkg
.sortDependencies('dependencies', dependencies)
.sortDependencies('devDependencies', devDependencies)
.sortDependencies('peerDependencies', peerDependencies);
}
updateVersion();