-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathcheck-and-rewrite-package-json.js
More file actions
165 lines (149 loc) · 5.97 KB
/
check-and-rewrite-package-json.js
File metadata and controls
165 lines (149 loc) · 5.97 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
/*
* Copyright (c) 2024, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
// Checks that various package.json files are structured the way we expect.
// Errors if the files are different, and writes the changed files to disk.
//
// The goals are:
// 1. to avoid having to manually keep package.json files in sync with each other
// 2. to have a consistent format for all the package.jsons we publish to npm
//
// Use the flag `--test` if you want it to fail with a non-zero exit code if the package.json
// files differ from what we expect.
const fs = require('node:fs');
const path = require('node:path');
const { globSync } = require('glob');
// This is the same list as in @lwc/rollup-plugin/src/index.ts
const LWC_EXPOSED_MODULES = {
'@lwc/engine-dom': ['lwc'],
'@lwc/synthetic-shadow': ['@lwc/synthetic-shadow'],
'@lwc/wire-service': [
'@lwc/wire-service',
// TODO [#3517]: remove support for deprecated 'wire-service' import
'wire-service',
],
};
/**
* Packages with static content that don't need to be built. The `lwc` package is not included
* because this script file only updates `@lwc/` packages.
*/
const STATIC_PACKAGES = ['@lwc/types'];
const expectedPkgJsons = [];
for (const dir of globSync('./packages/@lwc/*')) {
const filename = path.join('./', dir, 'package.json');
const actual = fs.readFileSync(filename, 'utf-8');
const pkg = JSON.parse(actual);
// Skip private packages
if (pkg.private) {
continue;
}
const { name, description, version, dependencies, devDependencies, peerDependencies } = pkg;
let { keywords } = pkg;
// Keywords aren't really important, but keep any that already exist and add 'lwc'
keywords = [...new Set((keywords || []).concat(['lwc']))].sort();
let buildProps;
if (STATIC_PACKAGES.includes(name)) {
// Static packages may or may not define all of these props; we don't need to worry about
// that because JSON.stringify automatically drops anything undefined.
buildProps = {
main: pkg.main,
module: pkg.module,
types: pkg.types,
files: pkg.files,
scripts: pkg.scripts,
nx: pkg.nx,
};
} else {
buildProps = {
main: 'dist/index.cjs.js',
module: 'dist/index.js',
types: 'dist/index.d.ts',
// It's important _not_ to use `./dist` here (with the `./`), because npm does not understand that
files: Array.from(new Set(['dist/**/*.js', 'dist/**/*.d.ts', ...pkg.files])),
scripts: {
build: 'rollup --config ../../../scripts/rollup/rollup.config.js',
dev: 'rollup --config ../../../scripts/rollup/rollup.config.js --watch --no-watch.clearScreen',
},
nx: {
targets: {
build: {
// It's important to use the `./` here, otherwise NX does not restore the dist files
// See https://github.com/salesforce/lwc/issues/3511
outputs: ['{projectRoot}/dist'],
},
},
},
};
}
const expectedJson = {
'//': [
'THIS FILE IS AUTOGENERATED. If you modify it, it will be rewritten by check-and-rewrite-package-json.js',
'You can safely modify dependencies, devDependencies, keywords, etc., but other props will be overwritten.',
],
name,
version,
description,
keywords,
homepage: 'https://lwc.dev',
repository: {
type: 'git',
url: 'https://github.com/salesforce/lwc.git',
directory: `packages/${name}`,
},
bugs: { url: 'https://github.com/salesforce/lwc/issues' },
license: 'MIT',
publishConfig: { access: 'public' },
// Use the same volta config in every subdirectory so that we always get the same node/yarn versions
// See: https://docs.volta.sh/advanced/workspaces
volta: {
extends: '../../../package.json',
},
...buildProps,
dependencies,
devDependencies,
peerDependencies,
};
const exposedModules = LWC_EXPOSED_MODULES[name];
if (exposedModules) {
// Special case - consumers can do `import { LightningElement } from 'lwc'` and have it resolve to
// `@lwc/engine-dom`. As for @lwc/synthetic-shadow and @lwc/wire-service, we have historically included these in
// the "default modules" defined in @lwc/rollup-plugin.
expectedJson.lwc = {
modules: exposedModules.map((exposedModule) => ({
name: exposedModule,
path: 'dist/index.js',
})),
expose: exposedModules,
};
}
const expected = JSON.stringify(expectedJson, null, 4) + '\n';
expectedPkgJsons.push({
filename,
expected,
actual,
});
}
// Check if any of the files are different than what we expect, so we can error in that case
const differingPackageJsonFiles = [];
for (const { filename, expected, actual } of expectedPkgJsons) {
if (actual !== expected) {
differingPackageJsonFiles.push(filename);
fs.writeFileSync(filename, expected, 'utf-8');
}
}
if (differingPackageJsonFiles.length > 0) {
console.error(
'Found package.json files with unexpected content. Content has been overwritten.\n' +
'Please run `git commit` and `node ./scripts/tasks/check-and-rewrite-package-json.js` again.\n' +
'Files:',
differingPackageJsonFiles
);
// Only in "test" mode do we actually emit a non-zero exit code. This is designed for CI tests.
// In other cases (e.g. as a git precommit hook), we can just exit with a normal 0 exit code.
if (process.argv.includes('--test')) {
process.exitCode = 1;
}
}