Simplify vendors validate (#12421)

This commit is contained in:
fisker Cheung authored and GitHub committed 2022-03-13 16:55:02 +08:00
1 parent db1f4e473f
commit 1b4b2c9205
8 files changed
+41 -204

No files matched your search

@@ -1,4 +1,4 @@
name: Validate_Vendor_Versions
name: Validate_Vendors
on:
push:
@@ -28,5 +28,5 @@ jobs:
- name: Install Dependencies
run: yarn install --frozen-lockfile
- name: Validate Vendor Versions
run: yarn run vendors:validate
- name: Validate Vendors
run: yarn run vendors:bundle && echo "Listing changed files:" && git diff --name-only --exit-code && echo "No files changed."
+1 -3
View File
@@ -131,7 +131,6 @@
"path-browserify": "1.0.1",
"prettier": "2.5.1",
"pretty-bytes": "5.6.0",
"read-pkg-up": "9.1.0",
"rimraf": "3.0.2",
"rollup-plugin-license": "2.6.1",
"snapshot-diff": "0.9.0",
@@ -161,8 +160,7 @@
"fix:prettier": "yarn lint:prettier --write",
"build": "node ./scripts/build/build.mjs",
"build:website": "node ./scripts/build-website.mjs",
"vendors:bundle": "node ./scripts/vendors/bundle-vendors.mjs",
"vendors:validate": "node ./scripts/vendors/validate-vendor-versions.mjs"
"vendors:bundle": "node ./scripts/vendors/bundle-vendors.mjs"
},
"browserslist": [
">0.5%",
+5 -19
View File
@@ -4,13 +4,13 @@ Scripts for bundling Pure ESM Packages to CommonJS so that Prettier can use them
## Context
https://github.com/prettier/prettier/issues/12209#issuecomment-1028212785
<https://github.com/prettier/prettier/issues/12209#issuecomment-1028212785>
Currently, Prettier is developed using CommonJS Modules. This is for historical reasons, and we are considering [migrating to ECMAScript Modules](https://github.com/prettier/prettier/issues/10157) in the future.
However, migrating to ECMAScript Modules is not an easy task.
While we are working on it, several important packages are being migrated to Pure ESM. Please see https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c from [@sindresorhus](https://github.com/sindresorhus) about Pure ESM Packages.
While we are working on it, several important packages are being migrated to Pure ESM. Please see <https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c> from [@sindresorhus](https://github.com/sindresorhus) about Pure ESM Packages.
It would be a problem if Prettier could not use the new Pure ESM Package until we have completed that work.
@@ -22,7 +22,7 @@ So we will put the files bundled with Pure ESM Packages as CommonJS Modules unde
### `/scripts/vendors/bundle-vendors.mjs`
```
```bash
yarn vendors:bundle
```
@@ -32,24 +32,10 @@ This script do the following 4 things:
Bundles listed packages in `vendors.mjs` into `/vendors/*.js` as CommonJS.
**2. Lock vendor versions**
Locks versions of packages to `vendor-meta.json`. It is used to validate by `validate-vendor-versions.mjs`.
**3. Save vendors license info**
**2. Save vendors license info**
Saves vendors licenses info to `vendor-meta.json`.
**4. Generate `/vendors/*.d.ts`**
**3. Generate `/vendors/*.d.ts`**
Generates a definition files that satisfies `lint:typecheck` for each vendors.
### `/scripts/vendors/validate-vendor-versions.mjs`
```
yarn vendors:validate
```
This script compares the version of packages locked in `vendor-versions.json` with the version of packages actually installed, and fails if they are different.
This is mainly run on CI to keep consistency between bundled and installed packages.
+27 -40
View File
@@ -1,49 +1,30 @@
#!/usr/bin/env node
import fs from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";
import createEsmUtils from "esm-utils";
import esbuild from "esbuild";
import { readPackageUp } from "read-pkg-up";
import { outdent } from "outdent";
import { PROJECT_ROOT } from "../utils/index.mjs";
import esbuildPluginLicense from "../build/esbuild-plugins/license.mjs";
import vendors from "./vendors.mjs";
import { saveVendorVersions, saveVendorLicenses } from "./utils.mjs";
import { vendorMetaFile, saveVendorLicenses } from "./utils.mjs";
import esbuildPluginTsNocheck from "./esbuild-plugin-ts-nocheck.mjs";
const { __dirname, require } = createEsmUtils(import.meta);
const rootDir = path.join(__dirname, "..", "..");
const { require } = createEsmUtils(import.meta);
const rootDir = new URL("../../", import.meta.url);
// prettier/vendors
const vendorsDir = path.join(rootDir, "vendors");
const vendorsDir = new URL("./vendors/", rootDir);
// prettier/vendors/*.js
const getVendorFilePath = (vendorName) =>
path.join(vendorsDir, `${vendorName}.js`);
fileURLToPath(new URL(`./${vendorName}.js`, vendorsDir));
async function lockVersions(vendors) {
const vendorVersions = {};
for (const vendor of vendors) {
const { packageJson: vendorPackage } = await readPackageUp({
cwd: path.dirname(require.resolve(vendor)),
});
const vendorVersion = vendorPackage.version;
vendorVersions[vendor] = vendorVersion;
}
await saveVendorVersions(vendorVersions);
}
async function fileExists(filePath) {
try {
return (await fs.stat(filePath)).isFile();
} catch {
return false;
}
}
async function cleanExistsBundledJS() {
for (const file of await fs.readdir(vendorsDir)) {
const filePath = path.join(vendorsDir, file);
if (path.extname(file) === ".js" && (await fileExists(filePath))) {
await fs.rm(filePath);
async function clean() {
for (const directoryOrFile of [vendorsDir, vendorMetaFile]) {
try {
await fs.rm(directoryOrFile, { recursive: true, force: true });
} catch {
// No op
}
}
}
@@ -53,7 +34,7 @@ async function generateDts(vendor) {
Boolean(module.default)
);
await fs.writeFile(
path.join(vendorsDir, `${vendor}.d.ts`),
new URL(`${vendor}.d.ts`, vendorsDir),
[
"// This file is generated automatically.",
hasDefault ? `export {default} from "${vendor}";` : null,
@@ -68,9 +49,6 @@ async function generateDts(vendor) {
async function bundle(vendor, options) {
const outfile = getVendorFilePath(vendor);
if (await fileExists(outfile)) {
await fs.rm(outfile);
}
/** @type {import("esbuild").CommonOptions} */
const esbuildOption = {
@@ -96,7 +74,7 @@ async function bundle(vendor, options) {
}
async function main() {
await cleanExistsBundledJS();
await clean();
const licenses = [];
for (const vendor of vendors) {
@@ -108,11 +86,20 @@ async function main() {
console.log(`Bundled: ${vendor}`);
}
await lockVersions(vendors);
console.log("Vendor versions saved");
await saveVendorLicenses(licenses);
console.log("Vendor licenses saved");
await fs.writeFile(
new URL("./README.md", vendorsDir),
outdent`
# \`./vendors\`
**JavaScript Files that included in this directory are auto generated by [\`/scripts/vendor/bundle-vendors.mjs\`](../scripts/vendors/bundle-vendors.mjs).**
See [\`/scripts/vendors/README.md\`](../scripts/vendors/README.md) for the context.
` + "\n"
);
console.log("`README.md` file saved");
}
main();
+1 -12
View File
@@ -1,21 +1,10 @@
import fs from "node:fs";
import { readJson, writeJson } from "../utils/index.mjs";
const vendorMetaFile = new URL("./vendor-meta.json", import.meta.url);
export const vendorMetaFile = new URL("./vendor-meta.json", import.meta.url);
const getMeta = () =>
fs.existsSync(vendorMetaFile) ? readJson(vendorMetaFile) : {};
export async function getVendorVersions() {
const meta = await getMeta();
return meta.versions;
}
export async function saveVendorVersions(versions) {
const meta = await getMeta();
meta.versions = versions;
await writeJson(vendorMetaFile, meta);
}
export async function saveVendorLicenses(licenses) {
const meta = await getMeta();
meta.licenses = licenses;
-42
View File
@@ -1,42 +0,0 @@
#!/usr/bin/env node
import createEsmUtils from "esm-utils";
import { readPackageUp } from "read-pkg-up";
import { getVendorVersions } from "./utils.mjs";
import vendors from "./vendors.mjs";
const { require } = createEsmUtils(import.meta);
async function main() {
const errors = [];
const vendorVersions = await getVendorVersions();
for (const vendor of vendors) {
const { packageJson: vendorPackage } = await readPackageUp({
cwd: require.resolve(vendor),
});
const vendorPackageVersion = vendorPackage.version;
const lockedVersion = vendorVersions[vendor];
if (vendorPackageVersion !== lockedVersion) {
errors.push({
oldVersion: lockedVersion,
newVersion: vendorPackageVersion,
packageName: vendor,
});
}
}
if (errors.length > 0) {
for (const { packageName, oldVersion, newVersion } of errors) {
console.error(
`The version of \`${packageName}\` is "${newVersion}", expected locked "${oldVersion}"`
);
}
console.error(
"Did you forget running `./scripts/vendors/bundle-vendors.mjs`?"
);
process.exitCode = 1;
} else {
console.log("Done");
}
}
main();
-6
View File
@@ -1,10 +1,4 @@
{
"versions": {
"string-width": "5.0.1",
"mem": "9.0.2",
"execa": "6.1.0",
"tempy": "2.0.0"
},
"licenses": [
{
"name": "ansi-regex",
+4 -79
View File
@@ -1656,7 +1656,7 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-17.0.15.tgz#97779282c09c09577120a2162e71d8380003590a"
integrity sha512-zWt4SDDv1S9WRBNxLFxFRHxdD9tvH8f5/kg5/IaLFdnSNXsDY4eL3Q3XXN+VxUnWIhyVFDwcsmAprvwXoM/ClA==
"@types/normalize-package-data@^2.4.0", "@types/normalize-package-data@^2.4.1":
"@types/normalize-package-data@^2.4.0":
version "2.4.1"
resolved "https://registry.yarnpkg.com/@types/normalize-package-data/-/normalize-package-data-2.4.1.tgz#d3357479a0fdfdd5907fe67e17e0a85c906e1301"
integrity sha512-Gj7cI7z+98M282Tqmp2K5EIsoouUEzbBJhQQzDE3jSIRk6r9gsz0oUokqIUR4u1R3dMHo0pDHM7sNOHyhulypw==
@@ -3343,14 +3343,6 @@ find-up@^5.0.0:
locate-path "^6.0.0"
path-exists "^4.0.0"
find-up@^6.3.0:
version "6.3.0"
resolved "https://registry.yarnpkg.com/find-up/-/find-up-6.3.0.tgz#2abab3d3280b2dc7ac10199ef324c4e002c8c790"
integrity sha512-v2ZsoEuVHYy8ZIlYqwPe/39Cy+cFDzp4dXPaxNvkEuouymu+2Jbz0PxpKarJHYJTmv2HWT3O382qY8l4jMWthw==
dependencies:
locate-path "^7.1.0"
path-exists "^5.0.0"
flat-cache@^3.0.4:
version "3.0.4"
resolved "https://registry.yarnpkg.com/flat-cache/-/flat-cache-3.0.4.tgz#61b0338302b2fe9f957dcc32fc2a87f1c3048b11"
@@ -3589,13 +3581,6 @@ hosted-git-info@^2.1.4:
resolved "https://registry.yarnpkg.com/hosted-git-info/-/hosted-git-info-2.8.9.tgz#dffc0bf9a21c02209090f2aa69429e1414daf3f9"
integrity sha512-mxIDAb9Lsm6DoOJ7xH+5+X4y1LU/4Hi50L9C5sIswK3JzULS4bwk1FvjdBgvYR4bzT4tuUQiC15FE2f5HbLvYw==
hosted-git-info@^4.0.1:
version "4.1.0"
resolved "https://registry.yarnpkg.com/hosted-git-info/-/hosted-git-info-4.1.0.tgz#827b82867e9ff1c8d0c4d9d53880397d2c86d224"
integrity sha512-kyCuEOWjJqZuDbRHzL8V93NzQhwIB71oFWSyzVo+KPZI+pnQPPxucdkrOZvkLRnrf5URsQM+IJ09Dw29cRALIA==
dependencies:
lru-cache "^6.0.0"
html-element-attributes@2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/html-element-attributes/-/html-element-attributes-2.3.0.tgz#a192ac90a457be9f1e2cc9ab69000ee89be74aa6"
@@ -3775,7 +3760,7 @@ is-callable@^1.1.4, is-callable@^1.2.4:
resolved "https://registry.yarnpkg.com/is-callable/-/is-callable-1.2.4.tgz#47301d58dd0259407865547853df6d61fe471945"
integrity sha512-nsuwtxZfMX67Oryl9LCQ+upnC0Z0BgpwntpS89m1H/TLF0zNfzfLMV/9Wa/6MZsj0acpEjAO0KF1xT6ZdLl95w==
is-core-module@^2.2.0, is-core-module@^2.5.0, is-core-module@^2.8.0, is-core-module@^2.8.1:
is-core-module@^2.2.0, is-core-module@^2.8.0, is-core-module@^2.8.1:
version "2.8.1"
resolved "https://registry.yarnpkg.com/is-core-module/-/is-core-module-2.8.1.tgz#f59fdfca701d5879d0a6b100a40aa1560ce27211"
integrity sha512-SdNCUs284hr40hFTFP6l0IfZ/RSrMXF3qgoRHd3/79unUTvrFO/JoXwkGm+5J/Oe3E/b5GsnG330uUNgRpu1PA==
@@ -4739,13 +4724,6 @@ locate-path@^6.0.0:
dependencies:
p-locate "^5.0.0"
locate-path@^7.1.0:
version "7.1.0"
resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-7.1.0.tgz#241d62af60739f6097c055efe10329c88b798425"
integrity sha512-HNx5uOnYeK4SxEoid5qnhRfprlJeGMzFRKPLCf/15N3/B4AiofNwC/yq7VBKdVk9dx7m+PiYCJOGg55JYTAqoQ==
dependencies:
p-locate "^6.0.0"
lodash.debounce@^4.0.8:
version "4.0.8"
resolved "https://registry.yarnpkg.com/lodash.debounce/-/lodash.debounce-4.0.8.tgz#82d79bff30a67c4005ffd5e2515300ad9ca4d7af"
@@ -4984,16 +4962,6 @@ normalize-package-data@^2.3.2, normalize-package-data@^2.5.0:
semver "2 || 3 || 4 || 5"
validate-npm-package-license "^3.0.1"
normalize-package-data@^3.0.2:
version "3.0.3"
resolved "https://registry.yarnpkg.com/normalize-package-data/-/normalize-package-data-3.0.3.tgz#dbcc3e2da59509a0983422884cd172eefdfa525e"
integrity sha512-p2W1sgqij3zMMyRC067Dg16bfzVH+w7hyegmpIvZ4JNjqtGOVAIvLmjBx3yP7YTe9vKJgkoNOPjwQGogDoMXFA==
dependencies:
hosted-git-info "^4.0.1"
is-core-module "^2.5.0"
semver "^7.3.4"
validate-npm-package-license "^3.0.1"
normalize-path@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/normalize-path/-/normalize-path-3.0.0.tgz#0dcd69ff23a1c9b11fd0978316644a0388216a65"
@@ -5177,13 +5145,6 @@ p-limit@^3.0.2:
dependencies:
yocto-queue "^0.1.0"
p-limit@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-4.0.0.tgz#914af6544ed32bfa54670b061cafcbd04984b644"
integrity sha512-5b0R4txpzjPWVw/cXXUResoD4hb6U/x9BH08L7nw+GN1sezDzPdxeRvpc9c433fZhBan/wusjbCsqwqm4EIBIQ==
dependencies:
yocto-queue "^1.0.0"
p-locate@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-2.0.0.tgz#20a0103b222a70c8fd39cc2e580680f3dde5ec43"
@@ -5205,13 +5166,6 @@ p-locate@^5.0.0:
dependencies:
p-limit "^3.0.2"
p-locate@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-6.0.0.tgz#3da9a49d4934b901089dca3302fa65dc5a05c04f"
integrity sha512-wPrq66Llhl7/4AGC6I+cqxT07LhXvWL08LNXz1fENOw0Ap4sRZZ/gZpTTJ5jpurzzzfS2W/Ge9BY3LgLjCShcw==
dependencies:
p-limit "^4.0.0"
p-map@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/p-map/-/p-map-4.0.0.tgz#bb2f95a5eda2ec168ec9274e06a747c3e2904d2b"
@@ -5302,11 +5256,6 @@ path-exists@^4.0.0:
resolved "https://registry.yarnpkg.com/path-exists/-/path-exists-4.0.0.tgz#513bdbe2d3b95d7762e8c1137efa195c6c61b5b3"
integrity sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w==
path-exists@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/path-exists/-/path-exists-5.0.0.tgz#a6aad9489200b21fab31e49cf09277e5116fb9e7"
integrity sha512-RjhtfwJOxzcFmNOi6ltcbcu4Iu+FL3zEj83dk4kAS+fVpTxXLO1b38RvJgT/0QwvV/L3aY9TAnyv0EOqW4GoMQ==
path-is-absolute@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f"
@@ -5519,15 +5468,6 @@ react-is@^17.0.1:
resolved "https://registry.yarnpkg.com/react-is/-/react-is-17.0.2.tgz#e691d4a8e9c789365655539ab372762b0efb54f0"
integrity sha512-w2GsyukL62IJnlaff/nRegPQR94C/XXamvMWmSHRJ4y7Ts/4ocGRmTHvOs8PSE6pB3dWOrD/nueuU5sduBsQ4w==
read-pkg-up@9.1.0:
version "9.1.0"
resolved "https://registry.yarnpkg.com/read-pkg-up/-/read-pkg-up-9.1.0.tgz#38ca48e0bc6c6b260464b14aad9bcd4e5b1fbdc3"
integrity sha512-vaMRR1AC1nrd5CQM0PhlRsO5oc2AAigqr7cCrZ/MW/Rsaflz4RlgzkpL4qoU/z1F6wrbd85iFv1OQj/y5RdGvg==
dependencies:
find-up "^6.3.0"
read-pkg "^7.1.0"
type-fest "^2.5.0"
read-pkg-up@^7.0.1:
version "7.0.1"
resolved "https://registry.yarnpkg.com/read-pkg-up/-/read-pkg-up-7.0.1.tgz#f3a6135758459733ae2b95638056e1854e7ef507"
@@ -5556,16 +5496,6 @@ read-pkg@^5.2.0:
parse-json "^5.0.0"
type-fest "^0.6.0"
read-pkg@^7.1.0:
version "7.1.0"
resolved "https://registry.yarnpkg.com/read-pkg/-/read-pkg-7.1.0.tgz#438b4caed1ad656ba359b3e00fd094f3c427a43e"
integrity sha512-5iOehe+WF75IccPc30bWTbpdDQLOCc3Uu8bi3Dte3Eueij81yx1Mrufk8qBx/YAbR4uL1FdUr+7BKXDwEtisXg==
dependencies:
"@types/normalize-package-data" "^2.4.1"
normalize-package-data "^3.0.2"
parse-json "^5.2.0"
type-fest "^2.0.0"
refa@^0.9.0:
version "0.9.1"
resolved "https://registry.yarnpkg.com/refa/-/refa-0.9.1.tgz#12731fce378d235731b1f73182b20083c8a75ca8"
@@ -5855,7 +5785,7 @@ semver@7.0.0:
resolved "https://registry.yarnpkg.com/semver/-/semver-7.0.0.tgz#5f3ca35761e47e05b206c6daff2cf814f0316b8e"
integrity sha512-+GB6zVA9LWh6zovYQLALHwv5rb2PHGlJi3lfiqIHxR0uuwCgefcOJc59v9fv1w8GbStwxuuqqAjI9NMAOOgq1A==
semver@7.3.5, semver@^7.3.2, semver@^7.3.4, semver@^7.3.5:
semver@7.3.5, semver@^7.3.2, semver@^7.3.5:
version "7.3.5"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.3.5.tgz#0b621c879348d8998e4b0e4be94b3f12e6018ef7"
integrity sha512-PoeGJYh8HK4BTO/a9Tf6ZG3veo/A7ZVsYrSA6J8ny9nb3B1VrpkuN+z9OE5wfE5p6H4LchYZsegiQgbJD94ZFQ==
@@ -6393,7 +6323,7 @@ type-fest@^1.0.1:
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-1.4.0.tgz#e9fb813fe3bf1744ec359d55d1affefa76f14be1"
integrity sha512-yGSza74xk0UG8k+pLh5oeoYirvIiWo5t0/o3zHHAO2tRDiZcxWP7fywNlXhqb6/r6sWvwi+RsyQMWhVLe4BVuA==
type-fest@^2.0.0, type-fest@^2.5.0:
type-fest@^2.0.0:
version "2.11.1"
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-2.11.1.tgz#04ddf5c0dbbd403fb8270d98ad1b4857e7ff7b24"
integrity sha512-fPcV5KLAqFfmhHobtAUwEpbpfYhVF7wSLVgbG/7mIGe/Pete7ky/bPAPRkzbWdrj0/EkswFAAR2feJCgigkUKg==
@@ -6812,8 +6742,3 @@ yocto-queue@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-0.1.0.tgz#0294eb3dee05028d31ee1a5fa2c556a6aaf10a1b"
integrity sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==
yocto-queue@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-1.0.0.tgz#7f816433fb2cbc511ec8bf7d263c3b58a1a3c251"
integrity sha512-9bnSc/HEW2uRy67wc+T8UwauLuPJVn28jb+GtJY16iiKWyvmYJRXVT4UamsAEGQfPohgr2q4Tq0sQbQlxTfi1g==