From 87f332f92540eef028bbed7284e30c1cf614cc96 Mon Sep 17 00:00:00 2001 From: bjoluc Date: Thu, 6 Jan 2022 20:35:43 +0100 Subject: [PATCH] Add updateUnpkgLinks Gulp task --- .changeset/sweet-cheetahs-grin.md | 5 ++++ gulpfile.js | 2 +- package.json | 1 + packages/config/gulp.js | 48 ++++++++++++++++++++++--------- 4 files changed, 42 insertions(+), 14 deletions(-) create mode 100644 .changeset/sweet-cheetahs-grin.md diff --git a/.changeset/sweet-cheetahs-grin.md b/.changeset/sweet-cheetahs-grin.md new file mode 100644 index 00000000..126055bb --- /dev/null +++ b/.changeset/sweet-cheetahs-grin.md @@ -0,0 +1,5 @@ +--- +"@jspsych/config": minor +--- + +Implement an `updateUnpkgLinks` Gulp task to update each unpkg link with a precise version number to the corresponding package's current version as defined in the package's `package.json`. diff --git a/gulpfile.js b/gulpfile.js index 0a4c8af2..ec2bccf8 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -1 +1 @@ -export { createCoreDistArchive } from "@jspsych/config/gulp"; +export { createCoreDistArchive, updateUnpkgLinks } from "@jspsych/config/gulp"; diff --git a/package.json b/package.json index b019f0d5..176fb549 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "test:watch": "npm test -- --watch", "build": "npm run build -w jspsych && npm run build -ws", "build:archive": "gulp createCoreDistArchive", + "update-unpkg-links": "gulp updateUnpkgLinks", "prepare": "husky install && npm run build", "tsc": "npm run tsc -ws", "changeset": "changeset", diff --git a/packages/config/gulp.js b/packages/config/gulp.js index 3e738774..6e105ea1 100644 --- a/packages/config/gulp.js +++ b/packages/config/gulp.js @@ -13,6 +13,20 @@ const { dest, src } = gulp; const readJsonFile = (filename) => JSON.parse(readFileSync(filename, "utf8")); +const getAllPackages = () => + glob + // Get an array of all package.json filenames + .sync("packages/*/package.json") + + // Map file names to package details + .map((filename) => { + const packageJson = readJsonFile(filename); + return { + name: packageJson.name, + version: packageJson.version, + }; + }); + const getVersionFileContents = () => [ "Included in this release:\n", @@ -25,19 +39,7 @@ const getVersionFileContents = () => "/"; return ( - glob - // Get an array of all package.json filenames - .sync("packages/*/package.json") - - // Map file names to package details - .map((filename) => { - const packageJson = readJsonFile(filename); - return { - name: packageJson.name, - version: packageJson.version, - }; - }) - + getAllPackages() // Filter packages that should not be listed .filter(({ name }) => !["@jspsych/config", "@jspsych/test-utils"].includes(name)) @@ -105,3 +107,23 @@ export const createCoreDistArchive = () => ) .pipe(zip("dist.zip")) .pipe(dest(".")); + +/** + * Updates each unpkg link with a precise version number to the corresponding package's current + * version as defined in the package's `package.json`. Only considers `.md` and `.html` files. + */ +export const updateUnpkgLinks = () => { + const packageVersions = new Map(getAllPackages().map(({ name, version }) => [name, version])); + + return src(["./**/*.{md,html}"]) + .pipe( + replace( + /"https:\/\/unpkg\.com\/(@?.*)@(\d+.\d+.\d+)(\/[^"]*)?"/g, + (url, packageName, currentVersion, path) => { + const latestVersion = packageVersions.get(packageName) ?? currentVersion; + return `"https://unpkg.com/${packageName}@${latestVersion}${path ?? ""}"`; + } + ) + ) + .pipe(dest("./")); +};