Skip to content

Commit

Permalink
Add update-www-stubs and associated test for www entrypoints
Browse files Browse the repository at this point in the history
  • Loading branch information
etrepum committed Apr 23, 2024
1 parent dba9d16 commit 1e2a496
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 8 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@
"update-version": "node ./scripts/updateVersion",
"update-tsconfig": "node ./scripts/update-tsconfig",
"update-flowconfig": "node ./scripts/update-flowconfig",
"update-packages": "npm run update-version && npm run update-tsconfig && npm run update-flowconfig && npm run update-docs",
"update-www-stubs": "node ./scripts/update-www-stubs",
"update-packages": "npm run update-version && npm run update-tsconfig && npm run update-flowconfig && npm run update-docs && npm run update-www-stubs",
"postversion": "git checkout -b ${npm_package_version}__release && npm install && npm run update-version && npm run update-changelog && git add -A && git commit -m v${npm_package_version} && git tag -a v${npm_package_version} -m v${npm_package_version}",
"publish-extension": "npm run zip -w @lexical/devtools && npm run publish -w @lexical/devtools",
"release": "npm run prepare-release && node ./scripts/npm/release.js",
Expand Down
11 changes: 6 additions & 5 deletions packages/lexical-website/docs/maintainers-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ not ready for consumption, it should probably still be set to
| Unit Tests | `packages/lexical-package-name/src/__tests__/unit/LexicalPackageName.test.{ts,tsx}` |
| dist (gitignore'd build product) | `packages/lexical-package-name/dist` |
| npm (gitignore'd prerelease product) | `packages/lexical-package-name/npm` |
| www entrypoint? | `packages/lexical-package-name/LexicalPackageName.js` |
| www entrypoint | `packages/lexical-package-name/LexicalPackageName.js` |

### Multiple module export (@lexical/react)

Expand Down Expand Up @@ -181,10 +181,11 @@ describe('LexicalEslintPlugin', () => {

### npm run update-packages

This script runs: update-version, update-tsconfig, update-flowconfig, and
update-docs. This is safe to do at any time and will ensure that package.json
files are all at the correct versions, paths are set up correctly for module
resolution of all public exports, and that various defaults are filled in.
This script runs: update-version, update-tsconfig, update-flowconfig,
update-docs, and update-www-stubs. This is safe to do at any time and will
ensure that package.json files are all at the correct versions, paths are
set up correctly for module resolution of all public exports, and that
various defaults are filled in.

These scripts can be run individually, but unless you're working on one
of these scripts you might as well run them all.
Expand Down
23 changes: 21 additions & 2 deletions scripts/__tests__/unit/build.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ const fs = require('fs-extra');
const glob = require('glob');
const path = require('node:path');
const {packagesManager} = require('../../shared/packagesManager');
const npmToWwwName = require('../../www/npmToWwwName');

const monorepoPackageJson = require('../../shared/readMonorepoPackageJson')();

const publicNpmNames = new Set(
packagesManager.getPublicPackages().map((pkg) => pkg.getNpmName()),
);

describe('public package.json audits (`npm run update-version` to fix most issues)', () => {
describe('public package.json audits (`npm run update-packages` to fix most issues)', () => {
packagesManager.getPublicPackages().forEach((pkg) => {
const npmName = pkg.getNpmName();
const packageJson = pkg.packageJson;
Expand Down Expand Up @@ -91,7 +92,7 @@ describe('public package.json audits (`npm run update-version` to fix most issue
});
});

describe('documentation audits (`npm run update-docs` to fix most issues)', () => {
describe('documentation audits (`npm run update-packages` to fix most issues)', () => {
const webPkg = packagesManager.getPackageByDirectoryName('lexical-website');
packagesManager.getPublicPackages().forEach((pkg) => {
const npmName = pkg.getNpmName();
Expand All @@ -115,3 +116,21 @@ describe('documentation audits (`npm run update-docs` to fix most issues)', () =
});
});
});

describe('www public package audits (`npm run update-packages` to fix most issues)', () => {
packagesManager.getPublicPackages().forEach((pkg) => {
const npmName = pkg.getNpmName();
const wwwEntrypoint = `${npmToWwwName(npmName)}.js`;
describe(npmName, () => {
it('has *.flow types', () => {
expect(glob.sync(pkg.resolve('flow', '*.flow'))).not.toEqual([]);
});
// Only worry about the entrypoint stub if it has a single module export
if (pkg.getExportedNpmModuleNames().every((name) => name === npmName)) {
it(`has a packages/${pkg.getDirectoryName()}/${wwwEntrypoint}`, () => {
expect(fs.existsSync(pkg.resolve(wwwEntrypoint))).toBe(true);
});
}
});
});
});
61 changes: 61 additions & 0 deletions scripts/update-www-stubs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* 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.
*
*/
// @ts-check
'use strict';

const fs = require('fs-extra');
const path = require('node:path');
const {packagesManager} = require('./shared/packagesManager');
const npmToWwwName = require('./www/npmToWwwName');

const headerTemplate = fs.readFileSync(
path.resolve(__dirname, 'www', 'headerTemplate.js'),
'utf8',
);

/** @param {string} filename */
function wwwStubTemplate(filename) {
return `
${headerTemplate}
'use strict';
/**
* 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.
*
*/
'use strict';
module.exports = require('./dist/${filename}');
`;
}

function updateWwwStubs() {
packagesManager.getPublicPackages().forEach((pkg) => {
const npmName = pkg.getNpmName();
// Only worry about the entrypoint stub if it has a single module export
if (pkg.getExportedNpmModuleNames().some((name) => name !== npmName)) {
return;
}

const filename = `${npmToWwwName(npmName)}.js`;
const stubPath = pkg.resolve(filename);
const root = pkg.resolve('..', '..');

if (!fs.existsSync(stubPath)) {
console.log(`Creating ${path.relative(root, stubPath)}`);
fs.writeFileSync(stubPath, wwwStubTemplate(filename));
}
});
}

updateWwwStubs();

0 comments on commit 1e2a496

Please sign in to comment.