Skip to content

Commit a7ed04d

Browse files
authored
chore(script): ignore empty directories (#5591)
1 parent 592d970 commit a7ed04d

File tree

4 files changed

+29
-50
lines changed

4 files changed

+29
-50
lines changed

scripts/shared/packages.mjs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
1-
import { readdirSync, readFileSync } from 'node:fs';
2-
import { dirname, join } from 'node:path';
3-
import { fileURLToPath } from 'node:url';
1+
import { readFileSync } from 'node:fs';
2+
import { dirname } from 'node:path';
3+
import { globSync } from 'glob';
44

5-
const readJsonSync = (filepath) => JSON.parse(readFileSync(filepath, 'utf8'));
5+
const allPackages = globSync(['packages/lwc/package.json', 'packages/@lwc/*/package.json']);
66

7-
const root = join(dirname(fileURLToPath(import.meta.url)), '../..');
8-
const lwcPackageDir = 'packages/lwc';
9-
const relativeNamespaceDir = 'packages/@lwc';
10-
const namespacedPackageDirs = readdirSync(join(root, 'packages/@lwc'), {
11-
withFileTypes: true,
12-
})
13-
.filter((fd) => fd.isDirectory() && !fd.name.startsWith('.'))
14-
.map((fd) => join(relativeNamespaceDir, fd.name));
15-
const allPackageDirs = [lwcPackageDir, ...namespacedPackageDirs];
16-
17-
export const ALL_PACKAGES = allPackageDirs.map((path) => ({
18-
path,
19-
package: readJsonSync(join(path, 'package.json')),
7+
export const ALL_PACKAGES = allPackages.map((pkg) => ({
8+
path: dirname(pkg),
9+
package: JSON.parse(readFileSync(pkg, 'utf8')),
2010
}));
2111
export const PRIVATE_PACKAGES = ALL_PACKAGES.filter((data) => data.package.private);
2212
export const PUBLIC_PACKAGES = ALL_PACKAGES.filter((data) => !data.package.private);
13+
export const SCOPED_PACKAGES = ALL_PACKAGES.filter((data) => data.package.name.startsWith('@lwc/'));

scripts/tasks/check-and-rewrite-package-json.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
const fs = require('node:fs');
1919
const path = require('node:path');
20-
const { globSync } = require('glob');
20+
const { SCOPED_PACKAGES } = require('../shared/packages.mjs');
2121

2222
// This is the same list as in @lwc/rollup-plugin/src/index.ts
2323
const LWC_EXPOSED_MODULES = {
@@ -38,10 +38,7 @@ const STATIC_PACKAGES = ['@lwc/types'];
3838

3939
const expectedPkgJsons = [];
4040

41-
for (const dir of globSync('./packages/@lwc/*')) {
42-
const filename = path.join('./', dir, 'package.json');
43-
const actual = fs.readFileSync(filename, 'utf-8').replace(/\r\n/g, '\n'); // Windows compat
44-
const pkg = JSON.parse(actual);
41+
for (const { package: pkg, path: dir } of SCOPED_PACKAGES) {
4542
// Skip private packages
4643
if (pkg.private) {
4744
continue;
@@ -131,12 +128,11 @@ for (const dir of globSync('./packages/@lwc/*')) {
131128
};
132129
}
133130

134-
const expected = JSON.stringify(expectedJson, null, 4) + '\n';
135-
136131
expectedPkgJsons.push({
137-
filename,
138-
expected,
139-
actual,
132+
filename: path.join(dir, 'package.json'),
133+
// Including \n because that's how prettier formats files
134+
expected: JSON.stringify(expectedJson, null, 4) + '\n',
135+
actual: JSON.stringify(pkg, null, 4) + '\n',
140136
});
141137
}
142138

scripts/tasks/check-imports-are-declared-dependencies.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,14 @@
1212
const path = require('node:path');
1313
const fs = require('node:fs');
1414
const { builtinModules } = require('node:module');
15-
const { globSync } = require('glob');
1615
const { init, parse } = require('es-module-lexer');
16+
const { SCOPED_PACKAGES } = require('../shared/packages.mjs');
1717

1818
async function main() {
1919
const errors = [];
20-
const pkgJsonFiles = globSync(path.join(__dirname, '../../packages/@lwc/*/package.json'));
2120

22-
for (const pkgJsonFile of pkgJsonFiles) {
23-
const { dependencies, peerDependencies, private, module, types } = JSON.parse(
24-
fs.readFileSync(pkgJsonFile, 'utf-8')
25-
);
21+
for (const { package: pkg, path: dir } of SCOPED_PACKAGES) {
22+
const { dependencies, peerDependencies, private, module, types } = pkg;
2623

2724
if (private) {
2825
continue; // not public, we don't care
@@ -33,7 +30,7 @@ async function main() {
3330
// We use three fields for exports: "main" for CJS, "module" for ESM, "types" for, y'know.
3431
// If a package has a "module" defined, we use that as the source of truth, otherwise we
3532
// assume it's a types-only package and we check that, instead.
36-
const fileToCheck = path.join(path.dirname(pkgJsonFile), module ?? types);
33+
const fileToCheck = path.join(dir, module ?? types);
3734

3835
const esmSource = fs.readFileSync(fileToCheck, 'utf-8');
3936

@@ -57,10 +54,7 @@ async function main() {
5754

5855
if (!(importedPackage in allDependencies)) {
5956
errors.push(
60-
`${pkgJsonFile
61-
.split(path.sep)
62-
.slice(-3, -1)
63-
.join(path.sep)} imports "${imported}", but it is not declared ` +
57+
`${pkg} imports "${imported}", but it is not declared ` +
6458
'as a dependency/peerDependency in package.json.'
6559
);
6660
}

scripts/tasks/generate-license-files.mjs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
import path from 'node:path';
2-
import { readFile, writeFile, stat, readdir } from 'node:fs/promises';
2+
import { readFile, writeFile, stat } from 'node:fs/promises';
33
import { createRequire } from 'node:module';
44
import { fileURLToPath } from 'node:url';
55
import prettier from 'prettier';
66
import { BUNDLED_DEPENDENCIES } from '../shared/bundled-dependencies.js';
7+
import { ALL_PACKAGES, SCOPED_PACKAGES } from '../shared/packages.mjs';
78

89
// We're using ESM here, but some packages still only export CJS, so we also need `require`
910
const require = createRequire(import.meta.url);
1011

11-
const atLwcPackages = (await readdir('packages/@lwc'))
12-
// skip dotfiles like .DS_Store
13-
.filter((_) => !_.startsWith('.'))
14-
.map((_) => `@lwc/${_}`);
15-
1612
// Generate our LICENSE files for each package, including any bundled dependencies
1713
// This is modeled after how Rollup does it:
1814
// https://github.com/rollup/rollup/blob/0b665c3/build-plugins/generate-license-file.ts
@@ -43,7 +39,7 @@ function tryResolve(specifier) {
4339
}
4440
// `require.resolve` accepts a second parameter of additional places to look
4541
return require.resolve(specifier, {
46-
paths: atLwcPackages.map((pkg) => path.join('packages', pkg, 'node_modules')),
42+
paths: SCOPED_PACKAGES.map((pkg) => path.join(pkg.path, 'node_modules')),
4743
});
4844
}
4945

@@ -77,7 +73,11 @@ async function findLicenseText(depName) {
7773
await readFile(path.join(resolvedDepPath, 'package.json'), 'utf-8')
7874
);
7975

80-
return `${license} license defined in package.json in v${version}.`;
76+
if (!license) {
77+
throw new Error(`${depName} does not define a license.`);
78+
}
79+
80+
return `"${license}" license defined in package.json in v${version}.`;
8181
}
8282

8383
const coreLicense = await readFile('LICENSE-CORE.md', 'utf-8');
@@ -106,11 +106,9 @@ await writeFile('LICENSE.md', formattedLicense, 'utf-8');
106106

107107
// License file for each package as well, so that we publish it to npm
108108

109-
const packages = ['lwc', ...atLwcPackages];
110-
111109
await Promise.all(
112-
packages.map(async (pkg) => {
113-
await writeFile(path.join('packages/', pkg, 'LICENSE.md'), formattedLicense, 'utf-8');
110+
ALL_PACKAGES.map(async (pkg) => {
111+
await writeFile(path.join(pkg.path, 'LICENSE.md'), formattedLicense, 'utf-8');
114112
})
115113
);
116114

0 commit comments

Comments
 (0)