From 051b1bd2d3ff88aa173f126d38913da49468d862 Mon Sep 17 00:00:00 2001 From: Pedro Cattori Date: Thu, 19 May 2022 12:07:20 -0400 Subject: [PATCH] chore: update Deno import maps on release (#3255) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Michaƫl De Boey --- scripts/utils.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/scripts/utils.js b/scripts/utils.js index 6c58735ad90..cd46b3b636b 100644 --- a/scripts/utils.js +++ b/scripts/utils.js @@ -176,6 +176,52 @@ async function updateDeploymentScriptVersion(nextVersion) { ); } +/** + * @param {string} importSpecifier + * @returns {[string, string]} [packageName, importPath] + */ +const getPackageNameFromImportSpecifier = (importSpecifier) => { + if (importSpecifier.startsWith("@")) { + let [scope, pkg, ...path] = importSpecifier.split("/"); + return [`${scope}/${pkg}`, path.join("/")]; + } + + let [pkg, ...path] = importSpecifier.split("/"); + return [pkg, path.join("/")]; +}; +/** + * @param {string} importMapPath + * @param {string} nextVersion + */ +const updateDenoImportMap = async (importMapPath, nextVersion) => { + let { imports, ...json } = await jsonfile.readFile(importMapPath); + let remixPackagesFull = remixPackages.all.map( + (remixPackage) => `@remix-run/${remixPackage}` + ); + + let newImports = Object.fromEntries( + Object.entries(imports).map(([importName, path]) => { + let [packageName, importPath] = + getPackageNameFromImportSpecifier(importName); + + return remixPackagesFull.includes(packageName) + ? [ + importName, + `https://esm.sh/${packageName}@${nextVersion}${ + importPath ? `/${importPath}` : "" + }`, + ] + : [importName, path]; + }) + ); + + return jsonfile.writeFile( + importMapPath, + { ...json, imports: newImports }, + { spaces: 2 } + ); +}; + /** * @param {string} nextVersion */ @@ -187,6 +233,16 @@ async function incrementRemixVersion(nextVersion) { await updateRemixVersion(`remix-${name}`, nextVersion); } + // Update version numbers in Deno's import maps + await Promise.all( + [ + path.join(".vscode", "deno_resolve_npm_imports.json"), + path.join("templates", "deno", ".vscode", "resolve_npm_imports.json"), + ].map((importMapPath) => + updateDenoImportMap(path.join(rootDir, importMapPath), nextVersion) + ) + ); + // Update versions in the examples await updateExamplesRemixVersion(nextVersion);